nemo_automodel.components.models.clm.heads
nemo_automodel.components.models.clm.heads
Contrastive Language Model (CLM) projection heads.
CLM (https://huggingface.co/Contrastive-LM/CLM-v0.1-8B) is a frozen Qwen3-8B encoder (last-token pooled, L2-normalised
hidden state) followed by two small MLP projection heads. A (state, candidate) pair is scored by
exp(logit_scale) * cos(state_head(e_state), action_head(e_candidate)) and a question’s answer distribution is the
softmax of those scores over its options.
Source architecture and checkpoint contract: https://github.com/Contrastive-LM/CLM (Apache-2.0;
https://github.com/Contrastive-LM/CLM/blob/main/LICENSE). The NeMo implementation adds typed configuration, validation,
and local checkpoint I/O. The modules reproduce the released architecture parameter-for-parameter so that checkpoints round-trip with the
contrastive-lm package (clm-serve / clm.Engine): the checkpoint is a torch.save dict with
state_head, action_head, logit_scale and cfg.
Module Contents
Classes
Functions
Data
API
Declare the complete frozen-encoder contract used to identify cached features.
Hub revisions must be immutable commit IDs. Different tokenizer, truncation, pooling, normalization, precision, attention, or Transformers settings produce different cache names even when the input texts are unchanged.
Resolve the cache path without creating directories or fetching model files.
Parameters:
Directory that will own the feature cache.
Ordered input strings, aligned with the embedding table rows.
Returns: Path
Cache path identified by all embedding-producing settings and exact texts.
Declarative configuration for a CLM state/action head pair.
Parameters:
Width of each frozen backbone embedding.
Hidden width of each projection MLP.
Number of linear layers in each projection MLP; at least two.
Width of the normalized decision embedding.
One of "gelu", "relu", or "silu".
Apply LayerNorm before intermediate hidden layers.
Add residual connections around intermediate hidden layers.
Build the configured head pair.
Parameters:
Positive InfoNCE softmax temperature.
Returns: CLMHeadPair
Trainable CLM state/action projection heads.
Bases: Module
Published CLM state and action projections plus learned temperature.
Return the scalar inverse temperature, capped at the released value 100.
Validate the encoder width without modifying its embeddings.
Parameters:
Tensor of shape [rows, hidden], with hidden equal to config.hidden_size.
Input name used in validation errors.
Score every state against every candidate action.
Parameters:
Tensor of shape [batch, hidden].
Tensor of shape [candidates, hidden].
Returns: torch.Tensor
Tensor of shape [batch, candidates] containing scaled cosine logits.
Load the official Contrastive-LM .pt head checkpoint.
Parameters:
Local CLM_v0.1-8B.pt-compatible checkpoint path.
Returns: CLMHeadPair
Head pair with the published state, action, and temperature tensors.
Project and normalize action embeddings.
Parameters:
Tensor of shape [candidates, hidden].
Returns: torch.Tensor
Tensor of shape [candidates, projection].
Project and normalize state embeddings.
Parameters:
Tensor of shape [batch, hidden].
Returns: torch.Tensor
Tensor of shape [batch, projection].
Score ragged question options in question-major order.
Parameters:
Tensor of shape [questions, hidden].
Tensor of shape [sum_options, hidden].
Positive option count for each question, summing to sum_options.
Returns: list[torch.Tensor]
Question-major tensors, each of shape [options].
Apply the decision-head temperature training policy before optimization.
Parameters:
Whether the scalar log inverse temperature receives gradients.
Text -> row lookup into a table of L2-normalised encoder embeddings.
Load a previously published cache using restricted tensor deserialization.
Return a tensor of shape [len(texts), hidden] (a copy, optionally moved to device).
Publish the cache atomically, preserving any prior cache on failure.
Bases: Module
Project frozen encoder embeddings.
Parameters:
Tensor of shape […, hidden].
Returns: torch.Tensor
Tensor of shape […, projection].
Gather encoder embeddings for a batch of records.
Returns: torch.Tensor
(state_emb, option_emb, option_counts, option_orders): state_emb has shape [questions, hidden] (one row
Every unique text the encoder must embed for these records (state texts first, then candidates).
Load a CLM head checkpoint (defaults to the released CLM-v0.1-8B heads from the Hugging Face Hub).
Save heads in the contrastive-lm checkpoint format (loadable by clm-serve and load_clm_heads).