HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/panomity.de/sharp/src/sharp/utils/module_surgery.py
"""Contains utility functionality to modify torch modules.

For licensing see accompanying LICENSE file.
Copyright (C) 2025 Apple Inc. All Rights Reserved.
"""

from __future__ import annotations

from typing import Any

from torch import nn

NORM_LAYER_TYPES = tuple(module_type for name, module_type in nn.__dict__.items() if "Norm" in name)
BATCH_NORM_LAYER_TYPES = tuple(
    module_type for name, module_type in nn.__dict__.items() if "BatchNorm" in name
)


def freeze_norm_layer(module: nn.Module) -> nn.Module:
    """Freeze all normalization layers."""

    def set_module_eval_mode(module: nn.Module, _: Any) -> None:
        module.eval()

    for submodule in module.modules():
        if isinstance(submodule, NORM_LAYER_TYPES):
            submodule.requires_grad_(False)
            # This is to ensure that batch norm layers are always called
            # with the precomputed running statistics.
            submodule.register_forward_pre_hook(set_module_eval_mode)

    return module