brainevent.csr_delay_deliver

brainevent.csr_delay_deliver#

brainevent.csr_delay_deliver(ring, events, data, indices, indptr, delays, t, *, shape, weighted=None, backend=None)[source]#

Deliver spikes through a CSR matrix with per-edge delays into a ring buffer.

For every edge e of every active presynaptic row i, adds the edge weight into the post-side arrival ring at the slot the spike will arrive in, (t + delays[e]) mod L:

ring_new[(t + d_e) % L, indices[e]] += w_e * f(x)[i]

where f(x) is the activity mask of the events (binary mode; a numeric event is active iff x != 0) or the event value itself (weighted mode). Only the rows of active events are visited.

Parameters:
  • ring (Array | ndarray | Quantity | Number) – Arrival buffer of shape (L, n_post) with a floating-point dtype (float16, bfloat16, float32 or float64). Its dtype is the output dtype. When data or events carry units, ring must carry unit(data) * unit(events).

  • events (Any) – Presynaptic events of shape (n_pre,). Event wrappers are unwrapped; a WeightedArray selects weighted delivery. Any boolean, integer or floating dtype.

  • data (Array | ndarray | Quantity | Number) – Synaptic weights: a scalar / shape (1,) (homogeneous) or shape (nse,) (heterogeneous). Cast to ring.dtype.

  • indices (Array | ndarray) – CSR column (postsynaptic) indices, shape (nse,), int32.

  • indptr (Array | ndarray) – CSR row pointers, shape (n_pre + 1,), int32 or int64.

  • delays (Any) – Integer per-edge delays in steps, shape (nse,), or a scalar broadcast to every edge.

  • t (Any) – Current integer time step. May be traced (e.g. the step counter of a jax.lax.scan()).

  • shape (Tuple[int, int]) – (n_pre, n_post), the logical shape of the CSR matrix.

  • weighted (bool | None) – False: binary delivery, each active row adds w_e. True: weighted delivery, each row adds x[i] * w_e. None (default): True for a WeightedArray, False otherwise.

  • backend (str | None) – 'jax_raw', 'numba' (CPU) or 'cuda_raw' (GPU), or None for the platform default.

Returns:

ring_new – Updated ring of shape (L, n_post), same dtype and unit as ring.

Return type:

Array | ndarray | Quantity | Number

Raises:
  • ValueError – If delays is concrete and any value is outside [1, L).

  • TypeError – If t or delays is not an integer array.

  • AssertionError – On shape or dtype mismatches of the CSR operands.

See also

binary_csrmv

Scalar-delay (no ring) event-driven delivery.

CSR.delay_deliver

Method form on a CSR matrix.

Notes

mod is the floor modulo, so the slot is in [0, L) for any t (t may grow without bound). t and each d are reduced modulo L separately, so t + d never overflows.

A delay of 0 would write into the slot being read in the current step and a delay >= L would alias a later arrival, so concrete delays are validated to lie in [1, L). When delays is traced the check is impossible; out-of-range values then wrap modulo L identically on every backend.

The output is linear in ring and data (and, in weighted mode, in events). With s_e = (t + d_e) mod L and output cotangent g: d ring = g, d data[e] = f(x)[row(e)] * g[s_e, indices[e]] (summed for homogeneous weights) and d x[i] = sum_{e in row i} w_e * g[s_e, indices[e]], which is exact in weighted mode and the straight-through estimator in binary mode. delays, t, indices and indptr are not differentiable.

Examples

>>> import jax.numpy as jnp
>>> from brainevent import csr_delay_deliver
>>> # 2 presynaptic, 3 postsynaptic neurons; row 0 -> {0, 2}, row 1 -> {1}
>>> indices = jnp.array([0, 2, 1], dtype=jnp.int32)
>>> indptr = jnp.array([0, 2, 3], dtype=jnp.int32)
>>> delays = jnp.array([1, 2, 1], dtype=jnp.int32)
>>> ring = jnp.zeros((3, 3), dtype=jnp.float32)
>>> spikes = jnp.array([True, False])
>>> csr_delay_deliver(ring, spikes, jnp.array([0.5]), indices, indptr,
...                   delays, 0, shape=(2, 3), backend='jax_raw')
Array([[0. , 0. , 0. ],
       [0.5, 0. , 0. ],
       [0. , 0. , 0.5]], dtype=float32)