Beyond the Transformer FFN: How CellularFlow Solves Catastrophic Forgetting with Multi-Head Associative Memory
By Celcilin C S (@celcilin)
A deep dive into replacing dense feed-forward networks with addressable DNA memory banks, achieving zero-backpropagation streaming learning, and preserving 83.9% domain retention.
1. The Elephant in the AI Room: Why Modern LLMs Forget
If you take any state-of-the-art Large Language Model (LLaMA, Mistral, GPT-4) and train it sequentially on new domains—say, Medical notes, then Legal contracts, then Rust code—something catastrophic happens.
It suffers from Catastrophic Forgetting. By the time the model masters Rust, its diagnostic medical reasoning has degraded significantly.
Why does this happen?
In a standard Transformer block, sequence reasoning is handled by Multi-Head Self-Attention, but all the model's factual knowledge, vocabulary associations, and world facts are packed into dense Feed-Forward Networks (FFN / SwiGLU / MLP).
Standard Transformer Block:
Input Token ──→ [ Self-Attention ] ──→ [ Dense FFN / MLP ] ──→ Output
▲
│
All world knowledge, facts, and syntax are
entangled across monolithic dense matrices!
Because an MLP is a dense matrix multiplication ( ), every single weight participates in every single token. There are no "folders", no "slots", and no isolated boundaries. When you backpropagate gradients on a new domain, you rewrite the same weights that held the old domain's knowledge.
To add insult to injury:
- You cannot teach an LLM a new fact during inference without full retraining, fine-tuning, or cluttering the context window with RAG (Retrieval-Augmented Generation).
- Scaling knowledge requires scaling compute per token: To make a Transformer store more knowledge, you must widen the MLP or add more layers, forcing every token to pay a heavy computational tax.
2. The CellularFlow Thesis: Decouple Memory from Reasoning
What if an LLM didn't store its factual knowledge inside dense, monolithic transform matrices?
What if, instead:
- The reasoning backbone (attention projections, LayerNorms, token embeddings) remained stable and anchored.
- Factual knowledge was routed into dynamic, addressable associative memory banks that could be selectively trained, expanded, pruned, or updated in real time during the forward pass?
This is the architectural thesis behind CellularFlow.
Input Sequence: X (B, T, d)
│
┌──────────────┴──────────────┐
▼ ▼
┌─────────────────────────┐ ┌─────────────────────────┐
│ Multi-Head DNA Memory │ │ Episodic Memory Slot │
│ Associative Banks │ │ Buffer (Fast-Write) │
└────────────┬────────────┘ └────────────┬────────────┘
│ │
└──────────────┬──────────────┘
│ (Gated Memory Enrichment)
▼
┌───────────────────────────────────────────────────────┐
│ Causal Multi-Head Self-Attention with RoPE (FlashAttn)│
└───────────────────────────┬───────────────────────────┘
│
▼
Output Sequence: Y (B, T, d)
3. Under the Hood: The Hybrid CMC Layer
CellularFlow fuses two computational engines into a unified Hybrid CMC Layer:
A. Multi-Head DNA Memory Banks (CMCLayer)
Instead of an MLP, each layer contains learned memory banks split across multiple independent heads (
).
Each head maintains:
- A matrix of learned Keys
- A matrix of learned Values
When a token arrives, it computes its cosine similarity against the keys in each head subspace:
Why the Gaussian Noise?
Sparse Top-K routing has a famous failure mode: dead slots. A few initially lucky keys monopolize all the routing, while 70% of the memory bank never learns. CellularFlow injects small Gaussian exploration noise during training, ensuring that every single memory slot receives gradient updates over time.
B. Causal FlashAttention with NTK-Aware Dynamic RoPE
Following memory enrichment, sequence tokens are routed through multi-head causal self-attention powered by FlashAttention-2 kernel dispatch (F.scaled_dot_product_attention).
To handle sequences longer than training length without breaking, CellularFlow uses Dynamic NTK-Aware RoPE scaling:
When sequence length exceeds the pretraining threshold (
), the base frequency is stretched dynamically:
This enables zero-shot context length extrapolation without fine-tuning.
C. The Episodic Memory Buffer
On the final layer, an explicit key-value buffer (EpisodicMemory) acts as a "working memory" scratchpad:
- Anisotropy Centering: Queries and keys are centered dynamically ( ) to prevent vector clustering in high dimensions.
- Temporal Age Decay: Older, unreinforced facts naturally fade over time via an exponential penalty: .
- Smooth Sigmoid Gate: Blends episodic memory into the residual stream with continuous gradient flow:
4. The Three Continual Learning Regimes
CellularFlow introduces a principled, 3-tier memory hierarchy:
Continual Learning Inputs
│
▼
[ Select Mode ]
│ │ └────────────────────────────────┐
▼ ▼ ▼
Mode 1: Live Learn Mode 2: Selective Fine-Tune Mode 3: Episodic Buffer
(Streaming EMA) (Freeze 85% Backbone) (Fast-Write Slot Buffer)
[0 Backpropagation] [Train DNA Banks Only] [Post-Epoch Consolidation]
Mode 1: Live Learning (Zero-Backprop Forward Updates)
trainer.live_learn("Streaming real-time log telemetry...")
- How it works: During the forward pass, the activations of active tokens are blended directly into the matching DNA memory values via Exponential Moving Average (EMA).
- Zero backpropagation: No backward pass, no optimizer states, zero training latency.
- Spherical Anisotropy Regularization: Prevents value collapse by projecting updated vectors back onto the hypersphere:
Mode 2: Selective Fine-Tuning (The Catastrophic Forgetting Cure)
trainer.selective_finetune("Technical medical notes on oncology...", epochs=10)
- How it works: Freezes ~85% of the model backbone (attention projections, LayerNorms, token embeddings). Gradients are computed exclusively for the DNA memory keys, values, and temperatures.
- Why it works: The model's syntactic parsing, grammar, and relational reasoning reside in the frozen backbone. Only the domain-specific associative memory slots adapt.
- Result: Achieves 83.9% retention across 5 sequential domains (Literature ➔ Science ➔ History ➔ Tech ➔ Poetry), compared to 61.8% under standard full fine-tuning.
Mode 3: Episodic Fact Injection
trainer.inject_fact("The capital of Mars colony is Bradbury Landing.")
- How it works: Encodes the fact and writes it directly into the episodic slot buffer. It is available immediately for recall on the very next forward pass.
- Consolidation: At the end of an epoch, high-utility episodic slots are vectorized and consolidated into permanent DNA banks.
5. What the Data Shows: Benchmarks & Empirical Proof
We benchmarked CellularFlow v4 against a standard autoregressive Transformer (GPT-mini) trained under identical conditions on a standardized multi-domain corpus.
Benchmark 1: Parameter Efficiency & Convergence
| Metric | GPT-mini (Baseline) | CellularFlow v4 (Hybrid CMC) | Advantage |
|---|---|---|---|
| Parameters | 810K | 379K | 2.1× smaller |
| Final Perplexity | 8.51 | 2.54 | −70.3% reduction |
| Top-1 Accuracy | 36.4% | 73.7% | +37.3 pp |
| Training Steps | 150 epochs | 150 epochs | Same compute budget |
Despite having less than half the parameters, CellularFlow achieved a dramatic reduction in perplexity and doubled prediction accuracy, demonstrating the high parametric density of associative memory banks compared to dense MLPs.
Benchmark 2: Catastrophic Forgetting Mitigation (Sequential Domains)
Models were trained sequentially across 5 disparate domains (Literature ➔ Science ➔ History ➔ Technical ➔ Poetry). After completing the final domain, retention accuracy was measured across all initial domains:
Domain Retention after 5 Sequential Tasks:
┌─────────────────────────────────────────────────────────────┐
│ Baseline Full Fine-Tuning: 61.8% [████████████░░░░░░░░] │
│ Mode 2 Selective Fine-Tune: 83.9% [████████████████░░░░] │
└─────────────────────────────────────────────────────────────┘
Advantage: +22.1 percentage points!
6. Real-Time Glassmorphic Dashboard
CellularFlow comes with an interactive glassmorphic web dashboard powered by a FastAPI backend and WebSockets.
Features:
- Interactive Generation: Test prompts with real-time streaming, temperature, top-P, and repetition penalty controls.
- Instant Fact Injection Panel: Type new facts and inject them directly into memory while the model is running.
- Layer-wise Memory Gauges: Live capacity bars showing episodic memory slot utilization across all model layers.
uvicorn server.app:app --host 0.0.0.0 --port 8000
# Open http://localhost:8000 in your browser
7. Quickstart: Running CellularFlow in 60 Seconds
Installation
git clone https://github.com/celcilin/cellularflow.git
cd cellularflow
pip install -e .
Python API
import torch
from cellularflow import CellularFlowLM, CellularFlowTrainer, BPEDataset
# 1. Dataset & Model
dataset = BPEDataset("Alice was beginning to get very tired...", context_len=256)
model = CellularFlowLM(
vocab_size=dataset.vocab,
dim=512,
n_layers=6,
n_heads=8,
n_entries=128,
context_len=256
)
# 2. Pretraining
trainer = CellularFlowTrainer(model, dataset, device="cuda" if torch.cuda.is_available() else "cpu")
trainer.pretrain(epochs=100, seed_dna=True)
# 3. Fast Incremental Generation (KV-Cache)
print(trainer.generate("Alice saw a", max_new=100))
# 4. Mode 3: Instant Fact Injection
trainer.inject_fact("The White Rabbit's pocket watch is made of titanium.")
# 5. Mode 2: Domain Adaptation (Backbone Frozen)
trainer.selective_finetune("Technical medical notes...", epochs=10)
# 6. Mode 1: Forward-Pass Streaming Learning (0 Backprop)
trainer.live_learn("Streaming user inputs...")
8. Summary & What's Next
CellularFlow proves that language models do not have to be rigid, monolithic black boxes that forget their past whenever they learn something new.
By replacing dense FFNs with multi-head associative memory banks, we can build models that:
- Learn continuously at inference time (Mode 1) without backward passes.
- Adapt to new domains (Mode 2) with 83.9% retention.
- Store facts explicitly (Mode 3) with temporal decay.
- Scale memory capacity independently of compute depth.
The entire codebase, training pipelines, interactive dashboard, and IEEE research paper are open source under the MIT License.
- Author: Celcilin C S (GitHub: @celcilin)
- GitHub: https://github.com/celcilin/cellularflow
- Hugging Face: https://huggingface.co/celcilin/cellularflow-v4
-
Contributions: We welcome PRs on hierarchical memory routing (PKM), surprisal-gated EMA, and Triton fused kernels. Check out
CONTRIBUTING.mdto get involved!
Top comments (0)