nemo_automodel.components.models.common.lm_head
nemo_automodel.components.models.common.lm_head
LM head whose logits dtype is independent of its weight dtype.
Module Contents
Classes
Functions
API
Bases: Linear
nn.Linear vocabulary head that returns logits_dtype logits whatever its weight dtype.
With logits_dtype set, the GEMM reads the input in the weight’s dtype, accumulates in FP32 and
writes FP32 logits (vLLM’s head_dtype=float32), unaffected by autocast, then casts them; the
backward casts the gradient to the weight’s dtype. Chunked losses, which gather the weight themselves, call
project, so subclasses that change the logits override it.
Repo models take logits_dtype as a constructor argument and build their head with it; their forwards pick
positions with slice_logits_to_keep before calling the head.
Project hidden states with the head’s own weight.
Parameters:
Tensor of shape […, hidden].
Returns: torch.Tensor
Logits of shape […, vocab], as project returns them.
Wrap the plain linear head a Hugging Face parent class built, sharing its weight and bias.
Ties, optimizer identity and checkpoint keys are unchanged.
Parameters:
Plain nn.Linear whose weight is a Tensor of shape [vocab, hidden] and whose bias is a Tensor
of shape [vocab] or None.
Dtype of the logits; None keeps the weight’s dtype, which autocast may lower.
Returns: Self
Head that shares head’s weight and bias Parameters without copying, in head’s training mode.
Raises:
ValueError: Ifheadis not a plainnn.Linear, so it has its own projection.
Project hidden states with a supplied weight and bias, without a softcap.
Parameters:
Tensor of shape […, hidden], cast to the weight’s dtype. With logits_dtype set, a DTensor
sharded on a leading dimension is replicated first.
Tensor of shape [vocab, hidden]; a column-parallel DTensor uses Shard(0), with the input replicated on the same mesh.
Optional tensor of shape [vocab].
Dtype of the logits. None runs a plain linear in the weight’s dtype that autocast may lower; otherwise the GEMM accumulates and writes FP32 logits, which are cast to this dtype.
Returns: torch.Tensor
Logits of shape […, vocab], sharded on the vocabulary axis for a column-parallel weight.
Project hidden states with a supplied vocabulary weight.
Parameters:
Tensor of shape […, hidden], with arbitrary leading dimensions. It is cast to the supplied weight’s dtype before projection.
Tensor of shape [vocab, hidden], on the input’s device. Chunked losses supply their gathered weight here. For column-parallel DTensors these are global shapes: the weight uses Shard(0), and the input is replicated on the same mesh.
Returns: torch.Tensor
Logits of shape […, vocab], with leading axes unchanged and the configured softcap applied.
Compute FP32 logits from operands in one dtype.
Parameters:
Tensor of shape […, hidden].
Tensor of shape [vocab, hidden], in input’s dtype.
Optional tensor of shape [vocab].
Returns: torch.Tensor
FP32 tensor of shape […, vocab]. CUDA BF16/FP16 operands accumulate in FP32 without an upcast copy;
Run the head’s backward GEMMs in the weight’s dtype.
Parameters:
Autograd context holding input, weight and bias.
FP32 gradient of the logits, shape […, vocab].
Returns: torch.Tensor | None
Gradients of input […, hidden] and weight [vocab, hidden] in the weight’s dtype and of bias [vocab] in
Allocate the logits of a traced call.
Parameters:
Tensor of shape […, hidden].
Tensor of shape [vocab, hidden].
Optional tensor of shape [vocab]; unused.
Returns: torch.Tensor
Empty FP32 tensor of shape […, vocab] on input’s device.
Count the projection’s FLOPs.
Parameters:
Shape […, hidden] of input.
Shape [vocab, hidden] of weight.
Shape [vocab] of bias, or None; bias additions are not counted.
Shape […, vocab] of the logits; unused.
Returns:
Two FLOPs per multiply-accumulate: 2 * input elements * vocab.
Save the operands for the backward GEMMs.
Parameters:
Autograd context that owns the saved tensors.
Tuple of input […, hidden], weight [vocab, hidden], and bias [vocab] or None.
FP32 logits of shape […, vocab]; unused.
Shard the logits like a column-parallel linear.
The backward flattens input’s leading dimensions, so LMHead.linear replicates an input sharded on one of
them before calling the op.
Parameters:
Tensor metadata of global shape […, hidden].
Tensor metadata of global shape [vocab, hidden].
Tensor metadata of global shape [vocab], or None.
Returns:
Supported output/input placements on each mesh axis: everything replicated, or the vocabulary sharded,
Select the positions of hidden states whose logits a forward returns, before the head projects them.
Hugging Face’s slice_logits_to_keep (newer than the pinned transformers), for an int or a 1D index tensor.
Parameters:
Tensor of shape [batch, sequence, hidden], or packed (THD) [tokens, hidden].
0 keeps every position and returns hidden_states itself, since a full
slice(0, None) of a DTensor has raised on aten.alias under sequence parallelism. A positive int
keeps the last N positions; an integer tensor of shape [kept] keeps those positions.
Returns: torch.Tensor
Tensor of shape [batch, kept, hidden], or [kept, hidden] for packed input: a view of hidden_states for