Repository navigation
PERF CodeBrain: compile into one graph (lazy kernel-norm init without a tensor truth check) - #1578
Merged
bruAristimunha merged 4 commits intoNov 18, 2026
Conversation
Under torch.compile the check 'if not self.kernel_norm_initialized:' broke the graph once per _GConv, on every device (19 breaks per forward, 21 graphs for two training steps). A Python bool mirror of the buffer, synced on load, gives one graph; checkpoints and outputs are unchanged.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1578 +/- ##
=======================================
Coverage 91.82% 91.82%
=======================================
Files 179 179
Lines 23375 23380 +5
=======================================
+ Hits 21464 21469 +5
Misses 1911 1911 🚀 New features to boost your workflow:
|
# Conflicts: # docs/whats_new.rst
# Conflicts: # docs/whats_new.rst
3 tasks done
3 tasks done
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
Under
torch.compile,CodeBrainbreaks its graph 19 times per forward, on every device. Each break is the lazy kernel-norm initialisation in_GConv.forward,if not self.kernel_norm_initialized:: that branches on a tensor's value, which also forces a host sync. Two training steps compile into 21 graphs.This PR keeps the buffer, for checkpoints, and adds a Python bool mirror that dynamo guards on once. A
_load_from_state_dictoverride syncs the mirror from a loaded checkpoint. Results: 0 graph breaks, and at most 2 graphs for the same two steps. Checkpoints and outputs are unchanged.Changes
models/codebrain.py, in_GConv:_kernel_norm_readyPython attribute, set together with the buffer on the first forward;_load_from_state_dictsets it from the loadedkernel_norm_initialized.test/unit_tests/models/test_codebrain_compile.py:torch._dynamo.explain);docs/whats_new.rstentry.Testing
On master, the two compile tests fail with 19 graph breaks (all "data-dependent branching",
codebrain.py:508) and 21 graphs. With this PR they pass.docs/whats_new.rstupdatedNotes for reviewers
load_state_dictfrom the original-checkpoint format (weight_g/weight_v) is untouched.aot_eager) and spectral dropout active, the training backward fails on master and on this branch in the same way, atspectral_proj. It fails with "view size is not compatible with input tensor's size and stride" on anaten.viewof thenative_dropout_backwardoutput. The likely cause is a stride mismatch between that op's meta and CPU kernels, i.e. PyTorch rather than CodeBrain, but this is unverified. It is not addressed here.