Skip to content

PERF CodeBrain: compile into one graph (lazy kernel-norm init without a tensor truth check) - #1578

Merged
bruAristimunha merged 4 commits into
braindecode:masterfrom
bruAristimunha:perf/codebrain-compile
Nov 18, 2026
Merged

bruAristimunha merged 4 commits into
braindecode:masterfrom
bruAristimunha:perf/codebrain-compile

Conversation

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Summary

Under torch.compile, CodeBrain breaks 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_dict override 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:
    • a _kernel_norm_ready Python attribute, set together with the buffer on the first forward;
    • _load_from_state_dict sets it from the loaded kernel_norm_initialized.
  • New test/unit_tests/models/test_codebrain_compile.py:
    • one graph and no graph break (torch._dynamo.explain);
    • at most 2 graphs over two training steps;
    • a checkpoint saved before any forward still initialises once, as before;
    • a loaded initialised checkpoint keeps its stored norm;
    • an inference-mode first forward followed by a training step raises no error.
  • docs/whats_new.rst entry.

Testing

uv run --extra tests python -m pytest test/unit_tests/models/test_codebrain_compile.py test/unit_tests/models/test_models.py test/unit_tests/models/test_ch_mask.py -q -k "codebrain or CodeBrain"
# 8 passed
pre-commit run --files braindecode/models/codebrain.py test/unit_tests/models/test_codebrain_compile.py docs/whats_new.rst
# all hooks passed

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.

  • New or changed behavior is covered by relevant regression tests
  • Style checks recorded (above)
  • docs/whats_new.rst updated

Notes for reviewers

  • The state_dict keys are unchanged. load_state_dict from the original-checkpoint format (weight_g/weight_v) is untouched.
  • Separate and pre-existing: with an AOT backend (aot_eager) and spectral dropout active, the training backward fails on master and on this branch in the same way, at spectral_proj. It fails with "view size is not compatible with input tensor's size and stride" on an aten.view of the native_dropout_backward output. 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.

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.
Copilot AI balanced review requested due to automatic review settings November 18, 2026 00:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 00:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.82%. Comparing base (a78cb98) to head (653bd54).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 05:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 06:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@bruAristimunha
bruAristimunha merged commit e8882f0 into braindecode:master Nov 18, 2026
10 checks passed
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.

2 participants