> This page is for version Latest.
> For other versions, use one of these documentation indexes:
> - Nightly (default): https://docs.nvidia.com/nemo/automodel/nightly/llms.txt
> - Latest: https://docs.nvidia.com/nemo/automodel/latest/llms.txt
> - 0.7.0 · 26.10: https://docs.nvidia.com/nemo/automodel/v0.7/llms.txt
> - 0.5.0 · 26.06: https://docs.nvidia.com/nemo/automodel/v0.5/llms.txt
> - 0.4.0 · 26.04: https://docs.nvidia.com/nemo/automodel/v0.4/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/automodel/llms.txt.

# nemo_automodel.components.models.common.lm_head

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

## Module Contents

### Classes

| Name                                                                | Description                                                                               |
| ------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| [`LMHead`](#nemo_automodel-components-models-common-lm_head-LMHead) | `nn.Linear` vocabulary head that returns `logits_dtype` logits whatever its weight dtype. |

### Functions

| Name                                                                                                        | Description                                                                                          |
| ----------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| [`_fp32_logits`](#nemo_automodel-components-models-common-lm_head-_fp32_logits)                             | Compute FP32 logits from operands in one dtype.                                                      |
| [`_fp32_logits_backward`](#nemo_automodel-components-models-common-lm_head-_fp32_logits_backward)           | Run the head's backward GEMMs in the weight's dtype.                                                 |
| [`_fp32_logits_fake`](#nemo_automodel-components-models-common-lm_head-_fp32_logits_fake)                   | Allocate the logits of a traced call.                                                                |
| [`_fp32_logits_flops`](#nemo_automodel-components-models-common-lm_head-_fp32_logits_flops)                 | Count the projection's FLOPs.                                                                        |
| [`_fp32_logits_setup_context`](#nemo_automodel-components-models-common-lm_head-_fp32_logits_setup_context) | Save the operands for the backward GEMMs.                                                            |
| [`_fp32_logits_sharding`](#nemo_automodel-components-models-common-lm_head-_fp32_logits_sharding)           | Shard the logits like a column-parallel linear.                                                      |
| [`slice_logits_to_keep`](#nemo_automodel-components-models-common-lm_head-slice_logits_to_keep)             | Select the positions of hidden states whose logits a forward returns, before the head projects them. |

### API

```python
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.

```python
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.

```python
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.

```python
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 | None` — default: 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.

```python
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.

```python
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;

```python
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

```python
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.

```python
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.

```python
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.

---

```python
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,

```python
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