nvalchemi.training.hooks.TrainableParameterHook#
- pydantic model nvalchemi.training.hooks.TrainableParameterHook[source]#
Select which parameters stay trainable during fine-tuning.
On registration with a
TrainingStrategy,freeze_patternsandtrainable_patterns(globs over fully-qualified parameter names), together with parameters previously registered as trainable by earlier hooks, resolve to the trainable set. What you set decides the behaviour:trainable_patternsonly — train exactly those, plus any parameters previously registered as trainable; freeze the rest,freeze_patternsonly — freeze those; train the rest, including any parameters previously registered as trainable.both — freeze the
freeze_patternsset, buttrainable_patternswin: a parameter they match stays trainable even if a freeze pattern also matches it.
Frozen parameters are temporarily marked
requires_grad=Falseduringrunand restored afterward. Setfreeze_mode="optimizer_only"to instead keep them out of the optimizer while preserving their gradients.- Raises:
ValueError – If no trainable parameters are selected, or if any pattern matches no parameter.
- Warns:
UserWarning – If registered after optimizers already exist. The stored filter is updated, but existing optimizer parameter groups are not rebuilt.
Examples
>>> from nvalchemi.training.hooks import TrainableParameterHook >>> TrainableParameterHook( ... freeze_patterns=("main.model.*",), ... trainable_patterns=("main.model.projection.*",), ... ).frequency 1
- field freeze_patterns: tuple[str, ...] = ()#
Glob patterns for parameters to freeze. Overridden by
trainable_patterns— a parameter matching both stays trainable.
- field trainable_patterns: tuple[str, ...] = ()#
Glob patterns for parameters to keep trainable. When supplied without
freeze_patterns, they define the full trainable set, along with the trainable parameters registered by earlier hooks, e.g.,ModulePatchHook. Everything else is frozen.
- field freeze_mode: Literal['requires_grad', 'optimizer_only'] = 'requires_grad'#
Whether excluded parameters are temporarily frozen via
requires_grad=Falseor only excluded from optimizer construction.