Skip to content

fix(prune): PruneCallback logs the applied ratio and traces on the fit's own batch - #100

Merged
nathanhubens merged 1 commit into
masterfrom
fix/prune-callback-log-inputs
Sep 25, 2026
Merged

nathanhubens merged 1 commit into
masterfrom
fix/prune-callback-log-inputs

Conversation

@nathanhubens

Copy link
Copy Markdown
Collaborator

Two PruneCallback fixes, follow-up of #98.

1. The epoch log printed the previous step's ratio

torch-pruning's step() increments current_step then applies per_step_pruning_ratio[current_step]. After completed_steps steps, the applied level is therefore levels[completed_steps], but after_epoch printed levels[completed_steps - 1]. The last epoch never showed the final target ratio.

  • Now: levels[completed_steps], with the guard completed_steps < len(levels).
  • RED on master, from the new test that cross-checks the printed ratio against torch-pruning's applied level: printed 4.05 % / 41.43 % / 49.87 % against 21.06 % / 46.38 % / 50.00 % applied. GREEN: the last epoch prints 50.00 %.

2. The Pruner traced a fixed 224×224 input

before_fit read dls.one_batch() but never passed it on, so the Pruner traced its default torch.randn(1, 3, 224, 224) instead.

  • Now the fit's own batch is passed as example_inputs. An example_inputs given by the user still wins.
  • RED on master: a model whose head is sized for 8×8 inputs failed in before_fit (mat1 and mat2 shapes cannot be multiplied). GREEN: it traces on the (16, 3, 8, 8) batch, on the model's device, and a user-supplied (2, 3, 8, 8) input is kept.

Tutorial

tutorials/prune/prune_callback was re-executed, since its stored epoch-log lines change. It is a single run with no seed, so every printed number moved. The prose was updated to the printed values and checked by a results review:

  • 4800 → 3349 channels (30.2 %);
  • MACs ratios 0.6335 / 0.6346;
  • parameter ratio 0.18, unchanged.

The review also caught a pre-existing error: the head goes from 512 to 502 units, not from 1024. 1024 is the layer's input width.

Evidence

  • Full suite nbdev-test passes, as do the slow prune tests.
  • Clean and export leave the tree unchanged with CI's nbdev 3.3.21.

…t's own batch

PruneCallback.after_epoch printed levels[k - 1] after k prune steps, while
torch-pruning's step() advances its counter before reading the level, so the
model is at levels[k]; the last epoch now prints the final target.

before_fit read a batch from dls.one_batch() and dropped it, so the Pruner traced
on its default 1x3x224x224 input and failed on models that do not accept it.
The batch is now passed as example_inputs; one given by the user still wins.
@nathanhubens
nathanhubens merged commit 4a845a7 into master Sep 25, 2026
2 checks passed
@nathanhubens
nathanhubens deleted the fix/prune-callback-log-inputs branch September 25, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant