logits
shrinkai.distillation.losses.logits
Logit-based Knowledge Distillation loss functions.
This module provides various loss functions that operate on the final output logits of the student and teacher models. Logit-based distillation (or "response-based" distillation) is the most common form of Knowledge Distillation.
These methods typically apply temperature scaling to the models' logits to soften the probability distributions. This exposes the "dark knowledge" of the teacher (i.e., the relative probabilities assigned to non-target classes), allowing the student to learn the teacher's generalization capabilities.
Available Losses
- HintonLoss: The standard KD loss (Cross-Entropy + KL Divergence).
- PureKDLoss: Pure distillation without ground-truth labels (KL Divergence only).
- ReverseKLLoss: Mode-seeking distillation, highly effective for LLMs.
- BCEKDLoss: Distillation for multi-label classification tasks (Sigmoid + BCE).
- JSDLoss: Symmetric distillation using Jensen-Shannon Divergence.
Examples:
>>> from shrinkai.distillation.losses import HintonLoss
>>> criterion = HintonLoss(temperature=4.0, alpha=0.5)
>>> loss = criterion(student_logits, teacher_logits, labels)
Classes:
| Name | Description |
|---|---|
BCEKDLoss |
Knowledge Distillation loss for Multi-Label classification tasks. |
HintonLoss |
Knowledge Distillation loss (Geoffrey Hinton et al. (2015)). |
JSDLoss |
Jensen-Shannon Divergence (JSD) loss for Knowledge Distillation. |
PureKDLoss |
Pure Knowledge Distillation loss. |
ReverseKLLoss |
Reverse Kullback-Leibler divergence for Knowledge Distillation (Gu et al. (2024), |
Functions:
| Name | Description |
|---|---|
expects_logits |
Decorator to automatically extract logits from FeatureExtractor tuples. |
Classes
BCEKDLoss
Bases: BaseDistillationLoss
Knowledge Distillation loss for Multi-Label classification tasks.
Unlike HintonLoss which uses Softmax (classes are mutually exclusive), this loss uses Sigmoid to treat each class independently. It computes the Binary Cross Entropy (BCE) between the student's logits and the teacher's softened targets.
Equation
Attributes:
| Name | Type | Description |
|---|---|---|
temperature |
float
|
Softening factor for logits. Must be > 0. |
alpha |
float
|
Weight balancing factor. Must be in range [0.0, 1.0]. |
Methods:
| Name | Description |
|---|---|
forward |
Computes combined hard BCE and soft BCE losses. |
Source code in src/shrinkai/distillation/losses/logits.py
Methods:
forward
forward(
student_outputs: Tensor,
teacher_outputs: Tensor,
labels: Tensor | None = None,
) -> torch.Tensor
Computes combined hard BCE and soft BCE losses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
student_outputs
|
Tensor
|
Raw unnormalized logits from the student model (Shape: [B, C] or [B, S, C]). |
required |
teacher_outputs
|
Tensor
|
Raw unnormalized logits from the teacher model (Shape: [B, C] or [B, S, C]). |
required |
labels
|
Tensor | None
|
Ground-truth labels (ignored, kept for API compatibility). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Scalar loss value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If shapes of |
Source code in src/shrinkai/distillation/losses/logits.py
HintonLoss
Bases: BaseDistillationLoss
Knowledge Distillation loss (Geoffrey Hinton et al. (2015)).
Combines standard task loss (Cross-Entropy with hard ground-truth labels) and distillation loss (Kullback-Leibler divergence on softened probabilities produced by a teacher model at a given temperature).
Equation
Attributes:
| Name | Type | Description |
|---|---|---|
temperature |
float
|
Softening factor for logits. Higher values produce smoother probability distributions over classes. Must be > 0. |
alpha |
float
|
Weight balancing factor between hard label loss and distillation loss. Must be in range [0.0, 1.0]. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the HintonLoss module. |
forward |
Computes combined Cross-Entropy and KD divergence losses. |
Source code in src/shrinkai/distillation/losses/logits.py
Methods:
__init__
Initializes the HintonLoss module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
float
|
Temperature scaling factor (T > 0). Defaults to 4.0. |
4.0
|
alpha
|
float
|
Weight for distillation loss (0.0 <= alpha <= 1.0). Defaults to 0.5. |
0.5
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/shrinkai/distillation/losses/logits.py
forward
forward(
student_outputs: Tensor,
teacher_outputs: Tensor,
labels: Tensor | None = None,
) -> torch.Tensor
Computes combined Cross-Entropy and KD divergence losses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
student_outputs
|
Tensor
|
Raw unnormalized logits from the student model (Shape: [B, C]). |
required |
teacher_outputs
|
Tensor
|
Raw unnormalized logits from the teacher model (Shape: [B, C]). |
required |
labels
|
Tensor | None
|
Ground-truth class indices (Shape: [B]). Optional if alpha == 1.0. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Weighted scalar loss value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If shapes of |
Source code in src/shrinkai/distillation/losses/logits.py
JSDLoss
Bases: BaseDistillationLoss
Jensen-Shannon Divergence (JSD) loss for Knowledge Distillation.
JSD is a symmetric and bounded alternative to the standard KL Divergence. It computes the divergence of both distributions from their average distribution M. This boundedness prevents gradient explosion, especially early in training when the student's predictions might diverge heavily from the teacher's.
Equation
Attributes:
| Name | Type | Description |
|---|---|---|
temperature |
float
|
Softening factor for logits. Must be > 0. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the JSDLoss module. |
forward |
Computes the JSD between softened student and teacher distributions. |
Source code in src/shrinkai/distillation/losses/logits.py
Methods:
__init__
Initializes the JSDLoss module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
float
|
Softening factor for logits (T > 0). Defaults to 4.0. |
4.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/shrinkai/distillation/losses/logits.py
forward
forward(
student_outputs: Tensor,
teacher_outputs: Tensor,
labels: Tensor | None = None,
) -> torch.Tensor
Computes the JSD between softened student and teacher distributions.
Args: student_outputs: Raw unnormalized logits from the student model (Shape: [B, C] or [B, S, C]). teacher_outputs: Raw unnormalized logits from the teacher model (Shape: [B, C] or [B, S, C]). labels: Ground-truth labels (ignored, kept for API compatibility).
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Scalar loss value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If shapes of |
Source code in src/shrinkai/distillation/losses/logits.py
PureKDLoss
Bases: HintonLoss
Pure Knowledge Distillation loss.
Implement distillation loss (Kullback-Leibler divergence on softened probabilities
produced by a teacher model at a given temperature). In fact, a PureKDLossobject
is just a HintonLoss object with alpha set to 1.
Equation
Attributes:
| Name | Type | Description |
|---|---|---|
temperature |
float
|
Softening factor for logits. Higher values produce smoother probability distributions over classes. Must be > 0. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the PureKDLoss module. |
Source code in src/shrinkai/distillation/losses/logits.py
Methods:
__init__
Initializes the PureKDLoss module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
float
|
Temperature scaling factor (T > 0). Defaults to 4.0. |
4.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/shrinkai/distillation/losses/logits.py
ReverseKLLoss
Bases: BaseDistillationLoss
Reverse Kullback-Leibler divergence for Knowledge Distillation (Gu et al. (2024), MiniLLM).
Standard KD (Forward KL) computes KL(P_teacher || P_student), which is "mode-covering". Reverse KL computes KL(P_student || P_teacher), which is "mode-seeking".
For Large Language Models (LLMs), Reverse KL is highly effective because it strongly penalizes the student for assigning high probabilities to tokens that the teacher considers unlikely, thereby reducing hallucinations and degeneration.
Equation
Attributes:
| Name | Type | Description |
|---|---|---|
temperature |
float
|
Softening factor for logits. Must be > 0. Note: In LLM distillation, temperature is often set close to 1.0. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the ReverseKLLoss module. |
forward |
Computes the Reverse KL divergence loss. |
Source code in src/shrinkai/distillation/losses/logits.py
Methods:
__init__
Initializes the ReverseKLLoss module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
float
|
Temperature scaling factor (T > 0). Defaults to 1.0. |
1.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/shrinkai/distillation/losses/logits.py
forward
forward(
student_outputs: Tensor,
teacher_outputs: Tensor,
labels: Tensor | None = None,
) -> torch.Tensor
Computes the Reverse KL divergence loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
student_outputs
|
Tensor
|
Raw unnormalized logits from the student model (Shape: [B, C] or [B, S, C]). |
required |
teacher_outputs
|
Tensor
|
Raw unnormalized logits from the teacher model (Shape: [B, C] or [B, S, C]). |
required |
labels
|
Tensor | None
|
Ground-truth labels (ignored, kept for API compatibility). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Scalar loss value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If shapes of |
Source code in src/shrinkai/distillation/losses/logits.py
Functions:
expects_logits
Decorator to automatically extract logits from FeatureExtractor tuples.