Debug a MAX model that loads and serves but produces wrong tokens compared to a reference implementation. Builds per-layer tensor-dump comparators for parity and coherence failures.
Ships in max-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
The model runs without errors but output is wrong. Scalar F.print taps hide
directional bugs, and each new tap costs a full recompile. Build a per-layer
tensor-dump comparator first. Every later check becomes a numpy read from disk.
Use this skill when MAX output disagrees with a PyTorch reference you can run and hook. The primary case is a custom-architecture port that serves but fails parity or coherence checks. The same protocol covers a quantized variant of a working port, a multi-GPU conversion of a working single-GPU port, and a regression after a MAX upgrade. It applies anywhere a trusted reference exists.
Don't use this skill when:
import-model)import-model Phase 2| File | Read when |
|---|---|
| comparator-build.md | Building HF/MAX dumpers and the comparator |
| comparator-output-patterns.md | Interpreting comparator output, false cliffs, token-0 invariant |
| agent-workflow.md | Dispatching parallel investigation agents |
| stacked-failures.md | A fix helped but verification still fails |
For MAX's built-in runtime debugging options (NaN checks, source tracebacks,
op logging), see
the MAX debugging tools.
max.nn.legacy.hooks.PrintHook (covered in comparator-build.md) prints layer
inputs and outputs for quick triage.
Run model.generate(...) on the same HF repo, prompt, and checkpoint. If HF is
incoherent, fix the tokenizer or chat template first. The MAX model isn't the
problem.
pixi run python -c "
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tok = AutoTokenizer.from_pretrained('<repo>')
model = AutoModelForCausalLM.from_pretrained('<repo>', dtype=torch.float32, device_map='auto')
text = tok.apply_chat_template([{'role':'user','content':'Hello!'}], tokenize=False, add_generation_prompt=True)
out = model.generate(**tok(text, return_tensors='pt').to(model.device), max_new_tokens=32, do_sample=False)
print(tok.decode(out[0]))
"
Follow comparator-build.md. You need three
artifacts: HF dumper, MAX dumper (forward() taps + standalone runner),
comparator script. Cast dump tensors to FP32 inside the MAX forward().
Guard: validate the dumpers before trusting them. Run both dumpers on a ModuleV3 model that MAX already serves correctly, with the taps added to its
forward(). Useolmo3, orllama3_modulev3served with--prefer-module-v3. Expect cos ≈ 0.999 at every layer, identicalprompt_tokens.npyon both sides, andpost_embedcos = 1.0. Anything less means the dumpers are broken. Fix them before reading anything into a comparison on your port.
Follow
comparator-output-patterns.md. Check
false cliffs (wrong hidden_states indexing, missing attention_mask on
decode-prefix dumps) before bisecting the model.
Choose the next step from the first comparator run that passes the guard:
Compute per-token and per-dim cosine slices when global cos looks ambiguous:
cos_per_token = [cos(h[t], m[t]) for t in range(h.shape[0])]
cos_per_dim = [cos(h[:, d], m[:, d]) for d in range(h.shape[1])]
High max_diff where HF spikes and MAX is flat usually means HF formed an
attention anchor your port didn't.
Follow agent-workflow.md. One lead agent analyzes dumps and ranks hypotheses with tensor evidence. Helpers run in parallel (weight stats, code diff, kernel inspection, sub-tap prep). Don't dispatch fix-attempt agents until the lead localizes.
For each hypothesis: read dump tensors, compute what the fix would produce, compare to HF. Match → recompile. No match → next hypothesis.
One compile, one smoke, full comparator pass (cos > 0.99 all layers). If verification still fails, see stacked-failures.md.
When teacher-forced dumps at decode step K match HF but generated text diverges, the model math is likely correct. Bisect before re-bisecting layers:
| Check | Pass | Fail → |
|---|---|---|
| Teacher-forced dump @ K | cos ≥ 0.99, argmax matches | Steps 1 to 5 (model bug) |
| Incremental pipeline decode @ K | token K matches HF | Decode-state bug (KV, conv cache) |
| Serve vs pipeline @ K | match | Harness bug (tokenizer, chat template, token recovery) |
Build if missing: pipeline decode compare, incremental layer dump, serve compare scripts. If teacher-forced and pipeline both pass but serve fails, don't edit the model.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/max-skills:debug-modelfrom max-skills
npx claudepluginhub modular/skills --plugin max-skills8 skills in this plugin
/benchmark-modelBenchmark a model served on MAX with the `max benchmark` command: measure throughput (tokens/sec), latency (TTFT, TPOT, inter-token latency), and GPU utilization by driving load against a running `max serve` endpoint. Use this whenever the user wants to benchmark, load-test, or measure the performance of a MAX model, get tokens-per-second / TTFT / TPOT numbers, run a concurrency or request-rate sweep, compare latency vs throughput, size a deployment, or produce benchmark JSON, even if they don't say "benchmark" by name. Also use when a `max benchmark` run fails to connect or reports zero/garbage numbers.
/eval-modelMeasures the task accuracy of text models served by MAX using standard benchmarks such as GSM8K, MMLU, HellaSwag, ARC, AIME, GPQA, TruthfulQA, WinoGrande, and BABILong. Use when benchmarking a served model, comparing it with model-card or reference scores, verifying that a new MAX model produces correct answers, or running repeatable dataset evaluations against a MAX OpenAI-compatible endpoint.
/import-modelUse when importing a new model architecture into MAX from a Hugging Face model ID. Triggers on: "import a model into MAX", "add model to MAX", "bring up <HF model> in MAX". Workflow: inspect the Hugging Face config and modeling code, then scaffold from a similar ModuleV3 architecture. Implement each layer as a max.nn Module that matches HF, serve, then verify against the Hugging Face reference. When the server runs but output is wrong (gibberish, greedy mismatch, coherent-then-diverges), load debug-model for the divergence hunt.
/migrate-max-v2-to-v3Use when migrating a MAX model from the V2 graph API (max.nn.legacy, TensorValue, explicit Graph construction) to ModuleV3 (max.nn, Tensor, F.lazy() + compile()). Triggers on: "migrate to ModuleV3", "port a max.nn.legacy model to max.experimental", "convert TensorValue to Tensor", "update my model to the V3 API", "ModuleV3 migration". Workflow: read the concept map, rewrite imports and __call__ signatures, and replace graph building with compile(weights=...). Align weight names with the new module hierarchy and move the KV cache unflatten into forward(). Port sharding to a DeviceMesh when the V2 model is sharded, then verify greedy outputs match the V2 implementation.
/new-modular-projectCreates a new Mojo or MAX project. Use when wanting to start a new Mojo or MAX project, initializing the `pixi` or `uv` environment to use Mojo or MAX, or when the user wants to begin a new Mojo or MAX project from scratch./profile-modelProfile a model running on MAX to find where it spends time and whether the GPU is saturated. Use when the user asks to "profile my model," "where is my model spending time," "why is inference slow," "is my GPU being utilized," "how much GPU am I using," "get a kernel breakdown," "capture an nsys/rocprof/ncu trace of max serve," or wants to measure MAX inference performance. Works for any model MAX can run — built-in architectures and custom ones loaded with --custom-architectures — from a pip or pixi install (max generate, max serve, or a Python script) on NVIDIA or AMD GPUs. Decide cheapest-first: a GPU utilization check, then a kernel breakdown, then a single-kernel deep dive only when one kernel dominates.
Other plugins' skills for the same job