model-recipe
Skill1 starsChanged 2 months agoApache-2.0View on GitHub
Write or review a Buckyball model definition (stack/models/models/<model>/model*.py) and the importer and frontend steps of its recipes (stack/models/archs/<arch>/<model>/), including whole-graph import, permute passes and their numerical verification.
In this repository25 other files it gives its agents.
- agent-runtimeSkill
- ball-alignSkill
- canvas-clientSkill
- checkSkill
- chip-designerSkill
- collaboration-panelSkill
- debug-bemuSkill
- debug-compilerSkill
- debug-p2eSkill
- debug-performanceSkill
- debug-verilatorSkill
- domain-modelingSkill
- graph-contractSkill
- grillingSkill
- grill-with-docsSkill
- ip-designerSkill
- model-optimizeSkill
- model-portSkill
- plugin-verifySkill
- programming-principlesSkill
- repo-installSkill
- reportSkill
- role-identitySkill
- waveformSkill
- workload-testsSkill
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 4 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 55
Import the model file, (graph,) = compiler.importer(module.load(), *module.inputs()) with torch._inductor.decomposition.decompositions …
- Very specific: line 64
Keep existing file headers.
- Very specific: line 72
Import with the inductor decompositions so aten.t already is a permute.
- Distinctive: line 83
Run every changed importer and compare with PyTorch, not only with a previous dump:
- Distinctive: line 100
Check the unmodified graph too: a mismatch there is a Buddy lowering bug, not the step's.
- Distinctive: line 102
When a refactor must not change outputs, compare the exported files byte for byte with the previous importer's (MLIR may differ only in a …
- Distinctive: line 107
When a check fails on the unmodified graph too, suspect the lowering.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Model Recipe
A model is defined once under stack/models/models/<model>/; every recipe that
compiles it (archs/{x86,nv,gemmini}/<model>, their origin/, and
archs/buckyball/<design>/<model>) imports that definition and adds only its
own frontend steps. Use model-port for the end-to-end
port and model-optimize for chip design.
Model files
- A model that is one graph has one
model.py. A model whose graphs a runner chains has onemodel-<graph>.pyper graph (model-text_encoder.py,model-unet.py, ...).<graph>names the whole chain: the functionforward_<graph>, the groupsubgraph0_<graph>and the parametersarg<i>_<graph>.data. Each file holds the
nn.Moduleof the graph,load()(the checkpoint-loaded module in eval mode, ready to import: in-place activations off and other capture settings that keep the numerics) andinputs()(a tuple of example inputs). Every user, including the model's own graph and prepare scripts, takes them the same way; hyphens in module names are fine:lenet = import_module("models.models.lenet.model") model = lenet.load() (graph,) = compiler.importer(model, *lenet.inputs())A recipe that changes the module itself before capture (folding batch norms for its Balls) does it on
modelin its importer, as a frontend step.- One graph is the whole computation of one invocation. Import it whole when Dynamo can capture it: a VLA with a fixed number of denoising steps unrolls into one graph with shared weights. Keep the graph to one step when the step count is large or data dependent: one diffusion step, one decoder step of an LLM with its KV cache as graph inputs and outputs.
- Target-independent rewrites that keep the numerics belong in the model file:
whole-image position ids instead of scattered ones, RoPE halves written with
catinstead of slice assignment, grouped-query attention without repeating KV per query head, a prefix cache the expert reads without cloning or concatenating. Check each rewrite in PyTorch against the original module first. Target-dependent steps (permute, quantization) stay in the recipe'sfrontend/. - Shared code between the graph files of one model is copied, not factored.
Importer
buddy-pytorch-importer.py of a recipe is a straight script, no argparse, no
helper functions:
model_dir = Path(__file__).resolve().parent,output = model_dir / "output".- Import the model file,
(graph,) = compiler.importer(module.load(), *module.inputs())withtorch._inductor.decomposition.decompositionsunless the recipe needs another table, andgraph.dump(output / "01-import.txt"). - Each frontend step from
frontend/<step>/graph.py, followed by its dump (02-permuted.txt, ...). - Lowering and export (when the recipe has reached that step):
graph.fuse_ops([simply_fuse]),GraphDriver,subgraph0*.mlir,forward*.mlir, one parameter pack per dtype in the order of the packed arguments.
Keep existing file headers. The CMake of a recipe lists every step's dump as an
output, depends on frontend/*.py, and ends with add_subdirectory(origin)
when an origin flavor exists.
Permute
- Linear: turn
t(W)intopermute(W, [1, 0]),decompose_addmm_to_mm_add, thentranspose_matmul_fusion; the weight keeps its[out, in]layout. Import with the inductor decompositions soaten.talready is a permute. - Convolutions on x86/nv/gemmini: one NHWC region around the convolutions
(conv
NHWC_FCHW, pools and batch normNHWC, means over[1, 2], cat and split on axis 3, elementwise layers whose 4-D inputs are all in the region); one permute into the region before its first user and one out of it where a non-region node reads it. Never insert nodes between placeholders: they take the function arguments in body order. - A pass works on op types, never on node names.
Verification
Run every changed importer and compare with PyTorch, not only with a previous dump:
- JIT the graph after the step:
graph.compile(), thencompiler._dynamo_run_for_graph(graph)(*params, *inputs). Graph inputs come in first-use order, and module attributes Dynamo lifts (yolo anchors) are inputs too: take the example inputsDynamoCompiler._compile_fxreceives, minus the parameters, instead of guessing the order. Randomize the float example inputs before capture so PyTorch and the graph see the same values. Disabletorch._dynamo.mark_dynamic(the JIT lowers static shapes only), drop the importer's trace config, and leaveeliminate-memref-copyout of the JIT pipeline: it turns cache updates into in-place writes of input buffers the Python call does not keep alive. - Reading the argument order of a compiled
forwardfor a runner: follow each argument offorward.mlirintosubgraph0.mlirand look at its first use (a mask is cast to i1, token ids index an embedding); never infer it from the Python signature. - Check the unmodified graph too: a mismatch there is a Buddy lowering bug, not the step's.
- When a refactor must not change outputs, compare the exported files byte for byte with the previous importer's (MLIR may differ only in a trailing newline).
Buddy lowerings fixed in this repository
When a check fails on the unmodified graph too, suspect the lowering. These were wrong and are fixed; keep the behaviour when touching them:
max_pool2dwith padding rounds the output down like PyTorch (trailing padding gives up the remainder), never symmetric SAME padding._to_copyconverts every dtype pair (float to int truncates).cumsumis a parallellinalg.generic,index_putwithout accumulation onescf.parallelof scalar copies: host-side loops over device buffers crash the nv pipeline.embeddingreshapes any index tensor to[1, N]fortosa.gather.- Scaled dot-product attention adds a float mask in four dimensions, so masks broadcast over batch, head and query.
- Batch norm and adaptive average pooling honour
_layout(NHWC).
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

