WeightDecayExclusion#
- class WeightDecayExclusion(exclude_bias_from_wd=True, exclude_norm_from_wd=True, exclude_gamma_from_wd=True, exclude_token_from_wd=True, exclude_ndim_1_from_wd=True)[source]#
Bases:
HookBaseRewrite optimizer param groups to exclude selected params from weight decay.
Runs once in
before_train: walks the optimizer’s existing parameter groups (preserving any layer-wise learning rates) and splits each into a decay group and a no-decay group based on parameter name/shape. Excluded parameters getweight_decay=0.0and are flaggedapply_wd=False; kept parameters are flaggedapply_wd=True. These flags are honoured byWeightDecayScheduler. Logs the resulting group counts. Registered asWeightDecayExclusion.- Parameters:
exclude_bias_from_wd (bool) – Exclude parameters whose name ends in
.bias. Defaults toTrue.exclude_norm_from_wd (bool) – Exclude parameters with
"norm"in their name. Defaults toTrue.exclude_gamma_from_wd (bool) – Exclude parameters with
"gamma"in their name. Defaults toTrue.exclude_token_from_wd (bool) – Exclude parameters with
"token"in their name. Defaults toTrue.exclude_ndim_1_from_wd (bool) – Exclude any 1-D parameter (biases, norm/scale vectors, etc.). Defaults to
True.
Note
Mutates
trainer.optimizer.param_groupsin place before training. Pair withWeightDecaySchedulerto schedule weight decay only on the kept (apply_wd=True) groups; place this hook before the scheduler.Example
Add to
cfg.hooksbefore the weight-decay scheduler; once inbefore_trainit rewrites the optimizer’s parameter groups:hooks = [ dict(type="WeightDecayExclusion"), dict(type="WeightDecayScheduler", base_value=0.04, final_value=0.2), ] # → splits each optimizer param group into an apply_wd=True group and an # apply_wd=False group (weight_decay=0.0) holding biases/norm/gamma/ # token/1-D params; logs the resulting with/without-wd group counts