pub struct STDPRule {
pub tau_plus_us: u32,
pub tau_minus_us: u32,
pub a_plus: i16,
pub a_minus: i16,
pub learning_rate: u16,
}unstable-stdp only.Expand description
Unstable: behind unstable-stdp.
Spike-Timing-Dependent Plasticity rule.
The audit flagged this as the better STDP implementation (vs stdp_plasticity.rs):
a linear stand-in for the exponential decay, in fixed point (the factor is
1000 − |dt|·1000/tau, clamped at 0), sign-correct (positive dt → LTD,
negative dt → LTP).
Fields§
§tau_plus_us: u32LTP time constant (μs) — pre-before-post window.
tau_minus_us: u32LTD time constant (μs) — post-before-pre window.
a_plus: i16LTP amplitude (fixed-point, 1000 = 1.0).
a_minus: i16LTD amplitude (negative).
learning_rate: u16Overall learning rate (fixed-point, 1000 = 1.0).
Implementations§
Source§impl STDPRule
impl STDPRule
Sourcepub fn new() -> Self
pub fn new() -> Self
Default biological STDP: 20ms/20ms tau, asymmetric amplitudes (LTD slightly larger).
Sourcepub fn calculate_weight_change(&self, dt_us: i32) -> i16
pub fn calculate_weight_change(&self, dt_us: i32) -> i16
Compute the weight delta for a given spike-time difference.
§Parameters
dt_us = pre_spike_time - post_spike_time(positive = post fired first → LTD).
§Returns
Weight delta (signed): non-negative for LTP, non-positive for LTD, zero outside the window.
§Invariants
dt > 0→ returns ≤ 0 (LTD)dt < 0→ returns ≥ 0 (LTP)|dt| ≥ tau→ returns 0 (the linear factor reaches 0)
§Bug fix vs v0.1
v0.1 used factor = SCALE - decay without clamping. When |dt| > tau,
decay > SCALE and factor goes negative, breaking the sign convention
(LTP returns negative, LTD returns positive). The property test
prop_stdp_sign_convention catches this at dt = -24000. Fix: clamp
factor to ≥ 0 — the linear exp approximation naturally floors at zero.
§Bug fix (R18 rider, 2026-08-22)
dt_us.abs() * SCALE overflowed i32 at |dt| > ~2.15 s (release:
silent wrap → spurious deltas far outside the window, e.g. −560 at
dt = ±2 200 000 μs where 0 is required; debug: panic) — and
dt_us.abs() itself is UB-adjacent at i32::MIN. The whole
expression now runs in i64, cast BEFORE the abs (total at
i32::MIN), which also covers the tail product
a · factor · learning_rate overflowing i32 at extreme
learning_rate (u16 max). Observable values identical everywhere
the old code didn’t overflow.