optimum-neuron / smolvlm
AGENTS.md270 starsChanged 10 months agoApache-2.0View on GitHub
This directory contains the Neuron-optimized SmolVLM/Idefics3 inference implementation. It extends the NxD decoder stack with a separately compiled vision encoder and on-device image-token feature injection. For shared NxD decoder guidance, read optimum/neuron/models/inference/AGENTS.md. For SmolVLM, there are exactly two compiled artifacts: model_vision.pt and model_text.pt. Both are managed by SmolVLMNxDModelForImageTextToText in modeling_smolvlm.py. The custom embeddings intentionally keep HF key names (patch_embedding, position_embedding) so state dict loading maps directly. Image features are inserted at image_token_id positions and passed into VLM wrappers: - NxDDecoderWrapperForImageTextToText…
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/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/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
3 rules. About 0 are common, 2 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 64
Only features for real (non-padding) tiles are used.
- Distinctive: line 66
If image token count exceeds available features, a warning is logged and remaining positions stay text-only.
- Very specific: line 91
After constructing the model, load_weights() is called to restore the shard checkpoints for all bundles.
≈ “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
SmolVLM (Idefics3) Inference Model Guide
This directory contains the Neuron-optimized SmolVLM/Idefics3 inference implementation. It extends the NxD decoder stack with a separately compiled vision encoder and on-device image-token feature injection.
For shared NxD decoder guidance, read optimum/neuron/models/inference/AGENTS.md.
HF references
- Transformers Idefics3 modeling: https://github.com/huggingface/transformers/tree/main/src/transformers/models/idefics3
Core architecture in this implementation
Compiled artifacts (named bundles)
Compiled bundles are saved with a named scheme defined in
backend/pretrained_model.py.
Multi-bundle models (like VLMs) use model_{bundle_name}.pt:
model_vision.pt: vision encoder bundle — SigLIP vision encoder + HFIdefics3Connectortraced with ModelBuilder.model_text.pt: text decoder graph bundle — context/chunked-prefill + token generation.
For SmolVLM, there are exactly two compiled artifacts: model_vision.pt and model_text.pt.
Both are managed by SmolVLMNxDModelForImageTextToText in modeling_smolvlm.py.
Vision stack is custom SigLIP-compatible
NeuronIdefics3VisionEncoder is built from:
NeuronSigLIPVisionEmbeddingsNeuronSigLIPEncoderLayer/NeuronSigLIPEncoderNeuronSigLIPVisionTransformer- HF
Idefics3Connector
The custom embeddings intentionally keep HF key names (patch_embedding, position_embedding) so
state dict loading maps directly.
Position IDs are precomputed for static compiled image size
NeuronSigLIPVisionEmbeddings precomputes position IDs using HF fractional-coordinate bucketing
for a full patch mask. This is important for numerical parity with HF embeddings.
Decoder remains Llama-style text core
NxDSmolVLMDecoderModelsubclassesNxDLlamaModeland usesconfig.text_config.- VLM-specific behavior is injected by VLM wrappers/builders, not by replacing the text stack.
VLM-specific forward behavior
Pixel values path through generate
generate() stores pixel_values on self._current_pixel_values because _sample() in the base
path does not forward pixel_values into forward().
Context/chunked-prefill uses image injection tensors
For context encoding (or each prefill chunk), the model builds:
image_embeds: shape[B, S, H]image_token_mask: shape[B, S](bool)
Image features are inserted at image_token_id positions and passed into VLM wrappers:
NxDDecoderWrapperForImageTextToText(context encoding / chunked prefill)NxDTokenGenerationWrapperForImageTextToText(token generation — passes dummy image tensors to match the uniform 6-tensor compiled signature)
Vision feature mapping rules
pixel_valuessupports[B, N, C, H, W]or[B, C, H, W].- Tiles are flattened and cast to Neuron dtype before vision forward.
- Inputs are padded/truncated to compiled vision batch size:
batch_size * max_num_images. - Only features for real (non-padding) tiles are used.
- Feature rows are consumed in order and mapped to
image_token_idpositions in token order. - If image token count exceeds available features, a warning is logged and remaining positions stay text-only.
Export/load details that matter
Export compiles all bundles via NxDPreTrainedModel.compile()
_export() (defined in NxDModelForImageTextToText, the shared VLM base class) calls
create_graph_builders() which returns {"vision": {...}, "text": {...}}, then passes
this to NxDPreTrainedModel.compile() which iterates over bundles and traces each one.
The compiled artifacts are saved as model_vision.pt and model_text.pt.
Vision checkpoint loader avoids from_pretrained allocation path
_vision_checkpoint_loader_fn() (in NxDModelForImageTextToText) is returned by
get_checkpoint_loader_fn("vision") and used by the multi-bundle weight loading
infrastructure. It calls _get_vision_encoder_state_dict() (overridden in
SmolVLMNxDModelForImageTextToText) to extract and remap vision weights from the
full HF state dict:
model.vision_model.*->vision_model.*model.connector.*->connector.*
This avoids from_pretrained allocation interactions under model-builder meta/init contexts.
Saved weights re-initialization on load
_from_pretrained() (in NxDPreTrainedModel) calls create_graph_builders() to
determine the expected bundle names ("vision", "text"), then loads each compiled
artifact by its expected filename (model_vision.pt, model_text.pt).
After constructing the model, load_weights() is called to restore the shard
checkpoints for all bundles.
Neuron config behavior
Auto max_num_images
_get_neuron_config() computes max_num_images to account for Idefics3 tiling
plus global view, based on image_size and a 2048 longest-edge assumption.
Image sequence length derivation
image_seq_len = (image_size // patch_size) ** 2 // (scale_factor ** 2).
State dict conversion behavior
convert_hf_to_neuron_state_dict():
- Removes
model.vision_model.*andmodel.connector.*(vision lives in separate artifact). - Optionally fuses QKV for text decoder (
convert_state_dict_to_fused_qkv). - Adds
rank_util.ranktensors required by Neuron attention modules.
update_state_dict_for_tied_weights() ties lm_head.weight to embed_tokens.weight if missing.
Key files
- optimum/neuron/models/inference/smolvlm/modeling_smolvlm.py
- optimum/neuron/models/inference/backend/modules/decoder/vlm_decoder.py
- optimum/neuron/models/inference/backend/modules/decoder/vlm_wrappers.py
- optimum/neuron/models/inference/backend/modules/decoder/vlm_builders.py
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.

