Teachers
Teachers can be trained inside RecDistillery or imported from external
artifacts. In both cases, the runtime representation is a framework-neutral
TeacherState saved as a .teacher artifact.
Teacher Training
Train a native teacher with:
python scripts/teacher_training/teacher_training.py \
--framework recbole \
--model BPRMF \
--dataset citeulike
Complete experiment configs are stored in:
Teacher Import
External teachers are converted through scripts/recdistill/import_teacher.py.
The import system supports generic checkpoints, prediction JSON exports, and
RecBole .pth checkpoints.
Teacher State
TeacherState is the normalized runtime object used after training or import.
It hides framework-specific checkpoint formats and gives distillers a single
interface.
| Object | Represents | Key attributes |
|---|---|---|
TeacherState |
A trained or imported teacher. | user_embeddings, item_embeddings, metadata, scorer |
PrecomputedScoresScorer |
A dense user-item score matrix. | scores |
PrecomputedTopKScorer |
A sparse top-k ranking export. | topk_items, topk_scores, fill_value, num_items_override |
TeacherScorer |
Protocol for scorer-only teachers. | to, score_items_for_user |
TeacherScorer
Bases: Protocol
Protocol implemented by scorer-only teacher representations.
A scorer computes item scores for one user without requiring explicit user/item embedding matrices. Import adapters use it for prediction exports, top-k rankings, or dense score matrices.
Source code in recdistill/teachers/state.py
to(device: torch.device | str)
Source code in recdistill/teachers/state.py
score_items_for_user(user: int, num_items: int) -> torch.Tensor
Source code in recdistill/teachers/state.py
PrecomputedScoresScorer
dataclass
Dense precomputed teacher score matrix.
Attributes:
| Name | Type | Description |
|---|---|---|
scores |
Tensor
|
Tensor with shape |
Source code in recdistill/teachers/state.py
scores: torch.Tensor
instance-attribute
num_users: int
property
num_items: int
property
__init__(scores: torch.Tensor) -> None
__post_init__() -> None
to(device: torch.device | str) -> 'PrecomputedScoresScorer'
score_items_for_user(user: int, num_items: int) -> torch.Tensor
Return the score vector for a user, padded or truncated to num_items.
Source code in recdistill/teachers/state.py
PrecomputedTopKScorer
dataclass
Sparse scorer backed by precomputed ranked items.
Attributes:
| Name | Type | Description |
|---|---|---|
topk_items |
Tensor
|
Integer tensor with shape |
topk_scores |
Tensor | None
|
Optional score tensor aligned with |
fill_value |
float
|
Score assigned to items that are absent from the top-k list. |
num_items_override |
int | None
|
Optional catalog size when it cannot be inferred from the maximum item id. |
Source code in recdistill/teachers/state.py
topk_items: torch.Tensor
instance-attribute
topk_scores: torch.Tensor | None = None
class-attribute
instance-attribute
fill_value: float = float('-inf')
class-attribute
instance-attribute
num_items_override: int | None = None
class-attribute
instance-attribute
num_users: int
property
top_k: int
property
num_items: int
property
__init__(topk_items: torch.Tensor, topk_scores: torch.Tensor | None = None, fill_value: float = float('-inf'), num_items_override: int | None = None) -> None
__post_init__() -> None
Source code in recdistill/teachers/state.py
to(device: torch.device | str) -> 'PrecomputedTopKScorer'
Source code in recdistill/teachers/state.py
score_items_for_user(user: int, num_items: int) -> torch.Tensor
Expand one user's top-k ranking into a full score vector.
Source code in recdistill/teachers/state.py
TeacherState
dataclass
Framework-neutral teacher representation used by distillation.
A teacher can be represented either by user/item embeddings or by a scorer.
The same state object is used for native teachers, imported checkpoints,
prediction JSON files, and serialized .teacher artifacts.
Attributes:
| Name | Type | Description |
|---|---|---|
user_embeddings |
Tensor | None
|
Optional user embedding matrix. |
item_embeddings |
Tensor | None
|
Optional item embedding matrix. |
metadata |
dict[str, object]
|
Free-form provenance and mapping information. |
scorer |
TeacherScorer | None
|
Optional scorer-only representation. |
Source code in recdistill/teachers/state.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
user_embeddings: torch.Tensor | None = None
class-attribute
instance-attribute
item_embeddings: torch.Tensor | None = None
class-attribute
instance-attribute
metadata: dict[str, object] = field(default_factory=dict)
class-attribute
instance-attribute
scorer: TeacherScorer | None = None
class-attribute
instance-attribute
num_users: int
property
num_items: int
property
embedding_dim: int
property
device: torch.device
property
has_embeddings: bool
property
__init__(user_embeddings: torch.Tensor | None = None, item_embeddings: torch.Tensor | None = None, metadata: dict[str, object] = dict(), scorer: TeacherScorer | None = None) -> None
__post_init__() -> None
Source code in recdistill/teachers/state.py
to(device: torch.device | str) -> 'TeacherState'
Return a copy of the teacher state moved to device.
Source code in recdistill/teachers/state.py
Teacher Sources
TeacherSource describes where an external teacher comes from and which hints
the adapter registry can use during import.
| Attribute | Meaning |
|---|---|
path |
Main artifact path, such as .teacher, .pth, .pt, .ckpt, or .json. |
framework |
Framework hint used for adapter resolution. |
format |
File/representation hint used for adapter resolution. |
model_name |
Optional model name saved into teacher metadata. |
adapter |
Explicit custom adapter import path. |
metadata |
Dataset, id mapping, provenance, and any additional import metadata. |
TeacherSource
dataclass
Input descriptor consumed by teacher import adapters.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path | None
|
Main checkpoint, prediction, or |
framework |
str
|
Framework hint such as |
format |
str
|
Format hint such as |
model_name |
str | None
|
Optional model/backbone name stored in metadata. |
adapter |
str | None
|
Optional explicit adapter import path. |
user_embeddings_path |
Path | None
|
Optional external user embedding file. |
item_embeddings_path |
Path | None
|
Optional external item embedding file. |
score_matrix_path |
Path | None
|
Optional dense score matrix file. |
topk_items_path |
Path | None
|
Optional top-k item matrix file. |
topk_scores_path |
Path | None
|
Optional top-k score matrix file. |
metadata |
dict[str, Any]
|
Extra provenance, mapping, and dataset information. |
Source code in recdistill/teachers/source.py
path: Path | None = None
class-attribute
instance-attribute
framework: str = 'auto'
class-attribute
instance-attribute
format: str = 'auto'
class-attribute
instance-attribute
model_name: str | None = None
class-attribute
instance-attribute
adapter: str | None = None
class-attribute
instance-attribute
user_embeddings_path: Path | None = None
class-attribute
instance-attribute
item_embeddings_path: Path | None = None
class-attribute
instance-attribute
score_matrix_path: Path | None = None
class-attribute
instance-attribute
topk_items_path: Path | None = None
class-attribute
instance-attribute
topk_scores_path: Path | None = None
class-attribute
instance-attribute
metadata: dict[str, Any] = field(default_factory=dict)
class-attribute
instance-attribute
__init__(path: Path | None = None, framework: str = 'auto', format: str = 'auto', model_name: str | None = None, adapter: str | None = None, user_embeddings_path: Path | None = None, item_embeddings_path: Path | None = None, score_matrix_path: Path | None = None, topk_items_path: Path | None = None, topk_scores_path: Path | None = None, metadata: dict[str, Any] = dict()) -> None
from_path(path: str | Path, *, framework: str = 'auto', format: str = 'auto', model_name: str | None = None, adapter: str | None = None, metadata: dict[str, Any] | None = None) -> 'TeacherSource'
classmethod
Create a source descriptor from one primary artifact path.
Source code in recdistill/teachers/source.py
Loading And Serialization
load_teacher(source: TeacherSource | str | Path, device: torch.device | str | None = None) -> TeacherState
Source code in recdistill/teachers/loaders.py
load_teacher_state(source: TeacherSource | str | Path, device: torch.device | str | None = None) -> TeacherState
Load a teacher state from any registered RecDistill teacher source.
This is the preferred public loader. It accepts either a TeacherSource or a direct artifact path and dispatches through the teacher adapter registry.
Source code in recdistill/teachers/loaders.py
register_default_teacher_adapters() -> None
Source code in recdistill/teachers/loaders.py
TEACHER_FORMAT_VERSION = 'recdistill.teacher.v2'
module-attribute
SUPPORTED_TEACHER_FORMAT_VERSIONS = {'recdistill.teacher.v1', TEACHER_FORMAT_VERSION}
module-attribute
teacher_state_to_payload(state: TeacherState, *, framework: str | None = None, model_name: str | None = None, metadata: dict[str, Any] | None = None) -> dict[str, Any]
Source code in recdistill/teachers/serialization.py
teacher_state_from_payload(payload: dict[str, Any]) -> TeacherState
Source code in recdistill/teachers/serialization.py
save_teacher_state(path: str | Path, state: TeacherState, *, framework: str | None = None, model_name: str | None = None, metadata: dict[str, Any] | None = None) -> dict[str, Any]
Source code in recdistill/teachers/serialization.py
load_teacher_payload(path: str | Path) -> dict[str, Any]
Source code in recdistill/teachers/serialization.py
Adapter Registry
The registry maps teacher sources to import adapters. Callers can register new
adapters, list available keys, resolve the adapter for a source, or load a
TeacherState directly.
| Function | Purpose |
|---|---|
register_teacher_adapter |
Adds an adapter and optional aliases. |
available_teacher_adapters |
Lists registered adapter keys. |
resolve_teacher_adapter |
Selects the adapter that can load a TeacherSource. |
load_teacher_state |
Resolves and loads a TeacherState. |
TeacherAdapter
Bases: Protocol
Protocol implemented by teacher import adapters.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Stable registry key used by |
Source code in recdistill/teachers/registry.py
name: str
instance-attribute
can_load(source: TeacherSource) -> bool
Source code in recdistill/teachers/registry.py
load(source: TeacherSource, device: torch.device | str | None = None) -> TeacherState
Source code in recdistill/teachers/registry.py
register_teacher_adapter(adapter: TeacherAdapter, *aliases: str) -> None
Register an adapter under its primary name and optional aliases.
available_teacher_adapters() -> tuple[str, ...]
resolve_teacher_adapter(source: TeacherSource) -> TeacherAdapter
Choose the adapter able to load source.
Resolution first honors an explicit adapter import path, then exact format or framework hints, and finally asks registered adapters whether they can load the source.
Source code in recdistill/teachers/registry.py
load_teacher_state(source: TeacherSource, device: torch.device | str | None = None) -> TeacherState
Resolve an adapter and load a TeacherState from source.
Source code in recdistill/teachers/registry.py
Import Adapters
Import adapters convert external artifacts into the shared TeacherState
format.
| Adapter | Accepted sources | Output representation |
|---|---|---|
CheckpointAdapter |
.teacher, .pt, .pth, .ckpt payloads. |
Embeddings, dense scores, or top-k scorer. |
PredictionsJsonAdapter |
JSON prediction rows or column-oriented prediction exports. | PrecomputedTopKScorer. |
RecBolePthAdapter |
RecBole .pth checkpoints with embedding tensors. |
Embedding-backed TeacherState. |
CheckpointAdapter
Load generic PyTorch checkpoints into TeacherState.
The adapter accepts serialized .teacher payloads, embedding dictionaries,
dense score matrices, and top-k ranking payloads stored in .pt, .pth,
.ckpt, or .teacher files.
Source code in recdistill/teachers/adapters/checkpoint.py
name = 'checkpoint'
class-attribute
instance-attribute
can_load(source: TeacherSource) -> bool
Return True when the checkpoint payload can form a teacher state.
Source code in recdistill/teachers/adapters/checkpoint.py
load(source: TeacherSource, device: torch.device | str | None = None) -> TeacherState
Load the checkpoint and convert it to TeacherState.
Source code in recdistill/teachers/adapters/checkpoint.py
PredictionsJsonAdapter
Import a teacher from JSON, TSV, or CSV prediction rows.
The prediction payload can be JSON (list of rows or dict with predictions),
or TSV/CSV prediction exports containing user, item, and score/rating.
Rows are converted into a PrecomputedTopKScorer.
Source code in recdistill/teachers/adapters/predictions_json.py
name = 'predictions_json'
class-attribute
instance-attribute
can_load(source: TeacherSource) -> bool
Return True for JSON/TSV/CSV prediction sources or matching format hints.
Source code in recdistill/teachers/adapters/predictions_json.py
load(source: TeacherSource, device: torch.device | str | None = None) -> TeacherState
Read the prediction payload and return a scorer-backed TeacherState.
Source code in recdistill/teachers/adapters/predictions_json.py
RecBolePthAdapter
Import RecBole .pth checkpoints that contain embedding tensors.
The adapter inspects the state dict, finds compatible user/item embedding matrices, and stores the source tensor keys in teacher metadata.
Source code in recdistill/teachers/adapters/recbole_pth.py
name = 'recbole_pth'
class-attribute
instance-attribute
can_load(source: TeacherSource) -> bool
Return True when a .pth checkpoint exposes teacher embeddings.
Source code in recdistill/teachers/adapters/recbole_pth.py
load(source: TeacherSource, device: torch.device | str | None = None) -> TeacherState
Load a RecBole checkpoint and convert its embeddings to TeacherState.