Parametric Distiller#
distiller
#
| CLASS | DESCRIPTION |
|---|---|
Distiller |
Universal knowledge distillation framework for |
Classes#
Distiller(model: dict[str, ModelType], loss_fn: str = 'distillation', loss_kwargs: dict | None = None, optimizer_fn: type[torch.optim.Optimizer] | str | None = None, optimizer_kwargs: dict | None = None, lr_scheduler_fn: Any | str | None = None, lr_scheduler_kwargs: dict | None = None, device: Literal['cuda', 'cpu'] = 'cuda')
#
Bases: Parametric
Universal knowledge distillation framework for Parametric methods.
Automatically adapts to any teacher model type and uses appropriate distillation strategies to transfer knowledge to smaller student models.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
A dictionary containing 'student' and 'teacher' keys.
TYPE:
|
loss_fn
|
Distillation loss function. Options include:
TYPE:
|
loss_kwargs
|
Additional keyword arguments for the loss function. See distillation losses for available options.
TYPE:
|
optimizer_fn
|
Optimizer specification for training.
TYPE:
|
optimizer_kwargs
|
Keyword arguments for optimizer instantiation
(e.g.,
TYPE:
|
lr_scheduler_fn
|
Learning rate scheduler specification.
TYPE:
|
lr_scheduler_kwargs
|
Keyword arguments for scheduler instantiation.
TYPE:
|
device
|
Device to run the distillation on.
TYPE:
|
Examples:
Basic distillation:
>>> import torch
>>> from spectre.core import Model
>>> from spectre.parametric import Distiller
>>> # Create teacher and student models
>>> teacher = Model(in_features=20, out_features=10, multipliers=[2.0, 2.0])
>>> student = teacher.compress(ratio=0.5, preserve_io=True)
>>>
>>> distiller = Distiller(
... model={"teacher": teacher, "student": student},
... loss_fn="distillation",
... loss_kwargs={"temperature": 4.0, "alpha": 0.7},
... )
Distillation with temperature and alpha annealing:
>>> from spectre.utils.callbacks import ParameterAnnealingCallback
>>> distiller = Distiller(
... model={"teacher": teacher, "student": student},
... loss_fn="distillation",
... loss_kwargs={"temperature": 4.0, "alpha": 0.7},
... )
>>> # Create separate callbacks for temperature and alpha annealing
>>> temp_callback = ParameterAnnealingCallback(
... target_attr="temperature",
... start_temperature=4.0,
... end_temperature=1.0,
... schedule="linear",
... )
>>> alpha_callback = ParameterAnnealingCallback(
... target_attr="alpha",
... start_temperature=0.7,
... end_temperature=0.3,
... schedule="linear",
... )
>>> distiller.fit(data, n_epochs=100, callbacks=[temp_callback, alpha_callback])
Spectral preservation with custom kernel:
>>> from spectre.kernel import t_kernel
>>> from spectre.pairwise_distance import pairwise_distance_euclidean
>>> distiller_spectral = Distiller(
... model={"teacher": teacher, "student": student},
... loss_fn="spectral_preservation",
... loss_kwargs={
... "eigenvalue_weight": 1.0,
... "eigengap_weight": 0.5,
... "distance_fn": pairwise_distance_euclidean,
... "kernel_fn": t_kernel,
... "kernel_kwargs": {
... "alpha": torch.tensor(1.0),
... "beta": torch.tensor(3.0),
... },
... },
... )
Notes
Parameter Annealing:
To anneal loss parameters (temperature, alpha) during training, use ParameterAnnealingCallback. This callback supports multiple schedules (linear, exponential, cosine) and can target any loss parameter. Multiple callbacks can be used simultaneously to anneal different parameters independently.
| METHOD | DESCRIPTION |
|---|---|
loss |
Compute distillation loss between student and teacher outputs. |
forward_step |
Forward pass through the student network. |
score_step |
Compute distillation loss for evaluation without training status checks. |
training_step |
Compute distillation loss for a training batch. |
compare_student_teacher |
Compare teacher and student outputs on given input. |
Source code in spectre/parametric/distiller.py
Functions#
loss(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute distillation loss between student and teacher outputs.
Performs forward pass through both student and teacher networks, then computes the selected distillation loss ('distillation', 'feature_matching', or 'spectral_preservation').
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data tensor to process through both networks.
TYPE:
|
weights
|
Sample weights for weighted loss computation.
TYPE:
|
target
|
Optional ground truth targets for supervised distillation. Used in combination with teacher knowledge when available.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar distillation loss value for optimization. |
Source code in spectre/parametric/distiller.py
forward_step(X: torch.Tensor) -> torch.Tensor
#
Forward pass through the student network.
Processes input data through the student model to generate predictions or representations for distillation training.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data to process through the student network.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
torch.Tensor of shape (n_samples, out_features)
|
Student model output. |
Source code in spectre/parametric/distiller.py
score_step(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute distillation loss for evaluation without training status checks.
This method provides direct access to the distillation loss computation
for evaluation purposes, bypassing the is_trained requirement
of the inherited score method.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data to process through student and teacher networks.
TYPE:
|
weights
|
Sample weights for weighted loss computation. If None, uniform weighting is applied.
TYPE:
|
target
|
Ground truth targets for supervised distillation evaluation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar distillation loss value. |
Source code in spectre/parametric/distiller.py
training_step(batch: DataBatch, batch_idx: int) -> torch.Tensor
#
Compute distillation loss for a training batch.
| PARAMETER | DESCRIPTION |
|---|---|
batch
|
Batch of data containing 'data', 'weights', and optionally 'target' attributes.
TYPE:
|
batch_idx
|
Batch index.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar distillation loss value for backpropagation. |
Source code in spectre/parametric/distiller.py
compare_student_teacher(X: torch.Tensor) -> dict[str, torch.Tensor]
#
Compare teacher and student outputs on given input.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input data for comparison.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Dictionary containing teacher and student outputs. |