Add Primus knowledge-distillation support (v1.2.4)#7
Merged
Conversation
Adds support for distilling upstream nnunetv2 Primus transformer teachers (sizes
S/B/M/L) into smaller Primus students. Mirrors the existing standard/ResEnc
distillation entry points and reuses the multi-teacher KL pipeline.
Changes:
- distillation/primus_distillation_trainer.py: new module exposing
reduce_primus_dims, LitePrimusStudent, nnUNetDistillationPrimusTrainer, and
nnUNetDistillationPrimusTrainerDA5. The student keeps head_dim constant
(Primus' 3D rotary positional embedding needs head_dim divisible by 6) and
shrinks num_heads/depth by the reduction factor; embed_dim follows. The
trainer reuses the parent KL pipeline, disables deep supervision (Primus
is single-resolution), and swaps in AdamW + linear-warmup -> polynomial
schedule that mirrors upstream AbstractPrimus.
- distillation/fast_nnunet_primus_distillation_train.py: new training entry
point with -ts/--teacher_size {S,B,M,L} and the usual -tf / --use_da5 /
-rotate_folds / etc. Teacher folder auto-derived from teacher_size.
- distillation/fast_nnunet_primus_distillation_export_onnx.py: new ONNX
export entry point. Single-tensor output (no deep-supervision wrapping),
3D-only, opset 17, dynamic batch+spatial axes by default.
- setup.py: bump 1.2.3 -> 1.2.4, register the two new console_scripts and
the three new py_modules.
- docs/Distillation.md: new Primus sections covering teacher prep, training
command examples, and ONNX export.
Tested locally on macOS CPU: 11/11 smoke tests pass, including LitePrimusStudent
forward+backward at multiple sizes and a Primus ONNX round-trip. Full GPU
end-to-end training + ONNX export against real upstream Primus checkpoints
needs to be run on the GPU box.
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds end-to-end distillation support for the upstream nnunetv2 Primus transformer family (sizes S/B/M/L). Same multi-teacher KL pipeline as the standard / ResEnc distillation entry points, with a Primus student instead of a CNN U-Net student.
What's new
New module:
distillation/primus_distillation_trainer.pyreduce_primus_dims(embed_dim, depth, num_heads, factor)— shrinks Primus hyperparameters while preserving the rotary-positional-embedding constraint. The 3D RoPE in Primus needshead_dim % 6 == 0, so the helper holdshead_dimconstant at the teacher's value and shrinksnum_heads/depthby the requested factor;embed_dimfollows.LitePrimusStudent— thin wrapper arounddynamic_network_architectures.architectures.primus.Primusthat accepts the reduced (embed_dim, depth, num_heads) and assertspatch_size % 8 == 0.nnUNetDistillationPrimusTrainer/nnUNetDistillationPrimusTrainerDA5— inherit the KL/multi-teacher/fold-rotation pipeline fromnnUNetDistillationTrainer, overridebuild_network_architectureto return a Primus student, disable deep supervision (Primus is single-resolution), and use AdamW + linear-warmup → polynomial schedule that mirrors upstreamAbstractPrimus.Entry-point scripts
nnUNetv2_primus_distillation_train(fast_nnunet_primus_distillation_train.py) — mirrors the resenc training script and adds-ts/--teacher_size {S,B,M,L}plus-w/--warmup_epochs. Default teacher folder auto-derived from-ts:{nnUNet_results}/{Dataset}/nnUNet_Primus_{S|B|M|L}_Trainer__nnUNetPlans__{configuration}/nnUNetv2_primus_distillation_export_onnx(fast_nnunet_primus_distillation_export_onnx.py) — single-tensor output (no deep-supervision wrapping), 3D-only, opset 17, dynamic batch + spatial axes by default. Reconstructs the student architecture from-ts+-rso weights load.Packaging
setup.py: bump1.2.3→1.2.4, register the two newconsole_scripts(nnUNetv2_primus_distillation_train,nnUNetv2_primus_distillation_export_onnx) and the three newpy_modules.Docs
docs/Distillation.md: new Primus training section (teacher prep, training command examples for all sizes, multi-teacher ensemble, DA5) and a Primus ONNX export section.Teacher → student size table (with default
-r 2)head_dimis preserved in every case (66 for S/B/L, 72 for M).Test plan
CPU smoke tests (local-only, not committed) — 11/11 pass on macOS:
LiteNNUNetStudentforward / backwardLiteResEncStudentforward / backwarddistillation_loss_fnfinite + differentiablereduce_primus_dimspreserves the divisibility invariants across all (S/B/M/L) × (r=1,2,4)LitePrimusStudent(M, r=2) forward + backward; shape(1, 3, 32, 32, 32), ~18.6M paramsLitePrimusStudentrejectspatch_sizenot divisible by 8Still needed on the GPU box before tagging:
nnUNetv2_train DATASET_ID 3d_fullres 0 -tr nnUNet_Primus_M_Trainer) and a few distillation steps withnnUNetv2_primus_distillation_train -d DATASET_ID -ts M -r 2.nnUNetv2_primus_distillation_train --use_da5to exercise the DA5 path.--simplify.Follow-up
After merge: tag
v1.2.4+ release. The refactor branch (PR #4) will then rebase onto the newmainso the Primus support ships in v1.3.0 too.