Skip to content

Fix eval and checkpointing under load-balanced CP#4613

Draft
huytransformer wants to merge 1 commit into
AI-Hypercomputer:mainfrom
huytransformer:htn/cp-reorder-iterator
Draft

Fix eval and checkpointing under load-balanced CP#4613
huytransformer wants to merge 1 commit into
AI-Hypercomputer:mainfrom
huytransformer:htn/cp-reorder-iterator

Conversation

@huytransformer

Copy link
Copy Markdown
Collaborator

Description

Start with a short description of what the PR does and how this is a change from
the past.

The rest of the description includes relevant details and context, examples:

  • why is this change being made,
  • the problem being solved and any relevant context,
  • why this is a good solution,
  • some information about the specific implementation,
  • shortcomings of the solution and possible future improvements.

If the change fixes a bug or a Github issue, please include a link, e.g.,:
FIXES: b/123456
FIXES: #123456

You can also provide a comma-separated list. If you don't want to close a bug but
simply to reference it, use BUGS, e.g.:
BUGS: b/123456

Notice 1: Once all tests pass, the "pull ready" label will automatically be assigned.
This label is used for administrative purposes. Please do not add it manually.

Notice 2: For external contributions, our settings currently require an approval from a MaxText maintainer to trigger CI tests.

Tests

Please describe how you tested this change, and include any instructions and/or
commands to reproduce.

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 tests and provided workload links above if applicable.
  • 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.

…rder

setup_train_loop installed the context-parallel load-balance reorder as a bare
map(reorder_fn, data_iterator) and passed the result everywhere the original
iterator went. A map object supports iteration but loses both the MaxText
iterator interface and the concrete iterator type. With
ici_context_parallelism > 1 and context_parallel_load_balance (default true),
affected runs fail at the first scheduled evaluation because the eval loop
calls eval_data_iterator.reset(), or at the first grain checkpoint save or
restore because checkpointing dispatches on the concrete iterator type
(isinstance branches for RemoteIteratorWrapper, PlaceHolderDataIterator, and
iterator lists in checkpointing.py, then data_iterator.local_iterator access).

Wrapping the iterator in a forwarding class cannot restore the checkpoint
behavior because the dispatch is by isinstance, not attribute presence.
Instead, the original iterator now continues to setup_training_state and
checkpointing unchanged, and only the batch consumers receive the reordered
view: the DataLoader gets _ReorderedDataIterator (per element when the grain
pipeline returns an iterator list for 0 < expansion_factor_real_data < 1,
matching the list support in data_loader.py), and the eval iterator is wrapped
directly since it is never checkpointed and only needs reset() and iteration.
Checkpoint restore mutates the iterator in place, so the loader's view tracks
restored state.

Unit tests cover the reordered view (per-batch reorder, reset forwarding,
iteration protocol) and the loader-view builder (single iterator wrapped,
lists preserved with each element wrapped around the original iterator). An
integration test in setup_train_loop_nnx_test.py pins both installation
sites: with ici_context_parallelism=2 and context_parallel_load_balance the
DataLoader holds the reordered view around the exact iterator object
setup_train_loop returns for checkpointing, that returned iterator stays
unwrapped, and the eval iterator is the reordered view.

Local checks: DECOUPLE_GCLOUD=TRUE PYTHONPATH=src python3 -m pytest
tests/unit/train_utils_test.py -q (20 passed) and tests/unit/checkpointing_test.py
tests/unit/grain_utility_test.py tests/unit/multihost_dataloading_test.py -q
(27 passed, 5 skipped; the env var stubs ml_goodput_measurement, absent
locally and unrelated); the integration test executed end-to-end and passed on
CPU with XLA_FLAGS=--xla_force_host_platform_device_count=4 in a venv
providing ml_collections; pyink --pyink-indentation=2 --line-length=122
--check on all three files (unchanged); git diff --check clean.
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@codecov

codecov Bot commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

1 participant