Skip to main content

megadata_rs: The Ultimate LLM Data Pipeline Engine

English | 简体中文

Rust Python PyO3 License: MIT

megadata_rs is a Rust-native, zero-copy data pipeline engine for LLM and VLM training at cluster scale. It removes the usual causes of GPU starvation — JSON parsing, the Python GIL, IPC copies between dataloader processes, and disk I/O congestion — and moves samples from raw corpora to GPU tensors with zero memory copies in between.

Key Features

  • Dual packing engine

    • SFT mode: native Jinja rendering of HuggingFace ChatML-style templates, a document-aware 2-D bin-packing algorithm, precise -100 masking, and cu_seqlens output that plugs straight into FlashAttention-2 varlen (no cross-document attention leakage).
    • PT mode: greedy packing that concatenates pretraining corpora into a seamless token stream to maximise MFU. The stream never loses or duplicates a token; if your loop computes loss over the whole (B, S) tile, use cu_seqlens to cut the EOS padding at the tail, or run varlen attention.
    • ⚠️ Long PT documents are split across batches. A document longer than batch_size × max_seq_len is laid out over several consecutive batches, and each piece becomes its own cu_seqlens segment — no data is lost, but under varlen training the cross-batch context is silently cut. The builder aggregates a WARN and records global_stats.split_docs / split_tokens in index.json; the loader warns again. Mitigation: raise max_seq_len until it covers the vast majority of documents.
  • Zero-copy FFI: a purpose-built DLPack C ABI plus the Arrow C Data Interface hand Rust heap buffers to PyTorch tensors in $O(1)$ time. No matter how large the batch, Python never copies a single byte.

  • Temperature mixer for multiple sources: mount Code / Math / Web streams at runtime; temperature scaling samples them concurrently underneath so you can steer the knowledge mix smoothly instead of shuffling statically.

  • Deterministic sharding and second-level resumption (MMP protocol): no centralised RPC. Thousands of ranks derive shard ownership from a deterministic PRNG with round-robin assignment, so every rank sees the same reproducible shard sequence within an epoch. File offsets and the PRNG state are captured too, so a crashed job resumes in milliseconds and replays the exact cross-source sampling order.

  • Production control plane

    • Shard aggregation: one shard file holds N batches (~128 MB/shard by default), cutting file count from "one per batch" to a few hundred; the loader hard-checks shards >= world_size so no rank idles.
    • Integrity: every shard is read back and sha256-ed into index.json. Cloud downloads are atomic (.tmp + rename), verified after download, re-downloaded on failure, retried with jittered exponential backoff, de-duplicated across processes (flock) and bounded by an LRU cache (MEGADATA_CACHE_LIMIT). Local datasets are not verified by default (zero overhead); opt in with MEGADATA_VERIFY_LOCAL=1 or =sample:P (results cached by size+mtime, so each file is hashed once).
    • Errors are never silent: I/O, decompression and corruption failures raise OSError with the shard name and batch index instead of looking like "end of data" or hanging in an infinite retry. Local reads get bounded retries (2 attempts, 50/200 ms backoff). EOF and user cancellation still end normally, and the reader stays alive after EOF so resumption can seek.
    • Prefetch-thread panics surface too: the prefetch thread is wrapped in catch_unwind, so any panic (including lock poisoning) becomes a fatal error at the consumer rather than a truncated epoch.
    • Optional compression: PT raw streams use per-batch zstd frames (Zstd-MDS, preserving O(1) random access) — on by default (real-corpus benchmark: 42% of original size on a 151k vocab, 629 MB/s decompression, +103 µs random access; disable with compression="none"). SFT uses Arrow IPC's native zstd/lz4 (explicit).
    • ⚠️ Automatic batches_per_shard is computed on uncompressed bytes (PT+zstd lands at ~42% of that); plan disk capacity at ~0.42×.
    • Prefetch window throttling: predownload caps in-flight batches (default 16), smoothing I/O bursts and bounding prefetch memory.
    • Cross-process video references: workers can hand video entries to the main process; the receiver reopens and verifies them against the manifest.
    • Bounded HTTP waits: configurable connect and body-read timeouts prevent stalled remote reads from hanging training indefinitely.
    • Permutation shuffle: Rust builds a deterministic batch-level permutation from (seed, epoch), shrinking state_dict from gigabytes of buffer to a few dozen bytes and making it reproducible across processes.
    • Elastic scaling: relaxed partitioning with num_canonical_nodes anchors, so global sample order is independent of cluster topology and a job can resume after a resize.
    • 3-D world topology: node × rank × worker; with num_workers > 0 each worker gets a disjoint shard subset (readers rebuilt lazily after fork).
    • Drop-in ecosystem: StreamingDataset(IterableDataset) and MegadataDataLoader(DataLoader) work with HF Trainer / Lightning as-is.

Installation

Install the published wheel from PyPI:

pip install megadata-rs

The distribution name is megadata-rs; the Python import name remains megadata.

To build from source, Rust 1.88+ (edition = "2024"), Python 3.8+ and maturin are required. bbpe_rs (the tokenizer) is a regular crates.io dependency and is fetched automatically — nothing to place by hand:

pip install maturin pytest torch pyarrow
maturin develop --release

To train or export your own tokenizer with bbpe_tokenizer:

pip install "git+https://github.com/hoare211/bbpe_rs@v0.5.0"

Quick Start

1. Build a dataset offline (DataBuilder)

DataBuilder uses Tokio async I/O and a Rayon thread pool to swallow JSONL corpora and write the on-disk format in one pass.

from megadata import DataBuilder

builder = DataBuilder(tokenizer_path="./model_tokenizer", max_seq_len=8192)

# SFT chat dataset (written as Arrow IPC)
builder.build_dataset(
    input_paths=["data/sft/raw.jsonl"],
    output_dir="data/sft_processed",
    mode="sft",
    template_str="{% for msg in messages %}<|im_start|>{{ msg.role }}\n{{ msg.content }}<|im_end|>\n{% endfor %}",
    batch_size=16,
)

# PT pretraining dataset (Megatron-style raw-stream .bin/.idx)
builder.build_dataset(
    input_paths=["data/pt/raw.jsonl"],
    output_dir="data/pt_processed",
    mode="pt",
    batch_size=16,
    # optional control-plane knobs:
    # batches_per_shard=0   # 0 = auto (~128MB/shard); loader checks shards >= world_size
    # compression="zstd"    # PT default (per-batch frame, Zstd-MDS); "none" to disable
)

2. Multi-source zero-copy loading

import torch
from megadata import MixBatchStream

# temperature-controlled multi-source stream (Code/Math/Web, curriculum, annealing)
loader = MixBatchStream(
    [
        {"path": "data/pt_processed/index.json", "weight": 0.7},
        {"path": "data/sft_processed/index.json", "weight": 0.3},
    ],
    batch_size=16,
    max_seq_len=8192,
    world_size=1024,
    rank=0,                  # DDP topology: no DistributedSampler needed
    steps_per_epoch=1000,    # required under DDP for a deterministic epoch length
)
loader.update_temperature(2.0)   # smooth the mix towards smaller sources

# resume (millisecond-level jump)
# loader.load_state_dict(checkpoint["megadata_state"])

for batch in loader:
    # tensors map Rust memory directly — zero copies
    input_ids = batch["input_ids"].cuda()

    loss = model(input_ids).loss
    loss.backward()

# exact snapshot of file cursors + PRNG state
# checkpoint["megadata_state"] = loader.state_dict()

Single-source training loops should use BatchStream; random access for evaluation uses RandomAccessBatchReader; HF Trainer / Lightning integration uses StreamingDataset + MegadataDataLoader. See the usage guide.

Documentation

CI (GitHub Actions)

.github/workflows/ci.yml runs four jobs: lint (ruff over python/ tests/ tools/), docs (docs_lint.py check + self-test, stdlib only), rust-test (cargo clippy --all-targets -p megadata_rs -- -D warnings + cargo test --lib) and python-test (maturin build + pytest). bench.yml runs throughput benchmarks on demand; nightly.yml runs the full proptest fuzz suite nightly.

No repository variables are required: bbpe_rs comes from crates.io.

Quality Defense

Four test dimensions back the claims above (see tests/):

  1. E2E parity — the FlashAttention topology drops no document.
  2. Distributed integrity — ranks and workers are strictly disjoint.
  3. Time travel — after an interrupt/restart, byte-level content and the PRNG sampling order match 100%.
  4. Memory leaks — high-frequency C FFI traffic and Python GC pressure leak nothing.

Contributing

Issues and PRs are welcome — please read CONTRIBUTING.md (local self-check commands included). For security reports use the private channel in SECURITY.md.

License

Released under the MIT license.

Acknowledgements

Tokenization is provided by the sister project bbpe_rs (a Rust BPE tokenizer trainer/encoder). object_store, arrow, pyo3, tokio and rayon form the infrastructure this engine is built on.

Metadata

Release files for megadata-rs 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for megadata-rs 0.3.0
File Size Uploaded Provenance Actions
megadata_rs-0.3.0.tar.gz 769.7 kB Without provenance Details for megadata_rs-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for megadata-rs 0.3.0
File Interpreter ABI Platform Actions
megadata_rs-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details for megadata_rs-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl

Total release size: 8.9 MB

Release files / megadata_rs-0.3.0.tar.gz

Download URL megadata_rs-0.3.0.tar.gz
Size 769.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2467b7e549a2f55610209981f86c9b930bbcabe64a98e03bb4b582866baccfcc
BLAKE2b-256 checksum
How to use checksums
069df948d000efbfe216706837f9eaa4c70d0e7a2a2f8f01776f3bd2d45d37c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / megadata_rs-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL megadata_rs-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl
Size 8.1 MB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
586f9ce53fc40625808e887753439a483bddf424627fb70989665472d186b349
BLAKE2b-256 checksum
How to use checksums
669f0b5f1ef890d309312dfc060dc3da2e463d7847f76b4667a86bcc8e211152
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Security for megadata-rs 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page