01

Adaptive Neural Infrastructure
with Memory Accumulation

Fixed recurrent state is lossy, not an unlimited exact transcript. The simple recurrence is an educational SSM, not the full selective Mamba model.

Beyond fixed-compute, fixed-memory transformers — models that route compute where it's needed and remember across time.

Adaptive Compute

MoE · MoD · early exit — different tokens, different compute

Memory Accumulation

KV cache · SSMs · external memory banks

Combined

An end-to-end implementation in PyTorch

Start with a concrete question

Can we select experts, accumulate state, and keep memory scoped to a user?

Your goal

Understand the data, the rule, and the required result. Then run the complete Python program and modify one thing at a time.

No companion presentation is required. Python syntax and any mathematical notation are explained as they are introduced.

Vocabulary you need before the algorithm

Router

Scores which experts receive a row.

Sparse dispatch

Only selected experts process that row.

Recurrent state

A finite summary passed from one step to the next.

Application memory

Explicit durable state outside the neural function.

Work one example before writing code

State h←.5h+x on [1,0,0,2] gives [1,.5,.25,2.125]. Running two chunks while passing the final state produces the same result as one full run.

Trace each step and name what changed. Ask which fact stayed true and what should happen on an empty, invalid, or boundary input.

02 / Motivation

What's Wrong with Vanilla Transformers? TWO BIG GAPS

A dense transformer usually applies the same layer stack to each token. Per-token cost also depends on context length, caching, and implementation. Cross-session storage is a runtime choice, not a property supplied automatically by attention.

Problem 1 — Uniform Compute

Dense blocks use the same parameterized transformations at each position, but attention cost varies with prefix length and batching. Adaptive routing changes which blocks or experts participate.

  • Predicting the next token after "hello," (probably " world") takes the same compute as predicting the next token in a complex math proof.
  • A dense model applies most dense projection weights per token. Embeddings are lookups, shared weights may be tied, and attention-over-context cost is additional; parameter count is only an approximate compute proxy.
  • Massive waste — and a hard ceiling on inference throughput.

Problem 2 — Bounded, Amnesiac Memory

The model has no state outside its current context window:

  • Conversations don't persist. Same user tomorrow → blank slate.
  • Attention is O(N²) in context length — long contexts get expensive fast.
  • KV state grows with retained context and batch size. Context limits, sliding windows, eviction, and compression bound or change that growth.

The Fix — Two Orthogonal Levers

Vanilla Transformer all tokens, all params no memory between sessions + Adaptive Compute router picks experts per token ~⅛ of weights per token + Accumulating Memory persistent across sessions key-value bank · grows over time

Public systems demonstrate several combinations. Architecture and memory claims should follow dated documentation; do not infer undisclosed proprietary model internals.

03 / Adaptive Compute

Adaptive Neural Infrastructure ROUTE THE COMPUTE

A learned router decides which sub-network fires for each token at each layer. Most parameters stay dormant for any given token.

Mixture of Experts (MoE)

Replace each FFN with N experts. Router picks top-k per token.

  • Total params: huge
  • Active params: small
  • Memory cost: full · compute cost: sparse

Public, documented examples include Switch Transformer, Mixtral, and DeepSeek-V3. Undisclosed proprietary architecture should not be inferred.

Mixture of Depths (MoD)

Per-token, per-block routing. Router decides which tokens enter each transformer block; the rest skip via the residual.

  • Easy tokens → fewer layers
  • Hard tokens → full depth
  • Variable per-token compute

In the wild: Google's "Mixture-of-Depths" (Raposo et al. 2024); related: PonderNet, Adaptive Computation Time

Early Exit / ACT

Stop early when confident. Each layer outputs an "exit signal." When confidence crosses a threshold, this token skips the remaining layers and is predicted from the current layer.

  • Fastest path for trivial tokens
  • Variable model depth at inference
  • Needs exit heads trained with a per-layer loss

In the wild: CALM (Schuster et al. 2022), DeeBERT, Adaptive Computation Time (Graves 2016)

The Common Mechanism — A Learned Router

Teaching excerpt · complete runnable lab available from “Code”
class Router(nn.Module):
    def __init__(self, d_model, n_choices):
        super().__init__()
        self.gate = nn.Linear(d_model, n_choices, bias=False)

    def forward(self, x, top_k=1):
        scores       = self.gate(x)                         # (B, T, n_choices)
        weights, idx = scores.topk(top_k, dim=-1)        # pick top-k
        weights      = F.softmax(weights, dim=-1)         # normalize
        return weights, idx                              # downstream uses these

MoE typically ranks experts for each token; MoD may rank tokens for a fixed block budget; ACT/early-exit methods use halting policies. They are related adaptive-compute ideas with different mechanisms and guarantees.

04 / Adaptive Compute — MoE

Deep Dive — Mixture of Experts SAMPLE CODE

Sparse dispatch changes active compute, not total parameter storage. Uniform balancing statistic equals 1; valid imbalanced routing can give values below 1.

Replace the dense FFN with N specialized FFNs. A router picks top-k of them per token, weighted by softmax of the gate scores.

The Architecture

token x router (linear) expert 0 expert 1 w=0.7 expert 2 expert 3 w=0.3 weighted sum output = 0.7·E₁(x) + 0.3·E₃(x)

With 8 experts and top-k=2, only ¼ of FFN weights are touched per token. Mixtral 8×7B has 47B total params but only ~13B fire per token.

Sample Code

Teaching excerpt · complete runnable lab available from “Code”
import torch
import torch.nn as nn
import torch.nn.functional as F


class Expert(nn.Module):
    def __init__(self, d):
        super().__init__()
        self.fc1 = nn.Linear(d, 4*d)
        self.fc2 = nn.Linear(4*d, d)

    def forward(self, x):
        return self.fc2(F.gelu(self.fc1(x)))


class MoEFFN(nn.Module):
    def __init__(self, d, n_experts=8, top_k=2):
        super().__init__()
        self.top_k   = top_k
        self.router  = nn.Linear(d, n_experts, bias=False)
        self.experts = nn.ModuleList([
            Expert(d) for _ in range(n_experts)
        ])

    def forward(self, x):                 # x: (B, T, d)
        scores = self.router(x)            # (B, T, N)

        # Pick top-k experts per token
        weights, idx = scores.topk(self.top_k, dim=-1)
        weights = F.softmax(weights, dim=-1)

        # Sparse dispatch: send each token only
        # to its chosen experts
        out = torch.zeros_like(x)
        for e in range(len(self.experts)):
            for k in range(self.top_k):
                mask = (idx[..., k] == e)
                if mask.any():
                    out[mask] += (
                        weights[..., k:k+1][mask]
                        * self.experts[e](x[mask])
                    )
        return out


# Drop-in replacement for the FFN sublayer
# in a standard transformer block:
#   self.ffn = MoEFFN(d=128, n_experts=8, top_k=2)

Real production MoE adds load-balancing loss (so all experts get used) and capacity factor (token dropping when an expert overflows). Omitted for clarity.

04 / MoE — By the Numbers

MoE by the Numbers WORKED EXAMPLE

Sparse dispatch changes active compute, not total parameter storage. Uniform balancing statistic equals 1; valid imbalanced routing can give values below 1.

Where does "47B total, 13B active" come from? Count it by hand for Mixtral 8×7B.

Intuition — A Hospital, Not a Single Doctor

A triage nurse (the router) sends each patient (token) to 2 of 8 specialists (experts). The hospital employs everyone (memory), but each patient only takes up two doctors' time (compute).

Mixtral: d = 4,096 · FFN = 14,336 · 32 layers · 8 experts, top-2

PieceArithmeticParams
One expert (SwiGLU, 3 matrices)3 × 4096 × 14336176M
All experts, all layers176M × 8 × 3245.1B
Attention (GQA), all layers41.9M × 321.3B
Embeddings + LM head2 × 32000 × 40960.26B
Total (stored)≈ 46.7B
Active per token176M × 2 × 32 + 1.3B + 0.26B≈ 12.9B

Not 8 × 7B = 56B: the experts share attention and embeddings. Only the FFNs are copied.

Keeping Experts Busy — Load-Balancing Loss

Left alone, the router tends to collapse: a few experts get every token, the rest never train. Switch Transformer adds:

Teaching excerpt · complete runnable lab available from “Code”
L_aux = α · N · Σᵢ fᵢ · Pᵢ          (α ≈ 0.01)
  • fᵢ = share of tokens sent to expert i
  • Pᵢ = average router probability for expert i
8 expertsN · Σ fᵢPᵢ
Perfectly balanced (all 1/8)1.0 (uniform-routing reference)
Collapsed (all tokens → expert 0, P₀ = 0.9)8 · (1 · 0.9) = 7.2
Teaching excerpt · complete runnable lab available from “Code”
probs = F.softmax(scores, dim=-1)                 # (B, T, N)
f = F.one_hot(idx[..., 0], N).float().mean((0, 1))  # token share
P = probs.mean((0, 1))                            # mean prob
loss = lm_loss + alpha * N * (f * P).sum()

All expert weights must be stored somewhere accessible, but serving can distribute experts across devices or offload weights. Sparse activation reduces active computation, not total parameter storage.

05 / Memory Accumulation

Memory Accumulation STATE THAT PERSISTS

Fixed recurrent state is lossy, not an unlimited exact transcript. The simple recurrence is an educational SSM, not the full selective Mamba model.

Three flavors of accumulating state — from microsecond working memory to cross-session persistent knowledge.

KV Cache

Working memory within one generation.

  • Cache K and V from each past token
  • The new token computes its own Q, K, and V. Cached past K/V avoid recomputing earlier projections; its query attends to retained keys and values.
  • O(N) per token instead of O(N²)
  • Lifetime: one inference call

Universal — every modern LLM serving framework uses it.

SSM Recurrent State

Compressed running summary. Replace attention with a fixed-size state vector updated each step.

  • Constant memory regardless of length
  • Linear-time inference
  • Lifetime: full sequence
  • "Infinite context" — in principle

In the wild: Mamba (Gu & Dao 2023), RWKV, RetNet, Jamba (hybrid).

External Memory Bank

Persistent across sessions. Learnable key-value store the model writes to and retrieves from.

  • Decoupled from context window
  • Survives between conversations
  • Lifetime: forever (or until pruned)
  • Read via attention, written via gate

In the wild: MemGPT (now Letta), Memorizing Transformers, RAG (a degenerate form).

Three Time-Scales of Memory

KV Cache milliseconds — minutes (within one generation) SSM Recurrent State seconds — hours (across one full sequence) External Memory Bank days — months — forever (across sessions, users, time)
06 / Memory — SSM

State Space Models — Constant-Memory Recurrence MAMBA · RWKV

Fixed recurrent state is lossy, not an unlimited exact transcript. The simple recurrence is an educational SSM, not the full selective Mamba model.

Replace attention with a tiny recurrent state updated each step. Inference becomes linear-time and constant-memory.

The Core Equation

Teaching excerpt · complete runnable lab available from “Code”
h_t = A · h_{t-1} + B · x_t       # update state
y_t = C · h_t                     # emit output

A classical recurrence. The whole sequence is summarized into h_t — a fixed-size vector. Compute per step is constant.

vs Attention

AttentionSSM
Compute / tokenO(N)O(1)
Total computeO(N²)O(N)
State / tokengrows with Nfixed
Random accessperfectlossy compression
Trainable in parallelyesyes (with tricks)

Mamba's clever bit: the step size Δ and B, C are computed from the input (so the effective decay eΔA is input-dependent too). That gives attention-like selectivity — "remember this, forget that" — while keeping linear complexity.

Sample Code — A Minimal SSM Layer

Teaching excerpt · complete runnable lab available from “Code”
class MinimalSSM(nn.Module):
    """Linear recurrent layer (simplified Mamba-style)."""

    def __init__(self, d_model, d_state=16):
        super().__init__()
        self.d_state = d_state

        # Learnable state-transition matrices
        self.A_log = nn.Parameter(torch.randn(d_state))
        self.B = nn.Linear(d_model, d_state, bias=False)
        self.C = nn.Linear(d_state, d_model, bias=False)

    def forward(self, x):                 # x: (B, T, d_model)
        B, T, _ = x.shape
        A = -torch.exp(self.A_log)        # stable, negative

        h = torch.zeros(B, self.d_state, device=x.device)
        outputs = []

        for t in range(T):
            # Recurrence — same shape every step
            h = torch.exp(A) * h + self.B(x[:, t])
            y = self.C(h)
            outputs.append(y)

        return torch.stack(outputs, dim=1)


# Drop-in replacement for self-attention.
# Notice: no QK^T matrix → no O(N²) cost.
# 'h' carries all information about the past in d_state floats.

Real Mamba: parallel scan for fast training, hardware-aware kernel, selective parameters that gate input flow. This minimal version is the conceptual core.

06 / SSM — By Hand

An SSM You Can Run in Your Head WORKED EXAMPLE

Fixed recurrent state is lossy, not an unlimited exact transcript. The simple recurrence is an educational SSM, not the full selective Mamba model.

Shrink the state to one number and the whole idea fits on a napkin: memory is a leaky running total.

Scalar SSM: ht = a·ht−1 + xt,  yt = ht,  a = 0.5

tinput xtstate htwhat happened
110.5·0 + 1 = 1store the 1
200.5·1 + 0 = 0.5memory halves
300.5·0.5 + 0 = 0.25halves again
420.5·0.25 + 2 = 2.125new input dominates

Unrolled: ht = Σs at−s xs. Old inputs fade by a factor a per step. This sum is a convolution — which is how SSMs train in parallel.

The Knob: How Long Is the Memory?

Half-life in steps = ln 0.5 / ln a.

  • a = 0.5 → 1 step.  a = 0.99 → ≈ 69 steps.
  • Real SSMs run thousands of these in parallel, each with its own a — some forget fast, some remember long.
  • Mamba makes the decay depend on the input, so it can choose to hold on to an important token.

Memory Bill: KV Cache vs. SSM State (fp16)

Context lengthLlama-2 7B KV cacheMamba-2.8B state
4,096 tokens2 GB≈ 10 MB
64 layers × 5,120 × 16 ≈ 5.2M numbers — at any length
32,768 tokens16 GB
131,072 tokens64 GB

KV storage grows with retained context. This comparison assumes full retained histories at the specified lengths; context limits, eviction, or compression prevent literal unbounded growth in real serving systems.

The Catch — Fixed Size Means Lossy

Millions of numbers cannot store a 100k-token book word for word. Pure SSMs struggle with exact recall, like copying a long random string (Jelassi et al. 2024). That's why hybrids such as Jamba keep a few attention layers.

Check yourself: with a = 0.9, after how many steps is an input below 1% of its size?

Solve 0.9n < 0.01 → n > ln 0.01 / ln 0.9 ≈ 43.7, so 44 steps.

07 / Memory — External Bank

External Memory Banks PERSISTENT KNOWLEDGE

Fixed recurrent state is lossy, not an unlimited exact transcript. The simple recurrence is an educational SSM, not the full selective Mamba model.

A learnable key-value store that lives outside the context window — written to during conversation, read from across sessions.

The Read/Write Cycle

Memory Bank important? x_t (hidden state) if yes: WRITE query q current context attend to bank: READ recalled info read = softmax(q·Kᵀ/√d) · V    (attention over the bank)

Sample Code — Memory Bank with Gated Write

Teaching excerpt · complete runnable lab available from “Code”
class MemoryBank(nn.Module):
    """Persistent KV bank — read by attention, write by gate."""

    def __init__(self, d_model, capacity=256):
        super().__init__()
        self.capacity = capacity
        self.d_model  = d_model

        # Slots stored as buffers (saved with state_dict)
        self.register_buffer("keys", torch.zeros(capacity, d_model))
        self.register_buffer("vals", torch.zeros(capacity, d_model))
        self.register_buffer("size", torch.tensor(0))

        self.write_gate = nn.Linear(d_model, 1)

    def read(self, q):                          # q: (B, T, d)
        if self.size == 0:
            return torch.zeros_like(q)
        K = self.keys[:self.size]                # (M, d)
        V = self.vals[:self.size]
        scores = q @ K.T / (self.d_model ** 0.5)   # (B, T, M)
        return F.softmax(scores, dim=-1) @ V

    def write(self, x):                         # x: (N, d) flattened
        importance = torch.sigmoid(self.write_gate(x))
        keep = (importance > 0.5).squeeze(-1)
        new = x[keep]
        n = min(new.size(0), self.capacity - self.size)
        if n == 0: return
        self.keys[self.size:self.size+n] = new[:n]
        self.vals[self.size:self.size+n] = new[:n]
        self.size += n

    def save(self, path):
        torch.save({"keys": self.keys, "vals": self.vals,
                    "size": self.size}, path)

The bank can be saved/loaded across sessions — that's what makes it persistent. Real systems add eviction policies, sharding, learned compression.

08 / Combined Architecture

Putting It Together — The AnimaBlock FULL IMPLEMENTATION

A drop-in transformer block that's adaptive (MoE FFN) AND has accumulating memory (external bank). ~80 lines of PyTorch.

The Wiring

input x memory.read(x) x + recall attention + MoE FFN + memory.write(x) output

Both adaptations are drop-in replacements. You can stack AnimaBlocks exactly like vanilla transformer blocks.

The AnimaBlock

Teaching excerpt · complete runnable lab available from “Code”
class AnimaBlock(nn.Module):
    """
    Transformer block with:
      - adaptive compute (MoE FFN)
      - accumulating memory (external bank)
    """

    def __init__(self, d_model, n_heads=4,
                 n_experts=8, top_k=2,
                 mem_capacity=256):
        super().__init__()
        self.ln1    = nn.LayerNorm(d_model)
        self.attn   = nn.MultiheadAttention(
                          d_model, n_heads, batch_first=True)
        self.ln2    = nn.LayerNorm(d_model)
        self.ffn    = MoEFFN(d_model, n_experts, top_k)
        self.memory = MemoryBank(d_model, mem_capacity)

    def forward(self, x):
        # 1. Recall: pull relevant past memory
        recall = self.memory.read(x)
        h = self.ln1(x + recall)

        # 2. Causal attention sublayer (True = blocked)
        T = x.size(1)
        causal = torch.triu(torch.ones(T, T, dtype=torch.bool,
                            device=x.device), diagonal=1)
        attn_out, _ = self.attn(h, h, h, attn_mask=causal,
                                need_weights=False)
        x = x + attn_out

        # 3. Adaptive FFN sublayer
        x = x + self.ffn(self.ln2(x))

        # 4. Persist what mattered
        with torch.no_grad():
            self.memory.write(x.reshape(-1, x.size(-1)))

        return x


# Stack them like normal transformer blocks:
blocks = nn.ModuleList([
    AnimaBlock(d_model=128) for _ in range(6)
])

# Forward pass:
for block in blocks:
    x = block(x)

Writes run under no_grad, so saved slots are detached — no gradients flow through memory across batches. Without the causal mask a decoder would peek at future tokens during training.

09 / In Production

Real-World Systems WHO USES WHAT

These dated public examples illustrate published architectures and research proposals. This is not a verified inventory of live production systems; some entries are papers rather than deployed products.

System Adaptive compute Memory accumulation Notable
Mixtral 8×7B (Mistral) MoE — 8 experts, top-2 Standard KV cache 47B total params, ~13B active. Open-weights.
DeepSeek-V3 MoE — 256 experts, top-8 + 1 shared Multi-head Latent Attention (compressed KV) 671B total / 37B active. Open weights.
Mamba / Mamba-2 — Selective State-Space (recurrent) Linear-time inference; competitive with transformers up to 7B.
Jamba (AI21) MoE Hybrid: SSM + attention layers Best of both worlds — long context + adaptive compute.
RWKV — Linear-attention recurrent state RNN-flavored transformer; very efficient inference.
MemGPT / Letta — External memory bank with self-managed read/write Persistent personality / facts across sessions.
Memorizing Transformers — kNN over external memory cache (one upper layer) Showed external memory works at scale (Wu et al. 2022).
Mixture of Depths (Google) Per-token block routing Standard KV cache Variable depth per token. Paper reports matching baseline quality with far fewer FLOPs per forward pass.

Pattern

Dense attention transformers remain common, especially at smaller sizes. At the frontier, MoE has become a mainstream way to get cheap inference; SSM hybrids and external memory are still rarer and more experimental. Even "dense" models now differ a lot from GPT-2 (RoPE, GQA, SwiGLU, RMSNorm).

10 / Common Pitfalls

Common Pitfalls WHAT GOES WRONG

Each of these has bitten real projects. Several show up in the simplified code on earlier slides — on purpose, so you can spot them.

Router collapse. A few experts win early, get all the gradient, and win forever. Fix: load-balancing loss, router noise, or capacity limits.

Sparse routing does not remove expert weights. Placement, expert parallelism, quantization, and offloading determine where the parameters reside and when they are moved.

Dropped tokens. With a capacity factor, an overloaded expert skips extra tokens — they pass through only the residual. Too low a capacity silently hurts quality.

Early exit vs. KV cache. A token that exits at layer 6 has no K/V for layers 7+. Later tokens need them, so systems like CALM copy the exited state upward.

The gate never learns. In MemoryBank.write, the hard > 0.5 threshold under no_grad gives the gate zero gradient. Real systems use an auxiliary loss or a straight-through trick.

The bank fills up and freezes. The demo stops writing once all 256 slots are used — often after one batch. Real banks need eviction (FIFO, LRU, least-used).

Memory leaks between users. A shared bank written during one user's session can be read in another's. Persistent memory needs per-user scoping and deletion.

"Constant memory = infinite context." An SSM runs forever, but its fixed-size state keeps a lossy summary, not a perfect transcript.

11 / Check Yourself

Check Yourself CLICK TO REVEAL

Try each one before you open the answer.

1. An MoE layer has 64 experts, top-4. What fraction of FFN weights does one token use?

4 / 64 = 1/16 ≈ 6%. Attention and embeddings are always fully used, so the whole-model fraction is higher.

2. Why does Mixtral have 47B parameters and not 8 × 7B = 56B?

Only the FFNs are copied into 8 experts. Attention, norms and embeddings are shared by all experts.

3. What does the load-balancing loss equal when routing is perfectly even?

Uniform routing gives 1 before the coefficient, but the statistic can be below 1 for imbalanced routing. It is an auxiliary objective, not an exact inequality characterizing balance.

4. Scalar SSM with a = 0.8, h₀ = 0, inputs [5, 0, 0]. What is h₃?

h₁ = 5, h₂ = 4, h₃ = 3.2. Each step keeps 80% of the state.

5. Why does a longer chat cost more memory for a transformer but not for Mamba?

The transformer keeps K and V for every past token (the KV cache grows linearly). Mamba keeps one fixed-size state and overwrites it each step.

6. Name one task where attention beats a pure SSM, and why.

Exact recall — copying a long string or looking up a phone number from far back. Attention can look at any past token directly; an SSM only has its compressed state.

7. What is the difference between RAG and a learned external memory bank?

RAG retrieves external evidence—often text using embeddings, lexical search, or reranking—and conditions generation on it. A neural vector bank is a different design; neither retrieval nor write policy must be fixed or differentiable.

8. What breaks if you remove the causal mask from AnimaBlock?

During training each token could attend to the tokens after it, so next-token prediction becomes copying. The model looks great in training and fails at generation.

12 / Glossary

Glossary TERMS IN ONE PLACE

FFNThe feed-forward network in each block; what MoE replaces.
ExpertOne of several FFNs in an MoE layer.
Router / gateSmall linear layer that scores experts (or tokens).
Top-k routingSend each token to its k highest-scoring experts.
Active paramsWeights actually used for one token.
Load-balancing lossExtra loss term that spreads tokens across experts.
Capacity factorMax tokens per expert per batch; overflow is dropped.
MoDMixture of Depths — only top-k tokens enter each block.
Early exit / ACTStop processing a token once it is confident enough.
KV cacheSaved keys and values of past tokens.
GQA / MLAWays to shrink the KV cache by sharing or compressing K/V.
SSMState space model — a linear recurrence with a fixed-size state.
Selective SSMSSM whose update depends on the input (Mamba).
HybridModel mixing SSM and attention layers (Jamba).
Memory bankKey-value store outside the context, read by attention.
EvictionRule for which memory slot to overwrite when full.
13 / Recap

The Whole Picture ONE-PAGE SUMMARY

Why Bother

  • Vanilla transformers spend equal compute on every token and forget everything between sessions.
  • Adaptive infrastructure routes compute where it's needed — sparse activation gives you a big model at a small model's cost.
  • Memory accumulation persists state across time — shorter latency, longer effective context, true cross-session memory.
  • The two are orthogonal; modern systems combine them.

Tools You Now Have

  • A working MoEFFN in ~30 lines
  • A minimal MinimalSSM as a recurrence primitive
  • A MemoryBank with gated writes & attention reads
  • An end-to-end AnimaBlock wiring it all together
  • A map of who uses what in production

What to Read Next

  • MoE: "Switch Transformer" (Fedus et al. 2021), "Mixtral of Experts" (Jiang et al. 2024)
  • MoD: "Mixture-of-Depths" (Raposo et al. 2024)
  • Mamba: "Mamba: Linear-Time Sequence Modeling" (Gu & Dao 2023)
  • RWKV: "RWKV: Reinventing RNNs for the Transformer Era" (2023)
  • External memory: "MemGPT" (Packer et al. 2023), "Memorizing Transformers" (Wu et al. 2022)
  • Hybrid: "Jamba: A Hybrid Transformer-Mamba Language Model" (AI21 2024)

Questions?

Run a complete program

Can we select experts, accumulate state, and keep memory scoped to a user?

The file contains its own imports, data generator or fixture, implementation, and checks.

python3 -m venv .venv
source .venv/bin/activate
python -m pip install torch
python adaptive_neural_infra.py
python adaptive_neural_infra.py --self-test

Use Python 3.10+. On Windows, activate with .venv\Scripts\Activate.ps1. Required packages: torch>=2.2.

Connect the concept to the implementation

Read the contract first. Identify the inputs, intermediate state, and return value in this excerpt from the verified file.

def forward(self, inputs):
        probabilities = self.router(inputs).softmax(-1)
        weights, selected = probabilities.topk(self.k, dim=-1)
        weights = weights/weights.sum(-1, keepdim=True)
        result = torch.zeros_like(inputs)
        # Invariant: result contains contributions of earlier experts only.
        # Bound: remaining experts; each receives only its selected rows.
        for index, expert in enumerate(self.experts):
            rows, slots = (selected == index).nonzero(as_tuple=True)
            if rows.numel():
                contribution = weights[rows, slots, None] * expert(inputs[rows])
                result = result.index_add(0, rows, contribution)
        return result, probabilities, selected

Compare with an actual run

MoE initial/final regression MSE: 3.9880120754241943 0.042410463094711304
Scalar state for [1,0,0,2]: ([1.0, 0.5, 0.25, 2.125], 2.125)
Ada's value: Python
Another user's value: None
Valid top-1 routing statistic below 1: 0.9359999299049377
These primitives are educational; the recurrence is not a full Mamba implementation.

Output was captured from the complete program running in an isolated temporary directory. Floating-point digits and compressed sizes may vary across versions/hardware.

Check the result, then try an exercise

Verified checks

8 checks passed, including boundary cases and independent reference comparisons where applicable. These executed checks complement the reasoning; they are not a universal proof.

Reset a user's memory, exceed the capacity, and query another user's store. Then compare uniform and imbalanced routing-loss examples.

Explain the expected result

State lifetime, capacity/eviction, and user identity are explicit API choices. Uniform auxiliary routing loss equals 1 but is not its universal minimum.

Implement it yourself and validate it

  1. Run the downloadable program and its checks.
  2. Reimplement MoE.forward from its contract.
  3. Compare outputs with the reference on valid, empty, and invalid cases.
  4. Explain the limitations of the algorithm or experiment.
All coding labs
Decks