nemo_automodel.recipes.vlm.kd
nemo_automodel.recipes.vlm.kd
Knowledge Distillation recipe for Vision-Language Models with NeMo AutoModel.
This recipe fine-tunes a student VLM using the logits of a frozen teacher VLM. It
extends FinetuneRecipeForVLM adding:
- teacher_model — an additional VLM loaded in
evalmode - kd_loss_fn — KL-divergence between temperature-scaled distributions
- kd_ratio — linear mix between CE loss and KD loss
The training loop preserves all VLM-specific input handling (pixel_values, image_grid_thw, etc.) and passes multimodal inputs to both teacher and student models.
The loss becomes: loss = (1-kd_ratio) * ce_loss + kd_ratio * kd_loss
Pipeline parallelism is not supported in this recipe.
The file exposes KnowledgeDistillationRecipeForVLM and a main entry-point
so it can be launched exactly the same way as other recipes:
python -m torch.distributed.run —nproc-per-node=8
nemo_automodel/recipes/vlm/kd.py
-c examples/vlm_kd/qwen3_5/qwen3_5_vl_4b_kd.yaml
Module Contents
Classes
Functions
Data
API
Bases: FinetuneRecipeForVLM
Fine-tune a student VLM via knowledge distillation from a teacher VLM.
Emit metadata for the teacher as well as the student.
Configure the teacher after construction and check its mask contract.
Run one student VLM microbatch with KD.
Teacher and student logits have global shape
[batch, sequence, vocab] and may use local TP/CP layouts inside the
step.
Parameters:
Zero-based accumulation microbatch index.
Mapping containing text tensors of shape
[batch, sequence]. Multimodal tensor leaves may have
arbitrary rank and axis order.
Output list receiving one detached scalar tensor.
Valid-label count across the optimizer step.
Number of accumulation microbatches in the step.
Whether to run backward.
Request teacher logits for one student VLM batch.
Parameters:
Mapping containing input_ids and labels as tensors of
shape [batch, sequence]. Multimodal tensor leaves may have
arbitrary rank and axis order and are transported unchanged.
Returns: torch.Tensor
Replicated tensor of shape [batch, sequence, vocab] containing
Serve teacher forwards until the student mesh broadcasts stop.
Execute a single training step with KD loss tracking.
Run one validation pass with KD loss computation.
Run one teacher VLM batch and materialize full logits.
Parameters:
Mapping containing input_ids and labels as tensors of
shape [batch, sequence]. Multimodal tensor leaves may have
arbitrary rank and axis order; their model processor owns those
layouts.
Returns: torch.Tensor
Detached tensor of shape [batch, sequence, vocab] containing
Run the student loop or serve teacher forwards on a separate mesh.
Build student & teacher, dataloaders, optimizers, etc.
Build and initialize the teacher VLM for knowledge distillation.
Uses the same build_model as the student but without PEFT, FP8, or QAT
since the teacher should be frozen in full precision.
Parameters:
Configuration for teacher model instantiation.
Freeze configuration for the teacher model.
Random seed for reproducibility.
Resolved distributed topology and policy object.
Device to place the teacher model on.
Returns: torch.nn.Module
The frozen teacher model ready for inference.
Run the VLM KD recipe from CLI or directly.