nemo_automodel.components.models.qwen2.model
nemo_automodel.components.models.qwen2.model
Custom Qwen2 model implementation for NeMo Automodel.
This module provides a self-contained Qwen2 implementation with separate HuggingFace-style q/k/v and gate/up projections.
Example (YAML):
Module Contents
Classes
Data
API
Bases: Module
Multi-headed attention with separate QKV projections — HuggingFace default layout.
Run Qwen2 attention over padded BSHD or packed THD states.
Parameters:
Hidden states [B, S, H] or packed local states
[T, H]. B is batch, S is sequence, T is local
total tokens, and H is hidden size.
RoPE tensors (cos, sin) or fused
(cos, sin, freqs_cis). Local tensors use [B, S, D] or
[T, D] and the fused raw table uses [S, 1, 1, D].
Padded attention mask for BSHD; THD uses cumulative
document lengths from kwargs.
Optional BSHD KV cache; unsupported for THD.
Optional BSHD cache positions [S].
THD requires qkv_format='thd' and cu_seqlens
[N + 1]; CP additionally supplies cp_size and cp_rank.
Returns: torch.Tensor
Attention output shaped like hidden_states and optional BSHD
Bases: GradientCheckpointingLayer
Single Qwen2 decoder layer with RMSNorm, attention, and MLP.
Bases: HFCheckpointingMixin, Qwen2PreTrainedModel
Qwen2 model with causal language modeling head.
Uses separate q/k/v and gate/up projections — HuggingFace layout.
Run causal LM projection for BSHD or packed THD inputs.
Parameters:
Token IDs [B, S] or packed local IDs [T].
Optional padded mask. THD uses cu_seqlens.
Position IDs [B, S] or packed local IDs [T].
Optional BSHD KV cache; unsupported for THD.
Optional hidden inputs [B, S, H] or [T, H].
Optional labels [B, S] or packed [T].
Whether to update the BSHD KV cache.
Whether to request attention outputs.
Whether to return per-layer hidden states.
Whether to return CausalLMOutputWithPast.
Optional BSHD cache positions [S].
Positions to project from hidden size H to
vocabulary size V.
THD metadata. cu_seqlens is [N + 1] and CP adds
cp_size and cp_rank.
Returns: CausalLMOutputWithPast
Causal LM output with BSHD logits [B, S, V]. Packed local
Bases: Qwen2PreTrainedModel
Qwen2 transformer model (embeddings + decoder layers + norm).
Run the Qwen2 decoder in padded BSHD or packed THD layout.
Parameters:
Token IDs [B, S] or packed local IDs [T].
Optional padded mask [B, S]. THD uses packed
document boundaries instead.
Position IDs [B, S] or packed local IDs [T].
Optional BSHD generation cache; unsupported for THD.
Alternative hidden inputs [B, S, H] or [T, H].
Whether to update the BSHD KV cache.
Whether to request attention outputs.
Whether to retain per-layer hidden states.
Whether to return BaseModelOutputWithPast.
Optional BSHD cache positions [S].
THD metadata including qkv_format, cu_seqlens,
max_seqlen, cp_size, and cp_rank.
Returns: BaseModelOutputWithPast
Decoder output with final states [B, S, H] or packed local
Bases: PreTrainedModel
Abstract class for Qwen2 pretrained models.
Bases: Module
SwiGLU MLP with separate gate_proj and up_proj — identical to HuggingFace default.