brainx-skill 1.0.6 → 1.0.8
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 +9 -2
- package/adapters/gemini.js +7 -0
- package/adapters/opencode.js +7 -0
- package/adapters/windsurf.js +7 -0
- package/installation/lib/installer.js +4 -1
- package/manifest.json +5 -1
- package/package.json +7 -2
- package/skills/braincell/SKILL.md +182 -0
- package/skills/braincell/references/area-scaled-hh-pattern.md +54 -0
- package/skills/braincell/references/braincell-custom-ion-channel-authoring.md +210 -0
- package/skills/braincell/references/channel-library.md +170 -0
- package/skills/braincell/references/ion-library.md +154 -0
- package/skills/braincell/references/mixions-for-adaptation.md +81 -0
- package/skills/braincell/references/multicompartment/braincell-manual-morphology-construction.md +180 -0
- package/skills/braincell/references/multicompartment/cv-policy-reference.md +133 -0
- package/skills/braincell/references/multicompartment/filter-function-library.md +162 -0
- package/skills/braincell/references/multicompartment/morphology-io-loading-validation.md +135 -0
- package/skills/braincell/references/multicompartment/multicompartment-cell-workflow.md +391 -0
- package/skills/braincell/references/multicompartment/probe-reference.md +128 -0
- package/skills/braincell/references/multicompartment/topology-building-and-visualization.md +121 -0
- package/skills/braincell/references/scripts/calcium_channel_gating.py +66 -0
- package/skills/braincell/references/scripts/cell_multicompartment_reference.py +221 -0
- package/skills/braincell/references/scripts/channel_ablation.py +61 -0
- package/skills/braincell/references/scripts/fi_curve.py +68 -0
- package/skills/braincell/references/scripts/hh_neuron_basics.py +59 -0
- package/skills/braincell/references/scripts/spike_frequency_adaptation.py +75 -0
- package/skills/braincell/references/scripts/t_current_rebound.py +72 -0
- package/skills/braincell/references/scripts/thalamic_neurons.py +228 -0
- package/skills/braincell/references/solver-library-with-effects.md +188 -0
- package/skills/brainevent/SKILL.md +268 -0
- package/skills/brainevent/references/connectivity-variants.md +312 -0
- package/skills/brainevent/references/custom-operators-cpu.md +304 -0
- package/skills/brainevent/references/custom-operators-gpu.md +468 -0
- package/skills/brainevent/references/scripts/102_EI_net_1996.py +107 -0
- package/skills/brainevent/references/scripts/204_joglekar_2018_propagation.py +168 -0
- package/skills/brainevent/references/scripts/coba_ei_teaching.py +356 -0
- package/skills/brainevent/references/sparse-formats.md +182 -0
- package/skills/brainevent/references/synaptic-plasticity.md +285 -0
- package/skills/brainpy-state/SKILL.md +232 -0
- package/skills/brainpy-state/references/array-creation.md +158 -0
- package/skills/brainpy-state/references/brain-dynamics-delay-protocol.md +96 -0
- package/skills/brainpy-state/references/braintools/brainstate-control-flow-patterns.md +334 -0
- package/skills/brainpy-state/references/braintools/data-preprocessing.md +79 -0
- package/skills/brainpy-state/references/braintools/metric.md +152 -0
- package/skills/brainpy-state/references/braintools/optimizer.md +179 -0
- package/skills/brainpy-state/references/braintools/parameter-initializer.md +183 -0
- package/skills/brainpy-state/references/braintools/surrogate.md +147 -0
- package/skills/brainpy-state/references/component-selection.md +273 -0
- package/skills/brainpy-state/references/nest-compatible/devices.md +157 -0
- package/skills/brainpy-state/references/nest-compatible/divergence-and-parity.md +114 -0
- package/skills/brainpy-state/references/nest-compatible/integration-categories.md +41 -0
- package/skills/brainpy-state/references/nest-compatible/model-library.md +200 -0
- package/skills/brainpy-state/references/nest-compatible/nest-workflow.md +156 -0
- package/skills/brainpy-state/references/nest-compatible/network-building.md +264 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brette_et_al_2007.py +320 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_alpha.py +108 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_delta.py +93 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/clopath_synapse_spike_pairing.py +171 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/evaluate_tsodyks2_synapse.py +147 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/spatial_gaussex.py +112 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/synapsecollection.py +198 -0
- package/skills/brainpy-state/references/nest-compatible/synapse-and-connectivity.md +153 -0
- package/skills/brainpy-state/references/projection-patterns.md +125 -0
- package/skills/brainpy-state/references/scripts/103_COBA_2005.py +86 -0
- package/skills/brainpy-state/references/scripts/106_COBA_HH_2007.py +181 -0
- package/skills/brainpy-state/references/scripts/107_gamma_oscillation_1996.py +156 -0
- package/skills/brainpy-state/references/scripts/109_fast_global_oscillation.py +111 -0
- package/skills/brainpy-state/references/scripts/201_surrogate_grad_lif_fashion_mnist.py +221 -0
- package/skills/brainpy-state/references/scripts/training-snn.py +119 -0
- package/skills/brainstate/SKILL.md +1 -1
- package/skills/brainstate/references/brainstate/brainstate-control-flow-patterns.md +250 -399
- package/skills/brainstate/references/brainstate/brainstate-transformed-diagnostics.md +6 -2
- package/skills/brainstate/references/brainstate/parameter-constraints-regularization.md +16 -2
- package/skills/brainstate/references/brainstate/parameter-transforms-regularizers-catalog.md +8 -3
- package/skills/brainstate/references/brainstate/randomness-and-reproducibility.md +64 -143
- package/skills/brainstate/references/brainstate/transformation-grad-expansion.md +13 -1
- package/skills/brainstate/references/brainstate/transformation-jit-expansion.md +11 -1
- package/skills/brainstate/references/brainstate/transformation-vmap-expansion.md +35 -7
- package/skills/brainstate/references/collective_model_operations.md +9 -9
- package/skills/brainstate/references/extension_mechanisms.md +12 -1
- package/skills/brainstate/references/libraries/prebuilt-activation-library.md +106 -97
- package/skills/brainstate/references/libraries/prebuilt-layer-library.md +102 -176
- package/skills/brainstate/references/model-interop-and-migration.md +11 -1
- package/skills/brainstate/references/size-inference-variations.md +11 -0
- package/skills/brainstate/references/state-graph-operations.md +13 -15
- package/skills/brainstate/references/state_collections_and_utilities.md +12 -1
- package/skills/brainx-general-guard/SKILL.md +153 -0
- package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/advanced-randomness.md +303 -0
- package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/randomness-and-reproducibility.md +62 -0
- package/skills/brainstate/references/braintools-optimizer-reference.md +0 -133
- package/skills/brainstate/references/libraries/solver-library-with-effects.md +0 -85
|
@@ -1,106 +1,115 @@
|
|
|
1
1
|
# Prebuilt activation library
|
|
2
2
|
|
|
3
|
-
Use this reference to
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
|
14
|
-
|
|
15
|
-
| `
|
|
16
|
-
| `
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
|
24
|
-
|
|
25
|
-
| `
|
|
26
|
-
| `
|
|
27
|
-
| `
|
|
28
|
-
| `
|
|
29
|
-
| `
|
|
30
|
-
| `
|
|
31
|
-
| `
|
|
32
|
-
| `
|
|
33
|
-
| `
|
|
34
|
-
| `
|
|
35
|
-
| `
|
|
36
|
-
| `
|
|
37
|
-
| `
|
|
38
|
-
| `
|
|
39
|
-
| `
|
|
40
|
-
| `
|
|
41
|
-
| `
|
|
42
|
-
| `
|
|
43
|
-
| `
|
|
44
|
-
| `
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
3
|
+
Use this reference to choose an exact `brainstate.nn` activation symbol and decide
|
|
4
|
+
between a Module and a functional form. Open
|
|
5
|
+
`prebuilt-layer-library.md` for normalization layers; `standardize` remains here
|
|
6
|
+
because the activation API exports it as a pure array operation.
|
|
7
|
+
|
|
8
|
+
## Selection map
|
|
9
|
+
|
|
10
|
+
| Choice | Use when | Key constraint |
|
|
11
|
+
|---|---|---|
|
|
12
|
+
| Module class, such as `nn.ReLU()` | The activation must be stored in a Module graph, `Sequential`, or descriptor-based composition. | Instantiate the class; parameterized classes keep their configuration in the graph. |
|
|
13
|
+
| Functional form, such as `nn.relu(x)` | The activation is a stateless operation inside `update()` or another function. | Pass the array directly; do not instantiate the function. |
|
|
14
|
+
| `Softmax`, `LogSoftmax`, `Softmin`, or lowercase equivalents | The output must be normalized across a specified dimension. | Set the dimension or axis deliberately; the default may not match the feature dimension. |
|
|
15
|
+
| `standardize`, `one_hot`, or `logsumexp` | The task is array standardization, encoding, or stable log reduction rather than nonlinear activation. | These are utilities, not activation Modules. |
|
|
16
|
+
| `SpikeBitwise` | The input is a spiking representation requiring the catalog's bitwise-addition behavior. | Do not use it as a general numeric activation. |
|
|
17
|
+
|
|
18
|
+
## Module activations
|
|
19
|
+
|
|
20
|
+
Use a Module class when graph composition or constructor-held configuration
|
|
21
|
+
matters. The exact class names are case-sensitive.
|
|
22
|
+
|
|
23
|
+
| API | Choose it when | Decision-changing control |
|
|
24
|
+
|---|---|---|
|
|
25
|
+
| `Threshold` | Values on one side of a threshold must be replaced. | `Threshold(threshold, value)` |
|
|
26
|
+
| `ReLU` | Use the standard rectified linear unit. | `ReLU(name=None)` |
|
|
27
|
+
| `RReLU` | Use randomized negative slopes. | `RReLU(lower=0.125, upper=1 / 3)` |
|
|
28
|
+
| `Hardtanh` | Clamp with a hard-tanh response. | `Hardtanh(min_val=-1.0, max_val=1.0)` |
|
|
29
|
+
| `ReLU6` | Clamp ReLU output at six. | No selection-specific constructor control is documented. |
|
|
30
|
+
| `Sigmoid` | Map values through the logistic sigmoid. | No selection-specific constructor control is documented. |
|
|
31
|
+
| `Hardsigmoid` | Use the piecewise-linear sigmoid approximation. | No selection-specific constructor control is documented. |
|
|
32
|
+
| `Tanh` | Map values through hyperbolic tangent. | No selection-specific constructor control is documented. |
|
|
33
|
+
| `SiLU` | Use SiLU, also called swish. | No selection-specific constructor control is documented. |
|
|
34
|
+
| `Mish` | Use the Mish activation. | No selection-specific constructor control is documented. |
|
|
35
|
+
| `Hardswish` | Use the piecewise-linear hard-swish variant. | No selection-specific constructor control is documented. |
|
|
36
|
+
| `ELU` | Use exponential linear units. | `ELU(alpha=1.0)` |
|
|
37
|
+
| `CELU` | Use continuously differentiable ELU. | `CELU(alpha=1.0)` |
|
|
38
|
+
| `SELU` | Use scaled ELU in an architecture designed for self-normalization. | Pair with architecture-appropriate initialization and dropout rather than substituting it blindly. |
|
|
39
|
+
| `GLU` | Split and gate the input along a dimension. | `GLU(dim=-1)` requires a compatible size on the split dimension. |
|
|
40
|
+
| `GELU` | Use Gaussian error linear units. | Check the generated page when an approximation variant matters. |
|
|
41
|
+
| `Hardshrink` | Apply hard shrinkage. | Check the generated page for the threshold control. |
|
|
42
|
+
| `LeakyReLU` | Preserve a fixed negative slope. | `LeakyReLU(negative_slope=0.01)` |
|
|
43
|
+
| `LogSigmoid` | Return the log of sigmoid stably. | No selection-specific constructor control is documented. |
|
|
44
|
+
| `Softplus` | Use a smooth positive rectifier. | The current generated constructor is `Softplus(name=None)`; do not assume PyTorch's `beta` or `threshold` arguments. |
|
|
45
|
+
| `Softshrink` | Apply soft shrinkage. | Check the generated page for the threshold control. |
|
|
46
|
+
| `PReLU` | Learn the negative slope. | `PReLU(num_parameters=1, init=0.25, dtype=None)` creates trainable slope parameters. |
|
|
47
|
+
| `Softsign` | Use the soft-sign response. | No selection-specific constructor control is documented. |
|
|
48
|
+
| `Tanhshrink` | Return the tanh-shrink response. | No selection-specific constructor control is documented. |
|
|
49
|
+
| `Softmin` | Normalize with larger probability on smaller inputs. | Set the normalization dimension explicitly. |
|
|
50
|
+
| `Softmax` | Convert logits to normalized probabilities. | `Softmax(dim=None)`; pass the intended feature dimension. |
|
|
51
|
+
| `Softmax2d` | Normalize features independently at each spatial location. | Use only for the documented 2D spatial layout. |
|
|
52
|
+
| `LogSoftmax` | Return normalized log-probabilities. | Set the normalization dimension explicitly. |
|
|
53
|
+
| `Identity` | Keep a configurable graph slot without changing the input. | It is a placeholder, not an activation. |
|
|
54
|
+
| `SpikeBitwise` | Apply bitwise addition to spiking inputs. | Keep it within spiking-data workflows. |
|
|
52
55
|
|
|
53
56
|
## Functional activations
|
|
54
57
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
The official page describes these as functional, non-module activation functions for flexible composition. They are pure functions that may be used directly in `update()` methods or combined with JAX transformations, and provide activation behavior without state or module overhead.
|
|
58
|
+
Use the lowercase form for stateless composition. Prefer the function whose name
|
|
59
|
+
matches the requested operation; aliases are shown explicitly.
|
|
58
60
|
|
|
59
|
-
|
|
|
61
|
+
| API | Distinguishing behavior |
|
|
60
62
|
|---|---|
|
|
61
|
-
| `tanh` | Hyperbolic tangent
|
|
62
|
-
| `relu` | Rectified
|
|
63
|
-
| `squareplus` |
|
|
64
|
-
| `softplus` |
|
|
65
|
-
| `soft_sign` | Soft-sign
|
|
66
|
-
| `sigmoid` |
|
|
67
|
-
| `silu` | SiLU
|
|
68
|
-
| `
|
|
69
|
-
| `
|
|
70
|
-
| `
|
|
71
|
-
| `
|
|
72
|
-
| `
|
|
73
|
-
| `
|
|
74
|
-
| `
|
|
75
|
-
| `
|
|
76
|
-
| `
|
|
77
|
-
| `
|
|
78
|
-
| `
|
|
79
|
-
| `
|
|
80
|
-
| `
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
63
|
+
| `tanh` | Hyperbolic tangent. |
|
|
64
|
+
| `relu` | Rectified linear unit. |
|
|
65
|
+
| `squareplus` | Smooth squareplus rectifier. |
|
|
66
|
+
| `softplus` | Smooth positive rectifier. |
|
|
67
|
+
| `soft_sign` | Soft-sign response. |
|
|
68
|
+
| `sigmoid` | Logistic sigmoid. |
|
|
69
|
+
| `silu`, `swish` | Equivalent SiLU/swish names. |
|
|
70
|
+
| `log_sigmoid` | Stable log-sigmoid. |
|
|
71
|
+
| `elu`, `celu`, `selu` | ELU-family functions; select by the requested activation contract. |
|
|
72
|
+
| `leaky_relu`, `rrelu`, `prelu` | Fixed, randomized, or supplied negative-slope behavior. |
|
|
73
|
+
| `hard_tanh`, `relu6`, `hard_sigmoid` | Piecewise-linear or clamped responses. |
|
|
74
|
+
| `hard_silu`, `hard_swish` | Equivalent hard-SiLU/hard-swish names. |
|
|
75
|
+
| `gelu`, `mish` | GELU or Mish response. |
|
|
76
|
+
| `glu` | Split-and-gate operation; preserve a compatible split-axis size. |
|
|
77
|
+
| `hard_shrink`, `soft_shrink`, `tanh_shrink` | Shrinkage-family functions. |
|
|
78
|
+
| `softmin`, `softmax`, `log_softmax` | Normalize across `axis`; choose probability, inverse-probability, or log-probability output. |
|
|
79
|
+
| `sparse_plus`, `sparse_sigmoid` | Sparse probability transformations. |
|
|
80
|
+
| `logsumexp` | Stable log of summed exponentials. |
|
|
81
|
+
| `standardize` | Standardize an array, optionally using supplied variance. |
|
|
82
|
+
| `one_hot` | Encode integer indices along a new class axis. |
|
|
83
|
+
|
|
84
|
+
## Canonical selection workflow
|
|
85
|
+
|
|
86
|
+
Use a Module in a registered layer pipeline and a function for an inline output
|
|
87
|
+
transformation:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
import brainstate
|
|
91
|
+
import brainstate.nn as nn
|
|
92
|
+
|
|
93
|
+
brainstate.random.seed(0)
|
|
94
|
+
feature_block = nn.Sequential(
|
|
95
|
+
nn.Linear(in_size=(8,), out_size=(4,)),
|
|
96
|
+
nn.ReLU(),
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
x = brainstate.random.randn(2, 8)
|
|
100
|
+
logits = feature_block(x)
|
|
101
|
+
probabilities = nn.softmax(logits, axis=-1)
|
|
102
|
+
|
|
103
|
+
assert probabilities.shape == logits.shape
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**Invariant:** choose the normalization axis from the tensor layout. A valid
|
|
107
|
+
call on the wrong axis can silently produce the wrong probability semantics.
|
|
108
|
+
|
|
109
|
+
For any parameter not shown above, open the version-matched generated page
|
|
110
|
+
linked from the activation catalog instead of borrowing a signature from JAX,
|
|
111
|
+
PyTorch, or another library.
|
|
112
|
+
|
|
113
|
+
## Official source
|
|
105
114
|
|
|
106
115
|
- https://brainx.chaobrain.com/brainstate/apis/nn/activation.html
|
|
@@ -1,213 +1,139 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
Use this reference only to select a prebuilt `brainstate.nn` layer and recover its constructor form. The canonical layer example, `Module` composition, and `.desc()` workflow stay in `SKILL.md`. Route convolution, pooling, padding, and flatten output formulas to `../size-inference-variations.md`; route activation functions to `prebuilt-activation-library.md`; route dropout randomness, reproducibility, and fitting-mode work to `../brainstate/randomness-and-reproducibility.md`.
|
|
4
|
-
|
|
5
|
-
The constructor forms below preserve the documented public parameter order and defaults. To keep the catalog readable, these aliases compress only verbose initializer and State-type representations:
|
|
6
|
-
|
|
7
|
-
```text
|
|
8
|
-
P = brainstate.ParamState
|
|
9
|
-
NP = brainstate.nn._normalizations.NormalizationParamState
|
|
10
|
-
MS = brainstate.BatchState
|
|
11
|
-
WK = KaimingNormal(scale=2.0, mode='fan_in', in_axis=-2,
|
|
12
|
-
out_axis=-1, distribution='truncated_normal', ...)
|
|
13
|
-
WX = XavierNormal(scale=1.0, mode='fan_avg', in_axis=-2,
|
|
14
|
-
out_axis=-1, distribution='truncated_normal', ...)
|
|
15
|
-
WL = LecunNormal(scale=1.0, mode='fan_in', in_axis=-2,
|
|
16
|
-
out_axis=-1, distribution='truncated_normal', ...)
|
|
17
|
-
WN = Normal(scale=1.0, mean=0.0, ...)
|
|
18
|
-
Z = ZeroInit(unit=Unit("1"))
|
|
19
|
-
C0 = Constant(value=0.0)
|
|
20
|
-
C1 = Constant(value=1.0)
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
The `...` inside an initializer alias stands only for its rendered RNG and unit representation, not for omitted layer arguments.
|
|
24
|
-
|
|
25
|
-
## Linear and connectivity layers
|
|
1
|
+
# Prebuilt layer library
|
|
26
2
|
|
|
27
|
-
|
|
3
|
+
Use this reference to choose a prebuilt `brainstate.nn` layer family and identify
|
|
4
|
+
the constructor controls that change model behavior. Keep ordinary composition
|
|
5
|
+
in `SKILL.md`; open `../size-inference-variations.md` for output-size formulas,
|
|
6
|
+
`prebuilt-activation-library.md` for activations, and
|
|
7
|
+
`../brainstate/randomness-and-reproducibility.md` for stochastic replay.
|
|
28
8
|
|
|
29
|
-
|
|
9
|
+
## Selection map
|
|
30
10
|
|
|
31
|
-
|
|
|
11
|
+
| Family | Use when | Key constraint |
|
|
32
12
|
|---|---|---|
|
|
33
|
-
|
|
|
34
|
-
|
|
|
35
|
-
|
|
|
36
|
-
|
|
|
37
|
-
|
|
|
38
|
-
|
|
|
39
|
-
| `OneToOne` | `OneToOne(in_size, w_init=WN, b_init=None, name=None, param_type=P)` | A one-to-one connection layer is required. |
|
|
40
|
-
|
|
41
|
-
Do not replace `SparseLinear`, `LoRA`, `AllToAll`, or `OneToOne` with a hand-written dense layer when the corresponding connection structure is the requirement.
|
|
42
|
-
|
|
43
|
-
## Convolutional layers
|
|
44
|
-
|
|
45
|
-
Source URL: https://brainx.chaobrain.com/brainstate/apis/nn/conv.html
|
|
46
|
-
|
|
47
|
-
Choose the suffix by spatial rank: `1d` for one spatial dimension, `2d` for two, and `3d` for three. The catalog provides the same three ranks for standard convolution, scaled weight-standardized convolution, and transposed convolution.
|
|
48
|
-
|
|
49
|
-
### Standard convolution
|
|
50
|
-
|
|
51
|
-
Exact classes: `Conv1d`, `Conv2d`, `Conv3d`.
|
|
52
|
-
|
|
53
|
-
```text
|
|
54
|
-
ConvNd(
|
|
55
|
-
in_size, out_channels, kernel_size,
|
|
56
|
-
stride=1, padding='SAME', lhs_dilation=1, rhs_dilation=1, groups=1,
|
|
57
|
-
w_init=WX, b_init=None, w_mask=None, channel_first=False,
|
|
58
|
-
name=None, param_type=P,
|
|
59
|
-
)
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
Use the matching concrete class name in place of `ConvNd`. `channel_first=False` is the documented default; make layout choice explicit when selecting the non-default layout. Size outcomes for `stride`, `padding`, dilation, and groups belong in the size-inference reference.
|
|
13
|
+
| Linear and connectivity | Features must be mixed densely, sparsely, by sign, one-to-one, all-to-all, or through low-rank adaptation. | Preserve the requested connectivity structure instead of replacing it with a dense matrix. |
|
|
14
|
+
| Convolution | Local kernels operate over one, two, or three spatial dimensions. | Match the class suffix and `channel_first` setting to the input layout. |
|
|
15
|
+
| Normalization | Activations or weights must be normalized by batch, layer, RMS, group, or weight statistics. | Choose reduction and feature axes from the actual tensor layout. |
|
|
16
|
+
| Pooling and reshaping | Spatial values must be reduced, restored from max indices, or rearranged. | Local pooling and adaptive pooling solve different output-size requirements. |
|
|
17
|
+
| Padding | A boundary condition must be explicit before another spatial operation. | Reflection, replication, circular, constant, and zero padding have different boundary semantics. |
|
|
18
|
+
| Dropout | Training requires elementwise, channelwise, alpha, or time-fixed stochastic masking. | Fitting mode and RNG state determine whether and how the mask is applied. |
|
|
63
19
|
|
|
64
|
-
|
|
20
|
+
The forms below show public arguments that affect selection. Open the generated
|
|
21
|
+
class page from the linked family catalog for version-specific initializer
|
|
22
|
+
objects, State subclasses, dtypes, names, and full defaults.
|
|
65
23
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
```text
|
|
69
|
-
ScaledWSConvNd(
|
|
70
|
-
in_size, out_channels, kernel_size,
|
|
71
|
-
stride=1, padding='SAME', lhs_dilation=1, rhs_dilation=1, groups=1,
|
|
72
|
-
ws_gain=True, eps=0.0001,
|
|
73
|
-
w_init=WX, b_init=None, w_mask=None, channel_first=False,
|
|
74
|
-
name=None, param_type=P,
|
|
75
|
-
)
|
|
76
|
-
```
|
|
24
|
+
## Linear and connectivity layers
|
|
77
25
|
|
|
78
|
-
|
|
26
|
+
| API | Constructor core | Choose it when |
|
|
27
|
+
|---|---|---|
|
|
28
|
+
| `Linear` | `Linear(in_size, out_size, ...)` | Apply a standard dense linear transformation. |
|
|
29
|
+
| `ScaledWSLinear` | `ScaledWSLinear(in_size, out_size, ..., ws_gain=True, eps=1e-4)` | Standardize linear weights during the forward computation. |
|
|
30
|
+
| `SignedWLinear` | `SignedWLinear(in_size, out_size, ..., w_sign=None)` | Enforce a supplied sign pattern over absolute weights. |
|
|
31
|
+
| `SparseLinear` | `SparseLinear(spar_mat, ..., in_size=None)` | Use an existing sparse weight matrix. |
|
|
32
|
+
| `LoRA` | `LoRA(in_features, lora_rank, out_features, *, base_module=None, ...)` | Add a low-rank trainable update, optionally around a base Module. |
|
|
33
|
+
| `AllToAll` | `AllToAll(in_size, out_size, ..., include_self=True)` | Connect every input and output, optionally excluding self-connections. |
|
|
34
|
+
| `OneToOne` | `OneToOne(in_size, ...)` | Give each feature or unit its own scalar connection. |
|
|
35
|
+
|
|
36
|
+
**Invariant:** `SparseLinear`, `LoRA`, `AllToAll`, and `OneToOne` encode model
|
|
37
|
+
structure. Replacing them with `Linear` changes parameterization and often the
|
|
38
|
+
scientific meaning.
|
|
79
39
|
|
|
80
|
-
|
|
40
|
+
## Convolutional layers
|
|
81
41
|
|
|
82
|
-
|
|
42
|
+
Choose `1d`, `2d`, or `3d` from the number of spatial axes, excluding batch and
|
|
43
|
+
channel axes.
|
|
83
44
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
name=None, param_type=P,
|
|
90
|
-
)
|
|
91
|
-
```
|
|
45
|
+
| Exact classes | Constructor family | Choose it when |
|
|
46
|
+
|---|---|---|
|
|
47
|
+
| `Conv1d`, `Conv2d`, `Conv3d` | `ConvNd(in_size, out_channels, kernel_size, stride=1, padding='SAME', lhs_dilation=1, rhs_dilation=1, groups=1, ..., channel_first=False)` | Apply a standard convolution. |
|
|
48
|
+
| `ScaledWSConv1d`, `ScaledWSConv2d`, `ScaledWSConv3d` | `ScaledWSConvNd(..., ws_gain=True, eps=1e-4, channel_first=False)` | Apply convolution with scaled weight standardization. |
|
|
49
|
+
| `ConvTranspose1d`, `ConvTranspose2d`, `ConvTranspose3d` | `ConvTransposeNd(in_size, out_channels, kernel_size, stride=1, padding='SAME', rhs_dilation=1, groups=1, ..., channel_first=False)` | Apply learned transposed-convolution upsampling. |
|
|
92
50
|
|
|
93
|
-
|
|
51
|
+
Do not pass `lhs_dilation` to the transposed-convolution family; it is not part
|
|
52
|
+
of its documented constructor. Open `../size-inference-variations.md` before
|
|
53
|
+
predicting shapes affected by stride, padding, dilation, or groups.
|
|
94
54
|
|
|
95
55
|
## Normalization layers
|
|
96
56
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
This section inventories normalization APIs only. Activation-function selection remains in `prebuilt-activation-library.md`.
|
|
100
|
-
|
|
101
|
-
### Batch normalization ranks
|
|
102
|
-
|
|
103
|
-
Exact classes: `BatchNorm0d`, `BatchNorm1d`, `BatchNorm2d`, `BatchNorm3d`.
|
|
104
|
-
|
|
105
|
-
```text
|
|
106
|
-
BatchNormNd(
|
|
107
|
-
in_size, feature_axis=-1, *,
|
|
108
|
-
track_running_stats=True, epsilon=1e-05, momentum=0.99, affine=True,
|
|
109
|
-
bias_initializer=C0, scale_initializer=C1,
|
|
110
|
-
axis_name=None, axis_index_groups=None, use_fast_variance=True,
|
|
111
|
-
name=None, dtype=None, param_type=NP, mean_type=MS,
|
|
112
|
-
)
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
Choose `0d`, `1d`, `2d`, or `3d` to match the required batch-normalization rank. Running-statistics and affine behavior are constructor choices through `track_running_stats` and `affine`; do not silently change their documented defaults.
|
|
116
|
-
|
|
117
|
-
### Axis- and group-based normalization
|
|
118
|
-
|
|
119
|
-
| API | Constructor | Selection distinction |
|
|
57
|
+
| API | Constructor core | Choose it when |
|
|
120
58
|
|---|---|---|
|
|
121
|
-
| `
|
|
122
|
-
| `
|
|
123
|
-
| `
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
```python
|
|
128
|
-
brainstate.nn.weight_standardization(w, eps=0.0001, gain=None, out_axis=-1)
|
|
129
|
-
```
|
|
59
|
+
| `BatchNorm0d`, `BatchNorm1d`, `BatchNorm2d`, `BatchNorm3d` | `BatchNormNd(in_size, feature_axis=-1, *, track_running_stats=True, epsilon=1e-5, momentum=0.99, affine=True, ...)` | Normalize with batch statistics and optionally maintain running statistics. |
|
|
60
|
+
| `LayerNorm` | `LayerNorm(in_size, reduction_axes=-1, feature_axes=-1, *, epsilon=1e-6, use_bias=True, use_scale=True, ...)` | Normalize selected axes independently for each example. |
|
|
61
|
+
| `RMSNorm` | `RMSNorm(in_size, *, epsilon=1e-6, use_scale=True, reduction_axes=-1, feature_axes=-1, ...)` | Normalize by root mean square without a bias control. |
|
|
62
|
+
| `GroupNorm` | `GroupNorm(in_size, feature_axis=-1, num_groups=32, group_size=None, *, epsilon=1e-6, ...)` | Partition channels into groups; specify `num_groups` or `group_size` coherently. |
|
|
63
|
+
| `weight_standardization` | `weight_standardization(w, eps=1e-4, gain=None, out_axis=-1)` | Standardize a weight array directly rather than selecting a complete layer. |
|
|
130
64
|
|
|
131
|
-
|
|
65
|
+
Batch normalization has separate ranks because accepted layouts differ. Do not
|
|
66
|
+
choose a suffix from the total array rank without accounting for batch and
|
|
67
|
+
feature axes.
|
|
132
68
|
|
|
133
69
|
## Pooling and reshaping
|
|
134
70
|
|
|
135
|
-
|
|
71
|
+
| Exact APIs | Constructor family | Choose it when |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| `Flatten` | `Flatten(start_axis=0, end_axis=-1, in_size=None)` | Collapse a contiguous range of runtime axes. |
|
|
74
|
+
| `Unflatten` | `Unflatten(axis, sizes, name=None, in_size=None)` | Expand one axis into the supplied sizes. |
|
|
75
|
+
| `AvgPool1d`, `AvgPool2d`, `AvgPool3d` | `AvgPoolNd(kernel_size, stride=1, padding='VALID', channel_axis=-1, ...)` | Reduce local windows by their mean. |
|
|
76
|
+
| `MaxPool1d`, `MaxPool2d`, `MaxPool3d` | `MaxPoolNd(kernel_size, stride=None, padding='VALID', channel_axis=-1, return_indices=False, ...)` | Reduce local windows by their maximum; request indices when unpooling follows. |
|
|
77
|
+
| `LPPool1d`, `LPPool2d`, `LPPool3d` | `LPPoolNd(norm_type, kernel_size, stride=None, padding='VALID', channel_axis=-1, ...)` | Apply local Lp pooling with an explicit norm order. |
|
|
78
|
+
| `MaxUnpool1d`, `MaxUnpool2d`, `MaxUnpool3d` | `MaxUnpoolNd(kernel_size, stride=None, padding=0, channel_axis=-1, ...)` | Place pooled maxima back using compatible max-pool indices. |
|
|
79
|
+
| `AdaptiveAvgPool1d`, `AdaptiveAvgPool2d`, `AdaptiveAvgPool3d` | `AdaptiveAvgPoolNd(target_size, channel_axis=-1, ...)` | Produce a fixed target size with average pooling. |
|
|
80
|
+
| `AdaptiveMaxPool1d`, `AdaptiveMaxPool2d`, `AdaptiveMaxPool3d` | `AdaptiveMaxPoolNd(target_size, channel_axis=-1, ...)` | Produce a fixed target size with max pooling. |
|
|
81
|
+
|
|
82
|
+
**Invariant:** `MaxUnpool*` is only a partial inverse. Preserve indices from a
|
|
83
|
+
compatible `MaxPool*` call and do not expect non-maximal values to be recovered.
|
|
136
84
|
|
|
137
|
-
|
|
85
|
+
## Padding layers
|
|
138
86
|
|
|
139
|
-
|
|
87
|
+
Choose the boundary behavior first, then the `1d`, `2d`, or `3d` suffix.
|
|
140
88
|
|
|
141
|
-
|
|
|
89
|
+
| Exact class families | Constructor family | Boundary behavior |
|
|
142
90
|
|---|---|---|
|
|
143
|
-
| `
|
|
144
|
-
| `
|
|
91
|
+
| `ReflectionPad1d`, `ReflectionPad2d`, `ReflectionPad3d` | `ReflectionPadNd(padding, in_size=None, name=None)` | Mirror values at the boundary. |
|
|
92
|
+
| `ReplicationPad1d`, `ReplicationPad2d`, `ReplicationPad3d` | `ReplicationPadNd(padding, in_size=None, name=None)` | Repeat edge values. |
|
|
93
|
+
| `ZeroPad1d`, `ZeroPad2d`, `ZeroPad3d` | `ZeroPadNd(padding, in_size=None, name=None)` | Insert zeros. |
|
|
94
|
+
| `ConstantPad1d`, `ConstantPad2d`, `ConstantPad3d` | `ConstantPadNd(padding, value=0, in_size=None, name=None)` | Insert an explicit constant. |
|
|
95
|
+
| `CircularPad1d`, `CircularPad2d`, `CircularPad3d` | `CircularPadNd(padding, in_size=None, name=None)` | Wrap values from the opposite boundary. |
|
|
145
96
|
|
|
146
|
-
|
|
97
|
+
## Dropout layers
|
|
147
98
|
|
|
148
|
-
|
|
|
99
|
+
| API | Constructor core | Choose it when |
|
|
149
100
|
|---|---|---|
|
|
150
|
-
| `
|
|
151
|
-
| `
|
|
152
|
-
| `
|
|
101
|
+
| `Dropout` | `Dropout(prob=0.5, broadcast_dims=(), name=None)` | Mask individual values, optionally sharing the mask across selected dimensions. |
|
|
102
|
+
| `Dropout1d`, `Dropout2d`, `Dropout3d` | `DropoutNd(prob=0.5, channel_axis=-1, name=None)` | Mask complete feature channels for the matching spatial rank. |
|
|
103
|
+
| `AlphaDropout` | `AlphaDropout(prob=0.5, name=None)` | Preserve self-normalizing-network statistics with elementwise alpha dropout. |
|
|
104
|
+
| `FeatureAlphaDropout` | `FeatureAlphaDropout(prob=0.5, channel_axis=-1, name=None)` | Apply alpha dropout to complete channels. |
|
|
105
|
+
| `DropoutFixed` | `DropoutFixed(in_size, prob=0.5, name=None)` | Reuse a mask along the time axis. |
|
|
153
106
|
|
|
154
|
-
|
|
107
|
+
Open `../brainstate/randomness-and-reproducibility.md` when dropout must use an
|
|
108
|
+
independent stream, replay exactly, map across an ensemble, or survive a
|
|
109
|
+
checkpoint.
|
|
155
110
|
|
|
156
|
-
|
|
111
|
+
## Representative composition
|
|
157
112
|
|
|
158
|
-
|
|
113
|
+
This example makes layout, spatial rank, normalization axis, and fixed output
|
|
114
|
+
size explicit:
|
|
159
115
|
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
116
|
+
```python
|
|
117
|
+
import brainstate.nn as nn
|
|
118
|
+
|
|
119
|
+
feature_extractor = nn.Sequential(
|
|
120
|
+
nn.Conv2d(
|
|
121
|
+
in_size=(28, 28, 1),
|
|
122
|
+
out_channels=16,
|
|
123
|
+
kernel_size=3,
|
|
124
|
+
padding="SAME",
|
|
125
|
+
channel_first=False,
|
|
126
|
+
),
|
|
127
|
+
nn.GroupNorm(in_size=(28, 28, 16), feature_axis=-1, num_groups=4),
|
|
128
|
+
nn.ReLU(),
|
|
129
|
+
nn.AdaptiveAvgPool2d(target_size=(1, 1), channel_axis=-1),
|
|
164
130
|
)
|
|
165
131
|
```
|
|
166
132
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
### Adaptive pooling
|
|
170
|
-
|
|
171
|
-
| Exact classes | Constructor family | Selection distinction |
|
|
172
|
-
|---|---|---|
|
|
173
|
-
| `AdaptiveAvgPool1d`, `AdaptiveAvgPool2d`, `AdaptiveAvgPool3d` | `AdaptiveAvgPoolNd(target_size, channel_axis=-1, name=None, in_size=None)` | Adaptive average pooling to a fixed target size. |
|
|
174
|
-
| `AdaptiveMaxPool1d`, `AdaptiveMaxPool2d`, `AdaptiveMaxPool3d` | `AdaptiveMaxPoolNd(target_size, channel_axis=-1, name=None, in_size=None)` | Adaptive max pooling to a fixed target size. |
|
|
175
|
-
|
|
176
|
-
## Padding layers
|
|
177
|
-
|
|
178
|
-
Source URL: https://brainx.chaobrain.com/brainstate/apis/nn/padding.html
|
|
179
|
-
|
|
180
|
-
All five boundary-condition families are available in 1D, 2D, and 3D. Choose the family first, then the suffix matching the spatial rank.
|
|
181
|
-
|
|
182
|
-
| Exact classes | Constructor family | Boundary behavior |
|
|
183
|
-
|---|---|---|
|
|
184
|
-
| `ReflectionPad1d`, `ReflectionPad2d`, `ReflectionPad3d` | `ReflectionPadNd(padding, in_size=None, name=None)` | Reflect the input boundary. |
|
|
185
|
-
| `ReplicationPad1d`, `ReplicationPad2d`, `ReplicationPad3d` | `ReplicationPadNd(padding, in_size=None, name=None)` | Replicate the input boundary. |
|
|
186
|
-
| `ZeroPad1d`, `ZeroPad2d`, `ZeroPad3d` | `ZeroPadNd(padding, in_size=None, name=None)` | Pad with zeros. |
|
|
187
|
-
| `ConstantPad1d`, `ConstantPad2d`, `ConstantPad3d` | `ConstantPadNd(padding, value=0, in_size=None, name=None)` | Pad with `value`; the documented default is zero. |
|
|
188
|
-
| `CircularPad1d`, `CircularPad2d`, `CircularPad3d` | `CircularPadNd(padding, in_size=None, name=None)` | Wrap around with circular padding. |
|
|
189
|
-
|
|
190
|
-
Do not derive output dimensions here. Open `../size-inference-variations.md` when `padding` interacts with convolution, pooling, stride, dilation, or flattening.
|
|
191
|
-
|
|
192
|
-
## Dropout layers
|
|
193
|
-
|
|
194
|
-
Source URL: https://brainx.chaobrain.com/brainstate/apis/nn/dropout.html
|
|
195
|
-
|
|
196
|
-
The official catalog distinguishes elementwise dropout, channelwise spatial dropout, alpha-dropout variants, and a fixed mask along the time axis.
|
|
197
|
-
|
|
198
|
-
| API | Constructor | Select when |
|
|
199
|
-
|---|---|---|
|
|
200
|
-
| `Dropout` | `Dropout(prob=0.5, broadcast_dims=(), name=None)` | A subset of inputs should be ignored stochastically each training step; `broadcast_dims` controls mask broadcasting. |
|
|
201
|
-
| `Dropout1d` | `Dropout1d(prob=0.5, channel_axis=-1, name=None)` | Entire channels of 1D feature maps should be zeroed. |
|
|
202
|
-
| `Dropout2d` | `Dropout2d(prob=0.5, channel_axis=-1, name=None)` | Entire channels of 2D feature maps should be zeroed. |
|
|
203
|
-
| `Dropout3d` | `Dropout3d(prob=0.5, channel_axis=-1, name=None)` | Entire channels of 3D feature maps should be zeroed. |
|
|
204
|
-
| `AlphaDropout` | `AlphaDropout(prob=0.5, name=None)` | Alpha Dropout is required. |
|
|
205
|
-
| `FeatureAlphaDropout` | `FeatureAlphaDropout(prob=0.5, channel_axis=-1, name=None)` | Entire channels should be masked with Alpha Dropout properties. |
|
|
206
|
-
| `DropoutFixed` | `DropoutFixed(in_size, prob=0.5, name=None)` | A dropout mask fixed along the time axis is required. |
|
|
207
|
-
|
|
208
|
-
After selecting the class, route RNG streams, deterministic replay, mapped randomness, checkpoint behavior, or training/evaluation mode to `../brainstate/randomness-and-reproducibility.md`; those concerns are deliberately not duplicated here.
|
|
133
|
+
Open `skills/brainstate/scripts/modern_cnn.py` for the full convolution,
|
|
134
|
+
normalization, pooling, dropout, and dense workflow.
|
|
209
135
|
|
|
210
|
-
##
|
|
136
|
+
## Official sources
|
|
211
137
|
|
|
212
138
|
- https://brainx.chaobrain.com/brainstate/apis/nn/linear.html
|
|
213
139
|
- https://brainx.chaobrain.com/brainstate/apis/nn/conv.html
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# BrainState model interoperation and migration
|
|
2
2
|
|
|
3
3
|
Use this reference after the main BrainState skill when a task must move registered weighted layers between BrainState, Flax NNX, Flax Linen, or Equinox, or translate a PyTorch workflow into BrainState equivalents. It assumes the main skill's `State`, `Module`, parameter-collection, randomness, and state-aware transformation model; this file covers only interoperability and migration deltas.
|
|
4
4
|
|
|
5
|
+
## Selection map
|
|
6
|
+
|
|
7
|
+
| Need | Use | Key constraint |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| Convert one supported layer or `Sequential` stack | `brainstate.interop.from_*` or `to_*` | Check `supported_layers()` in the installed version and verify numerical output equivalence. |
|
|
10
|
+
| Convert a spatial layer into BrainState | `from_nnx`, `from_linen`, or `from_equinox` with `sample_input` | The sample fixes shape and layout information needed for construction. |
|
|
11
|
+
| Add a conversion for a custom layer | `LayerMapping` and `register_layer_mapping` | Implement and test both conversion directions. |
|
|
12
|
+
| Port a PyTorch workflow | Translate parameters, gradient selection, optimizer ownership, and checkpoint paths separately | This is a semantic migration, not an automatic whole-model conversion. |
|
|
13
|
+
| Convert branching, skip connections, or arbitrary custom forward code | Rebuild and validate manually | The automatic boundary is registered layers and supported `Sequential` structure. |
|
|
14
|
+
|
|
5
15
|
## Interop contract and api surface
|
|
6
16
|
|
|
7
17
|
Source: https://brainx.chaobrain.com/brainstate/apis/interop.html
|
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
Use this reference to compose size-aware layers with `Sequential` and `.desc()`, or when stride, padding, dimensionality, pooling mode, or flatten axes make a layer's size behavior non-obvious.
|
|
4
4
|
|
|
5
|
+
## Selection map
|
|
6
|
+
|
|
7
|
+
| Need | Use | Key constraint |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| Infer each next layer from the previous output | Give the first layer `in_size`, then use `.desc()` | Every descriptor must receive enough non-size constructor arguments to instantiate later. |
|
|
10
|
+
| Predict convolution output | Inspect `stride`, `padding`, dilation, rank, and channel layout | Use `stride`, not the obsolete `strides` spelling. |
|
|
11
|
+
| Reduce by local windows | `AvgPool*`, `MaxPool*`, or `LPPool*` | Kernel, stride, padding, and channel axis determine the result. |
|
|
12
|
+
| Produce a fixed spatial target | `AdaptiveAvgPool*` or `AdaptiveMaxPool*` | `target_size` determines the spatial output independently of input size. |
|
|
13
|
+
| Flatten a runtime batch | Set `start_axis` to preserve the batch axis | Runtime axes include batch; Module `in_size` metadata does not. |
|
|
14
|
+
| Flatten feature metadata for composition | Construct `Flatten(in_size=feature_size)` or use `.desc()` | Feature `in_size` excludes the batch dimension. |
|
|
15
|
+
|
|
5
16
|
## Compose `ComplexNet` with `Sequential` and `.desc()`
|
|
6
17
|
|
|
7
18
|
Give the first layer an explicit `in_size`, then use `.desc()` wherever `Sequential` should instantiate a layer from the preceding `out_size`. Start the classifier from `self.features.out_size` so flattening and dense construction stay coupled to the feature extractor.
|