import-model
Original:🇺🇸 English
Translated
14 scripts
Use 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 Hugging Face config and modeling code, scaffold from a similar MAX architecture, implement each graph layer to match HF, serve, then debug against the Hugging Face reference until outputs match.
4installs
Sourcemodular/skills
Added on
NPX Install
npx skill4agent add modular/skills import-modelTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Import a model into MAX
Input: a Hugging Face model ID ().
$ARGUMENTSCopy references/template.md to track this port as
you work through the phases.
Porting a model to MAX means writing a MAX graph that performs the same
computation as the model's in Hugging Face ,
then loading the released weights into that graph and verifying the outputs
match.
modeling_<type>.pytransformersThe workflow has three phases: decide & plan, implement, verify.
Phase 1 is reading and planning. Phase 2 is the port: implement every divergent
sublayer in the graph. Phase 3 is verification, only after implementation is
complete. Guards (preconditions that stop the line) gate the transitions
between activities; they are not steps of their own.
Anti-pattern: running , tweaking , and serving
while still implements the donor (, , …). That is
not a bring-up — logit verification will fail because the wrong architecture
is running. Do not run verification scripts until
implement-graph.md completion criteria pass.
scaffold.pyarch.py<slug>.pyllama3qwen3Each phase links to references with the details. Read the reference for the
activity you're on, not all of them upfront.
Environment: run every command through the pixi env that has MAX
installed (, ), from the skill
root where lives (do not use bare or on the
shell PATH):
pixi run python …pixi run max serve …pixi.tomlpythonmaxbash
cd <path-to-skill>
pixi install
pixi run python scripts/inspect_hf.py <HF_MODEL_ID>
# Or: pixi run test-scripts # smoke-test all scripts (no GPU)Helper scripts live in this skill's directory (copy or vendor
them into your repo). All helpers are also reachable through a unified
dispatcher with the same argument names and exit codes:
scripts/bash
pixi run python scripts/import_model.py inspect <HF_MODEL_ID>
pixi run python scripts/import_model.py scaffold <HF_MODEL_ID> --start-from llama3 --output-dir ./
pixi run python scripts/import_model.py list-archs --match LlamaForCausalLM
pixi run python scripts/import_model.py check-walls <HF_MODEL_ID>
pixi run python scripts/import_model.py list-keys <HF_MODEL_ID> --summary
pixi run python scripts/import_model.py gates <HF_MODEL_ID> --port-dir <port_dir>/
pixi run python scripts/import_model.py compare <HF_MODEL_ID> --slug <slug> --port 8000Port layout:
- — slug folder containing
<port_dir>andarch.pyinARCHITECTURES(usually__init__.py). Pass this path to both<output_dir>/<slug>/and--custom-architectures.run_oss_gates.py --port-dir
MAX resolves by adding
to and importing as the module. Passing the
parent directory imports the wrong module name (e.g. instead of
your slug).
--custom-architectures <port_dir>dirname(<port_dir>)sys.pathbasename(<port_dir>)custom-archImport/API errors while editing: copy the donor arch under
; see
pitfalls-config.md § Import and config API traps.
modular/max/python/max/pipelines/architectures/<donor>/Phase 1 — Decide & plan
Guard: is the architecture already registered in MAX? Before writing any code, check whether MAX already registers the architecture class in your model's. Ifconfig.json::architectures[0]returns a slug, runpixi run python list_native_archs.py --match <Class>and stop — no port needed. Full procedure: native-arch-check.md.pixi run max serve --model <HF_MODEL_ID>
Read config.json
config.jsonPull the config and read every field:
bash
pixi run python -c "from transformers import AutoConfig; \
print(AutoConfig.from_pretrained('<HF_MODEL_ID>', trust_remote_code=True))"Or use the helper, which fetches raw from the Hub, runs the
native-arch check, and prints every key mapped to the MAX API:
config.jsonbash
pixi run python inspect_hf.py <HF_MODEL_ID>Then list safetensors metadata (keys, shapes, dtypes — no weight download):
bash
pixi run python list_checkpoint_keys.py <HF_MODEL_ID> --summaryEach row is one key →
(or in for , ).
Keys you cannot wire through are the deltas you
implement in the graph. Field meanings and common deltas:
read-config-json.md.
config.jsonpipeline_config.model.huggingface_configSupportedArchitecturearch.pyarchitecturestorch_dtypeMyConfig.initialize()Scan for hard blockers before you commit to a port:
bash
pixi run python check_walls.py <HF_MODEL_ID>Exit 0 → continue. Exit 1 → review
recognize-walls.md. Exit 2 → stop until the
wall is resolved or scoped out.
Read the model card
Open and read the model card for:
https://huggingface.co/<HF_MODEL_ID>- The paper or blog post. Skim its architecture section — authors call out the interesting modifications (QK-norm, MLA, sliding-window attention, MoE routing) because those are what they want credit for.
- "Tricks" mentioned in the card. Phrases like "we introduce", "unlike prior models", "this is the first model to" mark deltas that will bite you during implementation if you miss them now.
If the card says the model is from a known family (Llama, Mistral, Qwen,
Gemma), note that; the donor-comparison activity below will start from the
closest already-ported variant of that family.
If the card mentions custom CUDA kernels, custom attention with no public
reference, FP8/FP4-only released weights, ALiBi, recurrence or state-space
layers; see recognize-walls.md before going
further. Some models can't be ported with the public MAX surface alone.
Propose a plan; accept a veto
Before any code, write a short paragraph stating what you'd do by default,
then wait for the user to confirm or veto. Cover four axes (distribution
shape, quantization variants, validation depth, hardware target) — all
derived from what you've already read. Don't ask blank questions; state a
default and let them push back.
Full guidance and an example paragraph:
plan-and-veto.md.
If estimated weight bytes do not fit one GPU, read
distributed-transformer.md before
choosing — distribution shape matters more than attention
family alone.
--start-fromCompare with other MAX architectures
You're picking the closest already-ported MAX architecture to copy from.
"Closest" means: same attention shape (dense vs. GQA vs. MLA vs. MoE), same
MLP shape (gated vs. non-gated, dense vs. routed), same head layout (tied vs.
untied, single Linear vs. multi-step).
List what your installed MAX registers (do not hard-code a slug list):
bash
pixi run python list_native_archs.pyHeuristic HF-signal → donor slug hints are in
map-to-max.md. Quick version:
| Your model | Start from |
|---|---|
| Llama 3-ish (GQA, RoPE, SwiGLU MLP) | |
| Gemma-ish (RMSNorm scale, logit softcap, dual norm) | |
| Qwen-ish (GQA, RoPE, may have QK-norm) | |
| Mistral-ish (sliding window) | |
| Phi-ish (partial RoPE) | |
| MoE (sparse experts, top-k routing) | |
| MLA (latent KV) | |
Open the chosen MAX arch's directory and read its top-level model file
(usually ). You're answering: which functions/classes need to
change vs. stay the same when I port my model?
<slug>.pyNow read the corresponding Hugging Face modeling file:
bash
pixi run python -c "from transformers.models.<model_type> import modeling_<model_type>; \
print(modeling_<model_type>.__file__)"Read the , the attention , the MLP , the block
class, and the final head. Compare each to the MAX equivalent. The reference
read-modeling-code.md covers what to look
for in each.
__init__forwardforwardOutput of this activity: a delta list — one row per real difference between
HF and the donor MAX arch (attention, MLP/MoE, block wiring, head, RoPE,
masks). You implement every row in Phase 2. Three or fewer structural deltas
→ good donor choice. Many deltas → pick a closer donor or plan to rewrite
whole classes. Do not proceed to verification with an empty or "looks
Llama-ish" delta list.
Phase 2 — Implement
Scaffold the file layout
scaffold.pybash
pixi run python scaffold.py <HF_MODEL_ID> --start-from <max_arch_slug> --output-dir <output_dir>This reads from the Hub for
, then copies the chosen native MAX architecture into
as five files:
architectures[0]config.jsonarch.py::name<output_dir>/<slug>/- — registration shell (verify
arch.pyand encoding)name= - — donor config (must be rewired during implementation)
model_config.py - — pipeline model shell
model.py - — donor renames (must be rewritten for your checkpoint)
weight_adapters.py - — donor graph (must be edited to match HF during implementation)
<slug>.py
After scaffold, you have a directory layout and a wrong graph. Stop here
until the graph is implemented — do not serve.
Scaffold also leaves the donor's docstrings and code comments in place.
Sed-renaming class names doesn't touch text that records what the file
claims to do. After scaffold, opens with a docstring
describing the donor; the new class claims behaviors (single-GPU support,
QK-norm, post-attention norm, etc.) the new file may not have. Rewriting
those docstrings is a required part of the graph implementation — not
optional polish. See honest-docstrings.md
for the three-sentence pattern every module docstring should follow and a
mandatory audit checklist before declaring the implementation done.
<slug>.pyImplement the graph
This is the bring-up. Phase 1 produced the config map and delta list; the
implementation activity executes them in code.
Full checklist, work order, anti-patterns, and completion criteria:
implement-graph.md.
In order:
- — wire every
model_config.pykey from Phase 1 /config.json. Setinspect_hf.pyhead counts and head_dim to match HF.get_kv_params() - — map your checkpoint's safetensor keys to the MAX module names you will use. Run
weight_adapters.pyfirst; see rename-weights.md. After load, wire the coverage audit in state-dict-audit.md (especially MoE andlist_checkpoint_keys.pytied embeddings).strict=False - — for each row in the delta list, edit or replace the donor class so MAX
<slug>.pymirrors HFforward():forward()- Attention (Q/K/V, RoPE, mask, GQA, softcap, …)
- MLP or MoE (activation, routing, shared experts, …)
- Decoder block (norm order and residual wiring — not interchangeable with Llama)
- Final norm and LM head (tie, logit scale, softcap)
- — confirm
arch.pymatchesname=;architectures[0]matches Hubdefault_encoding.torch_dtype - — only if HF wraps the backbone differently (VL, multi-modal).
model.py
Read HF while editing, not after verification fails.
Subclass the donor only where HF and donor match; rewrite the class where the
delta list said they differ.
modeling_<type>.pyThe implementation is done when every item in
implement-graph.md
is checked — especially: every delta has a corresponding code change, weights
load without orphan keys, and the scaffold-comment audit in
honest-docstrings.md
has been run with each match classified as OK / Lie / Stale. A passing audit is
mandatory; declaring the implementation done without it leaves donor lies in
the codebase that nothing downstream will catch.
Quick grep recipe (full classification rules in
honest-docstrings.md):
bash
pixi run rg -i -n 'qwen|llama|mistral|cohere|gemma|phi|deepseek|exaone|olmo|granite|qwen3|mixtral|single-GPU|single GPU|RMSNorm|QK-norm' <port_dir>/Your implementation-complete message must explicitly attest to the audit
(e.g. ). A claim without the
attestation isn't a completion.
"docstrings rewritten to the three-sentence pattern; rg returns N hits, all legitimate lineage references"Preflight (Hub config + arch registration — run before first serve):
bash
pixi run python run_oss_gates.py <HF_MODEL_ID> --port-dir <port_dir>/Guard: local smoke gate (mandatory before Phase 3).cold-compiles for 5–25 minutes. Before serving, run the four local checks in serve-and-iterate.md (import smoke, graph dry-build, adapter⇄graph key diff, weights-format preflight).pixi run max servecovers walls, checkpoint metadata, andrun_oss_gates.pyname/encoding — not a substitute for those four.arch.py
Phase 3 — Verify
Check if it generates coherent text
Prerequisite: graph implementation complete. Do not serve to "see what
happens" during implementation — fix config, adapters, and graph first.
Sanity-check the HF reference FIRST. Run HF alone on the model card's
intended prompt template, before involving MAX. If HF itself produces
gibberish, your oracle is broken — fixing your port against a broken
oracle wastes days.
Then serve with
and probe with the model card's intended template (not just "The capital of
France is" — that prompt is wrong for PrefixLMs and instruction-tuned models).
Three possible outcomes: server crashes during load → fix config/adapters;
server starts but returns garbage → divergence hunt; server returns plausible
text → run at before celebrating.
pixi run max serve --model-path <HF_MODEL_ID> --custom-architectures <port_dir>max_tokens=64+Full HF-reference sanity check, encoder/embedding slug serve flow, and
fix-test loop discipline:
serve-and-iterate.md.
Layer-by-layer divergence hunt
This is the main loop. You're going to:
- Read the HF reference implementation to understand what should happen at each layer.
- Dump intermediate tensors from both implementations and find the first layer where they diverge.
- Fix that layer.
- Re-run the layer check.
- Repeat until all layers match.
Read the reference implementation
Before diving into the HF source, consult the symptom table at the top
of divergences.md. Match what you're
observing (gibberish at token 0, divergence growing with length, output
plausible but text drifts, etc.) to its candidate causes, and read every
candidate listed — not just the first plausible one. Several causes
produce the same symptom; the bug is the one you haven't checked yet.
Then open the HF as a debugger, not a reviewer. You're
looking for the specific detail you missed. Common ones:
modeling_<type>.py- A norm whose variant or position differs from the template
- A scale factor applied somewhere (,
hidden_states * scale, MuP multipliers)attn_weights / sqrt(d) - A different activation function in the MLP
- A different RoPE style (split-half vs. interleaved, partial vs. full)
- A boundary condition that only fires at certain layers (sliding-window vs. global attention, sink-token handling)
Easter-egg warning: HF modeling code inherits aggressively. A class named
may inherit critical behavior
from a different family entirely. Always chase inheritance up at least one
level before concluding "this is just Llama with renamed fields."
MyModelDecoderLayer(GraniteDecoderLayer)The catalog of "this differs from Llama and here is how" is in
divergences.md. Indexed by symptom.
Compare logits (and HF layer stats)
bash
pixi run python compare_layers.py <HF_MODEL_ID> \
--slug <your_slug> --port 8000 \
--prompt "The capital of France is"Requires with on
the same port.
pixi run max serve--custom-architectures <port_dir>MAX does not expose per-layer hidden states via the OpenAI API. This script:
- Prints HF-only per-layer stats (embedding + each block output) as a diagnostic snapshot while you read the modeling code.
- Compares top-1 logprob at the prompt between HF and MAX via
.
/v1/completions?logprobs=…
If logprobs diverge, use divergences.md and add
taps in your for true tensor diffs inside each
block — see
layer-by-layer-debugging.md.
ops.output(...)<slug>.pyFix the layer, then re-run
Edit to fix the identified layer, restart ,
re-run . When top-1 logprob matches (rel_diff < 5%), logits
are aligned at that prompt. For block-local confirmation, use manual
taps.
<slug>.pypixi run max servecompare_layers.pyops.output()If you fix a layer and the divergence point doesn't move, you fixed the
wrong thing. Revert and re-read the HF source for that layer.
Check against Hugging Face
Run the model end-to-end with pretrained weights, then run HF on the same
prompt with greedy sampling. On the MAX side, use the dtype that matches the
weight encoding the model supports (most models ship bfloat16). Outputs
should be identical or nearly identical; small BF16/FP16 rounding can cause
divergence past a dozen tokens. Persistent divergence in the first tokens
after the divergence hunt passed usually means tokenizer/chat-template
mismatch, dtype mismatch with the released weights, or nonzero MAX sampling.
When matching text comes out, the port is done for greedy text. Real
"done" depends on the validation depth picked during planning — pick a tier
from 1 (smoke) to 6 (logit parity).
Full HF-comparison recipe, divergence triage, and the 6-tier validation
table: validation-tiers.md.
Common pitfalls
Use pitfalls.md as an index — find your symptom,
then load the one category file (config, weights, graph, or serving) —
honest-docstrings.md for the docstring
audit specifically. The two big ones:
- Scaffold ≠ port. Do not serve or verify until the graph implements
every delta in .
<slug>.py - Sed-rename leaves donor docstrings intact. Class names get renamed, but docstrings and comments still describe the donor. Rewrite them and run the audit grep before declaring the implementation done.
Tests and CI
When you add tests for the ported model, minimize the number of
MAX graph compilations per file. Compile once via a module-scoped fixture
and reuse it across cases. For files that must
compile different graphs, parallelize them with Bazel
instead of splitting the file. Full patterns and examples:
tests-and-ci.md.
pytest@pytest.mark.parametrizeshard_count