Skip to main content
miniVLLM provides two built-in causal LM implementations that follow a common interface:
  • forward(input_ids) returns the final hidden states.
  • compute_logits(hidden_states) projects hidden states to vocabulary logits via the LM head.
  • A class-level packed_module_mapping dict maps model parameter names to checkpoint keys, enabling correct loading of fused/sharded weights.

Qwen3ForCausalLM

Qwen3 architecture with Q/K norms and GQA

LlamaForCausalLM

Llama 3 architecture with NTK-scaled RoPE

Qwen3ForCausalLM

myvllm.models.qwen3.Qwen3ForCausalLM Full Qwen3 causal language model. Stacks num_layers of Qwen3DecoderLayer inside a Qwen3Model backbone, then attaches a ParallelLMHead for next-token prediction. Supports optional weight tying between the token embedding and the LM head. Key architectural differences from Llama 3:
  • Per-head Q and K RMSNorm layers inside each attention block (applied when qkv_bias=False).
  • Default RoPE base of 10000 (versus 500000 for Llama 3).
  • Default max position of 16384.

Constructor

int
required
Vocabulary size. Controls the size of the token embedding table and the LM head.
int
required
Model hidden dimension (embedding size and residual stream width).
int
required
Total number of query attention heads across all tensor-parallel ranks.
int
Dimension of each attention head. Defaults to hidden_size // num_heads.
float
default:"1.0"
Attention scale multiplier applied alongside 1 / sqrt(head_dim).
int
Total number of key/value heads. Set to a value smaller than num_heads for grouped-query attention (GQA). Defaults to num_heads.
float
default:"1e-5"
Epsilon used in all RMSNorm layers.
bool
default:"False"
Whether to include bias in the QKV projection. When False, per-head Q and K norms are applied before attention.
int
default:"10000"
RoPE base frequency.
int
default:"16384"
Maximum sequence length the positional embedding cache is pre-computed for.
int
default:"4096"
Hidden dimension of the MLP feed-forward layers.
bool
default:"True"
Whether to include bias in the MLP projections.
int
default:"12"
Number of transformer decoder layers.
bool
default:"False"
If True, the LM head shares the same weight tensor as the token embedding.
int
default:"256"
Paged KV cache block size, passed through to each Attention module.

forward

Runs the full Qwen3 backbone (embedding → decoder layers → final RMSNorm) and returns the hidden states. Does not project to logits; call compute_logits separately.

compute_logits

Projects hidden states to vocabulary logits using ParallelLMHead. In a tensor-parallel setup, rank 0 gathers logits from all ranks and returns the full (batch_or_tokens, vocab_size) tensor; other ranks return a partial shard.

Sub-components

packed_module_mapping

Qwen3ForCausalLM defines a class attribute that maps internal parameter names to their corresponding keys in a HuggingFace-style checkpoint and the sub-index within a fused weight:
The model runner uses this mapping to call the correct weight_loader overload when loading pre-trained checkpoints. Quick start

LlamaForCausalLM

myvllm.models.llama.LlamaForCausalLM Llama 3 causal language model. Structurally identical to Qwen3ForCausalLM but with the following differences:
  • No Q/K normsLlamaAttn does not apply per-head RMSNorm to queries and keys.
  • NTK-scaled RoPERotaryEmbedding is initialized with is_llama3=True, enabling the NTK-by-parts long-context frequency scaling.
  • Higher RoPE base — defaults to 500000 instead of 10000.
  • Larger context window — defaults to 131072 instead of 16384.
  • tie_word_embeddings=True by default.

Constructor

int
default:"128256"
Vocabulary size.
int
default:"2048"
Model hidden dimension.
int
default:"64"
Dimension of each attention head.
int
default:"32"
Total number of query/output heads across all tensor-parallel ranks.
int
default:"8"
Total number of key/value heads.
bool
default:"False"
Whether to add bias in the QKV projection.
float
default:"1e-5"
Epsilon for all RMSNorm layers.
int
default:"500000"
RoPE base frequency. The higher value extends the effective context length.
int
default:"131072"
Maximum sequence length the positional cache covers.
int
default:"8192"
MLP feed-forward hidden dimension.
bool
default:"False"
Whether to include bias in the MLP projections.
int
default:"16"
Number of transformer decoder layers.
int
default:"256"
Paged KV cache block size.
bool
default:"True"
If True, the LM head shares the embedding weight.

forward

Same interface as Qwen3ForCausalLM.forward. Returns final hidden states.

compute_logits

Same interface as Qwen3ForCausalLM.compute_logits. Projects to vocabulary logits.

Sub-components

Quick start

Adding a new model

Follow these steps to add a new architecture to miniVLLM.
1

Implement the ForCausalLM class

Create src/myvllm/models/mymodel.py. The class must expose:
Use the existing layer primitives from myvllm.layers (ColumnParallelLinear, RowParallelLinear, LayerNorm, Attention, etc.) to ensure correct tensor-parallel behavior.
2

Implement weight_loader for custom sharding

If your model has parameters that do not map 1-to-1 to checkpoint keys — e.g., fused QKV or merged gate+up projections — override weight_loader on the relevant nn.Parameter:
Refer to QKVColumnParallelLinear.weight_loader and MergedColumnParallelLinear.weight_loader in myvllm/layers/linear.py for reference implementations.
3

Register in the model runner

Open the model runner’s model registry and add an entry for your new class:
The runner uses this registry to instantiate the correct class from the model config.
4

Export from the models package

Add the import to src/myvllm/models/__init__.py:
Keep each decoder layer’s forward signature consistent with Qwen3DecoderLayer — accepting (x, residual) and returning (x, residual) — so the backbone loop works without modification.