Skip to content

Refresh docs notebooks; fix clear_buffer/pmap runtime bugs; drive package-wide mypy to zero#863

Merged
chaoming0625 merged 2 commits into
masterfrom
worktree-docs-nb-gpu
Jul 8, 2026
Merged

Refresh docs notebooks; fix clear_buffer/pmap runtime bugs; drive package-wide mypy to zero#863
chaoming0625 merged 2 commits into
masterfrom
worktree-docs-nb-gpu

Conversation

@chaoming0625

Copy link
Copy Markdown
Member

Summary

Three related pieces of housekeeping, done together on this branch:

1. Docs notebook refresh

Re-executed every notebook under docs/ end-to-end against BrainPy 2.8.0 and refreshed their stored outputs in place (figures embedded). Repaired notebook-level API drift where cells had gone stale:

  • interoperation: bp.interop.FromFlaxbp.dnn.FromFlax, bp.layers.GRUCellbp.dyn.GRUCell, generator-based BPTT.fit(...), and bm.set(mode=bm.training_mode) before model construction.
  • parallel_for_parameter_exploration: set the XLA host-device flag before importing brainpy (import locks the backend); pass progress_bar=False to the multi-device pmap DSRunner.
  • gotchas_of_brainpy_transforms: fold the now-rejected Variable-as-arg call into the notebook's own try/except demonstration.
  • Dedicated_Operators: materialize sparse nonzero indices to numpy.

2. Runtime bugfixes (surfaced while re-executing the notebooks)

  • math/environment.py: clear_buffer_memory() now resets JAX's runtime effect-token registry after deleting live device buffers, so a subsequent ordered io_callback no longer crashes with "deleted or donated buffer".
  • running/jax_multiprocessing.py: jax_parallelize_map now routes through brainstate.transform.pmap2 (state-aware) instead of raw jax.pmap, and pulls each chunk's outputs to host before concatenation. Fixes a TraceContextError when the mapped function builds a stateful model (random init writes to a State) and a device-placement failure on a trailing partial chunk sharded on a subset of devices.
  • Added co-located regression tests for both modules.

3. Package-wide mypy typing

Drove mypy brainpy/ from 1568 errors → 0 across 123 files. Annotation-only, no runtime behavior change: implicit-Optional defaults promoted to Optional[T], missing var-annotations added, Optional values narrowed with asserts/guards, TypeVar-as-attribute annotations widened, and a small number of targeted # type: ignore[code] where the offending annotation lives in a third-party package. The curated CI mypy subset remains green.

Validation

  • python -m mypy brainpy/Success: no issues found in 432 source files.
  • Curated CI mypy subset still green.
  • Runtime smoke test (import edited modules, construct LIF/HH, run a short DSRunner sim) passes.
  • Regression tests for the two runtime fixes pass.

Latent bugs found but NOT fixed (out of scope — flagging for follow-up)

  • brainpy/mixin.py: SupportSTDP.stdp_update has a keyword typo onn_post (should be on_post) — this is the root of the STDP [override] signature mismatches.
  • brainpy/runners.py:455: raise warnings.warn(...) raises a TypeError instead of emitting a warning.

Re-executed every notebook under docs/ end-to-end against BrainPy 2.8.0
and refreshed their stored outputs in place (embedded figures included).

Repaired notebook-level API drift where cells had gone stale:
- interoperation: bp.interop.FromFlax -> bp.dnn.FromFlax,
  bp.layers.GRUCell -> bp.dyn.GRUCell, generator-based BPTT.fit(...),
  and bm.set(mode=bm.training_mode) before model construction.
- parallel_for_parameter_exploration: set the XLA host-device flag
  before importing brainpy (import locks the backend), and pass
  progress_bar=False to the multi-device pmap DSRunner (ordered
  io_callback effects cannot cross a multi-device pmap).
- gotchas_of_brainpy_transforms: fold the now-rejected Variable-as-arg
  call into the notebook's own try/except demonstration.
- Dedicated_Operators: materialize sparse nonzero indices to numpy.

All code cells run cleanly with sensible outputs.
…to zero

Runtime fixes (surfaced while re-executing the docs notebooks):
- math/environment.py: clear_buffer_memory() now resets JAX's runtime
  effect-token registry after deleting live device buffers, so a later
  ordered io_callback no longer crashes with "deleted or donated buffer".
- running/jax_multiprocessing.py: jax_parallelize_map now routes through
  brainstate.transform.pmap2 (state-aware) instead of raw jax.pmap, and
  pulls each chunk's outputs to host before concatenation. This fixes a
  TraceContextError when the mapped function builds a stateful model
  (random init writes to a State) and a device-placement failure on the
  trailing partial chunk sharded on a subset of devices.
  Added co-located regression tests for both modules.

Typing:
- Drove `mypy brainpy/` from 1568 errors to 0 across 123 files. These are
  annotation-only changes with no runtime behavior change: implicit-Optional
  defaults promoted to Optional[T], missing var-annotations added,
  Optional values narrowed with asserts/guards, TypeVar-as-attribute
  annotations widened, and a small number of targeted `# type: ignore[code]`
  where the offending annotation lives in a third-party package. The
  curated CI mypy subset remains green.

@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, we are unable to review this pull request

The GitHub API does not allow us to fetch diffs exceeding 20000 lines

@github-actions github-actions Bot added the tests label Jul 8, 2026
@chaoming0625 chaoming0625 merged commit d684af5 into master Jul 8, 2026
4 of 12 checks passed
@chaoming0625 chaoming0625 deleted the worktree-docs-nb-gpu branch July 8, 2026 17:36
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