MCPcopy Create free account
hub / github.com/apple/ml-sharp / freeze_norm_layer

Function freeze_norm_layer

src/sharp/utils/module_surgery.py:19–32  ·  view source on GitHub ↗

Freeze all normalization layers.

(module: nn.Module)

Source from the content-addressed store, hash-verified

17
18
19def freeze_norm_layer(module: nn.Module) -> nn.Module:
20 """Freeze all normalization layers."""
21
22 def set_module_eval_mode(module: nn.Module, _: Any) -> None:
23 module.eval()
24
25 for submodule in module.modules():
26 if isinstance(submodule, NORM_LAYER_TYPES):
27 submodule.requires_grad_(False)
28 # This is to ensure that batch norm layers are always called
29 # with the precomputed running statistics.
30 submodule.register_forward_pre_hook(set_module_eval_mode)
31
32 return module

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected