nemo_automodel.components.loss.infonce
nemo_automodel.components.loss.infonce
Module Contents
Classes
Functions
API
Bases: Module
InfoNCE soft listwise distillation loss module.
Bases: Module
InfoNCE loss module with optional learnable temperature.
Soft listwise distillation on InfoNCE candidate sets.
Builds the same candidate sets as infonce_loss for the student and
the (detached) teacher, then matches the student’s distribution over those
candidates to the teacher’s.
Parameters:
Tensor of shape [batch, student_hidden].
Tensor of shape [batch, student_hidden], row-aligned
with student_queries.
Tensor of shape [batch, teacher_hidden]. Detached.
Tensor of shape [batch, teacher_hidden], row-aligned
with teacher_queries. Detached.
Optional Tensor of shape
[batch, negatives, student_hidden] — mined negative documents,
per query, so they are candidates for "q2d" only.
Optional Tensor of shape
[batch, negatives, teacher_hidden], aligned with
student_hard_negatives. Required when it is given. Detached.
Optional Tensor of shape [batch, negatives] — 1
for a real negative, 0 for padding. Padded columns are scored as
-inf and masked out of the divergence.
Logit scale divisor; a 0-dim Tensor when it is learned.
"q2d", "d2q", or "symmetric".
Score every query against every document in the batch instead of only its own positive.
L2-normalize the embeddings before scoring.
"kl", "ce", or "mse".
Returns: torch.Tensor
Scalar Tensor: the mean divergence over the batch.
Raises:
ValueError: If the embedding shapes disagree with the layouts above, ifdirectionordivergenceis unknown, or if the requested directions have no candidates to score against —use_in_batch_negatives=Falseneeds hard negatives anddirection="q2d", because the document-to-query direction can only draw candidates from the batch.
InfoNCE contrastive loss with optional hard negatives.
Parameters:
Tensor of shape [batch, hidden] — pooled query embeddings.
Tensor of shape [batch, hidden] — pooled embeddings of the
positive document for each query, row-aligned with queries.
Optional Tensor of shape [batch, negatives, hidden] —
mined negative documents for each query. Rows are per-query, so
these are candidates for the "q2d" direction only.
Optional Tensor of shape [batch, negatives] —
1 for a real negative, 0 for padding in a ragged batch. Padded
columns are scored as -inf and contribute nothing.
Logit scale divisor; a 0-dim Tensor when it is learned.
"q2d", "d2q", or "symmetric".
Score every query against every document in the batch instead of only its own positive.
L2-normalize the embeddings before scoring.
Returns: torch.Tensor
Scalar Tensor: the mean cross-entropy over the batch.
Raises:
ValueError: If the embedding shapes disagree with the layouts above, ifdirectionis unknown, or if the requested directions have no candidates to score against —use_in_batch_negatives=Falseneedshard_negativesanddirection="q2d", because the document-to-query direction can only draw candidates from the batch.