Skip to content

fix(docs,inputs,rates): repair docs notebooks + Hz-input & RNNCell.reset(batch_size) bugs#857

Merged
chaoming0625 merged 1 commit into
masterfrom
worktree-notebooks-fix
Jun 19, 2026
Merged

fix(docs,inputs,rates): repair docs notebooks + Hz-input & RNNCell.reset(batch_size) bugs#857
chaoming0625 merged 1 commit into
masterfrom
worktree-notebooks-fix

Conversation

@chaoming0625

Copy link
Copy Markdown
Member

Summary

Ran every notebook under docs/ in a memory-capped, strictly-sequential sandbox (the task warned this is easy to crash the host), triaged failures, and fixed them.

Key finding: most failures were an artifact of executing the notebooks against a stale installed brainpy build (the kernel's cwd is the notebook dir, so import brainpy resolved to site-packages, not the repo). Re-running against the repo source via PYTHONPATH cleared simulation, training, optimizers, bp_training with no code change. The remainder were genuine notebook API-drift plus two real brainpy bugs.

brainpy bug fixes (with reproducing tests)

  • inputs.sinusoidal_input / square_inputbraintools now requires the frequency to carry Hz units (and, once dt is unit-carrying, the time args to carry ms); the wrappers passed bare numbers and raised AssertionError: Frequency must be in Hz. Now attach units before delegating. Tests in brainpy/inputs/currents_test.py.
  • dyn.rates RNNCell/GRUCell/LSTMCell/ConvLSTM reset_state — only understood batch_or_mode, so the canonical model.reset(batch_size=...) convention (shared with brainpy.dyn neurons) silently reset to an unbatched shape and raised MathError. Now accept batch_size as an alias (back-compat batch_or_mode preserved; the existing nvar keyword test still passes). Tests in brainpy/dyn/rates/rnncells_test.py.

Notebooks re-executed green

tutorial_math/{array,Dedicated_Operators,control_flows,variables}, tutorial_toolbox/inputs, tutorial_FAQs/gotchas_of_brainpy_transforms, tutorial_training/{esn_introduction,build_training_models} — fixes range from bm.Array(...) wrapping (in-place assignment on now-immutable jax arrays), split_keyssplit_key, while_loop tuple carry, bp.layers.{Reservoir,LSTMCell,NVAR}bp.dyn.*, a raises-exception tag for an intentional gotcha, to rewriting the build_training_models SNN example onto the bp.dyn projection API (HalfProjAlignPost/Expon/CUBA/LifRef/Leaky).

tutorial_advanced/interoperation gets a genuine b.value fix (wrap in bm.Array); its MNIST-download + multi-epoch CNN/RNN training tail is CPU-heavy and not re-run end-to-end here.

Left as-is (heavy / optional-dep, documented not dropped)

Given the explicit crash constraint: integrate_flax_into_brainpy (10-epoch flax CNN times out on CPU), integrate_bp_{convlstm,lif}_into_flax (need tensorflow_datasets), dde_numerical_solvers (bp.fde.GLShortMemory allocates a ~3 GiB array), and the clear_buffer=True/multi-device cells of parallel_for_parameter_exploration (clear_buffer_memory() deletes the live JAX ordered-effects token reused by the next batch; the core demo works).

Tests

currents_test.py, rnncells_test.py + touched-module regression sweep (currents_coverage_test.py, rates_test.py, nvar_test.py, nvar_coverage_test.py) all pass.

…reset(batch_size) bugs

Re-ran every notebook under docs/ in a memory-capped, sequential sandbox.
Most failures were an artifact of executing against a stale *installed*
brainpy build; the repo source already fixes them. The remainder were genuine
notebook API-drift and two real brainpy bugs.

Notebooks (re-executed green against the repo source):
- tutorial_math/array, Dedicated_Operators: wrap computed / random arrays in
  bm.Array so the documented in-place assignment works (bm.random.* and Array
  arithmetic now return immutable jax arrays).
- tutorial_math/control_flows: while_loop body returns a tuple matching operands.
- tutorial_math/variables: RandomState.split_keys -> split_key.
- tutorial_FAQs/gotchas_of_brainpy_transforms: tag the intentional "State as a
  transform argument" gotcha raises-exception; the "this works" example now
  passes a plain array instead of a State.
- tutorial_training/esn_introduction: bp.layers.Reservoir -> bp.dyn.Reservoir.
- tutorial_training/build_training_models: bp.layers.{LSTMCell,NVAR} ->
  bp.dyn.*, fix the DeepRNN.update layer chain, and rewrite the SNN example to
  the bp.dyn projection API (HalfProjAlignPost / Expon / CUBA / LifRef / Leaky).
- tutorial_advanced/interoperation: wrap b in bm.Array so b.value works.

brainpy fixes (with reproducing tests):
- inputs.sinusoidal_input / square_input: attach Hz (frequency) and ms (time)
  units before delegating to braintools, which now rejects bare numbers.
- dyn.rates RNNCell/GRUCell/LSTMCell/ConvLSTM reset_state: accept batch_size as
  an alias for batch_or_mode, matching the canonical model.reset(batch_size=...)
  convention used by brainpy.dyn neurons.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry @chaoming0625, your pull request is larger than the review limit of 150000 diff characters

@github-actions github-actions Bot added the tests label Jun 19, 2026
@chaoming0625 chaoming0625 merged commit a5e75b2 into master Jun 19, 2026
14 checks passed
@chaoming0625 chaoming0625 deleted the worktree-notebooks-fix branch June 19, 2026 06:25
@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
brainpy/dyn/rates/rnncells.py 87.50% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant