Interactive KV-Cache Visualization Demo
Frontend-Backend Integration — Fully Working ✅
cd backend
python -m uvicorn app.main:app --reload --port 8000Expected Output:
[CacheQuake] Mounted static data directory: C:\...\backend\data
[CacheQuake] Loaded trained weights from C:\...\backend\model_weights\toy_transformer.pt
INFO: Started server process
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000
cd frontend
npm run devExpected Output:
VITE v5.4.21 ready in 2334 ms
➜ Local: http://localhost:5173/
Navigate to: http://localhost:5173
-
Left Panel: Control Panel
- Budget Size slider (8-512 tokens)
- Sequence Length slider (32-512 tokens)
- Needle Count selector (1-5 needles)
- Cache Policy selector (Full Cache, Sliding Window, Heavy Hitter, BDH-Inspired State)
- "Run Simulation" button
-
Center Panel: KV Cache Visualization
- Heatmap showing which tokens are in cache (green) vs evicted (gray)
- Needles highlighted in orange
- Memory footprint chart below
-
Right Panel: Accuracy Results
- Live accuracy score
- Model output vs. correct answer comparison
- Accuracy-vs-budget curve (precomputed data)
- BDH module (when using bdh_inspired_state policy)
- LIVE badge (green): Connected to real backend
- DEMO MODE badge (orange): Using mock data (backend unreachable)
The model receives a long text sequence containing hidden "facts" (needles):
[FILLER TEXT]
the access code for vault 0 is 5906.
[FILLER TEXT]
the access code for vault 1 is 3271.
[FILLER TEXT]
what is the access code for vault 0?
Model's Job: Predict the first character of the correct access code.
- Keeps ALL tokens (no eviction)
- Highest accuracy, highest memory usage
- Baseline for comparison
- Keeps most recent N tokens + optional "sink" tokens at start
- Fixed memory usage, predictable behavior
- May lose early needles if they're outside the window
- Keeps N tokens with highest cumulative attention scores
- Adaptive eviction based on importance
- Better accuracy than sliding window at same budget
- Fixed-size memory slots (O(1) memory growth)
- Soft Hopfield-style associative memory
- Inspired by "Bye-Bye-Transformers" paper (toy implementation)
Backend: http://localhost:8000
Returns service status, model metadata, available policies.
Response:
{
"status": "ok",
"model": {
"d_model": 64,
"n_heads": 4,
"n_layers": 4,
"model_state": "lightly_trained",
"n_parameters": 180012
},
"cache_policies": ["full_cache", "sliding_window", "heavy_hitter", "bdh_inspired_state"]
}Run a full simulation with chosen cache policy.
Request:
{
"policy": "sliding_window",
"window_size": 64,
"num_sink_tokens": 4,
"n_facts": 3,
"seq_len": 200,
"question_target": 0,
"seed": 42
}Response:
{
"episode": {
"text": "full sequence text...",
"token_ids": [7, 4, 11, ...],
"fact_positions": {"vault 0": 45, "vault 1": 102},
"question_start": 183,
"answer": "5906"
},
"simulation": {
"policy": "sliding_window",
"steps": [
{
"step": 0,
"token_id": 7,
"token_char": "h",
"visible_token_indices": [0],
"cache_size_tokens": 1,
"cache_size_bytes": 2048
},
...
],
"final_answer": "5",
"ground_truth": "5906",
"correct": true,
"policy_info": { "window_size": 64, "evictions": 132 }
}
}Precomputed accuracy sweep data for all policies (static file).
Published BDH paper claims for comparison (static file).
- Backend: Python 3.11, FastAPI, PyTorch, uvicorn
- Frontend: React 18, Vite 5, Zustand (state)
- Model: Custom toy transformer (4 layers, 64 dims, 180K params)
User clicks "Run Simulation"
↓
Frontend (simulationStore.jsx)
↓
POST /simulate
↓
Backend (app/main.py)
↓
NeedleHaystackTask.generate() → create episode
↓
run_episode() → model forward pass with cache policy
↓
JSON response → Frontend
↓
Transform to UI format
↓
Render: Heatmap + Charts + Accuracy Panel
Backend:
app/main.py- FastAPI app, routesapp/simulation/runner.py- Simulation loopapp/model/toy_transformer.py- Model architectureapp/cache_policies/*.py- Cache policy implementationsapp/tasks/needle_haystack.py- Episode generator
Frontend:
src/App.jsx- Main app componentsrc/state/simulationStore.jsx- State managementsrc/api/client.js- Backend API wrappersrc/components/CacheHeatmap.jsx- Visualizationsrc/components/AccuracyPanel.jsx- Results display
cd backend
pytest tests/test_smoke.py -vResult: 72 tests passed (100%)
cd backend
python scripts/stress_test.pyResult: All 4 policies pass concurrent request test
- full_cache (seq=128): ~800-1100ms
- sliding_window (seq=128): ~400-600ms
- heavy_hitter (seq=128): ~500-700ms
- bdh_inspired_state (seq=128, optimized): ~480ms
- Server running on port 8000
-
/healthendpoint: 200 OK -
/simulateendpoint: All 4 policies working - Static data files: Serving correctly
- CORS: Enabled for all origins
- Model weights: Loaded (5000 training steps)
- Tests: 72/72 passing
- Server running on port 5173
- Connects to backend successfully
- "LIVE" badge showing (not DEMO MODE)
- All 4 cache policies selectable
- Real-time visualization working
- Accuracy display: Model output vs. ground truth
- Precomputed curves loading
- HMR (Hot Module Reload) functional
- Frontend → Backend: POST /simulate returns 200 OK
- Response transformation: Nested structure flattened correctly
- Needle positions: Object → Array conversion working
- Accuracy comparison: First character logic correct
- Episode metadata: Text, token_ids, positions available
- Precomputed data: JSON files loading and parsing
Cause: Backend not running or unreachable
Fix: Start backend with python -m uvicorn app.main:app --reload --port 8000
Cause: Old cached frontend code
Fix: Hard refresh browser (Ctrl+Shift+R or Cmd+Shift+R)
Cause: Model predictions wrong (training issue) OR comparison logic broken
Status: Fixed - comparison now checks first character only (model === correct[0])
Cause: Static files not mounted
Status: Fixed - StaticFiles middleware added to main.py
Cause: fact_positions is object, not array
Status: Fixed - Using Object.values() to extract positions
CacheQuake/
├── backend/
│ ├── app/
│ │ ├── main.py # FastAPI entry point
│ │ ├── cache_policies/ # 4 policy implementations
│ │ ├── model/ # Toy transformer
│ │ ├── simulation/ # runner.py - core loop
│ │ └── tasks/ # needle_haystack.py
│ ├── data/precomputed/ # Static JSON files
│ ├── model_weights/ # toy_transformer.pt (trained)
│ ├── tests/ # Pytest suite
│ └── scripts/ # Performance, stress tests
│
├── frontend/
│ ├── src/
│ │ ├── App.jsx # Main component
│ │ ├── api/client.js # Backend wrapper
│ │ ├── state/ # Zustand store
│ │ └── components/ # UI components
│ ├── package.json
│ └── vite.config.js
│
├── ARCHITECTURE.md # Design decisions
├── PRD_KV_Cache_Explainer.md # Product requirements
├── TECH_STACK.md # Technologies used
├── INTEGRATION_FIX.md # Bug fixes applied
└── STARTUP_GUIDE.md # This file
The toy transformer was trained for 5,000 steps on synthetic needle-haystack episodes:
- Loss function: Cross-entropy on answer position only
- Final loss: 1.09
- Training time: ~395 seconds
- Expected accuracy: ~30-60% depending on cache policy
This is intentional - the model is "lightly trained" to show meaningful differences between cache policies, not to achieve 100% accuracy.
The model predicts only the first character of the access code. This is by design:
- Demonstrates attention to the correct fact
- Simpler training objective
- Easier to visualize in UI
When the UI shows:
Model Output: ✓ 5
Correct Answer: 5906
This is correct - the model predicted "5", which matches the first character of "5906".
- Test in Browser: Open http://localhost:5173 and run simulations
- Try All Policies: Compare Full Cache vs. Sliding Window vs. Heavy Hitter vs. BDH
- Adjust Parameters: Change budget, sequence length, needle count
- Check Performance: Monitor response times in browser DevTools
- Explore Data: Inspect console logs for full backend responses
For issues or questions:
- Check browser console for errors
- Check backend terminal for logs
- Review INTEGRATION_FIX.md for known issues
- Verify both servers are running
Everything is working as expected! 🎉