Model Heads
About
The d9d.module.block.head package provides task heads. A task head turns backbone hidden states into a typed output. Each head takes the hidden states and its own shared input, such as labels or a pooling mask.
Heads
Causal Language Modelling
SplitLanguageModellingHead computes the per-token cross-entropy loss for causal language modeling. Labels equal to LM_IGNORE_INDEX are ignored.
It uses the fused linear cross-entropy kernel from Cut Cross-Entropy (paper), so it does not materialize the full logits tensor. It requires the d9d[cce] extra.
The vocabulary can consist of several independent splits, like in SplitTokenEmbeddings.
Classification
ClassificationHead applies dropout and a linear projection, and returns fp32 logits.
Embedding
EmbeddingHead returns fp32 embeddings, with an optional linear projection and L2 normalization.
Both ClassificationHead and EmbeddingHead take an optional pooling_mask that selects the tokens to use. You can build it with d9d.dataset.token_pooling_mask_from_attention_mask.
Usage
API Reference
d9d.module.block.head
Task heads that turn backbone hidden states into a typed output, and their I/O contracts.
LM_IGNORE_INDEX = -100
module-attribute
Label index that the LM head ignores when computing the loss.
ClassificationHead
Bases: TaskHead[SequencePoolingHeadShared, SequenceClassificationOutput]
Classification head on top of model hidden states.
Applies dropout and a linear projection to produce logits for num_labels classes.
An optional pooling mask selects specific tokens (e.g. [CLS] tokens) before the projection.
__init__(hidden_size, num_labels, dropout)
forward(hidden_states, shared)
Computes class logits from hidden states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Hidden states. Shape: |
required |
shared
|
SequencePoolingHeadShared
|
The head shared input. Its optional |
required |
Returns:
| Type | Description |
|---|---|
SequenceClassificationOutput
|
The classification output holding the unnormalized fp32 logits. |
reset_parameters()
Resets module parameters.
EmbeddingHead
Bases: TaskHead[SequencePoolingHeadShared, SequenceEmbeddingOutput]
Head that extracts dense embeddings from hidden states.
Optionally applies a linear projection and L2 normalization, e.g. for contrastive learning or retrieval. An optional pooling mask selects specific tokens (e.g. the last token) before the projection.
__init__(hidden_size, embedding_dim, normalize)
Constructs the EmbeddingHead object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_size
|
int
|
Hidden size. |
required |
embedding_dim
|
int | None
|
Dimensionality of the output embedding. If |
required |
normalize
|
bool
|
Whether to apply L2 normalization to the final embeddings. |
required |
forward(hidden_states, shared)
Computes dense embeddings from hidden states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Hidden states. Shape: |
required |
shared
|
SequencePoolingHeadShared
|
The head shared input. Its optional |
required |
Returns:
| Type | Description |
|---|---|
SequenceEmbeddingOutput
|
The embedding output holding the fp32 embeddings. |
reset_parameters()
Resets module parameters.
SequenceCausalLMHeadShared
dataclass
The shared input a causal language modeling head consumes.
Attributes:
| Name | Type | Description |
|---|---|---|
labels |
Tensor
|
Target token indices for the loss computation. Shape: |
SequenceCausalLMOutput
dataclass
The output of a causal language modeling head.
Attributes:
| Name | Type | Description |
|---|---|---|
cross_entropy |
Tensor
|
Per-token cross-entropy loss, the negative log-probability of each label. It is zero at
ignored positions. Shape: |
SequenceClassificationOutput
dataclass
The output of a classification head.
Attributes:
| Name | Type | Description |
|---|---|---|
scores |
Tensor
|
Classification logits. Shape: |
SequenceEmbeddingOutput
dataclass
The output of an embedding head.
Attributes:
| Name | Type | Description |
|---|---|---|
embeddings |
Tensor
|
Pooled embeddings. Shape: |
SequencePoolingHeadShared
dataclass
The shared input a pooled head (classification or embedding) consumes.
Attributes:
| Name | Type | Description |
|---|---|---|
pooling_mask |
Tensor | None
|
Binary mask of the tokens to pool. You can build it from an attention mask with
|
SplitLanguageModellingHead
Bases: TaskHead[SequenceCausalLMHeadShared, SequenceCausalLMOutput]
Language modeling head with a split vocabulary that computes per-token cross-entropy loss.
Each vocabulary split (e.g. regular and special tokens) has its own linear layer. The forward pass
concatenates their weights in split_order and computes the loss with the fused Cut Cross-Entropy
kernel, so full logits are not materialized. Requires the d9d[cce] extra.
__init__(split_vocab_size, split_order, hidden_size)
Constructs the SplitLanguageModellingHead object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
split_vocab_size
|
Mapping[str, int]
|
Mapping from split names to their vocabulary sizes. |
required |
split_order
|
Sequence[str]
|
Order in which vocabulary splits are concatenated. This defines which global indices belong to each split. |
required |
hidden_size
|
int
|
Hidden size. |
required |
forward(hidden_states, shared)
Computes the cross-entropy loss for the given hidden states and labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Hidden states. Shape: |
required |
shared
|
SequenceCausalLMHeadShared
|
The head shared input. Its |
required |
Returns:
| Type | Description |
|---|---|
SequenceCausalLMOutput
|
The causal LM output holding the unreduced per-token loss, with the shape of |
reset_parameters()
Resets module parameters.
TaskHead
Bases: Module, ModuleLateInit, ABC, Generic[THeadShared, THeadOutput]
Abstract base class for a task head that turns backbone hidden states into a typed output.
A head is confined to compute: it reads the shared hidden_states plus its own shared
input PyTree, and returns its own output PyTree. Its parallelization and checkpoint mapping
are kept out of the module as separate concerns.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
THeadShared
|
The head's own shared input, routed to it by name at composition time. |
required | |
THeadOutput
|
The output PyTree this head produces. |
required |
forward(hidden_states, shared)
abstractmethod
Computes the head output from hidden states and this head's shared input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Backbone hidden states. Shape: |
required |
shared
|
THeadShared
|
The head's own shared input (e.g. labels, a pooling mask). |
required |
Returns:
| Type | Description |
|---|---|
THeadOutput
|
The head's output PyTree. |
reset_parameters()
Resets the module parameters (i.e. performs random initialization).