Repository navigation
feat(regularize): GroupRegularizeCallback for sparse training before a structured prune - #99
Merged
Merged
Conversation
…ai fit before_step calls pruner.regularize(), ordered between GradientAccumulation and MixedPrecision, so the penalty is added once per optimizer step and multiplied by the loss scale of an active torch.amp.GradScaler. An optional Schedule ramps it and is copied, leaving the shared module-level schedules untouched. Pruning stays a separate call after the fit, followed by learn.fit(n, reset_opt=True). A Pruner with reg=0, a Pruner built on another model, NonNativeMixedPrecision and a PruneCallback in the same fit are refused; PruneCallback refuses reg and points to the new callback.
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.
Adds
GroupRegularizeCallback, the training-loop companion ofPruner.regularize(#96). It implements the sparse-training protocol of Network Slimming / DepGraph: regularize during a fit, prune once after it, then fine-tune.Mechanism
MixedPrecision.order - 2:GradientAccumulation, which cancels the micro-batches, so the penalty is applied once per real optimizer step;MixedPrecisionunscales the gradients, so the penalty is multiplied by the loss scale of a realGradScaler(1 otherwise).Scheduleramps the penalty. The callback keeps its own copy, so the module-level schedule objects other callbacks rely on are never mutated.Prunerbuilt withreg == 0;Prunerbuilt on another model thanlearn.model;NonNativeMixedPrecision;PruneCallbackin the same fit (mid-fit pruning would invalidate the penalized groups).PruneCallbacknow refusesreg: before, it was forwarded to thePrunerwithout ever being applied.regularize/, next toRegularizeCallback. TheRegularizeCallbackBatchNorm caveat and thePruner"Group sparse learning" section both point to it.Evidence
nbs/tests/test_group_regularize_callback.ipynb:GradientAccumulationapplies the penalty once per step;prune_model(), thenfit(reset_opt=True): the optimizer holds exactly the live parameters, while withoutreset_optit holds stale ones;GradientAccumulationandMixedPrecision;#| slowResNet-18 run ending inlearn.save.nbdev-testand the slow tests pass in a clean clone with CI's nbdev 3.3.21. Clean and export leave the tree unchanged. No circular import betweenpruneandregularize.