Skip to navigation

nemo_automodel.components.models.common.lm_head

View as MarkdownOpen in Claude

LM head whose logits dtype is independent of its weight dtype.

Module Contents

Classes

NameDescription
LMHeadnn.Linear vocabulary head that returns logits_dtype logits whatever its weight dtype.

Functions

NameDescription
_fp32_logitsCompute FP32 logits from operands in one dtype.
_fp32_logits_backwardRun the head’s backward GEMMs in the weight’s dtype.
_fp32_logits_fakeAllocate the logits of a traced call.
_fp32_logits_flopsCount the projection’s FLOPs.
_fp32_logits_setup_contextSave the operands for the backward GEMMs.
_fp32_logits_shardingShard the logits like a column-parallel linear.
slice_logits_to_keepSelect the positions of hidden states whose logits a forward returns, before the head projects them.

API

class nemo_automodel.components.models.common.lm_head.LMHead(
in_features: int,
out_features: int,
bias: bool = False,
logits_dtype: torch.dtype | None = None,
logit_softcapping: float | None = None,
device: torch.device | str | None = None,
dtype: torch.dtype | None = None
)

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.

nemo_automodel.components.models.common.lm_head.LMHead.forward(
input: torch.Tensor
) -> torch.Tensor

Project hidden states with the head’s own weight.

Parameters:

input
torch.Tensor

Tensor of shape […, hidden].

Returns: torch.Tensor

Logits of shape […, vocab], as project returns them.

nemo_automodel.components.models.common.lm_head.LMHead.from_linear(
head: torch.nn.Linear,
logits_dtype: torch.dtype | None
) -> typing.Self
classmethod

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:

head
nn.Linear

Plain nn.Linear whose weight is a Tensor of shape [vocab, hidden] and whose bias is a Tensor of shape [vocab] or None.

logits_dtype
torch.dtype | 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: If head is not a plain nn.Linear, so it has its own projection.
nemo_automodel.components.models.common.lm_head.LMHead.linear(
input: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor | None = None,
logits_dtype: torch.dtype | None
) -> torch.Tensor
staticmethod

Project hidden states with a supplied weight and bias, without a softcap.

Parameters:

input
torch.Tensor

Tensor of shape […, hidden], cast to the weight’s dtype. With logits_dtype set, a DTensor sharded on a leading dimension is replicated first.

weight
torch.Tensor

Tensor of shape [vocab, hidden]; a column-parallel DTensor uses Shard(0), with the input replicated on the same mesh.

bias
torch.Tensor | NoneDefaults to None

Optional tensor of shape [vocab].

logits_dtype
torch.dtype | None

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.

nemo_automodel.components.models.common.lm_head.LMHead.project(
input: torch.Tensor,
weight: torch.Tensor
) -> torch.Tensor

Project hidden states with a supplied vocabulary weight.

Parameters:

input
torch.Tensor

Tensor of shape […, hidden], with arbitrary leading dimensions. It is cast to the supplied weight’s dtype before projection.

weight
torch.Tensor

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.

nemo_automodel.components.models.common.lm_head._fp32_logits(
input: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor | None
) -> torch.Tensor

Compute FP32 logits from operands in one dtype.

Parameters:

input
torch.Tensor

Tensor of shape […, hidden].

weight
torch.Tensor

Tensor of shape [vocab, hidden], in input’s dtype.

bias
torch.Tensor | None

Optional tensor of shape [vocab].

Returns: torch.Tensor

FP32 tensor of shape […, vocab]. CUDA BF16/FP16 operands accumulate in FP32 without an upcast copy;

nemo_automodel.components.models.common.lm_head._fp32_logits_backward(
ctx,
grad: torch.Tensor
) -> tuple[torch.Tensor | None, ...]

Run the head’s backward GEMMs in the weight’s dtype.

Parameters:

ctx

Autograd context holding input, weight and bias.

grad
torch.Tensor

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

nemo_automodel.components.models.common.lm_head._fp32_logits_fake(
input: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor | None
) -> torch.Tensor

Allocate the logits of a traced call.

Parameters:

input
torch.Tensor

Tensor of shape […, hidden].

weight
torch.Tensor

Tensor of shape [vocab, hidden].

bias
torch.Tensor | None

Optional tensor of shape [vocab]; unused.

Returns: torch.Tensor

Empty FP32 tensor of shape […, vocab] on input’s device.

nemo_automodel.components.models.common.lm_head._fp32_logits_flops(
input_shape,
weight_shape,
bias_shape,
out_shape
)

Count the projection’s FLOPs.

Parameters:

input_shape

Shape […, hidden] of input.

weight_shape

Shape [vocab, hidden] of weight.

bias_shape

Shape [vocab] of bias, or None; bias additions are not counted.

out_shape

Shape […, vocab] of the logits; unused.

Returns:

Two FLOPs per multiply-accumulate: 2 * input elements * vocab.

nemo_automodel.components.models.common.lm_head._fp32_logits_setup_context(
ctx,
inputs,
output
)

Save the operands for the backward GEMMs.

Parameters:

ctx

Autograd context that owns the saved tensors.

inputs

Tuple of input […, hidden], weight [vocab, hidden], and bias [vocab] or None.

output

FP32 logits of shape […, vocab]; unused.

nemo_automodel.components.models.common.lm_head._fp32_logits_sharding(
input,
weight,
bias
)

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:

input

Tensor metadata of global shape […, hidden].

weight

Tensor metadata of global shape [vocab, hidden].

bias

Tensor metadata of global shape [vocab], or None.

Returns:

Supported output/input placements on each mesh axis: everything replicated, or the vocabulary sharded,

nemo_automodel.components.models.common.lm_head.slice_logits_to_keep(
hidden_states: torch.Tensor,
logits_to_keep: int | torch.Tensor
) -> torch.Tensor

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:

hidden_states
torch.Tensor

Tensor of shape [batch, sequence, hidden], or packed (THD) [tokens, hidden].

logits_to_keep
int | torch.Tensor

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