NIR mapping¶
What NIR is, and why we support it¶
The Neuromorphic Intermediate Representation (NIR) is a
framework-neutral format for describing spiking and continuous neuron networks as a graph of
well-defined node types (Linear, Affine, LIF, CubaLIF, …). It is the lingua franca that
lets a model trained in one framework be read by another. By consuming NIR, snn-mlir becomes
framework-agnostic for free: any simulator that exports the
supported NIR nodes can target the dialect, instead of us
writing a bespoke importer per framework.
NIR is broader than MLIR's digital world
NIR is designed to describe both digital and analog neuron models, using continuous
physical parameters — membrane and synaptic time constants (tau_mem, tau_syn),
resistance (r), leak (v_leak), threshold (v_threshold), and so on. The snn dialect,
by contrast, targets digital, discrete-time execution. The frontend therefore
discretizes the continuous NIR parameters into the per-timestep decay factors the dialect
uses.
Discretization¶
For each neuron node, the parser derives the discrete-time update factors from the NIR physical
parameters. For a CubaLIF node, for example:
dt = tau_mem / r
cur_decay = 1 − dt / tau_syn # current (synaptic) leak per step
vol_decay = 1 − dt / tau_mem # voltage (membrane) leak per step
threshold = v_threshold
The parser also enforces the dialect's assumptions — e.g. v_leak must be 0, and tau_syn,
tau_mem, v_threshold must be uniform across the layer (see
Limitations).
The non-leaky nodes are not the leaky ones with decay = 1¶
nir.IF and nir.I do describe the same dynamics as LIF/LI with the leak disabled, but that
is a statement about the neuron, not a recipe for the parser. They carry no tau and no
v_leak field at all, so:
- there is nothing to discretize —
decay = 1is the definition of the node, not a computed result — and the leaky parser would simply fail on the missingtau; and rchanges meaning. Intau·dv/dt = (v_leak − v) + r·ithe exporter conventionr = tau/dtmakesrcancel out of the input gain entirely (it survives only indecay = 1 − 1/r), which is whyLIF/LIaccept anyr.IF/Ihave the equationdv/dt = r·i: notau, so nothing carriesdtand nothing cancels.ris left as a bare gain on the input, and the update these emit isvoltage += input. A node withr ≠ 1is therefore rejected, in the same spirit as theCubaLIFgain check below.
snn_mlir handles this with two extra parser functions (parse_if, parse_i) feeding the
existing snn.lif / snn.li ops — the same shape as parse_linear/parse_affine, which both
build snn.linear. The dialect stays at four neuron ops.
The discretization convention¶
NIR is deliberately continuous — it describes neuron dynamics with physical time constants and
"abstracts away" the simulator's timestep. A discrete-time exporter therefore has to smuggle its
timestep into the file somewhere, and the ecosystem convention is to use r and w_in as the
carriers:
r = tau_mem / dt # so the parser can recover dt = tau_mem / r
w_in = tau_syn / dt # so the discrete input gain k = w_in · dt / tau_syn = 1
The frontend relies on this. By the dt = tau_mem / r construction, the LIF/LI input gain
and the CubaLIF/CubaLI voltage-stage gain are identically 1 for any r; the one remaining
free parameter is the current-stage gain k = w_in · dt / tau_syn, and the emitted update
current += input assumes k = 1. The parser checks exactly that: a CubaLIF/CubaLI node
whose parameters give k ≠ 1 (within float32 tolerance) is rejected with an error naming
the node and the computed k, rather than silently compiling dynamics different from the ones
trained.
Files written with true continuous time constants (e.g. r = 1, w_in = 1) will fail this
check. That is intentional for now: folding a k ≠ 1 gain into the weights or threshold is a
planned ingestion feature, and until it lands, rejecting is the honest alternative to
miscompiling.
Node mapping¶
Each SNN op covers a family of NIR nodes:
| NIR node | SNN op | Notes |
|---|---|---|
nir.Linear |
snn.linear |
No bias |
nir.Affine |
snn.linear |
Bias added as second operand |
nir.Conv2d |
snn.conv2d |
2-D convolutional synapse; float + quantized |
nir.Conv1d |
snn.conv1d |
1-D convolutional synapse; float + quantized |
nir.SumPool2d |
snn.sumpool2d |
Sum pooling; float + quantized |
nir.AvgPool2d |
snn.avgpool2d |
Average pooling; float + quantized |
nir.Flatten |
(reshape) | Structural — flattens a feature map to a vector; no dedicated op |
nir.CubaLIF |
snn.cubalif |
cur_decay, vol_decay < 1 |
nir.CubaLI |
snn.cubali |
cur_decay, vol_decay < 1 |
nir.LIF |
snn.lif |
decay < 1, derived from tau/r |
nir.IF |
snn.lif |
decay = 1.0 by definition; requires r = 1 |
nir.LI |
snn.li |
decay < 1, derived from tau/r |
nir.I |
snn.li |
decay = 1.0 by definition; requires r = 1 |
| (internal) | snn.rescale |
Inserted between a synapse and neuron ops during quantized export; no NIR equivalent |
NIR has no cumulative-current integrate-and-fire node — there is no nir.CubaIF or nir.CubaI
— so snn.cubalif/snn.cubali are reachable only from their leaky counterparts. Thirteen NIR
node types map today; the table above is the complete list. The four neuron families
(CubaLIF/CubaLI/LIF/LI, plus the non-leaky IF/I pair that reuses snn.lif/snn.li)
still collapse to just four neuron ops in the dialect.
Recurrence¶
The one supported cycle is the canonical SNN self-recurrence: a neuron whose spikes feed a recurrent synapse that projects back onto the same neuron —
The graph walk breaks the neuron→synapse edge (lif1 → w_rec) at the timestep boundary:
w_rec reads the previous timestep's spikes from a dedicated state buffer, which keeps that
buffer spike-typed (i8 quantized / f32 float). This is the standard discrete-time SNN
reading of a recurrent connection. Any other cycle — a self-loop, a longer loop, a loop without
a neuron→synapse edge — is rejected with an error.
Three consequences in the emitted MLIR:
- Execution order. The recurrent synapse runs first in the timestep (it depends only on
stored state), so for the graph above the order is
w_rec, fc1, lif1, fc2, …—GraphInfo.orderis the authority. - Fan-in merge. The neuron now has two inputs (
fc1andw_rec). Each edge gets its ownsnn.rescalein quantized mode, and the branches are summed elementwise by alinalg.generic(arith.addiin Q12;arith.addfin float) just before the neuron. Synapses feeding the same neuron are clamped to one sharedw_scale(the minimum of the candidates) — a uniform fan-in scale is what lets any backend fuse the merge without changing the result, since the rescale's left shift distributes over the addition. - State buffer + copy. The previous-spikes buffer is a function argument
(
%prev_spikes_<neuron>), and the function ends with amemref.copyof the neuron's fresh spikes into it for the next call.
The argument-order contract¶
The generated @snn_forward_step is called positionally from C, so its argument order is an
ABI. It is, deterministically:
%input- for each layer in
GraphInfo.order: its state buffers (e.g.%current_<n>,%voltage_<n>), and — immediately after them, when the layer is a recurrent neuron — its previous-spikes buffer%prev_spikes_<n> %output
Any main.c-style caller must mirror this exactly; a mismatch is silent memory corruption, not
a link error.
Because the ABI is positional, names never cross it — which matters for NIR node names that are
not valid C identifiers (dotted submodule paths like lif1.lif are common). MLIR keeps the
name verbatim; C generators use NodeInfo.c_name, the name with every non-identifier character
replaced by _ (lif1.lif → lif1_lif). Since that mangling can collide (a.b and a_b),
codegen checks the graph's C names are unique up front and fails loudly if not.
Current NIR coverage & pending nodes¶
The supported set above covers dense and convolutional feedforward networks, plus canonical SNN self-recurrence (see Recurrence). The NIR primitives not yet mapped are:
nir.Delay— a fixed time-delay edgenir.Scale— a scalar gain nodenir.Threshold— a standalone threshold node
Both convolutions map in float and quantized mode. LLVM ships no quantized rank-3 named
conv, so nir.Conv1d is lowered by embedding it in a 2-D convolution with a unit width axis and
reusing linalg.conv_2d_nchw_fchw_q — exact, since the extra axis is size 1. Branching and
residual topologies remain unsupported (only linear chains and the one recurrent cycle above).
Adding a node is a contained task — see Adding a NIR node type. If the model you care about uses an unsupported node, we'd be glad to help; send us the NIR graph.