wrappers
shrinkai.distillation.losses.wrappers
Wrapper modules for composing and routing distillation losses.
These classes do not compute mathematical distances themselves. Instead, they orchestrate other loss functions, handle tensor dimension mapping (projection), and combine multiple objectives into a single criterion.
Available Losses
- ProjectedFeatureLoss: Uses FeatureProjector to connect tensors with different dimensions.
- HybridLoss: Combines a logit loss with a feature loss.
- CombinedLoss: Weighted sum of several losses.
Classes:
| Name | Description |
|---|---|
CombinedLoss |
Combines an arbitrary number of distillation losses with specific weights. |
HybridLoss |
Combines a primary logit-based loss and a feature-based loss using a convex combination. |
ProjectedFeatureLoss |
Bridge between FeatureExtractors, FeatureProjectors, and Feature Losses. |
Classes
CombinedLoss
Bases: BaseDistillationLoss
Combines an arbitrary number of distillation losses with specific weights.
This acts as a transparent router. It passes the raw inputs (whether they are tensors or tuples) to each underlying loss.
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the CombinedLoss. |
forward |
Computes the weighted sum of all configured losses. |
Source code in src/shrinkai/distillation/losses/wrappers.py
Methods:
__init__
Initializes the CombinedLoss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weighted_losses
|
list[tuple[BaseDistillationLoss, float]]
|
A list of tuple with instantiated loss modules and their corresponding weights. |
required |
Source code in src/shrinkai/distillation/losses/wrappers.py
forward
forward(
student_outputs: Any,
teacher_outputs: Any,
labels: Tensor | None = None,
) -> torch.Tensor
Computes the weighted sum of all configured losses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
student_outputs
|
Any
|
Tuple of (student_logits, student_features_dict). |
required |
teacher_outputs
|
Any
|
Tuple of (teacher_logits, teacher_features_dict). |
required |
labels
|
Tensor | None
|
Ground-truth labels. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Aggregated scalar loss value. |
Source code in src/shrinkai/distillation/losses/wrappers.py
HybridLoss
Bases: CombinedLoss
Combines a primary logit-based loss and a feature-based loss using a convex combination.
Equation
where \(\lambda = \text{feature_weight}\).
This loss expects the models (or the FeatureExtractor wrapper) to return
a tuple containing (logits, features_dict).
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the HybridLoss. |
Source code in src/shrinkai/distillation/losses/wrappers.py
Methods:
__init__
__init__(
logit_loss: BaseDistillationLoss,
feature_loss: BaseDistillationLoss,
feature_weight: float = 1.0,
convex_weighting: bool = False,
) -> None
Initializes the HybridLoss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logit_loss
|
BaseDistillationLoss
|
Loss applied to the final logits (e.g., a HintonLoss object). |
required |
feature_loss
|
BaseDistillationLoss
|
Loss applied to the intermediate features (e.g., a FeatureLoss object). |
required |
feature_weight
|
float
|
Balancing factor (0.0 <= weight <= 1.0). Defaults to 1. |
1.0
|
convex_weighting
|
bool
|
If True, applies (1-w) to primary loss and (w) to feature loss. If False, strictly adds (w * feature_loss) to primary loss. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/shrinkai/distillation/losses/wrappers.py
ProjectedFeatureLoss
Bases: BaseDistillationLoss
Bridge between FeatureExtractors, FeatureProjectors, and Feature Losses.
When using FeatureExtractor, the model outputs a tuple: (logits, features_dict).
However, feature losses (like FeatureLoss, AttentionMapLoss) expect pure
dictionaries or tensors.
This wrapper seamlessly unpacks the tuples, applies the FeatureProjector to
align the student's feature dimensions with the teacher's, and computes the
underlying feature loss.
Attributes:
| Name | Type | Description |
|---|---|---|
projector |
Module
|
The module responsible for projecting student features. |
feature_loss |
BaseDistillationLoss
|
The loss function to apply to the aligned features. |
project_teacher |
bool
|
If False (default), the projector is applied to the student's features to match the teacher's larger dimensions. If True, the projector is applied to the teacher's features to downscale them (e.g., filtering a 12-head teacher down to match a 2-head student in Transformer attention distillation). |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the ProjectedFeatureLoss. |
forward |
Unpacks outputs, projects features (student or teacher), and computes the loss. |
Source code in src/shrinkai/distillation/losses/wrappers.py
Methods:
__init__
__init__(
projector: Module,
feature_loss: BaseDistillationLoss,
project_teacher: bool = False,
) -> None
Initializes the ProjectedFeatureLoss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projector
|
Module
|
An instantiated FeatureProjector. |
required |
feature_loss
|
BaseDistillationLoss
|
An instantiated feature distillation loss (e.g., FeatureLoss). |
required |
project_teacher
|
bool
|
Either the projector is applied to the teacher or not. |
False
|
Source code in src/shrinkai/distillation/losses/wrappers.py
forward
forward(
student_outputs: tuple[Tensor, dict[str, Tensor]],
teacher_outputs: tuple[Tensor, dict[str, Tensor]],
labels: Tensor | None = None,
) -> torch.Tensor
Unpacks outputs, projects features (student or teacher), and computes the loss.