Repository navigation
Keep weight decay off frozen vision and audio encoders - #6235
nuinashco-ks wants to merge 6 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request optimizes memory usage and training stability for multimodal models by ensuring that frozen vision and audio encoders are fully excluded from the optimizer's state and weight decay updates. By introducing an Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Review of #6235 (freeze vision/audio encoder weight decay) — the encoder-vs-projector split across encoders.py looks accurate. A few substantive items to address:
-
Inherits the
#6224bugs (and squash-merge rebase requirement):- Please pick up the
nnx_wrappers.pyand_leaf_path(composite PyTree / quantized weight) fixes from#6224— the_leaf_pathbug is directly relevant here if a frozen vision or audio encoder is quantized (QTensor/QArray). - Because this repo is squash-merge only, once
#6224lands this PR will need a rebase ontomain(keeping only49d1ffbea765) to avoid conflicts across the 6 shared files.
- Please pick up the
-
Test coverage gaps in
tests/unit/optimizers_test.py(FreezeEncodersTest):FreezeEncodersTestdoesn't assert thatnnx.state(optimizer)has nomu/nuslots allocated for frozen tower params (unlike#6224's_assert_untrainable_unchanged), so the zero-optimizer-state guarantee isn't verified by tests.- All tests use synthetic
_Encodermocks; adding a lightweightnnx.eval_shapetest against a realVisionEncoder/AudioEncoderwould ensure anencoder_nameor tower-attribute refactor can't silently re-enable weight decay.
— Review assisted by Jetski agent
49d1ffb to
773d80c
Compare
|
Both points are addressed, and the branch is rebased onto the updated #6224.
The test results are in the updated description. |
Description
freeze_vision_encoder_paramsandfreeze_audio_encoder_paramsnow also keep weight decay off the frozen encoder towers, and the optimizer no longer allocates state for them.Stacked on #6224 (
UntrainableParam), which adds thefreeze_paramswrapper this PR reuses. Until #6224 lands, this diff includes its commits; this PR's own commits: compare.Problem
Both flags default to
true, and both only applystop_gradientto the encoder output inVisionEncoder.__call__andAudioEncoder.__call__. The tower then gets a zero gradient, but AdamW's decoupled weight decay still shrinks every tower weight each step, and the optimizer keepsmu/nufor weights that never learn. To make the effect visible, a Gemma-4 E2B run (vision from #4790) took 3 AdamW steps withadam_weight_decay=2.0, the flag on andtrainable_parameters_mask=['vision_encoder']. The flag stops the tower's gradient, so weight decay was the only force on it, and 212 of the tower's 213 regular weights changed. At usual settings (lr 2e-5, weight decay 0.1) the shrink is 2e-6 per step: it rounds away with bfloat16 weights, and with float32 weights it adds up to about 0.2% per 1000 steps.Change
A new
frozen_param_paths(model, config)inoptimizers.pyreturns the paths the optimizer must not change: theUntrainableParamweights from #6224, plus, for each encoder whose flag is on, the weights under itsencoder_namechild.get_optimizer, the distillation optimizer and the RL optimizer route these paths tooptax.set_to_zero(), so they get no update, no weight decay and no optimizer state.stop_gradientalready cuts off.TunixMaxTextAdapter, which keeps the model underbase, is covered._ENCODER_FREEZE_FLAGS, tiesvision_encoderandaudio_encoderto their flags, so each flag freezes only its own encoder.The
stop_gradientstays: it skips the backward pass through the tower and keeps the tower out of gradient clipping andgrad_norm.Impact
Multimodal runs with a frozen encoder, which is the default, no longer hold that tower in the optimizer state: less memory, and a run started before this change cannot resume its optimizer state, while its weights load unchanged. Text-only models are unaffected, since their optimizer is built exactly as before. The optimizer in
m3/train/loop.pyis built without the model and is not covered by this change.Tests
Unit tests:
tests/unit/optimizers_test.py: newFreezeEncodersTest, 10 cases.VisionEncoderandAudioEncoderof Qwen3-Omni withnnx.eval_shapeand check that exactly the tower's weights are frozen.tests/post_training/unit/rl_utils_test.pyandtests/post_training/unit/train_distill_test.py: the existing weight-decay tests now include a vision encoder with the flag on.get_optimizernot using the paths, the RL change reverted, the distillation change reverted, optimizer state still allocated for a frozen tower, the tower attribute renamed in the real encoders.To reproduce:
JAX_PLATFORMS=cpu python3 -m pytest tests/unit/optimizers_test.py tests/unit/untrainable_param_test.py tests/post_training/unit/rl_utils_test.py tests/unit/gemma4_small_test.pypython3 -m pytest -m tpu_only tests/post_training/unit/train_distill_test.pyRun locally on
main(f8dfb87), on the #6224 branch and on this branch. Nothing that passes on #6224 fails on this branch:main, 1242 on Add UntrainableParam for weights that are never trained #6224, 1252 on this branch.main, 114 on Add UntrainableParam for weights that are never trained #6224 and on this branch.create_nnx_abstract_modelandget_optimizer, counting the weights that get optimizer state:Projectors and text weights are trained in every row (apart from Gemma-4's
layer_scalar, frozen by #6224), and wrapping the model inTunixMaxTextAdapter, as the RL trainer does, gives the same frozen set. Without this change, the same check shows the Gemma-3 and Gemma-4 towers trained with the flag on.pyink24.10.1 andpylintwith the repository settings: nothing new.Local failures are the same on
main, on #6224 and on this branch: 79 tests and 4 files that cannot be imported in the first CPU run (106 tests in the second), and 118 tests in the TPU run. The TPU failures need more devices, or library versions and packages that the local setup does not have. The 4 files needtunix.dist_rl, which is missing locally, so they rely on CI.Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.skip-ci-testsso 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 therun-tpu7xlabel to run the full TPU7X suite.)