Skip to content

Drop vendored nnunetv2; depend on upstream pip package (v1.3.0)#4

Open
77even wants to merge 3 commits into
mainfrom
refactor/drop-vendored-nnunetv2
Open

Drop vendored nnunetv2; depend on upstream pip package (v1.3.0)#4
77even wants to merge 3 commits into
mainfrom
refactor/drop-vendored-nnunetv2

Conversation

@77even
Copy link
Copy Markdown
Owner

@77even 77even commented May 11, 2026

Summary

BREAKING CHANGE — drops the vendored distillation/nnunetv2/ tree (~190 files, 1.7 MB) and switches to a plain pip dependency on nnunetv2>=2.5,<3.0. Bumps the package version to v1.3.0.

  • The vendored snapshot had drifted from upstream and there was no automated way to refresh it.
  • We had only ever authored one file inside that tree (nnUNetDistillationTrainer.py); the other ~190 files were stale upstream code.
  • That single file is now distillation/nnunet_distillation_trainer.py (top-level, next to the other scripts).
  • The sys.path.insert hack that forced the vendored copy to win over pip has been removed from all entry-point scripts.

Rebased onto main (post-v1.2.4)

This branch has been rebased onto main after v1.2.3 (PR #6) and v1.2.4 (PR #7) landed. v1.3.0 therefore includes everything from both:

  • From v1.2.32d, 3d_lowres, and 3d_cascade_fullres configurations work end-to-end (was hardcoded 3D-only before). ONNX export adapts dummy input shape and dynamic axes per configuration dim. Output filenames include the configuration name so different configs don't overwrite each other.
  • From v1.2.4 — Primus knowledge-distillation support for upstream nnUNet_Primus_{S,B,M,L}_Trainer teachers. New nnUNetv2_primus_distillation_train and nnUNetv2_primus_distillation_export_onnx entry points; new primus_distillation_trainer module.

After rebase, primus_distillation_trainer.py's import of the distillation trainer was updated to point at the unvendored top-level module (see the third commit on this branch).

Compatibility fix surfaced by the refactor

nnunetv2 2.7 renamed nnUNetLogger -> LocalLogger. The trainer's import and instantiation have been updated. (This drift was previously hidden by the vendored copy.)

ONNX export prerequisites

PyTorch >= 2.6 changed its ONNX exporter to require onnxscript. Addressed by:

  • setup.py: added extras_require={'onnx': ['onnx', 'onnxruntime', 'onnxscript', 'onnx-simplifier']}
  • docs/Distillation.md: prerequisites callout before the ONNX export section

Install paths after this PR:

# Training only
pip install -e .

# Training + ONNX export (recommended)
pip install -e '.[onnx]'    # quotes needed for zsh

Commits on this branch

  1. Drop vendored nnunetv2; depend on upstream pip package (v1.3.0)
  2. Document ONNX prerequisites and add [onnx] extras_require
  3. Update Primus trainer imports for the unvendored layout

Test plan

  • python -m py_compile passes on all touched files
  • After pip install -e '.[onnx]' in distillation/, all six console entry points run --help against pip-installed nnunetv2 2.7.0 (the three existing ones plus the two new Primus ones)
  • nnunet_distillation_trainer exposes nnUNetDistillationTrainer, nnUNetDistillationTrainerDA5, LiteNNUNetStudent, LiteResEncStudent
  • primus_distillation_trainer exposes nnUNetDistillationPrimusTrainer, nnUNetDistillationPrimusTrainerDA5, LitePrimusStudent, reduce_primus_dims
  • CPU smoke test (local-only, not committed) — 11/11 pass: 3D/2D forward+backward for both U-Net student types, cascade-shaped input, distillation loss, 2D ONNX round-trip, Primus reduce-dim invariants, Primus forward/backward, Primus patch_size validation, Primus ONNX round-trip
  • TODO before merging: GPU end-to-end on a real dataset for each configuration / teacher type:
    • U-Net student: 3d_fullres, 2d, 3d_cascade_fullres. Cascade requires upstream nnUNetv2_train ... 3d_lowres + nnUNetv2_predict --save_probabilities so predicted_next_stage/ exists.
    • Primus student: at least one of -ts S / M / L with -r 2. Primus teacher must be trained first with stock nnunetv2 (nnUNetv2_train DATASET_ID 3d_fullres 0 -tr nnUNet_Primus_M_Trainer).
  • TODO before merging: ONNX export end-to-end against a real trained checkpoint (U-Net and Primus), including the --simplify path.

Upgrade notes for users

git pull
pip install -U -e distillation/                # core only
pip install -U -e 'distillation/[onnx]'        # with ONNX export

Existing distilled checkpoints continue to load; the trainer/student APIs are unchanged from v1.2.4.

@77even 77even added refactor Code restructuring without functional changes breaking-change Change that breaks backward compatibility labels May 11, 2026
@77even 77even force-pushed the refactor/drop-vendored-nnunetv2 branch from d22e479 to a8575e6 Compare May 11, 2026 16:16
@77even 77even force-pushed the refactor/drop-vendored-nnunetv2 branch from a8575e6 to edc76b8 Compare May 12, 2026 16:29
@77even 77even force-pushed the refactor/drop-vendored-nnunetv2 branch from edc76b8 to 747c902 Compare May 12, 2026 16:37
77even added 3 commits May 13, 2026 01:02
BREAKING CHANGE: distillation/nnunetv2/ is removed and replaced with a
pip dependency on nnunetv2>=2.5,<3.0. Users must reinstall to pick up
the new layout.

What changed:
- Removed entire distillation/nnunetv2/ vendored tree (~190 files, 1.7 MB).
  The only file we had authored was nnUNetDistillationTrainer.py; the rest
  was a stale snapshot of upstream nnUNet that the repo had to manually
  keep in sync.
- Moved the trainer to distillation/nnunet_distillation_trainer.py at the
  top of the package so it sits next to the entry-point scripts.
- Updated the four entry-point scripts to import the trainer from its new
  location and dropped the sys.path.insert hack that forced the vendored
  copy to win over pip.
- setup.py: pin nnunetv2>=2.5,<3.0, list nnunet_distillation_trainer in
  py_modules, bump version 1.2.1 -> 1.3.0.

Compatibility fix surfaced by the refactor:
- nnunetv2 2.7 renamed nnUNetLogger to LocalLogger. Updated the trainer's
  import and instantiation accordingly. (The vendored copy hid this drift.)

Verified locally:
- python -m py_compile passes on all touched files.
- After `pip install -e .` in distillation/, the four console entry points
  (nnUNetv2_distillation_train, nnUNetv2_resenc_distillation_train,
  nnUNetv2_distillation_export_onnx, nnUNetv2_resenc_distillation_export_onnx)
  all run --help successfully against pip-installed nnunetv2 2.7.0.
- nnunet_distillation_trainer exposes nnUNetDistillationTrainer,
  nnUNetDistillationTrainerDA5, LiteNNUNetStudent, LiteResEncStudent.

Not yet verified (requires GPU + dataset):
- An end-to-end training step. The autocast/Logger changes need a real
  training run before this is shipped.
torch>=2.6 changed its ONNX exporter to require onnxscript, so users
hitting the export scripts on a fresh install were getting
ModuleNotFoundError. This adds:

- setup.py: extras_require={'onnx': [onnx, onnxruntime, onnxscript, onnx-simplifier]}
  so users can pip install nnunetv2_distillation[onnx].
- docs/Distillation.md: a Prerequisites callout before the ONNX export
  section listing both the extras install and the manual pip command.
After the rebase, primus_distillation_trainer.py still referenced
nnunetv2.training.nnUNetTrainer.variants.nnUNetDistillationTrainer (the
vendored path on main), which no longer exists on this branch. Switch the
import to the top-level nnunet_distillation_trainer module that the
"Drop vendored nnunetv2" commit introduced.
@77even 77even force-pushed the refactor/drop-vendored-nnunetv2 branch from 747c902 to 72eb6b1 Compare May 12, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change Change that breaks backward compatibility refactor Code restructuring without functional changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant