"I benchmarked six architectures. The state-space model won on the sensor that never stops streaming, and lost on the one that fits in a single glance."
A Pragmatic AI Agent
The big picture
The previous six sections gave you the machinery: the quadratic wall attention hits on long sequences (16.1), the recurrence-plus-convolution duality of S4 and S5 (16.2), the input-dependent selectivity of Mamba (16.3), the time-series variants (16.4), the head-to-head numbers (16.5), and hybrids that mix both (16.6). This section is the decision itself. When you stand in front of a real sensor problem, with a real sampling rate, a real latency budget, and a real deployment target, which of these do you actually build? An SSM is not a default and not a fad. It is a tool with a sharp profile: linear-time streaming, a bounded recurrent state, and a graceful appetite for very long context. Knowing that profile tells you exactly when to reach for it and, just as usefully, when to leave it on the shelf.
This is an [A][R] chapter, so we assume you are comfortable with the transformer baseline from Chapter 15 and the recurrent and convolutional families from Chapter 14. The goal here is a repeatable decision procedure, not another architecture. Read this section as the checklist you run before committing engineering time.
The three questions that pick the architecture
Nearly every sensor modeling choice collapses onto three axes. Answer them honestly for your problem and the architecture usually falls out.
1. How long is the effective context, and how does it grow? Attention costs \(O(L^2)\) time and memory in sequence length \(L\); an SSM costs \(O(L)\) to scan and, crucially, \(O(1)\) memory per emitted step when run as a recurrence. If your window is 128 samples of accelerometer data for a gesture, \(L^2\) is nothing and the SSM advantage is invisible. If your window is a 24-hour photoplethysmography (PPG) trace at 25 Hz, that is over two million samples, and the quadratic term is fatal. The SSM's linear scan is not a nice-to-have there; it is the difference between training and not training.
2. Does the signal stream, and must inference be causal and stateful? An SSM carries a fixed-size hidden state \(h_t\) forward through the recurrence
$$h_t = \bar{A}\, h_{t-1} + \bar{B}\, x_t, \qquad y_t = C\, h_t,$$so at deployment you keep one small state vector and update it per sample in constant time and constant memory, forever. A transformer, by contrast, either recomputes over a growing cache or slides a fixed window and forgets. For an always-on wearable or an industrial monitor that must run for weeks between resets, that bounded state is the whole argument. See Chapter 60 for the streaming-inference machinery this enables.
3. What is the compute and memory envelope of the deployment target? A cloud GPU forgives a lot; a microcontroller forgives nothing. The SSM's constant per-step cost and small parameter footprint make it a natural fit for the edge (Chapter 59), but the selective scan needs a hardware-aware kernel to hit its theoretical speed, and that kernel is not free on every accelerator.
Key insight
SSMs win where two conditions hold together: the sequence is long and the inference is streaming. Long-but-batched problems (offline forecasting on a big GPU) often go to transformers or foundation models; short-but-streaming problems (a 1-second window classifier) often go to a temporal CNN. The SSM's home turf is the intersection: a signal that is both long and never stops.
A decision matrix you can actually use
Map your problem onto the table below. It is deliberately opinionated; treat it as a strong prior you then confirm with the Lab 16 benchmark, not as gospel.
| Situation | Reach for | Why |
|---|---|---|
| Very long context (10k to millions of steps), streaming, on-device | SSM (Mamba/S5) | Linear scan, \(O(1)\) state, small footprint |
| Long context but offline, big GPU, need max accuracy and rich cross-channel attention | Transformer or time-series foundation model | Attention captures arbitrary pairwise structure; batch compute hides the \(L^2\) cost |
| Short fixed window (< ~1k steps), latency-critical classification | Temporal CNN / TCN (Ch 14) | Fully parallel, tiny, no scan kernel needed |
| Long context, need both long memory and sharp local content-based retrieval | Hybrid SSM-attention (16.6) | SSM backbone for context, sparse attention for recall |
| Linear-Gaussian dynamics with a known model and calibrated uncertainty | Kalman family (Ch 9) | Optimal, interpretable, no training data needed |
The last row deserves emphasis. An SSM is a learned linear state-space recurrence, structurally cousin to the Kalman filter you met in Chapter 9. If you already know the physics, if the dynamics are linear-Gaussian and you need calibrated covariance, a Kalman filter gives you optimality and uncertainty for free and needs zero labels. Reach for the neural SSM when the dynamics are unknown, nonlinear in the mapping, or high-dimensional enough that hand-specifying \(A\), \(B\), \(C\) is hopeless, and when you have data to learn them.
The costs that do not show up in the accuracy column
Three failure modes catch teams who adopt SSMs on hype rather than on fit.
Kernel dependence. Mamba's speed comes from a fused selective-scan CUDA kernel. On a platform without it (some edge NPUs, older toolchains, WebGPU), you fall back to a slower path and the latency advantage over a well-tuned TCN can evaporate. Confirm the kernel exists for your target before you commit.
Retrieval weakness. A fixed-size hidden state is a lossy summary. When the task needs exact recall of a specific earlier event (find the one heartbeat that looked like this), pure SSMs underperform attention, which is exactly why the hybrids of 16.6 exist. If your task is recall-heavy, budget for attention somewhere in the stack.
Short sequences give nothing. On windows of a few hundred steps, the \(L^2\) term is cheap and a transformer or TCN is simpler to train, easier to debug, and often just as fast. Do not pay the SSM's complexity tax for context you do not have.
Practical example: a patch-pump insulin monitor
A clinical wearable team builds a hypoglycemia early-warning model on a body-worn patch. The device samples glucose, motion, and skin temperature and must run for 14 days on a coin cell, emitting a risk score every minute from a stream that never resets. They start with a sliding-window transformer: it hits the accuracy target in the cloud, but on-device the growing key-value cache blows the RAM budget and the fixed window keeps forgetting the slow overnight drift that precedes morning lows. Switching the backbone to a Bi-Mamba encoder (16.4) collapses per-step inference to a constant-size 64-dimensional state update, holds a full multi-day context implicitly, and drops steady-state RAM by roughly an order of magnitude. The score-per-minute latency becomes flat and predictable, which is what the safety case in Chapter 34 actually requires. The SSM did not win on raw accuracy; it won on the streaming envelope.
A quick screening test before you build
Before committing to any architecture, run the cheap screen below. It computes the two ratios that most reliably predict whether an SSM pays off, so you learn the answer in seconds rather than after a week of training.
def ssm_fit_score(seq_len, streaming, ram_kb, needs_exact_recall):
"""Heuristic screen: is a selective SSM the right first bet?"""
reasons = []
score = 0
if seq_len >= 2000: # quadratic attention starts to hurt
score += 2; reasons.append("long context favors O(L) scan")
if streaming: # always-on, causal, per-sample emit
score += 2; reasons.append("O(1) recurrent state fits streaming")
if ram_kb < 512: # tight edge budget
score += 1; reasons.append("bounded state fits small RAM")
if needs_exact_recall: # SSMs summarize, they do not index
score -= 2; reasons.append("recall-heavy: add attention (16.6)")
verdict = ("reach for an SSM" if score >= 3
else "consider a hybrid" if score >= 1
else "SSM unlikely to help")
return verdict, reasons
print(ssm_fit_score(seq_len=2_160_000, streaming=True,
ram_kb=256, needs_exact_recall=False))
# ('reach for an SSM', ['long context favors O(L) scan',
# 'O(1) recurrent state fits streaming', 'bounded state fits small RAM'])
The ssm_fit_score function above encodes exactly the three-question logic: it rewards long context and streaming, gives a small bonus for tight RAM, and penalizes tasks that need exact retrieval. When it returns "reach for an SSM," you still verify empirically, but you start with a strong prior instead of a blank page.
Right tool: swap the backbone in a few lines
You do not implement the selective scan yourself. With the mamba-ssm package the entire long-context backbone is a drop-in block:
from mamba_ssm import Mamba
block = Mamba(d_model=64, d_state=16, d_conv=4, expand=2) # 1 line
y = block(x) # x: (batch, seq_len, 64) -> same shape, linear-time scan
That single constructor replaces roughly 150 lines of hand-written discretization, parallel-scan, and CUDA-kernel plumbing, and it ships the hardware-aware kernel that makes the linear-time claim real. Your job shrinks to choosing d_state and confirming the kernel exists on your target.
Research frontier
As of 2026 the practical default for long sensor streams is trending toward hybrids rather than pure SSMs: architectures in the Jamba and Zamba lineage interleave Mamba-2 blocks with a thin layer of attention, capturing the SSM's linear streaming cost while restoring exact recall. On the time-series side, SSM backbones increasingly appear inside foundation models rather than as standalone task models. The open question for sensing is calibration under distribution shift: a bounded recurrent state can silently drift when the input statistics move, which ties directly into the conformal and adaptive methods of Chapter 18.
Exercise
Take three sensor tasks you have seen in earlier chapters: a 1-second inertial gesture classifier (Chapter 26), a multi-day continuous ECG arrhythmia monitor (Chapter 29), and an offline vibration-based remaining-useful-life forecast on a server. For each, fill in the three decision questions (context length and growth, streaming and causality, compute envelope), then predict the winning architecture and justify it in two sentences. Where would a hybrid change your answer?
Self-check
- Give the two conditions that must hold together for a pure SSM to be the clear choice, and name one situation where each holds alone but the SSM still is not the pick.
- Why does a fixed-size hidden state hurt on retrieval-heavy tasks, and what does 16.6 add to fix it?
- Your target is an edge NPU with no selective-scan kernel. How does that change the SSM-versus-TCN comparison, and what would you measure to decide?
Lab 16
benchmark a Mamba-style model vs a transformer on long-horizon sensor sequences (accuracy vs latency vs memory).
Bibliography
Foundational state-space models
The paper that made long-range SSMs practical; the reference point for why linear-time recurrence competes with attention on long context.
Gu and Dao (2023). Mamba: Linear-Time Sequence Modeling with Selective State Spaces.
Introduces input-dependent selectivity and the hardware-aware scan kernel; the model this section's decision matrix is built around.
The parallel-scan MIMO simplification that clarifies the recurrence-versus-convolution duality behind the streaming argument.
Hybrids and the recall tradeoff
Lieber et al. (2024). Jamba: A Hybrid Transformer-Mamba Language Model.
Concrete evidence that interleaving attention with SSM blocks restores exact recall while keeping most of the linear-time benefit; grounds the "consider a hybrid" branch.
Formalizes the duality between attention and state-space models, explaining why the choice is a spectrum rather than a binary.
Time-series and sensor SSMs
Shows an SSM backbone competitive on multivariate forecasting benchmarks; a concrete data point for the offline-versus-streaming tradeoff.
Ahamed and Cheng (2024). TimeMachine: A Time Series is Worth 4 Mambas for Long-Term Forecasting.
A multi-scale Mamba design for long-horizon forecasting; useful when weighing SSMs against transformers on very long windows.
Liang et al. (2024). Bi-Mamba+: Bidirectional Mamba for Time Series Forecasting.
The bidirectional variant referenced in the clinical example; relevant when non-causal encoding of a bounded history is acceptable.
What's Next
In Chapter 17, we tackle the problem that quietly undermines every architecture in this part: labels are scarce and expensive in sensing. Whether your backbone is a transformer, a TCN, or the SSM you just learned to choose, a strong self-supervised pretraining objective is often what turns a mediocre model into a deployable one. We turn to contrastive and masked-reconstruction learning that squeezes signal out of the mountains of unlabeled sensor streams you already have.