• 文档 >
  • 模块代码 >
  • torch >
  • torch.ao.nn.quantized.modules.batchnorm
快捷键

torch.ao.nn.quantized.modules.batchnorm 的源代码

# mypy: 允许未类型化定义
导入 火炬
导入 torch.ao.nn.intrinsic 作为 nni


__all__ = [BatchNorm2d, 批标准化 3D]


 _批标准化(火炬.nn.模块.批标准化._批标准化):
    def 初始化(
        自身, 特征数量, eps=0.00001, 动量=0.1, 设备=, 数据类型=None
    ) -> :
        工厂参数 = {"设备": 设备, "dtype": 数据类型}
        超级().初始化(特征数量, eps, 动量, True, True, **工厂参数)
        自身.注册缓冲区("缩放", 火炬.张量(1.0, **工厂参数))
        自身.注册缓冲区(零点, 火炬.张量(0, **工厂参数))

    @staticmethod
    def 从浮点数(, 模块, 使用预计算的假量化=错误):
        activation_post_process = 模块.activation_post_process
        如果 类型(模块) == ._NNI_BN_RELU 模块:
            修饰 = 模块[0]
        比例, 零点 = 激活后处理.计算 q 参数()
        new_mod = (模块.特征数量, 模块.eps)
        新模块.权重 = 模块.权重
        新模块.bias = 模块.bias
        新模块.运行均值 = 模块.运行均值
        新模块.运行变量 = 模块.运行变量
        新模块.缩放 = 缩放
        新模块.零点 = 零点
        返回 new_mod

    @classmethod
    def 来自参考(, bn, 输出尺度, 输出零点):
        qbn = (
            bn.特征数量,
            bn.eps,
            bn.动量,
            设备=班纳.重量.设备,
            数据类型=班纳.重量.数据类型,
        )
        贵班纳.权重 = 班纳.权重
        qbn.bias = bn.bias
        qbn.运行平均值 = 孟加拉语.运行均值
        q 孟加拉语.运行方差 = 孟加拉语.运行变量
        q 孟加拉语.缩放 = 输出尺度
        qbn.零点 = 输出零点
        返回 qbn


[文档]类 BatchNorm2d(_BatchNorm): 这是 `torch.nn.BatchNorm2d` 的量化版本。 _NNI_BN_RELU_MODULE = nni.BNReLU2d def __init__( self, num_features, eps=1e-5, momentum=0.1, device=None, dtype=None ) -> None: factory_kwargs = {"device": device, "dtype": dtype} super().__init__(num_features, eps, momentum, **factory_kwargs) def _get_name(self): 返回 "QuantizedBatchNorm2d" def _check_input_dim(self, input): # 临时使用 len(shape) 而不是 ndim 以解决 JIT 问题 # https://github.com/pytorch/pytorch/issues/23890 如果输入形状的长度不等于 4: 抛出 ValueError 异常("输入形状必须是 `(N, C, H, W)`!") def forward(self, input: torch.Tensor) -> torch.Tensor: # 禁用此行,因为这个不是符号可追踪的 # self._检查输入维度(input) 返回 torch.ops.quantized.batch_norm2d( 输入 self.weight, self.bias, self.running_mean, self.running_var, self.eps, self.scale, self.zero_point, ) @classmethod def from_float(cls, mod, use_precomputed_fake_quant=False): return _BatchNorm.from_float( cls, mod, use_precomputed_fake_quant=use_precomputed_fake_quant )
[文档]class BatchNorm3d(_BatchNorm): r"""这是 :class:`~torch.nn.BatchNorm3d` 的量化版本。""" _NNI_BN_RELU_MODULE = nni.BNReLU3d def __init__(self, num_features, eps=1e-5, momentum=0.1, device=None, dtype=None): factory_kwargs = {"device": device, "dtype": dtype} super().__init__(num_features, eps, momentum, **factory_kwargs) def _get_name(self): return "QuantizedBatchNorm3d" def _check_input_dim(self, input): # 临时使用 len(shape) 而不是 ndim 以解决 JIT 问题 # https://github.com/pytorch/pytorch/issues/23890 if len(input.shape) != 5: 必须输入形状为 `(N, C, H, W)` 的数据! def forward(self, input: torch.Tensor) -> torch.Tensor: # 禁用此行,因为这不可符号追踪 # self._check_input_dim(input) return torch.ops.quantized.batch_norm3d( input, self.weight, self.bias, self.running_mean, self.running_var, self.eps, self.scale, self.zero_point, ) @classmethod def from_float(cls, mod, use_precomputed_fake_quant=False): return _BatchNorm.from_float( 类别,模块,使用预计算的假量化=使用预计算的假量化 )

© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源,获取您的疑问解答

查看资源