optimum-neuron / inference
AGENTS.md270 starsChanged 10 months agoApache-2.0View on GitHub
This guide focuses on NxD inference models, decoder graphs, attention, and porting practices. For project-wide workflows see AGENTS.md. Architecture documents with class hierarchies, graph variants, forward dispatch, and generation loop diagrams: - Text generation (CausalLM): ARCHITECTURE_CAUSAL_LM.md - Image-text-to-text (VLM): ARCHITECTURE_IMAGE_TEXT_TO_TEXT.md Implementation details live in: - optimum/neuron/models/inference/backend/modules/decoder/modeling_decoder.py - optimum/neuron/models/inference/backend/modules/decoder/decoder_builders.py - optimum/neuron/models/inference/backend/modules/decoder/decoder_wrappers.py See reference implementation: - optimum/neuron/models/inference/llama/modeling_llama.py Use NxDI for neuron-specific graph changes and HF Transformers for base architecture. The Optimum Neuron implementation prioritizes stability, maintainability, and HF ecosystem compatibility over cutting-edge…
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.
Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository32 other files it gives its agents.
- AGENTS.mdAGENTS.md
- optimum/neuron/cache/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/backend/modules/attention/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/gemma3/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/granite/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/llama4/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/llama/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/mixtral/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/phi3/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/qwen2/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/qwen3/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/qwen3_moe/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/smollm3/AGENTS.mdAGENTS.md
- optimum/neuron/models/inference/smolvlm/AGENTS.mdAGENTS.md
- optimum/neuron/vllm/AGENTS.mdAGENTS.md
- tests/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- optimum/neuron/cache/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/gemma3/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/granite/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/llama4/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/llama/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/mixtral/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/phi3/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/qwen2/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/qwen3/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/qwen3_moe/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/smollm3/CLAUDE.mdCLAUDE.md
- optimum/neuron/models/inference/smolvlm/CLAUDE.mdCLAUDE.md
- optimum/neuron/vllm/CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
The file
We split this AGENTS.md into the rules it gives an agent
5 rules. About 0 are common, 3 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 32
Call context encoding before token generation.
- Very specific: line 55
Use KVCacheManager instead of HF dynamic cache.
- Distinctive: line 70
Use NxDI for neuron-specific graph changes and HF Transformers for base architecture.
- Distinctive: line 82
When adding a new model directory:
- Very specific: line 83
Create CLAUDE.md in the model directory containing @AGENTS.md so Claude Code auto-loads the model-specific guide.
≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md 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
Optimum Neuron Inference Models (NxD) Guide
This guide focuses on NxD inference models, decoder graphs, attention, and porting practices. For project-wide workflows see AGENTS.md.
NxD Decoder Models
Architecture documents with class hierarchies, graph variants, forward dispatch, and generation loop diagrams:
- Text generation (CausalLM): ARCHITECTURE_CAUSAL_LM.md
- Image-text-to-text (VLM): ARCHITECTURE_IMAGE_TEXT_TO_TEXT.md
Three-Graph Architecture
- Context encoding graph: multi-token prompt → KV cache
- Token generation graph: 1 token in → 1 token out
- Speculation graph: optional multi-token proposal
Implementation details live in:
- optimum/neuron/models/inference/backend/modules/decoder/modeling_decoder.py
- optimum/neuron/models/inference/backend/modules/decoder/decoder_builders.py
- optimum/neuron/models/inference/backend/modules/decoder/decoder_wrappers.py
KV Cache Management
KV cache is managed by KVCacheManager with BHSD layout and in-place aliasing:
- optimum/neuron/models/inference/backend/modules/kvcache/kv_cache_manager.py
On-Device Sampling
Sampling on NeuronCores uses nxd_topk, nxd_argmax, NKI cumsum kernels:
- optimum/neuron/models/inference/backend/modules/generation/sampling.py
Common Pitfalls
- Runtime shapes must match compiled shapes.
- Call context encoding before token generation.
- TP degree must match compiled model.
- Decoder graph changes require cache prune:
python tools/prune_test_models.py.
Attention Mechanisms
Grouped Query Attention (GQA)
- Sharding strategy selection: REPLICATE_TO_TP_DEGREE vs CONVERT_TO_MHA
- Logic in optimum/neuron/models/inference/backend/modules/attention/gqa.py
Flash Attention on Neuron
- Attention module guide (dispatch table, NKI kernel for head_dim > 128, sliding window): optimum/neuron/models/inference/backend/modules/attention/AGENTS.md
- Training path uses
attn_implementation="flash_attention_2".
Parallel Attention Layers
Parallel QKV and output projections use ColumnParallelLinear/RowParallelLinear in:
- optimum/neuron/models/inference/backend/modules/attention/attention_base.py
Neuron vs HF Modeling Differences
Llama-like models (decoder-only)
- Replace HF
nn.Linear/Embeddingwith TP-aware parallel layers. - Replace HF attention with
NeuronAttentionBasefor static shapes. - Use
KVCacheManagerinstead of HF dynamic cache. - Optional fused QKV/MLP kernels (Neuron-only).
- State dict remaps (e.g., QKV concatenation).
See reference implementation:
- optimum/neuron/models/inference/llama/modeling_llama.py
MoE models (Mixtral, Qwen3 MoE)
- Expert routing and sharding are TP/EP aware.
- Expert capacity and dispatch are statically shaped.
- Expert MLPs use parallel layers or fused kernels.
- State dict remaps for expert sharding when required.
Porting from NxDI
Use NxDI for neuron-specific graph changes and HF Transformers for base architecture.
The Optimum Neuron implementation prioritizes stability, maintainability, and HF ecosystem compatibility over cutting-edge performance optimizations. For production deployments requiring maximum throughput, NxDI remains the reference implementation.
Per-Module Parity Tests
Track numerical differences using module-level tests before full graph tests:
- tests/decoder/test_modules.py compares HF layers to Neuron equivalents using
nxd_testing.build_module()andvalidate_accuracy(). - tests/decoder/test_attention.py validates attention with explicit rotary embedding and mask handling.
These isolate drift or state-dict conversion issues early.
New Model Checklist
When adding a new model directory:
- Create
CLAUDE.mdin the model directory containing@AGENTS.mdso Claude Code auto-loads the model-specific guide.
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.

