Skip to content

About

TransformerScope is an interactive interpretability platform for visualizing attention, Q/K/V mechanics, and token level reasoning inside GPT style transformers.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Transformer Interpretability Dashboard

Modern transformer models are incredibly powerful, but most function as black boxes: text goes in, predictions come out, and the internal reasoning process remains hidden behind layers of tensor operations.

TransformerScope is an interactive interpretability platform built to expose those internal mechanics in real time.

The project combines a GPT style transformer implemented entirely from scratch in PyTorch with a full stack visualization dashboard for exploring token embeddings, Q/K/V projections, causal attention masking, attention probability flow, and autoregressive token generation.

Rather than treating transformers as opaque APIs, TransformerScope focuses on making attention mechanics inspectable, understandable, and visually intuitive through direct interaction with the model’s internal representations.

Highlights

  • End-to-end ownership — tokenizer, embeddings, attention, FFN, residuals, layer norms, and LM head; no dependency on Hugging Face transformers or hosted inference APIs for the core model.
  • Explicit attention — Q, K, V projections, scaled dot-product scores, causal masking, and softmax weights surfaced for visualization and API consumers (models/attention.py).
  • Interpretability API — structured JSON for tokenization, embeddings, per-layer Q/K/V, attention probabilities, and next-token distributions (backend/app/).
  • Interactive dashboard — token strip, layer/head controls, attention heatmaps, Q/K/V views, and next-token probability charts with concise on-page explanations.
  • Reproducible training — Tiny Shakespeare pipeline with checkpointing and loss logging (training/).

Gallery

Dashboard — analyze and model context. Character-level forward pass with checkpoint metadata (embedding dimension, depth, heads, context length, vocabulary size) and inline educational copy.

Dashboard — analyze forward pass and model context

Next-token distribution. Top-32 softmax probabilities at the final sequence position—useful for inspecting how the model allocates mass across the character vocabulary at small scale.

Next-token probability distribution (top 32)

Tech stack

Layer Technologies
Model PyTorch
API FastAPI, Pydantic, Uvicorn
UI Vite, React 18, TypeScript, Tailwind CSS, Plotly

Repository layout

Path Description
models/ Character-level tokenizer, manual CausalSelfAttention, TinyGPT
training/ Dataset helpers, training loop, CLI entrypoint
backend/app/ REST API, request/response schemas, model loading and analysis
frontend/ Dashboard application
visualizations/ Placeholder package for future export and analysis utilities
docs/screenshots/ UI captures for documentation (see Gallery)

Architecture

flowchart LR
  subgraph core [Model]
    Tok[CharTokenizer]
    GPT[TinyGPT]
  end
  subgraph api [API]
    Svc[ModelService]
    A1[POST_/api/analyze]
    G1[POST_/api/generate]
  end
  subgraph ui [Dashboard]
    Dash[React_UI]
  end
  Tok --> GPT
  GPT --> Svc
  Svc --> A1
  Svc --> G1
  Dash --> A1
  Dash --> G1
Loading

Getting started

Prerequisites

  • Python 3.10+
  • Node.js 18+ (for the frontend)

1. Python environment

cd transformer_interpretability
python3 -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -r requirements.txt

2. Train a checkpoint

A checkpoint bundles weights, vocabulary, and hyperparameters for the API to load. Example (downloads Tiny Shakespeare on first run):

PYTHONPATH=. python -m training.train --max_iters 2000

Outputs:

  • checkpoints/checkpoint.pt — model state and metadata
  • checkpoints/loss_log.json — logged loss over training

Hyperparameters are adjustable via CLI flags in training/train.py.

3. Install frontend dependencies

cd frontend
npm install

4. Run the application

Backend (from repository root):

source .venv/bin/activate
PYTHONPATH=. uvicorn backend.app.main:app --reload

Frontend (from frontend/):

npm run dev

Open the local URL shown in the terminal (Vite defaults to port 5173). The API is served on port 8000 unless you change it. To point the UI at a different API origin, set VITE_API_URL in frontend/.env.

Useful endpoints

Method Path Purpose
GET /health Service health
GET /api/model/info Loaded checkpoint and model hyperparameters
POST /api/analyze Forward pass with interpretability payload
POST /api/generate Autoregressive generation with per-step probabilities

Interactive documentation: http://127.0.0.1:8000/docs while the backend is running.

Note: Inference uses the character vocabulary induced during training. After training on Tiny Shakespeare, prompts should use characters present in that corpus so tokenization succeeds.

Implementation notes

License

This project is released under the MIT License.

About

TransformerScope is an interactive interpretability platform for visualizing attention, Q/K/V mechanics, and token level reasoning inside GPT style transformers.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages