brainx-skill 1.0.12 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/installation/lib/bundle.js +51 -13
- package/manifest.json +2 -0
- package/mcp-servers/codex/README.md +62 -0
- package/mcp-servers/codex/UPSTREAM.md +20 -0
- package/mcp-servers/codex/server.mjs +140 -0
- package/mcp-servers/codex/skills.json +10 -0
- package/mcp-servers/codex/system-prompt.md +62 -0
- package/mcp-servers/codex/test/fixtures/fake-codex.mjs +6 -0
- package/mcp-servers/codex/test/server.test.mjs +88 -0
- package/package.json +6 -9
- package/skills/brainx-acceleration/SKILL.md +459 -0
- package/skills/brainx-general-guard/SKILL.md +14 -9
- package/skills/brainx-modeling-loop/SKILL.md +268 -0
- package/skills/brainx-modeling-loop/agents/openai.yaml +4 -0
- package/skills/brainx-modeling-loop/references/monitor-experiment.md +49 -0
- package/skills/brainx-modeling-loop/references/parameter-fitting-workflow/scripts/fitting_hh_neuron.py +254 -0
- package/skills/brainx-modeling-loop/references/parameter-fitting-workflow.md +232 -0
- package/skills/brainx-modeling-loop/references/run-experiment.md +179 -0
- package/skills/brainx-modeling-loop/references/training-workflow.md +270 -0
- package/skills/{braincell → package-skills/braincell}/SKILL.md +2 -1
- package/skills/{braincell → package-skills/braincell}/references/multicompartment/multicompartment-cell-workflow.md +2 -2
- package/skills/package-skills/braincell/references/scripts/fitting_hh_neuron.py +254 -0
- package/skills/{brainmass → package-skills/brainmass}/references/brainstate/parameter-constraints-regularization.md +1 -1
- package/skills/{brainmass → package-skills/brainmass}/references/coupling-network-api.md +121 -3
- package/skills/{brainmass → package-skills/brainmass}/references/modellibrary.md +79 -1
- package/skills/package-skills/brainmass/references/parameter-sweeps-and-regime-analysis.md +188 -0
- package/skills/{brainmass → package-skills/brainmass}/references/visualization-analysis-api.md +6 -6
- package/skills/{brainpy-state → package-skills/brainpy-state}/SKILL.md +4 -3
- package/skills/{brainpy-state → package-skills/brainpy-state}/references/braintools/connectivity.md +1 -1
- package/skills/{brainpy-state → package-skills/brainpy-state}/references/braintools/input-current.md +1 -1
- package/skills/package-skills/brainpy-state/references/perturbation-experiment-validity.md +76 -0
- package/skills/{brainpy-state → package-skills/brainpy-state}/references/projection-patterns.md +1 -1
- package/skills/{brainstate → package-skills/brainstate}/SKILL.md +1 -0
- package/skills/{brainstate → package-skills/brainstate}/references/brainstate/brainstate-transformed-diagnostics.md +1 -1
- package/skills/{brainstate → package-skills/brainstate}/references/brainstate/parameter-constraints-regularization.md +1 -1
- package/skills/{brainstate → package-skills/brainstate}/references/brainstate/randomness-and-reproducibility.md +1 -1
- package/skills/{brainstate → package-skills/brainstate}/references/brainstate/transformation-grad-expansion.md +2 -2
- package/skills/{brainstate → package-skills/brainstate}/references/brainstate/transformation-vmap-expansion.md +1 -1
- package/skills/{brainstate → package-skills/brainstate}/references/libraries/prebuilt-layer-library.md +1 -1
- package/skills/package-skills/brainstate/references/paired-perturbation-execution.md +113 -0
- package/skills/package-skills/braintrace/SKILL.md +187 -0
- package/skills/package-skills/braintrace/references/Drtrl.md +133 -0
- package/skills/{braintrace → package-skills/braintrace}/references/ETP operators.md +31 -4
- package/skills/package-skills/braintrace/references/algorithm selection.md +64 -0
- package/skills/package-skills/braintrace/references/batching.md +140 -0
- package/skills/{braintrace → package-skills/braintrace}/references/compiler_internal.md +2 -2
- package/skills/{braintrace → package-skills/braintrace}/references/custom algorithms.md +8 -10
- package/skills/package-skills/braintrace/references/pp_pprop workflow.md +179 -0
- package/skills/{braintrace → package-skills/braintrace}/references/pre-built-braintrace-layer.md +2 -2
- package/skills/brainmass/references/parameter-sweeps-and-regime-analysis.md +0 -130
- package/skills/braintrace/SKILL.md +0 -161
- package/skills/braintrace/references/Drtrl.md +0 -179
- package/skills/braintrace/references/algorithm selection.md +0 -120
- package/skills/braintrace/references/batching.md +0 -150
- package/skills/braintrace/references/pp_pprop workflow.md +0 -196
- /package/skills/{braincell → package-skills/braincell}/references/area-scaled-hh-pattern.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/braincell-custom-ion-channel-authoring.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/channel-library.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/ion-library.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/mixions-for-adaptation.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/multicompartment/braincell-manual-morphology-construction.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/multicompartment/cv-policy-reference.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/multicompartment/filter-function-library.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/multicompartment/morphology-io-loading-validation.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/multicompartment/probe-reference.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/multicompartment/topology-building-and-visualization.md +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/scripts/calcium_channel_gating.py +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/scripts/cell_multicompartment_reference.py +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/scripts/channel_ablation.py +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/scripts/fi_curve.py +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/scripts/hh_neuron_basics.py +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/scripts/spike_frequency_adaptation.py +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/scripts/t_current_rebound.py +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/scripts/thalamic_neurons.py +0 -0
- /package/skills/{braincell → package-skills/braincell}/references/solver-library-with-effects.md +0 -0
- /package/skills/{brainevent → package-skills/brainevent}/SKILL.md +0 -0
- /package/skills/{brainevent → package-skills/brainevent}/references/connectivity-variants.md +0 -0
- /package/skills/{brainevent → package-skills/brainevent}/references/custom-operators-cpu.md +0 -0
- /package/skills/{brainevent → package-skills/brainevent}/references/custom-operators-gpu.md +0 -0
- /package/skills/{brainevent → package-skills/brainevent}/references/scripts/102_EI_net_1996.py +0 -0
- /package/skills/{brainevent → package-skills/brainevent}/references/scripts/204_joglekar_2018_propagation.py +0 -0
- /package/skills/{brainevent → package-skills/brainevent}/references/scripts/coba_ei_teaching.py +0 -0
- /package/skills/{brainevent → package-skills/brainevent}/references/sparse-formats.md +0 -0
- /package/skills/{brainevent → package-skills/brainevent}/references/synaptic-plasticity.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/SKILL.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/batch-transform-acceleration.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/brainstate/parameter-transforms-regularizers-catalog.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/braintools/cogtask.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/braintools/data-preprocessing.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/braintools/metric.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/braintools/optimizer.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/braintools/parameter-initializer.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/braintools/surrogate.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/datasets-api.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/fitting-with-objectives-api.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/forward-observation-api.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/horn-task-training.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/noiseprocesses.md +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/eeg-fitting-with-gradients.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/gradient-free-fitting.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/hopf-bifurcation-single-node.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/horn-cognitive-task-training.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/jansen-rit-eeg-proxy.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/kuramoto-synchronization.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/linear-baseline-node.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/resting-state-meg-whole-brain-pipeline.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/seizure-epileptor-case-study.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/wilson-cowan-ei-dynamics.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/wong-wang-decision-making.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/scripts/wong-wang-dmf-resting-state.py +0 -0
- /package/skills/{brainmass → package-skills/brainmass}/references/simulator-input-monitor-api.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/array-creation.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brain-dynamics-delay-protocol.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/analysis.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/brainpy legacy workflow.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/built-in dynamic neuron model.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/connecting neurons.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/customize neuron and synpase.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/infrastructure/Input generation.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/infrastructure/More about simulation.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/infrastructure/Multi-device array sharding.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/infrastructure/Parallel experiment execution.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/infrastructure/array creation and mechanics.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/infrastructure/brainpy math environment setting.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/infrastructure/delays.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/infrastructure/object oriented transformations and control flows.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/integrators.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/route activity through connectivity.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/synaptic projections.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/synpase properties.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/training/loss library.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/training/optimizers.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/training/parameter initializers.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/training/prebuilt neural network layers.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/training/surrogate gradients.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/training/trainer library.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/brainPy(legacy)/training/trainingworkflows.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/braintools/brainstate-control-flow-patterns.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/braintools/data-preprocessing.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/braintools/metric.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/braintools/optimizer.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/braintools/parameter-initializer.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/braintools/surrogate.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/component-selection.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/devices.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/divergence-and-parity.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/integration-categories.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/model-library.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/nest-workflow.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/network-building.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/scripts/brette_et_al_2007.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/scripts/brunel_alpha.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/scripts/brunel_delta.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/scripts/clopath_synapse_spike_pairing.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/scripts/evaluate_tsodyks2_synapse.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/scripts/spatial_gaussex.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/scripts/synapsecollection.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/nest-compatible/synapse-and-connectivity.md +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/scripts/103_COBA_2005.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/scripts/106_COBA_HH_2007.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/scripts/107_gamma_oscillation_1996.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/scripts/109_fast_global_oscillation.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/scripts/201_surrogate_grad_lif_fashion_mnist.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/scripts/sound_localization.py +0 -0
- /package/skills/{brainpy-state → package-skills/brainpy-state}/references/scripts/training-snn.py +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/agents/openai.yaml +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/brainstate/brainstate-control-flow-patterns.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/brainstate/parameter-transforms-regularizers-catalog.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/brainstate/transformation-jit-expansion.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/collective_model_operations.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/extension_mechanisms.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/libraries/prebuilt-activation-library.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/model-interop-and-migration.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/simulation-environment.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/size-inference-variations.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/state-graph-operations.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/references/state_collections_and_utilities.md +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/scripts/integrator_rnn.py +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/scripts/lif_neuron_model.py +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/scripts/modern_cnn.py +0 -0
- /package/skills/{brainstate → package-skills/brainstate}/scripts/resnet.py +0 -0
- /package/skills/{braintrace → package-skills/braintrace}/references/custom ETP primitives.md +0 -0
- /package/skills/{braintrace → package-skills/braintrace}/references/customizing_primitive_transforms.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/SKILL.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/references/array-creation.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/references/array-mechanics/functional-structural-api.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/references/array-mechanics.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/references/math-function-library.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/references/physical-constant-library.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/references/prefix-library.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/references/quantity-inspection-and-conversion.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/references/typing-and-runtime-validation.md +0 -0
- /package/skills/{brainunit → package-skills/brainunit}/references/unit-structure-and-definition.md +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Brainstate randomness and reproducibility
|
|
2
2
|
|
|
3
|
-
Use this reference after `skills/brainstate/SKILL.md` for advanced randomness. It covers independent streams, stochastic transforms, key control, parallel keys, replay, and checkpointing.
|
|
3
|
+
Use this reference after `skills/package-skills/brainstate/SKILL.md` for advanced randomness. It covers independent streams, stochastic transforms, key control, parallel keys, replay, and checkpointing.
|
|
4
4
|
|
|
5
5
|
The root skill owns `brainstate.random.DEFAULT` and ordinary seeding. This reference owns advanced stream and key control.
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# BrainState gradient and autodiff expansion
|
|
2
2
|
|
|
3
|
-
Use this reference after `skills/brainstate/SKILL.md` when a task needs gradient targets beyond the main skill's minimal parameter update, exact `return_value`/`has_aux` destructuring, state discovery in an arbitrary function, vector- or matrix-valued sensitivities, second derivatives, or an optimizer-integrated fitting step. The main skill owns the canonical `ParamState` collection and manual update; the parameter reference owns the choice between `ParamState` and `nn.Param`.
|
|
3
|
+
Use this reference after `skills/package-skills/brainstate/SKILL.md` when a task needs gradient targets beyond the main skill's minimal parameter update, exact `return_value`/`has_aux` destructuring, state discovery in an arbitrary function, vector- or matrix-valued sensitivities, second derivatives, or an optimizer-integrated fitting step. The main skill owns the canonical `ParamState` collection and manual update; the parameter reference owns the choice between `ParamState` and `nn.Param`.
|
|
4
4
|
|
|
5
5
|
These three sources do not establish BrainCell solver, reset, or conductance-fitting semantics. Specialized dynamics belongs to the matching BrainCell or BrainPy-State skill; use this reference only for the source-supported autodiff contract around that computation.
|
|
6
6
|
|
|
@@ -440,7 +440,7 @@ def train_step(x, y):
|
|
|
440
440
|
return loss
|
|
441
441
|
```
|
|
442
442
|
|
|
443
|
-
Return to `skills/brainstate/SKILL.md` for the general training structure. Open `references/braintools-optimizer-reference.md` only for optimizer or scheduler selection.
|
|
443
|
+
Return to `skills/package-skills/brainstate/SKILL.md` for the general training structure. Open `references/braintools-optimizer-reference.md` only for optimizer or scheduler selection.
|
|
444
444
|
|
|
445
445
|
Official source: https://brainx.chaobrain.com/brainstate/tutorials/core/07_training_and_metrics.html
|
|
446
446
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# BrainState vmap expansion
|
|
2
2
|
|
|
3
3
|
Use this reference after the minimal `brainstate.transform.vmap` example in
|
|
4
|
-
`skills/brainstate/SKILL.md` when argument axes, State axes, random-key policy,
|
|
4
|
+
`skills/package-skills/brainstate/SKILL.md` when argument axes, State axes, random-key policy,
|
|
5
5
|
nested mappings, or unexpected State writes require explicit control.
|
|
6
6
|
|
|
7
7
|
This reference covers only behavior demonstrated by these routed official tutorials:
|
|
@@ -130,7 +130,7 @@ feature_extractor = nn.Sequential(
|
|
|
130
130
|
)
|
|
131
131
|
```
|
|
132
132
|
|
|
133
|
-
Open `skills/brainstate/scripts/modern_cnn.py` for the full convolution,
|
|
133
|
+
Open `skills/package-skills/brainstate/scripts/modern_cnn.py` for the full convolution,
|
|
134
134
|
normalization, pooling, dropout, and dense workflow.
|
|
135
135
|
|
|
136
136
|
## Official sources
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Reliable paired perturbation execution
|
|
2
|
+
|
|
3
|
+
Open this reference when a stateful simulation compares matched baseline and perturbation trials, especially when recurrent dynamics, stochastic input, JIT compilation, batching, or long rollouts make causal matching and resource use easy to get wrong. It settles how to branch from one shared pre-perturbation trajectory, scale the run in fixed-shape chunks, and distinguish execution failure from scientific non-reproduction.
|
|
4
|
+
|
|
5
|
+
## Preserve the causal comparison
|
|
6
|
+
|
|
7
|
+
Treat each matched pair as one trajectory until the perturbation begins. Run the common prefix once, snapshot every model State at the intervention boundary, then run the baseline suffix, restore the boundary snapshot, and run the perturbed suffix with the same external inputs.
|
|
8
|
+
|
|
9
|
+
| API | Description |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `brainstate.nn.init_all_states(model, **kwargs)` | Use before the shared prefix to initialize the complete Module graph; verify the resulting State shapes before compiling the full experiment. |
|
|
12
|
+
| `model.states()` | Use at the intervention boundary to collect every registered State by absolute path; retain parameters, dynamics, synapses, delays, and model-owned random State needed for exact replay. |
|
|
13
|
+
| `brainstate.nn.assign_state_values(model, snapshot)` | Use before the second suffix to restore the intervention-boundary snapshot; reject both unexpected and missing paths before continuing. |
|
|
14
|
+
| `brainstate.transform.for_loop(step, *xs)` | Use to execute each time-major prefix or suffix while model State carries the recurrent dynamics; return only observations required by the analysis. |
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
import brainstate
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def restore_exact(model, snapshot):
|
|
21
|
+
unexpected, missing = brainstate.nn.assign_state_values(model, snapshot)
|
|
22
|
+
if unexpected or missing:
|
|
23
|
+
raise ValueError(
|
|
24
|
+
f"state restore mismatch: unexpected={unexpected}, missing={missing}"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
brainstate.nn.init_all_states(model, batch_size=batch_size)
|
|
29
|
+
|
|
30
|
+
# Both conditions share this execution and therefore cannot diverge before onset.
|
|
31
|
+
prefix_observations = run_prefix(shared_inputs[:onset])
|
|
32
|
+
onset_snapshot = {
|
|
33
|
+
path: state.value
|
|
34
|
+
for path, state in model.states().items()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
baseline = run_suffix(shared_inputs[onset:], perturbation=None)
|
|
38
|
+
restore_exact(model, onset_snapshot)
|
|
39
|
+
perturbed = run_suffix(shared_inputs[onset:], perturbation=protocol)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Generate each trial's stimulus and external noise once, then pass the same arrays to both suffixes. Do not regenerate noise by reseeding inside each branch. Include a model-owned random generator in the State snapshot when the transition itself samples randomness.
|
|
43
|
+
|
|
44
|
+
Measure the perturbation from the executed trajectory, not only from the requested waveform. For a stimulated neuron, report the intended waveform and the paired change in its spikes or other target response over the declared dose window.
|
|
45
|
+
|
|
46
|
+
Open `references/collective_model_operations.md` instead when the task only needs general initialization, reset, vmapped lifecycle, or checkpoint restoration without a matched causal intervention.
|
|
47
|
+
|
|
48
|
+
## Keep transformed execution shape-stable
|
|
49
|
+
|
|
50
|
+
Use one fixed trial-axis shape for a compiled paired runner. Split a larger experiment into chunks of that shape; pad only the final chunk, mask padded lanes from every statistic, and verify that model operations do not couple trial lanes.
|
|
51
|
+
|
|
52
|
+
| Mechanism | Action |
|
|
53
|
+
|---|---|
|
|
54
|
+
| Fixed chunk size | Choose the largest batch that passes the resource preflight, then keep it unchanged throughout the formal run. |
|
|
55
|
+
| Final partial chunk | Pad inputs to the fixed shape and carry an explicit validity mask into output collection and analysis. |
|
|
56
|
+
| Independent trials | Keep recurrence within each trial lane; do not reduce, normalize, or communicate across the trial axis. |
|
|
57
|
+
| Different logical workloads | Use the same runner shape or separate model/runner instances; do not mutate an initialized State graph between incompatible batch layouts. |
|
|
58
|
+
|
|
59
|
+
Do not increase batch size merely to finish sooner. In a time-unrolled recurrent model, simultaneous trial lanes multiply dynamical State and monitored output storage. Prefer smaller chunks when a full run exits without a Python traceback or produces no artifacts.
|
|
60
|
+
|
|
61
|
+
## Pass progressive execution gates
|
|
62
|
+
|
|
63
|
+
Freeze scientific parameters before inspecting perturbation outcomes, then advance through these gates in order. Stop at the first failure and preserve its run as an execution failure.
|
|
64
|
+
|
|
65
|
+
| Gate | Required check |
|
|
66
|
+
|---|---|
|
|
67
|
+
| Construction | Import packages, construct the complete model, initialize State, and record State paths, shapes, dtypes, units, and device. |
|
|
68
|
+
| One-step | Advance one step and verify output shapes, finite values, State write-back, and expected units. |
|
|
69
|
+
| One unperturbed trial | Run one complete trial and verify onset indices, baseline activity, monitoring windows, and artifact writing. |
|
|
70
|
+
| One matched pair | Branch at perturbation onset, restore every State without mismatches, verify identical suffix inputs, and measure the target dose. |
|
|
71
|
+
| Small experiment | Run at least two perturbation targets and enough conditions to exercise every analysis path; require the owning modeling skill's declared baseline-observability, intervention-dose, finite-value, bin-support, and effective-sample checks to pass without inspecting the requested causal outcome. |
|
|
72
|
+
| Scale probe | Run the intended model with one candidate chunk, record elapsed time and peak resource use, then choose and freeze the formal chunk size. |
|
|
73
|
+
| Formal run | Create a new immutable run directory, execute all frozen trials, and write parameters, logs, raw summaries, statistics, figures, and run notes. |
|
|
74
|
+
|
|
75
|
+
Unit tests for analysis helpers do not replace these gates. Add an end-to-end test that constructs the actual model, executes one matched pair, restores State, writes artifacts, and reads them back.
|
|
76
|
+
|
|
77
|
+
Do not enter the formal-run gate when the small experiment has silent or
|
|
78
|
+
saturated observables, degenerate feature estimates, unsupported analysis bins,
|
|
79
|
+
or an out-of-tolerance delivered intervention. Classify and preserve that pilot
|
|
80
|
+
as an invalid or inconclusive protocol. Calibrating the target dose against a
|
|
81
|
+
predeclared delivery rule is protocol development; changing model or stimulus
|
|
82
|
+
parameters after inspecting downstream causal signs is outcome-driven tuning.
|
|
83
|
+
|
|
84
|
+
## Limit monitoring before reducing model size
|
|
85
|
+
|
|
86
|
+
Estimate the largest retained arrays before execution from `time steps * trial lanes * monitored elements * bytes per element`. Count compiler intermediates and recurrent State separately; the output estimate is a lower bound, not a memory guarantee.
|
|
87
|
+
|
|
88
|
+
Return aggregates from transformed code when the analysis only needs spike counts, rates, sums, or mismatch counts. Return full time series only for signals that the scientific analysis or a named validation gate requires. Use a one-pair diagnostic run for exact full-trajectory inspection instead of transferring every neuron's trajectory for every formal trial.
|
|
89
|
+
|
|
90
|
+
If a process disappears without a Python traceback, record the command, exit status or signal when available, last completed gate, artifact inventory, device, batch shape, and available system evidence. Label resource exhaustion as suspected unless the runtime or operating system confirms it.
|
|
91
|
+
|
|
92
|
+
## Classify the outcome correctly
|
|
93
|
+
|
|
94
|
+
Assign exactly one primary status before interpreting the science:
|
|
95
|
+
|
|
96
|
+
| Status | Use when |
|
|
97
|
+
|---|---|
|
|
98
|
+
| Execution failure | An exception, failed invariant, incomplete artifact set, invalid causal pairing, or unexplained process termination prevents analysis. |
|
|
99
|
+
| Invalid protocol | The run completes but the delivered perturbation, timing, baseline regime, sample definition, or analysis differs materially from the declared experiment. |
|
|
100
|
+
| Inconclusive | The protocol is valid but uncertainty, target count, effective pair count, firing-rate regime, or bin occupancy cannot support the requested inference. |
|
|
101
|
+
| Scientific non-reproduction | The complete frozen protocol is valid and adequately powered, yet one or more preregistered signatures fail. |
|
|
102
|
+
| Reproduction | The complete frozen protocol is valid and the preregistered effect-size and uncertainty criteria pass without outcome-driven tuning. |
|
|
103
|
+
|
|
104
|
+
Do not tune scientific parameters in response to execution failures. Fix only the execution defect in the next run and state exactly what changed. After a valid scientific run, retain negative or inconclusive results and identify the failed mechanistic or statistical checks before proposing a new parameter hypothesis.
|
|
105
|
+
|
|
106
|
+
## Boundaries and common failures
|
|
107
|
+
|
|
108
|
+
- Running baseline and perturbation as unrelated trials and assuming equal seeds imply equal recurrent trajectories.
|
|
109
|
+
- Reinitializing only membrane voltage while leaving synaptic, refractory, delay, adaptation, or random State unmatched.
|
|
110
|
+
- Changing scientific parameters while repairing a JIT, batching, State, or resource failure.
|
|
111
|
+
- Treating passing helper tests as evidence that the full transformed workflow runs.
|
|
112
|
+
- Treating an empty result directory or silent process exit as a biological null result.
|
|
113
|
+
- Reporting a sign in the expected direction as reproduction when its uncertainty includes the null or its effective sample count is too small.
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: braintrace
|
|
3
|
+
description: BrainTrace relieves training-memory pressure in recurrent and spiking models by replacing BPTT's sequence-length-dependent graph with eligibility traces. Use this skill for memory-efficient temporal training, long sequences, BPTT out-of-memory problems, activation-memory reduction, and BrainTrace estimator selection or compilation. Do not use for general speed optimization, offline BPTT, or model dynamics.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# BrainTrace
|
|
7
|
+
|
|
8
|
+
## Purpose and boundary
|
|
9
|
+
|
|
10
|
+
Use BrainTrace when recurrent or spiking training is limited by BPTT memory. It carries eligibility traces forward instead of retaining the full sequence graph, so memory is constant in sequence length but still scales with batch size, participating parameters, hidden dimensions, and the selected estimator.
|
|
11
|
+
|
|
12
|
+
Follow this path:
|
|
13
|
+
|
|
14
|
+
`build with braintrace.nn -> choose a trace representation -> compile once -> inspect -> reset both State systems -> accumulate sequence gradients -> update once -> verify`
|
|
15
|
+
|
|
16
|
+
This skill targets BrainTrace `0.2.4`, bundled by BrainX `v2026.7.9`. Do not use `etrace_grad()`, `etrace_evolve()`, `SequenceDriverMixin`, or `ETraceVmap`; those APIs start in BrainTrace `0.2.5`.
|
|
17
|
+
|
|
18
|
+
Route general `State`, `Module`, transformations, mapped lifecycle operations, and optimizers to BrainState. Route neuron and synapse dynamics to BrainPy-State or BrainCell. Use ordinary BrainState training when offline BPTT is acceptable.
|
|
19
|
+
|
|
20
|
+
## Underlying principle of BrainTrace
|
|
21
|
+
|
|
22
|
+
Hidden State represents recurrent memory. It carries neural activity between time steps.
|
|
23
|
+
|
|
24
|
+
ETP operations represent parameterized paths that need temporal credit. A parameter consumed by an ETP operation can receive an eligibility trace; a parameter consumed only by an ordinary JAX operation cannot.
|
|
25
|
+
|
|
26
|
+
`ETraceGraph` represents compiled relationships between ETP parameters and hidden-State groups. It records which parameter influences which recurrent state.
|
|
27
|
+
|
|
28
|
+
Eligibility State represents compressed gradient history. The selected algorithm updates it during each learner call.
|
|
29
|
+
|
|
30
|
+
## API structure overview
|
|
31
|
+
|
|
32
|
+
| API family | Responsibility |
|
|
33
|
+
|---|---|
|
|
34
|
+
| `braintrace.nn` | Prebuilt ETP-aware recurrent, linear, convolutional, sparse, and readout layers. Start here. |
|
|
35
|
+
| ETP operations | Mark parameterized operations in a custom layer for eligibility-trace participation. |
|
|
36
|
+
| `braintrace.compile()` | Initialize State, discover hidden and parameter paths, build `ETraceGraph`, and return a learner. |
|
|
37
|
+
| `D_RTRL`, `pp_prop`, SNN algorithms | Define how eligibility State is represented and updated. |
|
|
38
|
+
| Compiled learner | Advance one step, update eligibility State, expose compiler reports, and participate in BrainState differentiation. |
|
|
39
|
+
|
|
40
|
+
## Choose the memory strategy and compile
|
|
41
|
+
|
|
42
|
+
Compile one representative time step once; the selected algorithm determines eligibility-memory shape and approximation.
|
|
43
|
+
|
|
44
|
+
| API | Description |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `braintrace.nn.MiniGRU(...)` | Use for the canonical recurrent model; it carries hidden State and applies ETP-aware parameter operations. |
|
|
47
|
+
| `braintrace.nn.Linear(...)` | Use for an ETP-aware projection; a readout that does not return to hidden State remains trainable but needs no temporal trace. |
|
|
48
|
+
| `braintrace.D_RTRL` | Use when parameter-shaped trace memory fits; it applies a diagonal hidden-recurrence approximation. |
|
|
49
|
+
| `braintrace.pp_prop` | Use when parameter-shaped traces are too costly, especially for recurrent SNNs; it stores input/output factors. |
|
|
50
|
+
| `braintrace.compile(model, algorithm, example_step, batch_size=..., vmap=...)` | Use once with the real per-step shape; it initializes State, compiles relations, rejects a graph with no online-trainable path, and returns a learner. |
|
|
51
|
+
| `learner.report.show(2)` | Use after compilation; level 2 includes diagnostics needed to distinguish included batched relations from rejected duplicate paths. |
|
|
52
|
+
| `learner.report.counts`, `etrace_weights`, `excluded_weights` | Use for programmatic participation checks; require zero errors and verify every intended recurrent weight explicitly. |
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
import brainstate
|
|
56
|
+
import braintrace
|
|
57
|
+
import jax.numpy as jnp
|
|
58
|
+
|
|
59
|
+
brainstate.random.seed(7)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class SequenceModel(brainstate.nn.Module):
|
|
63
|
+
def __init__(self):
|
|
64
|
+
super().__init__()
|
|
65
|
+
self.rnn = braintrace.nn.MiniGRU(in_size=1, out_size=6)
|
|
66
|
+
self.readout = braintrace.nn.Linear(6, 1)
|
|
67
|
+
|
|
68
|
+
def update(self, x):
|
|
69
|
+
return self.readout(self.rnn(x))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
model = SequenceModel()
|
|
73
|
+
inputs = jnp.linspace(-1.0, 1.0, 12).reshape(12, 1, 1)
|
|
74
|
+
learner = braintrace.compile(
|
|
75
|
+
model,
|
|
76
|
+
braintrace.D_RTRL,
|
|
77
|
+
inputs[0],
|
|
78
|
+
batch_size=1,
|
|
79
|
+
)
|
|
80
|
+
learner.report.show(2)
|
|
81
|
+
assert learner.report.counts['errors'] == 0
|
|
82
|
+
assert learner.report.etrace_weights
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Open `references/pre-built-braintrace-layer.md` when selecting another ETP-aware layer. Open `references/Drtrl.md` for D-RTRL memory and validation. Open `references/pp_pprop workflow.md` when parameter-shaped traces are too large or the model is a recurrent SNN. Open `references/algorithm selection.md` only when neither canonical choice fits.
|
|
86
|
+
|
|
87
|
+
## Train and evaluate a sequence
|
|
88
|
+
|
|
89
|
+
Differentiate exactly one learner call per time step, carry the accumulated gradient through `brainstate.transform.scan`, and update parameters once after the sequence.
|
|
90
|
+
|
|
91
|
+
| API | Description |
|
|
92
|
+
|---|---|
|
|
93
|
+
| `brainstate.nn.reset_all_states(model, batch_size=...)` | Use at an independent sequence boundary to reset recurrent State. |
|
|
94
|
+
| `learner.reset_state(batch_size=...)` | Use at the same boundary to reset eligibility State and the running index. |
|
|
95
|
+
| `brainstate.transform.grad(step_loss, weights, ...)` | Use around one learner call; BrainTrace supplies the online parameter gradient from current eligibility State. |
|
|
96
|
+
| `brainstate.transform.scan(body, initial_grads, sequence)` | Use to advance time and sum per-step gradients in an explicit carry. |
|
|
97
|
+
| `optimizer.update(grads)` | Use once after the complete sequence gradient has been accumulated. |
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
import jax
|
|
101
|
+
import braintools
|
|
102
|
+
|
|
103
|
+
targets = 0.7 * inputs + 0.2
|
|
104
|
+
weights = model.states(brainstate.ParamState)
|
|
105
|
+
optimizer = braintools.optim.SGD(lr=0.005)
|
|
106
|
+
optimizer.register_trainable_weights(weights)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def reset_sequence():
|
|
110
|
+
brainstate.nn.reset_all_states(model, batch_size=1)
|
|
111
|
+
learner.reset_state(batch_size=1)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def step_loss(x, target):
|
|
115
|
+
return jnp.mean((learner(x) - target) ** 2)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
loss_grad = brainstate.transform.grad(
|
|
119
|
+
step_loss,
|
|
120
|
+
weights,
|
|
121
|
+
return_value=True,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def train_epoch(_):
|
|
126
|
+
reset_sequence()
|
|
127
|
+
initial_grads = jax.tree.map(
|
|
128
|
+
jnp.zeros_like,
|
|
129
|
+
{key: state.value for key, state in weights.items()},
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
def accumulate(grads, sample):
|
|
133
|
+
step_grads, loss = loss_grad(*sample)
|
|
134
|
+
grads = jax.tree.map(
|
|
135
|
+
lambda total, value: total + value,
|
|
136
|
+
grads,
|
|
137
|
+
step_grads,
|
|
138
|
+
)
|
|
139
|
+
return grads, loss
|
|
140
|
+
|
|
141
|
+
grads, losses = brainstate.transform.scan(
|
|
142
|
+
accumulate,
|
|
143
|
+
initial_grads,
|
|
144
|
+
(inputs, targets),
|
|
145
|
+
)
|
|
146
|
+
optimizer.update(grads)
|
|
147
|
+
return losses.mean()
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
losses = brainstate.transform.for_loop(train_epoch, jnp.arange(5))
|
|
151
|
+
reset_sequence()
|
|
152
|
+
predictions = brainstate.transform.for_loop(learner, inputs)
|
|
153
|
+
|
|
154
|
+
assert losses[-1] < losses[0]
|
|
155
|
+
assert predictions.shape == targets.shape
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Open `references/batching.md` before adding a batch axis. For compile-owned vmap, reset the returned wrapper through its mapped State collection and inspect the underlying report through `.module`; do not apply a second mapping wrapper. Open the BrainState collective-operations reference when a manual mapped lifecycle is unavoidable.
|
|
159
|
+
|
|
160
|
+
## Reference routing
|
|
161
|
+
|
|
162
|
+
Escalate model construction from prebuilt layers to built-in ETP operations, then to a custom ETP primitive only when the previous level cannot express the computation.
|
|
163
|
+
|
|
164
|
+
| Reference | Open when | Contains |
|
|
165
|
+
|---|---|---|
|
|
166
|
+
| `references/pre-built-braintrace-layer.md` | Selecting another ETP-aware layer or composing the default model. | Layer families, parameterized-operation choices, and BrainState-owned supporting layers. |
|
|
167
|
+
| `references/Drtrl.md` | Using or validating parameter-shaped D-RTRL traces. | Memory scaling, recurrence, sequence resets, scan accumulation, and the BPTT boundary. |
|
|
168
|
+
| `references/pp_pprop workflow.md` | Parameter-shaped traces are too costly or a recurrent SNN needs factorized traces. | Input/output factorization, SNN integration, report-window training, and validation. |
|
|
169
|
+
| `references/algorithm selection.md` | Neither canonical memory strategy fits the estimator requirements. | BrainTrace `0.2.4` estimators, guarantees, and configuration options. |
|
|
170
|
+
| `references/batching.md` | Adding a batch axis, mapped model, or multi-step wrapper. | Mapping ownership, batched compilation, mapped resets, and sequence scan shape. |
|
|
171
|
+
| `references/ETP operators.md` | A prebuilt layer cannot express the required parameterized operation. | Built-in ETP operations, participation rules, sparse inputs, units, and transform behavior. |
|
|
172
|
+
| `references/custom ETP primitives.md` | No built-in ETP operation can express the computation. | Primitive registration, ETP rules, compiler integration, and validation. |
|
|
173
|
+
| `references/customizing_primitive_transforms.md` | An ETP weight needs masking, constraints, normalization, LoRA, or a bias transform. | Transform hooks and raw-parameter gradient attachment. |
|
|
174
|
+
| `references/custom algorithms.md` | No built-in BrainTrace `0.2.4` algorithm expresses the research method. | Estimator bases, trace lifecycle, solve hooks, and inspection. |
|
|
175
|
+
| `references/compiler_internal.md` | Structured report fields cannot explain custom graph behavior. | Discovery, graph execution, control-flow policy, and low-level diagnostics. |
|
|
176
|
+
|
|
177
|
+
## Boundaries and common failures
|
|
178
|
+
|
|
179
|
+
- Do not describe BrainTrace as constant-total-memory training. Memory is constant only in sequence length.
|
|
180
|
+
- Do not claim that an online estimator equals BPTT without its documented regime and a reduced gradient-oracle comparison.
|
|
181
|
+
- Do not compile inside training or compile from a full sequence; pass one representative time step with the real batch and feature shape.
|
|
182
|
+
- Do not treat every warning or excluded candidate as a failed parameter. Inspect `etrace_weights`, `excluded_weights`, and level-2 diagnostics together.
|
|
183
|
+
- Reset both recurrent State and eligibility State between independent sequences and verify mapped shapes survive a mapped reset.
|
|
184
|
+
- Use exactly one batching owner. Prefer `braintrace.compile(..., vmap=True)` for independent per-sample State or native batched compilation when the model supports it.
|
|
185
|
+
- Do not update the optimizer inside the temporal scan when the intended schedule is one update per sequence.
|
|
186
|
+
- Do not use BrainTrace `0.2.5` sequence-driver APIs with the bundled `0.2.4` release.
|
|
187
|
+
- Use `pp_prop` in new code; `ES_D_RTRL` is its compatibility alias.
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# D-RTRL
|
|
2
|
+
|
|
3
|
+
Use this reference when implementing or validating the parameter-dimensional D-RTRL workflow in BrainTrace `0.2.4`. Open `algorithm selection.md` when choosing another estimator and `batching.md` before adding a batch axis.
|
|
4
|
+
|
|
5
|
+
## Understand the approximation
|
|
6
|
+
|
|
7
|
+
`D_RTRL` carries parameter-dimensional eligibility traces forward while its default recurrence drops cross-hidden terms from the hidden-to-hidden Jacobian.
|
|
8
|
+
|
|
9
|
+
| API | Description |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `braintrace.D_RTRL` | Use when parameter-shaped trace memory is feasible; it computes approximate online gradients without retaining the full sequence graph. |
|
|
12
|
+
| `braintrace.compile(model, braintrace.D_RTRL, example_step, ...)` | Use once to initialize State, construct the learner, and compile the ETP graph from a representative step. |
|
|
13
|
+
| `brainstate.transform.grad(step_loss, weights, ...)` | Use around exactly one learner call to obtain the current online parameter gradient. |
|
|
14
|
+
| `brainstate.transform.scan(body, initial_grads, sequence)` | Use to carry the summed gradient across time while the learner advances hidden and eligibility State. |
|
|
15
|
+
|
|
16
|
+
Trace memory scales as `O(B * |theta|)`, where `B` is batch size and `theta` is the participating parameter set. Do not describe D-RTRL as generally identical to BPTT; equality depends on the recurrence and mathematical regime being tested.
|
|
17
|
+
|
|
18
|
+
## Follow the sequence lifecycle
|
|
19
|
+
|
|
20
|
+
Reset recurrent and eligibility State together, accumulate gradients across the complete sequence, then update parameters once.
|
|
21
|
+
|
|
22
|
+
| Step | Action | Important result |
|
|
23
|
+
|---|---|---|
|
|
24
|
+
| 1. Build | Compose ETP-aware recurrent and readout layers. | Parameter-to-hidden paths remain visible to the compiler. |
|
|
25
|
+
| 2. Compile | Call `braintrace.compile(...)` once with one real per-step input. | The graph and State layout are reused. |
|
|
26
|
+
| 3. Inspect | Show report level 2 and inspect structured participation fields. | Intended recurrent weights are confirmed rather than inferred from warning text. |
|
|
27
|
+
| 4. Reset | Reset model State and learner State at every independent sequence. | Hidden State, eligibility traces, and running index share one boundary. |
|
|
28
|
+
| 5. Accumulate | Differentiate one learner call inside a `scan` with an explicit gradient carry. | Every time step advances online State while gradients sum across time. |
|
|
29
|
+
| 6. Update | Clip if needed and call the optimizer once after the scan. | The printed sequence count matches the optimizer schedule. |
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import jax
|
|
33
|
+
import jax.numpy as jnp
|
|
34
|
+
import brainstate
|
|
35
|
+
import braintrace
|
|
36
|
+
import braintools
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class SequenceModel(brainstate.nn.Module):
|
|
40
|
+
def __init__(self):
|
|
41
|
+
super().__init__()
|
|
42
|
+
self.rnn = braintrace.nn.MiniGRU(in_size=1, out_size=6)
|
|
43
|
+
self.readout = braintrace.nn.Linear(6, 1)
|
|
44
|
+
|
|
45
|
+
def update(self, x):
|
|
46
|
+
return self.readout(self.rnn(x))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
model = SequenceModel()
|
|
50
|
+
inputs = jnp.linspace(-1.0, 1.0, 12).reshape(12, 1, 1)
|
|
51
|
+
targets = 0.7 * inputs + 0.2
|
|
52
|
+
learner = braintrace.compile(
|
|
53
|
+
model,
|
|
54
|
+
braintrace.D_RTRL,
|
|
55
|
+
inputs[0],
|
|
56
|
+
batch_size=1,
|
|
57
|
+
)
|
|
58
|
+
learner.report.show(2)
|
|
59
|
+
|
|
60
|
+
weights = model.states(brainstate.ParamState)
|
|
61
|
+
optimizer = braintools.optim.SGD(lr=0.005)
|
|
62
|
+
optimizer.register_trainable_weights(weights)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def reset_sequence():
|
|
66
|
+
brainstate.nn.reset_all_states(model, batch_size=1)
|
|
67
|
+
learner.reset_state(batch_size=1)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def step_loss(x, target):
|
|
71
|
+
return jnp.mean((learner(x) - target) ** 2)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
loss_grad = brainstate.transform.grad(step_loss, weights, return_value=True)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def train_sequence():
|
|
78
|
+
reset_sequence()
|
|
79
|
+
initial_grads = jax.tree.map(
|
|
80
|
+
jnp.zeros_like,
|
|
81
|
+
{key: state.value for key, state in weights.items()},
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
def accumulate(grads, sample):
|
|
85
|
+
step_grads, loss = loss_grad(*sample)
|
|
86
|
+
grads = jax.tree.map(lambda x, y: x + y, grads, step_grads)
|
|
87
|
+
return grads, loss
|
|
88
|
+
|
|
89
|
+
grads, losses = brainstate.transform.scan(
|
|
90
|
+
accumulate,
|
|
91
|
+
initial_grads,
|
|
92
|
+
(inputs, targets),
|
|
93
|
+
)
|
|
94
|
+
optimizer.update(grads)
|
|
95
|
+
return losses.mean()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
loss = train_sequence()
|
|
99
|
+
assert jnp.isfinite(loss)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Hoist `loss_grad` outside the scan body. Apply a temporal loss mask inside `step_loss` when only selected steps produce learning signals; still call the learner on every step so hidden and eligibility State advance throughout the sequence.
|
|
103
|
+
|
|
104
|
+
## Reset both State domains
|
|
105
|
+
|
|
106
|
+
| API | State reset |
|
|
107
|
+
|---|---|
|
|
108
|
+
| `brainstate.nn.reset_all_states(model, batch_size=B)` | Resets native batched recurrent State. |
|
|
109
|
+
| `learner.reset_state(batch_size=B)` | Resets native batched eligibility traces and the running index. |
|
|
110
|
+
| Mapped reset from `batching.md` | Resets a compile-owned vmap wrapper lane by lane without collapsing its State axis. |
|
|
111
|
+
|
|
112
|
+
Consecutive learner calls otherwise continue from installed State. Preserve continuation only when the calls belong to one trajectory.
|
|
113
|
+
|
|
114
|
+
## Interpret compiler participation
|
|
115
|
+
|
|
116
|
+
Use `learner.report.show(2)` and inspect `counts`, `etrace_weights`, `excluded_weights`, and diagnostics together. A readout that does not feed hidden State is correctly non-temporal. A warning for an unbatched candidate can coexist with an included batched relation for the same parameter; do not decide participation from warning strings alone.
|
|
117
|
+
|
|
118
|
+
Open `ETP operators.md` when a custom layer's intended recurrent parameter is absent. Open `compiler_internal.md` only when structured report fields cannot explain the decision.
|
|
119
|
+
|
|
120
|
+
## Keep the BPTT boundary explicit
|
|
121
|
+
|
|
122
|
+
| Property | D-RTRL | BPTT |
|
|
123
|
+
|---|---|---|
|
|
124
|
+
| Gradient timing | Updates eligibility information during the forward sequence. | Backpropagates after unrolling the sequence. |
|
|
125
|
+
| Sequence storage | Does not retain the full unrolled graph. | Stores or rematerializes sequence activations. |
|
|
126
|
+
| Gradient claim | Uses the configured recurrent approximation. | Provides the matched full-sequence baseline. |
|
|
127
|
+
|
|
128
|
+
Use a reduced matched task as a BPTT oracle only when the estimator's documented regime predicts equality. A descending loss verifies training mechanics, not gradient equivalence.
|
|
129
|
+
|
|
130
|
+
## Sources
|
|
131
|
+
|
|
132
|
+
- [BrainTrace `v0.2.4` D-RTRL examples](https://github.com/chaobrain/braintrace/tree/v0.2.4/examples/drtrl)
|
|
133
|
+
- [BrainTrace `v0.2.4` algorithms API source](https://github.com/chaobrain/braintrace/blob/v0.2.4/docs/apis/algorithms.rst)
|
|
@@ -59,7 +59,7 @@ a direct built-in operator with a custom primitive.
|
|
|
59
59
|
| `braintrace.einsum(...)` | Use for a two-operand Einstein contraction that is linear in the trainable weight. |
|
|
60
60
|
| `braintrace.element_wise(weight, *, weight_fn=None)` | Use for diagonal element-wise parameter operations such as gates, time constants, or thresholds. `weight_fn=None` is identity; otherwise provide a JAX-differentiable transform. |
|
|
61
61
|
| `braintrace.conv(x, kernel, bias=None, *, strides, padding, ...)` | Use for convolution through `jax.lax.conv_general_dilated` options. The input must include a batch dimension. |
|
|
62
|
-
| `braintrace.sparse_matmul(x, weight_data, *, sparse_mat, bias=None)` | Use when connectivity structure is sparse.
|
|
62
|
+
| `braintrace.sparse_matmul(x, weight_data, *, sparse_mat, bias=None)` | Use when connectivity structure is sparse. Pass a raw numeric or boolean array as `x`, trainable nonzero values as `weight_data`, and a BrainEvent `DataRepresentation` as `sparse_mat`. |
|
|
63
63
|
| `braintrace.lora_matmul(x, B, A, *, alpha=1.0, bias=None)` | Use for a low-rank update with `B` shaped `(in, rank)` and `A` shaped `(rank, out)`. Only the low-rank factors are trained. |
|
|
64
64
|
|
|
65
65
|
`element_wise` is the only listed primitive whose compiler registration has
|
|
@@ -99,9 +99,36 @@ assert conv_output.shape == (2, 16, 8)
|
|
|
99
99
|
primitive when `x.ndim >= 2`. `conv` is the critical exception: always supply a
|
|
100
100
|
batch dimension.
|
|
101
101
|
|
|
102
|
-
For sparse multiplication,
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
For sparse multiplication, keep the three roles separate:
|
|
103
|
+
|
|
104
|
+
| Argument | Contract |
|
|
105
|
+
|---|---|
|
|
106
|
+
| `x` | Pass a raw numeric or boolean array. Do not wrap spikes in `brainevent.BinaryArray`; `sparse_matmul` dispatches from array rank and expects numerical array operations. |
|
|
107
|
+
| `weight_data` | Pass the trainable values for the nonzero connections, with one value per stored sparse entry. |
|
|
108
|
+
| `sparse_mat` | Pass a `brainevent.DataRepresentation`, such as `brainevent.CSR`, that owns the fixed indices, orientation, shape, and sparse kernels. |
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
import brainevent
|
|
112
|
+
|
|
113
|
+
indices = jnp.array([1, 2, 0], dtype=jnp.int32)
|
|
114
|
+
indptr = jnp.array([0, 1, 2, 3], dtype=jnp.int32)
|
|
115
|
+
topology = brainevent.CSR(
|
|
116
|
+
(jnp.ones(3), indices, indptr),
|
|
117
|
+
shape=(3, 3),
|
|
118
|
+
)
|
|
119
|
+
spikes = jnp.array([True, False, True])
|
|
120
|
+
weight_data = jnp.ones(3)
|
|
121
|
+
|
|
122
|
+
current = braintrace.sparse_matmul(
|
|
123
|
+
spikes,
|
|
124
|
+
weight_data,
|
|
125
|
+
sparse_mat=topology,
|
|
126
|
+
)
|
|
127
|
+
assert current.shape == (3,)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The structure's own `data` can be a construction placeholder; the separately
|
|
131
|
+
passed `weight_data` is the trainable operand seen by the ETP primitive.
|
|
105
132
|
|
|
106
133
|
## Preserve physical units
|
|
107
134
|
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Algorithm selection
|
|
2
|
+
|
|
3
|
+
Use this reference when choosing a BrainTrace `0.2.4` estimator beyond the default `D_RTRL`. Use `Drtrl.md` or `pp_pprop workflow.md` for the two canonical training workflows; open `custom algorithms.md` only when no built-in class expresses the method.
|
|
4
|
+
|
|
5
|
+
## Choose an estimator
|
|
6
|
+
|
|
7
|
+
Choose by trace representation, recurrence approximation, learning signal, and supported mathematical regime.
|
|
8
|
+
|
|
9
|
+
| API | Use when | Essential behavior and constraint |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `D_RTRL` | Use as the general RNN default when parameter-dimensional trace memory fits. | Carries parameter-shaped traces with a diagonal hidden-recurrence approximation. |
|
|
12
|
+
| `pp_prop` | Use when parameter-shaped traces are too large, especially for SNNs. | Factorizes each trace into input and output components with exponential smoothing; `ES_D_RTRL` is its compatibility alias. |
|
|
13
|
+
| `EProp` | Use for recurrent SNNs that need kappa-filtered traces or fixed random-feedback learning signals. | Select feedback and filter decay through constructor options. |
|
|
14
|
+
| `OSTLRecurrent` | Use for the OSTL with-H recurrent regime. | Retains recurrent influence according to the implemented OSTL rule. |
|
|
15
|
+
| `OSTLFeedforward` | Use for the OSTL without-H feedforward regime. | Uses an input/output-factorized path and omits recurrent influence. |
|
|
16
|
+
| `OTPE` | Use for deep SNNs requiring the implemented online temporal-processing estimator. | Requires `leak`; select full or approximate mode explicitly. |
|
|
17
|
+
| `OTTT` | Use for very large SNNs using the implemented presynaptic trace rule. | Requires `leak`; report the selected mode. |
|
|
18
|
+
| `OSTTP` | Use for target-projection learning with supplied per-layer feedback matrices. | Requires `B_list`; declare target timing. |
|
|
19
|
+
|
|
20
|
+
`SnAp`, `UORO`, `ThreeFactor`, `DNI`, `SyntheticGradient`, and `ETraceConfig` are not public BrainTrace `0.2.4` APIs. Do not select them from newer documentation when targeting BrainX `v2026.7.9`.
|
|
21
|
+
|
|
22
|
+
## Compile the chosen algorithm
|
|
23
|
+
|
|
24
|
+
`braintrace.compile` is the shared entry point; options are forwarded to the selected algorithm constructor.
|
|
25
|
+
|
|
26
|
+
| API or option | Description |
|
|
27
|
+
|---|---|
|
|
28
|
+
| `braintrace.compile(model, algorithm, example_step, ...)` | Use once to initialize State, build the graph, and return a learner. |
|
|
29
|
+
| `vjp_method='single-step'` | Use for the normal per-step learning signal. |
|
|
30
|
+
| `vjp_method='multi-step'` | Use only when the algorithm and `MultiStepData` workflow require a recent-window learning signal. |
|
|
31
|
+
| `fast_solve=True` | Use the estimator's fast supported solve path; disable only for a documented incompatibility or comparison. |
|
|
32
|
+
| `trace_dtype=...` | Use when a supported estimator should store eligibility traces in a selected dtype. |
|
|
33
|
+
| `decay_or_rank=...` | Supply for `pp_prop`; use a float decay in `(0, 1)` or a positive integer rank parameterization. |
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
learner = braintrace.compile(
|
|
37
|
+
model,
|
|
38
|
+
braintrace.pp_prop,
|
|
39
|
+
example_step,
|
|
40
|
+
batch_size=batch_size,
|
|
41
|
+
vmap=True,
|
|
42
|
+
decay_or_rank=0.95,
|
|
43
|
+
vjp_method='single-step',
|
|
44
|
+
)
|
|
45
|
+
learner.module.report.show(2)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Drive the sequence in `0.2.4`
|
|
49
|
+
|
|
50
|
+
BrainTrace `0.2.4` advances hidden and eligibility State on every learner call; BrainState owns the explicit temporal carry and gradient accumulation.
|
|
51
|
+
|
|
52
|
+
| API | Description |
|
|
53
|
+
|---|---|
|
|
54
|
+
| `brainstate.transform.grad(step_loss, weights, ...)` | Use around one learner call to obtain the current online gradient. |
|
|
55
|
+
| `brainstate.transform.scan(...)` | Use when an explicit gradient carry must sum across time. |
|
|
56
|
+
| `braintrace.SingleStepData(data)` | Use to mark one current input; a plain array is equivalent. |
|
|
57
|
+
| `braintrace.MultiStepData(sequence)` | Use when one learner call intentionally consumes a multi-step window. |
|
|
58
|
+
|
|
59
|
+
Do not use `etrace_grad`, `etrace_evolve`, `SequenceDriverMixin`, or `ETraceVmap`; they start in BrainTrace `0.2.5`. Open `batching.md` for mapped and native-batch reset patterns.
|
|
60
|
+
|
|
61
|
+
## Sources
|
|
62
|
+
|
|
63
|
+
- [BrainTrace `v0.2.4` algorithms API source](https://github.com/chaobrain/braintrace/blob/v0.2.4/docs/apis/algorithms.rst)
|
|
64
|
+
- [BrainTrace `v0.2.4` public API](https://github.com/chaobrain/braintrace/blob/v0.2.4/braintrace/__init__.py)
|