Skip to content

fix(dyn/rates): RNN cell reset_state crash + ThresholdLinearModel noise dt-scaling#850

Merged
chaoming0625 merged 1 commit into
masterfrom
fix/audit-20260619-dyn-rates-base
Jun 18, 2026
Merged

fix(dyn/rates): RNN cell reset_state crash + ThresholdLinearModel noise dt-scaling#850
chaoming0625 merged 1 commit into
masterfrom
fix/audit-20260619-dyn-rates-base

Conversation

@chaoming0625

@chaoming0625 chaoming0625 commented Jun 18, 2026

Copy link
Copy Markdown
Member

Fresh review of brainpy/dyn/{rates,outs,others} + dyn base/utils.

  • HighRNNCell/GRUCell/LSTMCell.reset_state() crashed (ValueError) in default usage; state now built via variable() instead of parameter((None, ...)).
  • MediumThresholdLinearModel scaled Euler-Maruyama noise by dt instead of sqrt(dt) (dt-dependent noise intensity).

Prior-audit rate/oscillator/noise bugs (StuartLandau coupling, etc.) verified already-fixed. In-scope: 118 passed. Findings: docs/issues-found-20260619-dyn-rates-base.md.

Summary by Sourcery

Fix RNN cell state resetting and correct ThresholdLinearModel noise scaling in the dyn/rates module.

Bug Fixes:

  • Prevent RNNCell, GRUCell, and LSTMCell reset_state from crashing when called with the default unbatched arguments or via bp.reset_state, while preserving correct batched behavior.
  • Correct ThresholdLinearModel stochastic update so noise intensity is independent of the integration time step by using proper Euler–Maruyama sqrt(dt) scaling for excitatory and inhibitory populations.

Documentation:

  • Add an audit report documenting the dyn/rates and related modules review, the fixed issues, and remaining low-priority findings.

Tests:

  • Add regression tests covering reset_state behavior for RNNCell, GRUCell, and LSTMCell across unbatched, bp.reset_state, and integer-batch modes.
  • Add statistical tests to verify ThresholdLinearModel noise follows sqrt(dt) scaling and produces finite outputs under noisy conditions.

…odel noise dt-scaling

- RNNCell/GRUCell/LSTMCell.reset_state() crashed (ValueError) in default
  bp.reset_state usage; build state via variable() not parameter((None,...)) (High)
- ThresholdLinearModel scaled its Euler-Maruyama noise by dt instead of
  sqrt(dt), making the noise intensity dt-dependent (Medium)

Findings recorded in docs/issues-found-20260619-dyn-rates-base.md
@chaoming0625 chaoming0625 merged commit 3714692 into master Jun 18, 2026
2 of 5 checks passed
@sourcery-ai

sourcery-ai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Reviewer's Guide

Fixes a crash in RNN-style rate cells when resetting state by constructing state via mode-aware variables instead of parameters, and corrects the stochastic update of ThresholdLinearModel so its noise term scales as sqrt(dt) rather than dt, with new regression tests and an audit doc recording other findings.

File-Level Changes

Change Details Files
Fix RNNCell/GRUCell/LSTMCell.reset_state to work with default (None) and batched modes by using mode-aware variable construction instead of parameter-based shapes.
  • Replace parameter-based state construction in reset_state with variable(self._state_initializer, batch_or_mode, state_dim) so None, int, and Mode inputs are handled consistently with init.
  • Keep train_state support by leaving state2train as a parameter and re-synchronizing self.state from self.state2train after reset.
  • Add regression tests covering reset_state(None), bp.reset_state(cell), and integer batch sizes for RNNCell, GRUCell, and LSTMCell.
brainpy/dyn/rates/rnncells.py
brainpy/dyn/rates/rnncells_test.py
Correct ThresholdLinearModel stochastic dynamics to use Euler–Maruyama sqrt(dt) noise scaling and ensure outputs remain finite.
  • Refactor e and i updates so deterministic drift is divided by tau and multiplied by dt before clamping, rather than mixing noise into a pre-scaled increment.
  • Apply Gaussian noise as a separate term scaled by noise_/tau_ * sqrt(dt) * randn, making noise intensity independent of dt.
  • Set the new state directly from the drift-plus-noise result and clamp with bm.maximum(..., 0.).
  • Add tests that empirically verify the noise increment standard deviation scales as sqrt(dt) and that outputs remain finite for nonzero noise.
brainpy/dyn/rates/populations.py
brainpy/dyn/rates/rates_test.py
Record the broader dyn/rates + dyn/outs + dyn/others + dyn base/utils audit context and low-priority findings without changing behavior.
  • Add an audit document summarizing previously fixed high/critical issues, the newly fixed reset_state and noise-scaling bugs, and several low-priority style/false-positive items left as-is.
  • Document that certain prior audit items (e.g., FeedbackFHN reset_state style and delay usage) are not functional bugs under the current stack and are recorded only.
docs/issues-found-20260619-dyn-rates-base.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@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.

Hey - I've left some high level feedback:

  • The new TestThresholdLinearModelNoise tests rely on large Monte Carlo samples and a relatively tight delta for a stochastic ratio; consider either reducing the sample size and/or relaxing the tolerance or structuring the test to avoid randomness to reduce the risk of slow or flaky behavior across platforms and JAX backends.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `TestThresholdLinearModelNoise` tests rely on large Monte Carlo samples and a relatively tight `delta` for a stochastic ratio; consider either reducing the sample size and/or relaxing the tolerance or structuring the test to avoid randomness to reduce the risk of slow or flaky behavior across platforms and JAX backends.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@github-actions github-actions Bot added documentation Improvements or additions to documentation tests labels Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant