Skip to content

Add Bagz format support#4496

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

Add Bagz format support#4496
Marlon666 wants to merge 1 commit 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

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: Added `BagzDataSource` to support 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:
- 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).
- Resiliency: Verified resilience against rate limits and process crashes with
  standalone protobuf execution.

Test Logs URL: https://paste.googleplex.com/6387838210932736
" controller) enabled (`enable_single_controller=True`)."
)
if self.grain_use_elastic_iterator and self.grain_file_type != "arrayrecord":
if self.grain_use_elastic_iterator and self.grain_file_type not in ("arrayrecord", "bagz"):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Have we tested the elastic iterator with BagZ?

elastic=elastic,
)
return dataset
elif data_file_type == "bagz":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This logic looks exactly the same as arrayrecord besides create_dataset_from_pattern, should we combine them into one?

elif data_file_type == "bagz":
def create_dataset_from_pattern(pattern):
files = find_data_files(pattern)
source = input_pipeline_utils.BagzDataSource(files)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Any reasons not directly using bagz.Reader here, do we have to add the BagZDataSource? Example code: https://google-grain.readthedocs.io/en/latest/tutorials/data_sources/bagz_data_source_tutorial.html.

"""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?

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.

2 participants