Migrate examples, docs and docstrings onto compile / etrace_grad / etrace_evolve - #153
Merged
Merged
Conversation
…race_evolve braintrace.compile has been the documented entry point since before 0.2 and #151 landed the two sequence drivers, but neither migration finished: #151's spec scoped a list of sites, main moved underneath it twice (#148 reorganized the tutorials, #150 rewrote the API reference), and examples/ has no CI coverage — CI.yml runs pytest braintrace/ and nothing else. The repo therefore taught three different setups depending on which file you opened, and two of the un-migrated sites raised on the first call. Fixes two shipped bugs, both invisible to CI: - examples/drtrl/02-batching-vmap.py raised AttributeError: 'Vmap' object has no attribute 'etrace_grad'. #151 migrated its loop to etrace_grad but left the wrapper a bare brainstate.nn.Vmap; only ETraceVmap carries the drivers. - examples/004-feedforward-conv-snn.py:302 raised TypeError: got multiple values for argument 'model' — the model passed both positionally and by keyword, plus a decay_or_rank positional D_RTRL does not take. Survived because OnlineBatchTrainer is never instantiated in main() and 004 is skipped for want of NMNIST. The compile migration has nowhere to put either bad argument; both trainers were driven directly on a synthetic batch with tonic stubbed, which is what proves it fixed rather than rewritten. All five "# kept manual:" justifications were false or imprecise. Three sites migrated; drtrl/02 keeps its code as the worked expansion of compile(vmap=True) and pp_prop/05 keeps its (it had been routing through compile via _shared since #151 while its docstring still described the manual path). The 003 benchmarks' reasons are restated as the precise limitations: a third state scheme compile does not offer, and a ShapeDtypeStruct example input compile's vmap axis-strip cannot subscript. Docs: one scan-accumulate block, byte-identical in three notebooks, becomes etrace_grad(reduction='mean'); concepts collapses to reduction='sum'; four prose passages that still named brainstate.transform.scan as the mechanism are rewritten. Nine docstring Examples blocks gain a driver tail — ThreeFactor rides its modulator as a second sequence, UORO documents window mode. Both driver substitutions are bit-exact against the current code, which is what licenses re-executing the notebooks and requiring unchanged numbers. drtrl and pp_prop text output is identical; quickstart differs only in repr addresses. concepts cell 13 drifts in the last float digits, but the pre-edit notebook executed on the same machine drifts identically, so it is environmental — and cell 16, the migrated one, is bit-identical. Verified: pytest examples/ 49 passed 5 skipped; pytest braintrace/ 2661 passed; mypy clean; all 15 docstring snippets execute; both 004 arms run.
There was a problem hiding this comment.
Sorry @chaoming0625, your pull request is larger than the review limit of 150000 diff characters
Reviewer's GuideMigrates examples, tutorials, notebooks, and algorithm docstrings to consistently use Sequence diagram for training and evaluation with compile + etrace driverssequenceDiagram
actor User
participant Learner as compiled_learner
participant Model
participant Optimizer
%% Training epoch
User->>Learner: etrace_grad(inputs, targets, step_fn=local_loss, reduction='mean', return_value=true)
loop for each time_step
Learner->>Model: local_loss(x_t, y_t)
Model-->>Learner: step_loss_t, step_grads_t
Learner->>Learner: accumulate_gradients(step_grads_t)
end
Learner-->>User: grads, step_losses
User->>Optimizer: update(grads)
%% Evaluation sequence
User->>Learner: etrace_evolve(inputs, return_outputs=true)
loop for each time_step
Learner->>Model: forward(x_t)
Model-->>Learner: y_t
Learner->>Learner: update_hidden_and_etrace
end
Learner-->>User: predictions
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Why
braintrace.compilehas been the documented entry point since before 0.2, and #151 landed the two sequence drivers. Neither migration finished: #151's spec scoped a list of sites,mainmoved underneath it twice (#148 reorganized the tutorials, #150 rewrote the API reference), andexamples/has no CI coverage —CI.ymlrunspytest braintrace/and nothing else.So the answer to "how do I train a model with braintrace?" depended on which file you opened:
compile(...)+etrace_grad(...)examples/drtrl/01,docs/advanced/batching.ipynbcompile(...), then a hand-writtenscan-accumulatedocs/quickstart/quickstart.ipynb,docs/tutorials/drtrl.ipynbAlgo(model)+init_all_states+compile_graph+Vmapexamples/004,examples/100-gru'batch'armTwo shipped bugs fixed
Both invisible to CI.
examples/drtrl/02-batching-vmap.pyraisedAttributeError: 'Vmap' object has no attribute 'etrace_grad'. Sequence driver API (etrace_grad / etrace_evolve), and a test suite that finishes #151 migrated its loop toetrace_gradbut left the wrapper a barebrainstate.nn.Vmap; onlyETraceVmapcarries the drivers.examples/004-feedforward-conv-snn.py:302raisedTypeError: ParamDimVjpAlgorithm.__init__() got multiple values for argument 'model'— the model passed both positionally and by keyword, plus adecay_or_rankpositionalD_RTRLdoes not take. It survived becauseOnlineBatchTraineris never instantiated inmain()and004is skipped for want of NMNIST.The
compilemigration has nowhere to put either bad argument. Since no test reaches004, both trainers were driven directly on a synthetic batch withtonicstubbed — that run is what proves bug 2 fixed rather than merely rewritten, and it confirms both trainers compile the same trace graph.All five
# kept manual:reasons were wrongChecked against
_compile.py. Three sites migrated. Two keep their code with a corrected reason:drtrl/02because it is the worked expansion ofcompile(vmap=True), andpp_prop/05because it had been routing throughcompilevia_sharedsince #151 while its note and docstring still described the manual path. The003-*benchmarks' reasons are restated as the precise limitations — a third state schemecompiledoes not offer, and aShapeDtypeStructexample input its vmap axis-strip cannot subscript.Docs and docstrings
One
scan-accumulate block, byte-identical in three notebooks, becomesetrace_grad(reduction='mean');conceptscollapses toreduction='sum'. Four prose passages still namedbrainstate.transform.scanas the mechanism — that matters as much as the code, since it is why a reader would write ascanafter reading migrated code.Nine docstring
Examplesblocks built a learner and stopped at one forward call, so the per-class API pages never showed a driver. Two differ on purpose:ThreeFactorrides its per-stepmodulatoras a second sequence, andUOROis the only documented example of window mode.Equivalence
Both driver substitutions are bit-exact against the current code, which is what licenses re-executing the notebooks and requiring unchanged numbers.
drtrlandpp_proptext output is identical;quickstartdiffers only inrepraddresses.conceptscell 13 drifts in the last float digits — but the pre-edit notebook executed on the same machine drifts identically, so it is environmental, and cell 16 (the migrated one) is bit-identical across all three runs.Verification
pytest examples/pytest braintrace/ -n 6mypy braintrace004arms (skipped by tests)Spec:
docs/specs/2026-07-28-compile-and-driver-migration.md, recording the canonical replacements, the measured facts, what stays manual and why, and four declared limitations found while auditing (ETraceVmapdoes not forwardshow_graph/graph/report/param_states;compile(**options)cannot carrymodel=; no path for the003-*state scheme;ShapeDtypeStructfails the vmap axis-strip). Recorded, not fixed — changingcompilewas out of scope.Worth flagging:
examples/and the docstring snippets are both documentation that can break silently, and these two bugs are what that costs.braintrace/_docs_examples_test.pywas deleted in #150, so nothing runs the snippets now. Wiring either into CI is the change that would stop this recurring.🤖 Generated with Claude Code
Summary by Sourcery
Migrate examples, tutorials, notebooks and algorithm docstrings onto braintrace.compile plus the etrace_grad/etrace_evolve drivers, fix two broken example trainers, and record the compile/vmap limitations and state schemes in a dedicated spec so the repository consistently teaches and uses a single way to build and drive learners over sequences.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: