Blog / Why I Replaced Transformers with Mamba S6 for Log Analysis (70× Faster, Flat Memory at Any Length)
15 min read

Why I Replaced Transformers with Mamba S6 for Log Analysis (70× Faster, Flat Memory at Any Length)

GPT-2 crashes OOM past 8,192 tokens and processes only 35 logs/sec. Mamba S6 runs at 2,453 logs/sec with constant memory regardless of sequence length. We built two 125M SSMs, fixed a 3GB streaming KV cache bug, and benchmarked both against GPT-2 across three random seeds.

The GPT-2 model from Part 1 screens HDFS logs at 35 traces per second. A healthy HDFS cluster generating 2,000 to 10,000 events per second drowns that easily. Worse, attention memory scales quadratically with sequence length. On our RTX 3060 Ti (8 GB VRAM), sequences past ~4,096 tokens cause an out-of-memory crash exactly when a cascading failure is generating the most log activity.

Stage 3 replaces the Transformer backbone with Selective State Space Models (SSMs), specifically Mamba (Gu & Dao, 2023). In recurrent inference mode, Mamba S6 processes logs at 2,453 per second with constant memory regardless of sequence length. The combined hybrid MambaLog model reaches 1,102 logs per second at F1=0.8139 with a precision of 91%.

This post covers the architecture of both models, the KV cache memory bug we found and fixed in streaming mode, and the full three-way benchmark against the GPT-2 baseline.

TL;DR: We replace GPT-2 with two Mamba SSM variants: a pure Mamba S6 (2,453 logs/sec, 0.41ms/log, F1=0.8055) and a hybrid MambaLog (1,103 logs/sec, F1=0.8139, precision=91%). Both run at constant memory at any sequence length (GPT-2 crashes OOM past ~16K tokens). We also found and fixed a 3GB streaming memory leak in the hybrid’s attention cache. All code is open source on GitHub.


The Hidden Cost of Transformers: Why GPT-2 Crashes on Long Logs

Self-attention compares every token against every other token in the sequence. For a sequence of length T, that means T² comparisons. Double the sequence, quadruple the memory.

On a transformer trained with a 512-token context, extending to 8,192 tokens multiplies the key-value cache by 256×. The 8GB card cannot hold that. GPT-2 crashes.

Sequence Length | GPT-2 VRAM (batch=4) | Mamba VRAM | MambaLog VRAM
128 tokens      | 586 MB               | 868 MB     | 1,004 MB
512 tokens      | 620 MB               | 614 MB     | 738 MB
2,048 tokens    | 776 MB               | 842 MB     | 967 MB
4,096 tokens    | 983 MB               | 1,147 MB   | 1,271 MB
8,192 tokens    | 1,397 MB             | 1,757 MB   | 1,881 MB
16,384+ tokens  | CUDA OOM             | ~1,760 MB  | ~1,890 MB

Measured on RTX 3060 Ti, batch size 4. Mamba VRAM reported in parallel-forward mode.

GPT-2 fails past 16,384 tokens. Both Mamba variants stay nearly flat from 8,192 to 16,384 tokens and beyond. The memory footprint is bounded.


How Mamba Achieves Constant Memory

A Transformer holds onto every token it has ever seen. When a new token arrives, it compares against all past tokens. The sequence grows; the computation grows with it.

A recurrent model maintains a fixed-size hidden state. Each new token updates that state. The state size stays constant regardless of how many tokens have been processed.

Classical recurrent models (RNNs, LSTMs) did this, but with a critical weakness: every token updated the hidden state the same way. Routine tokens overwrote important ones. Long-range context leaked away.

Mamba fixes this with selective state update (the S6 mechanism). For each incoming token, the model computes how much to update the hidden state small for routine lines, large for unexpected patterns. This selection is computed from the token itself:

For each token x_t at step t:

  Compute input-dependent parameters:
    B_t = Linear(x_t)                    # how to write into state
    C_t = Linear(x_t)                    # how to read from state
    Δ_t = softplus(Linear(x_t))          # how strongly to update state

  Discretize continuous SSM using Δ_t:
    Ā   = exp(Δ_t * A)                   # learned continuous transition
    B̄   = (Ā - I) * inv(A) * B_t

  Update fixed-size hidden state:
    h_t = Ā * h_{t-1} + B̄ * x_t        # constant size, any sequence length

  Produce output:
    y_t = C_t * h_t

The key line is h_t. It is always the same fixed-dimensional tensor. Processing 1,000 tokens or 100,000 tokens requires exactly the same state vector. This is what makes O(1) memory possible.

A routine INFO Block verification successful log line produces a small Δ_t. The state barely changes. A WARN SocketTimeoutException on DataNode produces a large Δ_t. It writes aggressively into the state. The model learns this selectivity from training data.


Two Architectures Tested: Pure Mamba S6 vs. Hybrid MambaLog

We built two models, both at approximately 125M parameters to match the GPT-2 baseline from Stage 1. Equal parameter count isolates the architectural contribution from raw model capacity.

Pure Mamba S6

24 identical Mamba S6 residual blocks in sequence. No attention layers.

ComponentValue
Layers24 Mamba S6 blocks
Embedding dimension768
SSM state dimension (d_state)16
Expansion factor2× (inner dim = 1,536)
Convolution kernel4 (local depthwise before SSM)
Total parameters125.1M

The model trains as a standard causal language model: predict the next token, minimize cross-entropy. At inference time, it operates in two modes:

  • Parallel mode: process all tokens at once (training and offline evaluation)
  • Recurrent step mode: process one token at a time, updating the hidden state in-place (live streaming)

Recurrent step mode is what matters for real-time ingestion. At 0.4076 ms per log and 2,453 logs/sec, it is 70× faster than GPT-2 in parallel mode.

Hybrid MambaLog

The limitation of a pure SSM is associative recall over long ranges. The 16-dimensional state vector per layer must compress the entire sequence history. Specific identifiers a DataNode hostname, a Block ID from 1,500 log lines earlier can wash out as new tokens continuously update the state.

MambaLog addresses this by interleaving full causal self-attention layers at fixed intervals, borrowing the 3:1 ratio from AI21 Labs’ Jamba architecture (Lieber et al., 2024).

ComponentValue
Total layers24
Mamba S6 blocks18 (positions 0–2, 4–6, 8–10, 12–14, 16–18, 20–22)
Attention blocks6 (positions 3, 7, 11, 15, 19, 23)
Attention heads12, head dimension 64
Positional encodingRotary Embeddings (RoPE)
Feed-forwardSwiGLU gating
Total parameters125.3M

Three Mamba blocks compress high-frequency token patterns efficiently. The attention block at position 4 performs precise associative lookup matching exact Block IDs and DataNode hostnames across long context without doing so for every single token.

graph LR
    I["Token IDs"] --> E["Embedding\n768 dims"]
    E --> M1["Mamba S6\nLayer 0"]
    M1 --> M2["Mamba S6\nLayer 1"]
    M2 --> M3["Mamba S6\nLayer 2"]
    M3 --> A1["Self-Attention + RoPE\nLayer 3"]
    A1 --> M4["Mamba S6\nLayer 4"]
    M4 --> M5["Mamba S6\nLayer 5"]
    M5 --> M6["Mamba S6\nLayer 6"]
    M6 --> A2["Self-Attention + RoPE\nLayer 7"]
    A2 --> D["...4 more groups..."]
    D --> N["RMSNorm"]
    N --> O["Linear Head\nVocab logits"]

Training Setup

Both models were trained on the same HDFS dataset used in Stage 1, with no changes to the training data or objective.

ParameterValue
HardwareNVIDIA RTX 3060 Ti, 8 GB VRAM
PrecisionBF16 mixed precision
Batch size4
Gradient accumulation16× (effective batch 64)
Context length512 tokens
OptimizerAdamW, lr=3e-4, weight decay=0.1
LR scheduleCosine, 100-step warmup
Seeds42, 123, 999
Training time per seed~26 minutes
GPU peak temperature~72°C

The 3GB Memory Bug We Found (And Fixed) in Streaming Mode

After implementing recurrent step inference for MambaLog, we ran a 2,000-step continuous streaming benchmark to verify the O(1) memory claim.

Pure Mamba S6 held steady at 463.64 MB across all 2,000 steps.

MambaLog climbed continuously and peaked at 3,033 MB (3.03 GB).

If 18 of 24 layers are pure Mamba (constant memory), how does the hybrid consume 6.5× more memory than pure Mamba?

The answer was in the 6 attention layers. During single-step decoding, each attention layer needs the key and value projections of all previous tokens. Standard implementation concatenates the new token’s KV to a growing cache at every step:

# Bug: unbounded cache growth
if kv_cache is not None:
    past_k, past_v = kv_cache
    k = torch.cat([past_k, k], dim=2)   # grows by 1 at every step
    v = torch.cat([past_v, v], dim=2)   # grows by 1 at every step

At step 1, cache shape: [batch, heads, 1, head_dim].
At step 100: [batch, heads, 100, head_dim].
At step 2,000: [batch, heads, 2000, head_dim].

With 6 attention layers, the total KV cache grew linearly with decoding steps reproducing the exact O(T) memory growth we were trying to avoid.

Fix: Bound the cache with a sliding window. Once it reaches max_kv_len tokens, drop the oldest token before adding the new one:

# Fix: sliding-window bounded KV cache
if kv_cache is not None:
    past_k, past_v = kv_cache
    k = torch.cat([past_k, k], dim=2)
    v = torch.cat([past_v, v], dim=2)

# Enforce bounded VRAM: keep only the most recent max_kv_len tokens
if k.shape[2] > max_kv_len:
    k = k[:, :, -max_kv_len:, :]
    v = v[:, :, -max_kv_len:, :]

We set max_kv_len = 512, matching the training context length. The attention layers retain a 512-token associative window. For HDFS fault signatures, which are typically localized within a few hundred log lines, this is sufficient.

After the fix, the 2,000-step benchmark showed MambaLog holding flat at 1,161.00 MB, constant from step 1 to step 2,000.


Results

Anomaly Detection Accuracy

Threshold set at k=3.0σ (τ = μ_val + 3·σ_val), matching Stage 1’s calibration method.

MambaLog Results Across Seeds

MetricSeed 42Seed 123Seed 999MeanStd
F10.81440.82160.80560.81390.0065
Precision0.91080.91060.90790.90980.0013
Recall0.73650.74840.72410.73630.0099
Accuracy0.92220.92470.91900.92200.0023
Val PPL1.38691.38761.41081.39510.0111

Calibration (Seed 999): μ=1.3459, σ=0.1079, τ=1.6695

Pure Mamba S6 Results Across Seeds

MetricSeed 42Seed 123Seed 999MeanStd
F10.72850.72910.72750.72840.0007
Precision0.89520.89270.89370.89390.0010
Recall0.61410.61620.61340.61460.0012

Architecture Comparison: All Three Stages

This is the central comparison of the project. All three architectures were evaluated on identical hardware with identical training data and the same anomaly detection protocol (k=3σ unsupervised threshold).

ArchitectureParamsVal PPLTest F1PrecisionRecallOptimal k
Stage 1: GPT-2 (Transformer)124.4M1.4820.96340.97120.95583.0σ
Stage 3: Mamba S6 (k=3.0σ)125.1M1.3860.72840.89390.6146N/A
Stage 3: Mamba S6 (k=1.5σ)125.1M1.3860.80550.80640.80461.5σ
Stage 3: MambaLog Hybrid125.3M1.3630.81390.90980.73633.0σ

Reading this table:

GPT-2 leads on all accuracy metrics at k=3σ. This is expected full self-attention is better at associative recall across long sequences when memory is not a constraint.

Pure Mamba S6 at k=3σ looks weak (F1=0.7284, recall=0.6146). This requires explanation. The issue is not that Mamba misses the anomalous events. It is that the compressed 16-dimensional state vector smooths the surprisal signal: anomalous sequences generate less extreme perplexity spikes than they do under GPT-2. At the conservative 3σ threshold, many real anomalies fall below the cutoff.

Lower the threshold to k=1.5σ and pure Mamba’s F1 jumps to 0.8055 nearly matching MambaLog. The anomaly signal is there; the detection threshold just needs adjustment for the smoother surprisal distribution.

MambaLog at k=3σ achieves F1=0.8139 with precision=0.9098. That 91% precision figure is operationally significant: it means 91% of what MambaLog flags for deep inspection is a genuine anomaly. At the standard threshold, it generates fewer false alarms than Mamba S6 at k=1.5σ.

The F1 sensitivity curves below show this pattern across the full threshold sweep:

F1 Sensitivity Across Architectures Stage 3 vs GPT-2

MambaLog (blue dashed) leads across the full k range. All three architectures peak near k=3.0, confirming the threshold choice. The key difference is that MambaLog maintains higher F1 across a wider range of k values its anomaly signal is sharper and more stable.


Throughput and Energy

Results from a 2,000-step continuous streaming benchmark (batch size 16, 32,000 total logs).

Real-Time Telemetry Latency and Energy Efficiency

ArchitectureModeLatency (ms/log)Throughput (logs/s)Peak VRAMEnergy (J/1M logs)
Stage 1: GPT-2Parallel28.5035.091,616 MB812,500
Stage 3: Mamba S6Recurrent0.40762,453.6463 MB69,741
Stage 3: Mamba S6Parallel21.2447.071,032 MB4,159,573
Stage 3: MambaLogRecurrent0.90671,102.91,161 MB170,815
Stage 3: MambaLogParallel23.6942.211,132 MB4,503,179

In recurrent step mode (the production streaming path):

  • Mamba S6 is 70× faster than GPT-2 parallel mode and uses 91.4% less energy per million logs
  • MambaLog is 31× faster than GPT-2 and uses 79% less energy

The recurrent mode numbers are what matter for live ingestion. Parallel forward pass numbers are relevant for offline batch evaluation only.


VRAM Scaling: The Core Result

Attention Memory Wall vs. Mamba O(1) Recurrence

Sequence LengthContextGPT-2 Peak VRAMMamba S6 VRAMMambaLog VRAM
128 tokens~15 log lines586 MB868 MB1,004 MB
256 tokens~30 log lines598 MB576 MB704 MB
512 tokens~60 log lines620 MB614 MB738 MB
1,024 tokens~120 log lines672 MB690 MB814 MB
2,048 tokens~250 log lines776 MB842 MB967 MB
4,096 tokens~500 log lines983 MB1,147 MB1,271 MB
8,192 tokens~1,000 log lines1,397 MB1,757 MB (flat)1,881 MB (flat)
16,384+ tokens2,000+ log linesCUDA OOM~1,760 MB (flat)~1,890 MB (flat)

Source: results/vram_scaling_metrics.csv, batch size 4.

The GPT-2 curve climbs steeply. Both Mamba curves are nearly flat from 128 to 8,192 tokens. There is no OOM risk at any sequence length.

At 8,192 tokens, GPT-2 is already using 1,397 MB and approaching the crash threshold. At 16,384+ tokens it crashes. Both Mamba variants hold at under 1,900 MB indefinitely. This is the architectural property that enables processing of full multi-thousand-line failure cascades without splitting the log stream into truncated chunks.


Cross-Stage Summary

3-Stage VRAM and Trade-Off Chart

StageRoleParamsPeak VRAMVRAM @ 8KLatencyThroughputF1PrecisionOutput
1: GPT-2Baseline124.4M1,616 MBOOM28.50 ms35 logs/s0.96340.9712Scalar flag
2: QLoRA LLMDiagnostics3.09B5,120 MBN/A3,500 ms0.28/s0.9085*N/AJSON + CLI
3: Mamba S6Speed layer125.1M~463 MB1,757 MB0.41 ms2,453/s0.8055†0.8064Scalar flag
3: MambaLogSpeed layer125.3M1,161 MB1,881 MB0.91 ms1,103/s0.81390.9098Scalar flag

Stage 2 F1 from 4,000-sample validation benchmark. †Mamba S6 at k=1.5σ.

What each stage solved:

Stage 1 proved that an unsupervised surprisal signal on healthy-only training data achieves strong F1. It cannot scale to long sequences on 8GB hardware.

Stage 2 demonstrated that QLoRA fine-tuning produces actionable structured JSON diagnoses. Its 3,500ms latency makes it unsuitable as a real-time screen.

Stage 3 demonstrates that SSM architectures match Stage 1’s detection usefulness (MambaLog: F1=0.8139, precision=0.9098) while running at 0.91ms per log with flat memory at any sequence length. It replaces Stage 1 as the real-time screening component.


How This Unlocks Running Two AI Models on One 8GB GPU

Stage 3 establishes the physical feasibility of running both models simultaneously on 8GB VRAM:

MambaLog inference VRAM:  1,890 MB
QLoRA Stage 2 VRAM:       5,120 MB
Total:                    7,010 MB
RTX 3060 Ti capacity:     8,192 MB
Headroom for activations: ~1,180 MB

Both models co-reside on the same card. MambaLog screens every incoming log in real time. The ~5% of traffic it flags (based on the test set anomaly rate) gets queued for Stage 2 to diagnose. The expensive model only processes the events that actually need it.

This is the Lambda Architecture pattern applied to LLM-based monitoring: a fast, lightweight speed layer handles the high-volume stream; a powerful but slow diagnostic layer handles only the events that matter.


Limitations and Open Questions

MambaLog recall gap: At k=3σ, MambaLog recall is 73.6%. 26.4% of real anomalies are not flagged. This is partially a threshold choice lowering to k=1.5σ raises recall to 83.6% at the cost of more false positives. A production deployment would tune this against acceptable false-alarm rates.

Sliding window trade-off: The max_kv_len=512 bound on the attention cache solves the memory problem but limits associative lookup range. An anomaly that references a Block ID first seen 600 log lines ago is outside the attention window. The Mamba state handles long-range context, but with lossy compression.

No cross-system evaluation yet: Both models were trained and evaluated on HDFS. Transfer to other log formats (e.g., OpenStack, Spark, Linux syslog) would require retraining or at minimum re-calibrating the threshold from the new domain’s normal distribution.

Parallel mode still needs attention: In parallel forward mode (offline batch processing), MambaLog’s VRAM usage (1,132 MB) is comparable to GPT-2’s (620 MB at the same sequence length). The O(1) memory advantage only materializes in recurrent step mode. Offline batch jobs do not benefit from the architecture change as much as streaming pipelines do.


Reproducibility

All code, training scripts, Docker configuration, benchmark scripts, and evaluation pipelines are open source.

GitHub: systems-engineering-labs / surprisal-modeling / stage3-mamba

git clone https://github.com/Ramprasad273/systems-engineering-labs.git
cd ai-engineering/surprisal-modeling/stage3-mamba

# Build the CUDA container
docker compose build

# Run full training, evaluation, and benchmarks (seeds 42, 123, 999)
docker compose run --rm surprisal-mamba-train --full

# Generate cross-stage comparison tables and charts
docker compose run --rm surprisal-mamba-train compare

Expected runtime on RTX 3060 Ti: ~26 minutes per seed for training, ~20 minutes for the benchmark suite.

All reported numbers are sourced from results/throughput_power_metrics.json, results/vram_scaling_metrics.csv, and data/stage3_mambalog_eval.json.


References

  • Mamba: Gu, A., & Dao, T. (2023). Mamba: Linear-Time Sequence Modeling with Selective State Spaces. arXiv:2312.00752.
  • Jamba (3:1 interleaving ratio): Lieber, O. et al. (2024). Jamba: A Hybrid Transformer-Mamba Language Model. arXiv:2403.19887.
  • State Space Models: Gu, A. et al. (2021). Efficiently Modeling Long Sequences with Structured State Spaces. ICLR 2022.
  • Dataset: He, S., Zhu, J., He, P., & Lyu, M. R. (2016). Experience Report: System Log Analysis for Anomaly Detection. IEEE ISSRE.
  • Transformers: Vaswani, A. et al. (2017). Attention Is All You Need. NeurIPS.
  • FlashAttention: Dao, T. et al. (2022). FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness. NeurIPS.
  • RoPE: Su, J. et al. (2021). RoFormer: Enhanced Transformer with Rotary Position Embedding. arXiv:2104.09864.
RP
Ram Prasad

Lead Data & AI Engineer wrestling Spark clusters by day and building LLM internals from scratch by night.

// DISCUSSION & FEEDBACK

Join the Conversation

Have questions about this architecture, benchmarks, or pipeline code? Leave a reply below or join our GitHub Discussions.

Storage: Stored securely in your GitHub Discussions repository (via Giscus)