-
Notifications
You must be signed in to change notification settings - Fork 460
Fix gradient accumulation in post training sft #3040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
69e7031 to
f36a364
Compare
b891b70 to
d44bc7b
Compare
5ddf27b to
99d4d28
Compare
|
🤖 Hi @ChingTsai, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📋 Review Summary
This pull request introduces a new implementation for gradient accumulation, "Tunix GA," which is handled at the data pipeline level. The changes are well-structured, and the logic is consistently applied across the configuration, data processing, and training loop. The addition of a comprehensive integration test is a great way to ensure the correctness of this new feature.
🔍 General Feedback
- The implementation is clean and avoids major refactoring by integrating the logic into the existing data pipeline.
- The use of a specific entrypoint to enable the feature (
use_tunix_ga=True) is a good way to control the configuration. - The integration test is robust and covers the essential aspects of the feature.
| os.environ["TF_CPP_MIN_LOG_LEVEL"] = "0" | ||
|
|
||
| mt_config = pyconfig.initialize(argv) | ||
| mt_config = pyconfig.initialize(argv, use_tunix_ga=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can use_tunix_ga be part of SFT configs: https://github.com/AI-Hypercomputer/maxtext/blob/main/src/MaxText/configs/sft.yml? It can be set to True by default. Also, we can rename it to use_tunix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
e628e2c to
5a4f67e
Compare
src/MaxText/sft_trainer.py
Outdated
| os.environ.get("LIBTPU_INIT_ARGS", "") + " --xla_tpu_spmd_rng_bit_generator_unsafe=true" | ||
| ) | ||
| config = pyconfig.initialize(argv) | ||
| config = pyconfig.initialize(argv, use_tunix=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add an override here to maintain backward compatibility with the native sft_trainer.
6926f52 to
a49598f
Compare
| use_tunix: bool = Field( | ||
| False, | ||
| description="Whether to use the Tunix implementation for gradient accumulation.", | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name use_tunix is too broad. If we are only interested in cases for sft + tunix + GA, please use a more specific name, e.g. use_tunix_gradient_accumulation.
| batch_size = global_batch_size // jax.process_count() | ||
| # Tunix GA requires per-micro-batch slicing at the data level, | ||
| # whereas Native GA processes the full batch and splits it internally. | ||
| batch_size = global_batch_size // jax.process_count() // (config.gradient_accumulation_steps if config.use_tunix else 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would perfer
if config.use_tunix:
batch_size = ()
else:
batch_size = ()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. I love expressions over statements but this is a bit complex for the former
| assert global_batch_size % global_mesh.size == 0, "Batch size should be divisible by number of global devices." | ||
| # Tunix GA requires per-micro-batch slicing at the data level, | ||
| # whereas Native GA processes the full batch and splits it internally. | ||
| batch_size = global_batch_size // jax.process_count() // (num_microbatches if use_tunix else 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same with comment above
src/MaxText/train.py
Outdated
| # EPS was used to avoid division by zero, but it's not needed when gradient | ||
| # accumulation is enabled since there's no division. | ||
| if config.gradient_accumulation_steps > 1: | ||
| if config.gradient_accumulation_steps > 1 and not config.use_tunix: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add comments explaining why we don't follow standard GA path when use_tunix=true
NuojCheng
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bit concerning to me why this PR fails tests/integration/train_tests.py::TrainTests::test_tpu_zero1_gradient_accumulation with OOM. The changes made by this PR should not affect regular GA performance in train.py. We will need to take a look on xprofs to figure out why.
richjames0
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once Nuojin's comments are addressed and he's happy :)
| batch_size = global_batch_size // jax.process_count() | ||
| # Tunix GA requires per-micro-batch slicing at the data level, | ||
| # whereas Native GA processes the full batch and splits it internally. | ||
| batch_size = global_batch_size // jax.process_count() // (config.gradient_accumulation_steps if config.use_tunix else 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. I love expressions over statements but this is a bit complex for the former
49587e1 to
d56abda
Compare
f14c32a to
d7e843e
Compare
f70b81c to
6855036
Compare
Description
FIXES: b/478823561
This PR resolves discrepancies in loss calculation and training step counts when running SFT with gradient accumulation enabled. Currently,
MaxText.sft.sft_trainer(which uses the Tunix trainer) exhibits breaking behavior when gradient accumulation is turned on, diverging significantly from the native implementation inMaxText.sft_trainer. This change aligns the Tunix-based SFT logic to match the native behavior.Problem Statement
Changes
Introduced a new configuration
use_tunixModified the loss function
Pre-sliced batches in data preprocessing
Add Tunix SFT integration test for gradient accumulation loss verification here
Tests
End2End
After applying the changes, the loss graphs of both versions are now almost identical.
Integration Test
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.