Skip to content

Add Bagz format support - #4496

Open
Marlon666 wants to merge 2 commits into
AI-Hypercomputer:mainfrom
Marlon666:feat/bagz-gcsfuse-support
Open

Add Bagz format support#4496
Marlon666 wants to merge 2 commits into
AI-Hypercomputer:mainfrom
Marlon666:feat/bagz-gcsfuse-support

Conversation

@Marlon666

@Marlon666 Marlon666 commented Jul 15, 2026

Copy link
Copy Markdown

Description

This Pull Request introduces end-to-end support for the Gemini-backed Bagz (.bagz) dataset format within MaxText's PyGrain input pipeline.

Context & Problem Solved:
Previously, MaxText users relying on the ultra-efficient Bagz format for large-scale pre-training faced challenges when bridging cloud storage with PyGrain workers. This PR enables a highly optimized, POSIX-compliant file ingestion path designed explicitly for local filesystem and naturally GCS Fuse mounts. By avoiding complex REST/RPC shims, this approach achieves robust, multiprocess-safe read scalability out-of-the-box.

Scope of Changes:

  • Input Pipeline: Added the BagzDataSource wrapper in input_pipeline_utils.py for parallel and resilient Bagz shard reader instantiation.
  • Grain Integration: Registered bagz as a first-class supported file type in grain_data_processing.py, enabling native ParseFeatures and NormalizeFeatures handling.
  • Configurations: Expanded base.yml and types.py to seamlessly accept "bagz" as a valid grain_file_type.
  • Tooling: Created download_hf_dataset_as_bagz.py with multi-worker parallelism, HuggingFace streaming, and robust checkpoint recovery.

FIXES: (If there is a Github Issue #, paste it here, otherwise leave blank)

Tests

The implementation was successfully validated end-to-end on both Data Generation and Model Training levels:

  1. Parallel Shard Generation: Successfully streamed and converted the Salesforce/wikitext dataset (wikitext-103-raw-v1) into 38 contiguous .bagz file shards using multi-worker execution.
  2. End-to-End Model Training: Executed a 3-step offline training loop on a CPU-only environment using the generated local Bagz dataset via DECOUPLE_GCLOUD=TRUE (Log URL attached below).

Commands used for local testing:

# Data Generation
python tools/data_generation/download_hf_dataset_as_bagz.py \
  --dataset Salesforce/wikitext \
  --config '{"name": "wikitext-103-raw-v1"}' \
  --output ~/datasets/wikitext_bagz \
  --file-size-mb 10 \
  --workers 2

# MaxText CPU Smoke Test
export DECOUPLE_GCLOUD=TRUE
python -m maxtext.trainers.pre_train.train \
  src/maxtext/configs/base.yml \
  run_name="bagz_cpu_smoke_test" \
  steps=3 \
  dataset_type="grain" \
  grain_file_type="bagz" \
  grain_train_files="$HOME/datasets/wikitext_bagz/*.bagz" \
  grain_eval_files="$HOME/datasets/wikitext_bagz/*.bagz" \
  tokenize_eval_data=True \
  per_device_batch_size=1 \
  max_target_length=64 \
  enable_checkpointing=false \
  override_model_config=true \
  base_num_decoder_layers=1 \
  base_emb_dim=32 \
  base_mlp_dim=32 \
  head_dim=8 \
  base_num_kv_heads=1 \
  base_num_query_heads=1

Test Logs URL: https://paste.googleplex.com/6387838210932736

Comment thread src/maxtext/configs/types.py
Comment thread src/maxtext/input_pipeline/grain_data_processing.py Outdated
Comment thread src/maxtext/input_pipeline/grain_data_processing.py Outdated
"""A fully picklable and fork-safe RandomAccessDataSource for Bagz files."""
def __init__(self, paths: list[str] | str):
if isinstance(paths, (list, tuple)):
self._path = ",".join(sorted(paths))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we have to sort the paths here?

@aireenmei aireenmei self-assigned this Jul 25, 2026

@aireenmei aireenmei left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the feature! Could you also add unit tests in grain_data_processing_test.py? You will need a class setup class similar to _GrainArrayRecordSetup and a test class similar to GrainArrayRecordProcessingTest

Comment thread src/maxtext/input_pipeline/grain_data_processing.py Outdated
# limitations under the License.

"""
Download a HuggingFace dataset via streaming and save as Bagz files.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some example commands in the docstring?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I've added detailed example commands in the docstring covering basic local conversion, GCSFUSE-mounted bucket output with custom prefix/file size, and authenticated access for gated datasets

Implements end-to-end support for the Bagz (.bagz) dataset format inside the
MaxText training and data processing pipeline. Enables highly efficient,
POSIX-compliant file loading optimized specifically for GCS FUSE and local
filesystem environments.

Scope of Changes:
- Input Pipeline: Integrated upstream `grain.BagzDataSource` for direct, multiprocess-safe
  reading of Bagz file shards via PyGrain.
- Data Processing: Registered `bagz` as a first-class supported file type across
  PyGrain data loaders, feature normalizers, and tokenizers.
- Tooling: Created `download_hf_dataset_as_bagz.py` with multi-worker support,
  HuggingFace streaming, and resilient checkpointing for GCSFUSE mounts.

Testing:
- Unit Tests: Added comprehensive unit test suite in `tests/unit/bagz_data_processing_test.py`
  covering RandomAccessDataSource compatibility, MapDataset, ElasticIterator (single and multi-worker),
  and end-to-end `get_datasets` integration.
- End-to-End Local Smoke Test: Executed a 3-step offline training loop on a
  local CPU environment using the newly generated Bagz dataset via `DECOUPLE_GCLOUD=TRUE`.
- Parallel Data Generation: Successfully converted and streamed HF datasets into
  multi-shard Bagz files (38 shards, Salesforce/wikitext).
@Marlon666
Marlon666 force-pushed the feat/bagz-gcsfuse-support branch from 85206e8 to 7abdb4d Compare August 4, 2026 21:56
@Marlon666
Marlon666 requested a review from shuningjin as a code owner August 4, 2026 21:56
…ring

Add practical example commands to the docstring of `download_hf_dataset_as_bagz.py`:
- Basic local directory conversion (Salesforce/wikitext)
- Write-optimized GCSFUSE mounted bucket output with custom shard prefix and file size
- Private/gated HuggingFace dataset download using auth token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants