A CUDA / PyTorch port of WeZZard/jlens-qwen36
(an MLX / Apple-Silicon visual debugger for the Jacobian lens from Anthropic's
Verbalizable Representations Form a Global Workspace in Language Models),
retargeted to Qwen/Qwen3.8-27B in bf16 on H200 GPUs.
Step-by-step tutorial (中文): docs/TUTORIAL.md
HPC H200 runbook: docs/HPC_H200_RUNBOOK.md.
Linux uses the official PyTorch 2.11.0 + CUDA 12.8 wheel and Triton 3.6.0.
H200 GDN backward uses FLA's TileLang workaround for a Hopper/Triton bug;
load a host CUDA 12.8 toolkit with nvcc before fitting (see the runbook).
PyPI's torch 2.14.0 + CUDA 13 build needs a newer driver than the HPC node reported here.
| upstream (MLX) | this port (CUDA) | |
|---|---|---|
| model | mlx-community/Qwen3.x-27B-4bit |
Qwen/Qwen3.8-27B, bf16, HF transformers |
| GDN backward | custom Metal VJP kernel | FLA kernels, with TileLang for the affected Hopper backward op |
| fit estimator | analytic per-layer M_l, chain J_{l-1} = J_l M_l |
Anthropic jlens.fit (exact end-to-end Jacobian, paper's estimator) |
| fit cost | ~7–20 min / prompt on an M4 | measure with scripts/verify_fit.py; shards across GPUs |
| web app | serve.py + web/index.html |
the same files; only the MLX touchpoints in serve.py changed |
Files:
jlens_qwen/model.py TorchLensModel + StreamSession (hooks + HF hybrid cache)
jlens_qwen/lens.py JacobianLens (loads .npz and jlens/Neuronpedia .pt)
jlens_qwen/interventions.py steer / swap / ablate (J-lens vectors)
jlens_qwen/agent.py agent cases, mock tools, Qwen XML tool-call parsing
jlens_qwen/serve.py upstream FastAPI server, ported
web/index.html upstream UI, unchanged
scripts/check_env.py GPU + kernel check
scripts/prepare_corpus.py download the fitting corpus (login node)
scripts/verify_fit.py correctness + cost check on one prompt
scripts/fit_lens.py fit one shard on one GPU
scripts/merge_lens.py merge shards -> .npz + .pt
scripts/readout_smoke.py sanity readout of a fitted lens
scripts/measure_bands.py sensory / workspace / motor bands for the UI
slurm/*.sbatch Slurm templates (fit array, one-node fit, serve)
uv sync
uv run python scripts/check_env.py
uv run python scripts/prepare_corpus.py --n 1000 --min-chars 600 # needs internet
uv run python scripts/verify_fit.py --dim-batch 16 # on a GPU node
sbatch slurm/fit_array.sbatch # 8 shards
uv run python scripts/merge_lens.py data/lens/shards/*.pt --out data/lens/qwen38_27b_n1000 --expect-n 1000
JLENS_PATH=data/lens/qwen38_27b_n1000.npz uv run uvicorn jlens_qwen.serve:app --host 127.0.0.1 --port 8765The Tools button (bottom left) edits cases: a system prompt, the tool
schemas the model sees, and what each tool returns. Cases are data stored on
the server in data/cases/<id>.json (example: data/cases/weather_demo.json),
so adding tools never needs server code. Tick Use tools for new messages and
each message runs /api/agent_stream: the server parses Qwen's XML
<tool_call>s, answers them from the case (fixed, match by arguments,
sequence, manual = typed in the chat, or http), feeds the
<tool_response> back into the same cached session, and keeps generating until
a final answer. Tool results get their own J-lens prefill rows. A manual result
can be saved as a fixture with one click, and on_miss: "error" makes a run
stop instead of silently diverging, for strict reproduction. Every run reports
the case hash and a ledger check: whether re-rendering the transcript
reproduces exactly the tokens the model saw.
Authoring cases needs no GPU: JLENS_NO_MODEL=1 starts the server without the
model or lens (seconds, on a login node or laptop). The UI, saved sessions and
the Tools editor work; generation endpoints return 503. The GPU server reads
data/cases/ on every run, so cases edited this way are picked up as-is.
JLENS_NO_MODEL=1 uv run uvicorn jlens_qwen.serve:app --host 127.0.0.1 --port 8765scripts/export_static.py turns saved sessions (data/sessions/*.json) into a
static, read-only site: one index.html (presentation mode) plus gzipped,
chunked session payloads. No server or model is needed to view it, and each
conversation has a shareable #session=<id> link.
uv run python scripts/export_static.py --out site --clean # all saved sessions
python -m http.server -d site 8000 # preview locallyBy default the sensory band (L0–L18) is left out of every grid (the UI shows it as
one "not shown in this export" placeholder column), which cuts the payload by
about a third; --min-layer 0 keeps all layers (or set another
cutoff). Pass session files to export a subset. Pass --server http://127.0.0.1:8765 to
copy the model/lens metadata (including measured bands) from a running server;
otherwise it is inferred from the sessions plus --model-id / --bands. A site
carries one metadata set, so sessions recorded with a different model or lens
are rejected; export them to separate sites. To
publish, push the contents of site/ to a GitHub Pages branch (e.g. gh-pages)
or to the docs/ folder Pages serves from. GitHub rejects files over 100 MB;
the exporter keeps every chunk well under that.
uv run pytest runs on a laptop (no GPU, no weights): upstream's serve-logic
contract tests plus numerical tests of the torch backend on a tiny random
Qwen3.5 model.
Apache-2.0 (as upstream). web/index.html, jlens_qwen/serve.py and
jlens_qwen/prompts.py are from WeZZard/jlens-qwen36; the fitting estimator is
Anthropic's jacobian-lens.