kempnerforge.training.freeze¶
Parameter freezing helpers.
freeze_params is the primitive: toggle requires_grad on parameters
whose fully-qualified name matches any of the provided fnmatch patterns.
apply_freeze_specs consumes a list of FreezeSpec entries (e.g. from
VLMConfig.freeze) and resolves each spec’s module field against a
pattern map (typically DEFAULT_MODULE_PATTERNS or an arch-specific
override on the config). A raw fnmatch pattern is passed through unchanged
when it is not a known alias.
canonical_freeze_meta produces a stable, reorder-invariant
serialization of a freeze-spec list for checkpoint metadata, so a
checkpoint saved with [A, B] matches one loaded with [B, A] as
long as the effective mask is identical.
effective_freeze resolves the active freeze-spec list at a given
training step from a base list (always-on) and a list of
FreezeStage step-boundary transitions. Used at save (records the
post-transition state in metadata), at load (computes the expected
metadata for the compare), and at the training-loop hook site
(applies stage transitions when step reaches them).
Functions
|
Apply a list of freeze specs to |
|
Return a sorted, deduplicated serialization of freeze specs. |
|
Compute the active freeze-spec list at |
|
Toggle |
- kempnerforge.training.freeze.freeze_params(model, patterns, *, frozen=True)[source]¶
Toggle
requires_gradon parameters matching any fnmatch pattern.Only parameters whose current state differs from the target are flipped, so calling this twice with the same arguments is idempotent. Returns the number of elements (
param.numel()summed) that were actually flipped.- Parameters:
model (torch.nn.Module)
frozen (bool)
- Return type:
- kempnerforge.training.freeze.apply_freeze_specs(model, specs, pattern_map)[source]¶
Apply a list of freeze specs to
model.For each spec, the
modulefield is looked up inpattern_map; if present, its pattern list is used. Otherwisemoduleis treated as a raw fnmatch pattern. Returns{spec.module: n_params_flipped}.- Parameters:
model (torch.nn.Module)
specs (Iterable[FreezeSpec])
- Return type:
- kempnerforge.training.freeze.canonical_freeze_meta(specs)[source]¶
Return a sorted, deduplicated serialization of freeze specs.
The output is safe to JSON-encode and compare across runs: two semantically equivalent freeze-spec lists (same
(module, frozen)pairs, any order or duplicates) produce byte-equal JSON.
- kempnerforge.training.freeze.effective_freeze(step, base, schedule, valid_modules=None)[source]¶
Compute the active freeze-spec list at
step.Resolution rule:
Start from
base(build-time freeze list).For each
FreezeStagewithstart_step <= step, in ascendingstart_steporder, overridebaseentries on conflictingmodulekeys (last-write-wins). Stages withstart_step > stepare ignored.
Module-key validation:
When
valid_modulesis provided, everyspec.modulereferenced inbaseor in any applied stage must appear in the set; otherwiseValueError. This catches typos at load time rather than at the next step boundary.
Returns the list of active specs (one per module key).
- Parameters:
step (int)
base (Iterable[FreezeSpec])
schedule (Iterable[FreezeStage])
- Return type: