Repository navigation
FIX CodeBrain: keep the kernel norm in a copy fed through its buffers - #1866
Merged
bruAristimunha merged 3 commits intoDec 1, 2026
Merged
Conversation
A copy made before the first forward and then given the model's values (an EMA copy), or a DataParallel replica, has kernel_norm_initialized set but the Python flag unset, so its first forward overwrote the kernel norm with the norm of the current kernels. The norm is now written only where the buffer is unset.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Since #1578 (2.1.0),
_GConvcomputes its kernel norm when a Python flag is unset, and only_load_from_state_dictsyncs that flag with thekernel_norm_initializedbuffer. A copy made before the first forward and then given the model's values in place, as an EMA copy is, has the buffer set but not the flag: its first forward overwriteskernel_norm, so it returns other outputs than the model with the samestate_dict. ADataParallelreplica sets the flag on itself, so every step recomputes the norm.Toy run (script below):
AveragedModel(use_buffers=True)or in-place copy, made before the first forwardDataParallel:kernel_normchanged at steps 1, 2Changes
_GConv.forwardwrites the computed norm only wherekernel_norm_initializedis unset (torch.where, no host read; the model still compiles into one graph).test_codebrain_compile.py;whats_new.rst.Testing
Windows 11, Python 3.12.10, torch 2.15.0+cpu, on
masterat 92cb203 and on this branch, with-p no:cacheproviderand-n 2where shown. Counts are master / branch.pytest -rfEs -n 2 test/unit_tests/models/ --select-models=CodeBrain -k "CodeBrain or completeness or summary_csv or categorization": 83 / 84 passed (the new test), 8 / 8 skipped, 0 failed; 71 test ids contain[CodeBrain.pytest -rfEs -n 2 test/unit_tests/models/test_model_contract.py --select-models=CodeBrain -k CodeBrain: 12 / 12 passed, 3 / 3 skipped; 15 ids contain[CodeBrain.pytest -rfEs test/unit_tests/models/test_pretrained_compat.py --select-models=CodeBrain -k "CodeBrain or covers_every or no_strategy": 15 / 15 passed.pytest -rfEs test/unit_tests/models/test_codebrain_compile.py: 5 / 6 passed.Skip reasons: pydantic not installed, fixed length (2),
torch.compileon Windows, no embedding parameter, forward reads tensor values onmeta(2), noch_mask.test_config.py --select-models=CodeBrain, run again with numpydantic 2.0.1 installed: 19 passed on master and here.torch 2.6.0 (the floor),
test_integration.py --select-models=CodeBrain -k "CodeBrain and (exported or torch_script)": 2 passed on master and here; the new test passes.The 23 other files listed by
scripts/ci_plan.py(select_models: CodeBrain),-n 2 --select-models=CodeBrain: 1681 / 1681 passed, 43 / 43 skipped (network 18, MPS 6, noch_mask4, Windowstorch.compile2,meta2, fixed length 2, no embedding parameter 2, EEGNet**kwargs2, then one each: pydantic, sphinx, Windows bfloat16 fold, CUDA, HF token), 13 / 13 failed, the same on both: 5 Windows symlink tests (WinError 1314) and 8UnicodeDecodeError(cp1252). WithPYTHONUTF8=1,test_model_rules.py,test_private_blocks.pyandtest_dependencies.py: 19 / 19 passed.Mutation: restoring
self.kernel_norm.copy_(norm)failstest_codebrain_copy_fed_through_its_buffers_keeps_the_kernel_norm.Script below: master, branch and v2.0.0 outputs as in the table.
pre-commit run --fileson the three files: passed.Not run: several GPUs.
New or changed behavior is covered by relevant regression tests (or N/A with reason)
Style checks recorded, e.g.
pre-commit run --files <changed files>docs/whats_new.rstupdated (at most 3 lines), or theno changeloglabel, which a maintainer adds, for changes to code that is not released yet (the changelog CI check requires one of the two)Notes for reviewers
n_times400 and 1000, outputs,return_features, all gradients,state_dict, a reload andfrom_config(get_config())are bitwise equal to master (1570 tensors)._V2_MODELS, which would need fixtures regenerated under torch 2.6.DataParallelwas emulated on CPU, not run on GPUs. The effect was not measured on a real training.Script