Skip to content

Wire deterministic SyntheticMultimodalDataSource into Grain multimodal input pipeline - #6587

Draft
hengtaoguo wants to merge 1 commit into
mainfrom
hengtaoguo-mmperf
Draft

hengtaoguo wants to merge 1 commit into
mainfrom
hengtaoguo-mmperf

Conversation

@hengtaoguo

@hengtaoguo hengtaoguo commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Wires PR #6515's SyntheticMultimodalDataSource into MaxText's Grain multimodal SFT pipeline when dataset_type=synthetic and use_multimodal=true, while preserving the existing text-only SyntheticDataIterator fallback when no media modality is enabled. Each synthetic row is a pure function of (seed, index) and is deterministically sharded across hosts via strided slicing, establishing a reproducible multimodal performance baseline (PR 1) before adding packing and bucketing in follow-up PRs.

  • src/maxtext/input_pipeline/input_pipeline_interface.py: Route dataset_type == "synthetic" to make_synthetic_multimodal_{train,eval}_iterator when synthetic_modalities(config) is non-empty; fall back to SyntheticDataIterator otherwise.
  • src/maxtext/input_pipeline/grain_data_processing.py: Add make_synthetic_multimodal_{train,eval}_iterator backed by SyntheticMultimodalDataSource ([shard_index::shard_count]) piped into vision_sft_preprocessing_pipeline.
  • src/maxtext/input_pipeline/data_processing_utils.py: Skip ParseFeatures/NormalizeFeatures when config.dataset_type == "synthetic".
  • tests/unit/synthetic_multimodal_data_test.py: Add SyntheticMultimodalIteratorTest for Qwen3-VL shapes, determinism (grain_worker_count=0, 2), seed sensitivity, and [0, 0] text-only fallback.

Baseline MFU (qwen3-vl-2b, TPU v4-8 / 4 chips, from scratch, image_size_for_vit=768, 1 image/sample, steps 4–14 steady-state):

Step Time (median) TFLOP/s/device (reported) Tokens/s/device Baseline MFU (reported)*
0.351 s 64.44 5,844.2 23.43%

*Decoder-only FLOPs over full ViT+Decoder step time (64.44 / 275 TPU v4 BF16 peak); MaxText currently logs Vision encoder TFLOPs calculation not implemented for model qwen3-vl-2b, counting as 0 for now.

Tests

  • Unit tests (11 passed + 2 subtests for grain_worker_count=0, 2):
    JAX_PLATFORMS=cpu pytest tests/unit/synthetic_multimodal_data_test.py -v
  • E2E qwen3-vl-2b baseline (TPU v4-8):
    python -m maxtext.trainers.post_train.sft.train_sft_native \
      src/maxtext/configs/base.yml \
      model_name=qwen3-vl-2b \
      run_name=qwen3_vl_2b_mm_perf_baseline \
      base_output_directory=/tmp/mm_perf_runs \
      enable_checkpointing=false \
      async_checkpointing=false \
      dataset_type=synthetic \
      use_multimodal=true \
      sft_train_on_completion_only=true \
      packing=false \
      scan_layers=false \
      per_device_batch_size=1 \
      max_prefill_predict_length=1024 \
      max_target_length=2048 \
      synthetic_num_images_per_example_range=[1,1] \
      tokenizer_type=huggingface \
      grain_worker_count=2 \
      data_shuffle_seed=0 \
      steps=15 \
      eval_interval=0 \
      skip_jax_distributed_system=true
  • Determinism: Verified bit-identical batches across runs in unit tests (grain_worker_count=0, 2) and identical Step 0 loss (370.937) and total_weights across steps 0–2 (5571, 5564, 5580) across two fresh TPU runs.

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests and provided workload links above if applicable. (Note: New PRs get skip-ci-tests so only CPU/lint/docs checks run during review; a bot removes it after 2 approvals on a 1-commit PR to run TPU/GPU suites, or remove it yourself / ask a maintainer on fork PRs to run them sooner. On TPU7X, only the TPU tests that the PR adds or modifies run; add the run-tpu7x label to run the full TPU7X suite.)
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@github-actions github-actions Bot added the skip-ci-tests Add this label to the PR to skip running CI tests label Nov 25, 2026
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 73.80952% with 11 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...maxtext/input_pipeline/input_pipeline_interface.py 47.61% 8 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

🤖 Hi @hengtaoguo, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 Review Summary

Verdict: Spec partially met; multimodal synthetic training works as intended, but eval batch sizing and host expansion have latent defects and the eval iterator is untested.

Top issues:

  1. [Bug] When eval_per_device_batch_size differs from per_device_batch_size, _make_synthetic_multimodal_iterator asserts global_batch_size_to_load_eval but discards it, while vision_sft_preprocessing_pipeline batches strictly by global_batch_size_to_load (see inline comment on grain_data_processing.py). → Pass the resolved eval batch size into the pipeline or validate that eval and train batch sizes match.
  2. [Bug] Clamping expansion_loading_factor_for_grain=1 on eval breaks MultiHostDataLoadIterator when expansion_factor_real_data > 1, leaving hosts with unconcatenated, undersized batches (see inline comment on grain_data_processing.py). → Pass config.expansion_factor_real_data directly, or assert expansion_factor_real_data <= 1 for multimodal data since PlaceHolderDataIterator lacks media keys.
  3. [Tests] make_synthetic_multimodal_eval_iterator is untested because all unit tests set eval_interval=0 and assert assertIsNone (see inline comment on synthetic_multimodal_data_test.py). → Add a unit test with eval_interval > 0 verifying the eval iterator produces valid batches with the seed + 1 offset.
Inferred spec & validation
  • Sources: PR #6587 description, PR #6515 (SyntheticMultimodalDataSource), code before/after in src/maxtext/input_pipeline/.
  • Problem: SyntheticMultimodalDataSource was not wired into create_data_iterator when dataset_type=synthetic and use_multimodal=true, preventing reproducible multimodal SFT training benchmarks without external data files.
Spec item Implementation Validating test
R1 (stated): When dataset_type="synthetic" and synthetic_modalities(config) is non-empty, create_data_iterator returns multimodal iterators backed by SyntheticMultimodalDataSource sharded via strided slice across hosts ✅ met synthetic_multimodal_data_test.py::SyntheticMultimodalIteratorTest.test_create_data_iterator_qwen3_vl_shapes_and_determinism ✅
R2 (stated): When dataset_type="synthetic" and no media modality is active, fallback to SyntheticDataIterator is preserved ✅ met synthetic_multimodal_data_test.py::SyntheticMultimodalIteratorTest.test_zero_images_falls_back_to_text_synthetic_iterator ✅
R3 (stated): Skip protobuf ParseFeatures/NormalizeFeatures when dataset_type="synthetic", retaining in-memory synthetic features via KeepFeatures ✅ met synthetic_multimodal_data_test.py::SyntheticMultimodalIteratorTest.test_create_data_iterator_qwen3_vl_shapes_and_determinism ✅
R4 (stated): Return independent multimodal eval iterator when eval_interval > 0 with seed + 1 offset ⚠️ partial (batches by train batch size and breaks with expansion_factor_real_data > 1) ❌ none (eval_interval=0 in all unit tests)
C1 (stated): Deterministic row generation per host across varying worker counts (grain_worker_count=0, 2) ✅ preserved synthetic_multimodal_data_test.py::SyntheticMultimodalIteratorTest.test_create_data_iterator_qwen3_vl_shapes_and_determinism ✅
C2 (inferred): Evaluation data does not duplicate training data for corresponding step indices ✅ preserved (seed = config.data_shuffle_seed + 1) ❌ none
C3 (inferred): Proper batch sizing and multi-host data loading when expansion_factor_real_data > 1 or eval_per_device_batch_size != per_device_batch_size ❌ violated ❌ none

Comment on lines +1289 to +1290
global_batch_size = config.global_batch_size_to_load if is_training else config.global_batch_size_to_load_eval
assert global_batch_size % global_mesh.size == 0, "Batch size should be divisible by number of global devices."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟠 global_batch_size checks global_batch_size_to_load_eval for eval, but vision_sft_preprocessing_pipeline derives its batch size strictly from get_local_batch_size(config) which uses config.global_batch_size_to_load. If eval_per_device_batch_size is configured differently from per_device_batch_size, the assertion validates global_batch_size_to_load_eval while the iterator emits batches sized for training, causing a shape mismatch in the eval step.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch — added an is_training flag to get_local_batch_size and threaded it through vision_sft_preprocessing_pipeline so eval uses global_batch_size_to_load_eval. Fixed in 73b8d53.

Comment on lines +1312 to +1314
return multihost_dataloading.MultiHostDataLoadIterator(
dataloader,
global_mesh,
generate_padding_batch=(config.generate_padding_batch_train if is_training else config.generate_padding_batch_eval),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟠 Setting expansion_loading_factor_for_grain=1 when not is_training breaks eval when expansion_factor_real_data > 1. input_pipeline_interface.py reduces process_indices_eval to jax.process_count() // expansion_factor_real_data and get_local_batch_size divides the batch size by expansion_factor_real_data, so clamping to 1 leaves each loading host yielding an undersized batch without micro-batch concatenation. Pass config.expansion_factor_real_data directly, or assert expansion_factor_real_data <= 1 for multimodal data since PlaceHolderDataIterator lacks media keys.

Suggested change
generate_padding_batch=(config.generate_padding_batch_train if is_training else config.generate_padding_batch_eval),
expansion_loading_factor_for_grain=config.expansion_factor_real_data,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch — passing config.expansion_factor_real_data directly for eval as well now. Fixed in 73b8d53.

self.assertIsNone(eval_iter_a)

for _ in range(2):
batch_a = next(train_iter_a)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟠 make_synthetic_multimodal_eval_iterator is untested. Every test case runs with eval_interval=0 and asserts assertIsNone(eval_iter_a). Add a test with eval_interval > 0 asserting that eval_iter is instantiated, yields batches with the expected multimodal keys/shapes, and verifies that its content differs from train_iter (confirming the data_shuffle_seed + 1 offset).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added test_eval_iterator_uses_eval_batch_size_and_offset_seed covering eval batch shape, determinism, and the seed + 1 offset. Fixed in 73b8d53.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gemini-review skip-ci-tests Add this label to the PR to skip running CI tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant