# mypy: 允许未类型化定义
导入
火炬
来自 torch.nn
导入 (
批标准化 1d,
批标准化 2d,
批标准化 3d,
卷积 1D,
卷积 2D,
卷积 3D,
线性,
ReLU 激活函数,
)
来自 torch.nn.utils.parametrize
导入 type_before_parametrizations
__all__ = [
ConvReLU1d,
ConvReLU2d,
ConvReLU3d,
LinearReLU,
ConvBn1d,
ConvBn2d,
"卷积归一化 ReLU1d",
"卷积归一化 ReLU2d",
"卷积归一化 3d",
"卷积归一化 ReLU3d",
"BNReLU2d",
"BNReLU3d",
"LinearBn1d",
"LinearLeakyReLU",
线性双曲正切,
卷积加 2d,
卷积加 ReLU2d,
]
用于识别量化中使用的内在模块
类
混合模块(
火炬.nn.
顺序的):
通过
[docs]class ConvReLU1d(_FusedModule):
r"""这是一个调用 Conv1d 和 ReLU 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。”
def __init__(self, conv, relu):
assert (
type_before_parametrizations(conv) == Conv1d
and type_before_parametrizations(relu) == ReLU
), f"Incorrect types for input modules{type_before_parametrizations(conv)}{type_before_parametrizations(relu)}"
super().__init__(conv, relu)
[文档]class ConvReLU2d(_FusedModule):
r"""这是一个调用 Conv2d 和 ReLU 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。"""
def __init__(self, conv, relu):
assert (
type_before_parametrizations(conv) == Conv2d
and type_before_parametrizations(relu) == ReLU
), f"输入模块类型错误{type_before_parametrizations(conv)}{type_before_parametrizations(relu)}"
super().__init__(conv, relu)
[文档]class ConvReLU3d(_FusedModule):
r"""这是一个调用 Conv3d 和 ReLU 模块的顺序容器。
在量化过程中,这将被替换为相应的融合模块。"""
def __init__(self, conv, relu):
assert (
type_before_parametrizations(conv) == Conv3d
并且 type_before_parametrizations(relu) == ReLU
), f"输入模块类型错误{type_before_parametrizations(conv)}{type_before_parametrizations(relu)}"
super().__init__(conv, relu)
[文档]class LinearReLU(_FusedModule):
这是一个调用线性模块和 ReLU 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。
def __init__(self, linear, relu):
assert (
type_before_parametrizations(linear) == 线性
and type_before_parametrizations(relu) == ReLU
), f"Incorrect types for input modules{type_before_parametrizations(linear)}{type_before_parametrizations(relu)}"
super().__init__(linear, relu)
[文档]class ConvBn1d(_FusedModule):
r"""这是一个调用 Conv 1d 和 Batch Norm 1d 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。"""
def __init__(self, conv, bn):
assert (
type_before_parametrizations(conv) == Conv1d
and type_before_parametrizations(bn) == BatchNorm1d
), f"Incorrect types for input modules{type_before_parametrizations(conv)}{type_before_parametrizations(bn)}"
super().__init__(conv, bn)
[文档]class ConvBn2d(_FusedModule):
r"""这是一个调用 Conv 2d 和 Batch Norm 2d 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。"""
def __init__(self, conv, bn):
assert (
type_before_parametrizations(conv) == Conv2d
and type_before_parametrizations(bn) == BatchNorm2d
), f"输入模块类型错误{type_before_parametrizations(conv)}{type_before_parametrizations(bn)}"
super().__init__(conv, bn)
[文档]class ConvBnReLU1d(_FusedModule):
r"""这是一个顺序容器,它调用 Conv 1d、Batch Norm 1d 和 ReLU 模块。"""
在量化过程中,这将被替换为相应的融合模块。"""
def __init__(self, conv, bn, relu):
assert (
type_before_parametrizations(conv) == Conv1d
并且 type_before_parametrizations(bn) == BatchNorm1d
并且 type_before_parametrizations(relu) == ReLU
), f"输入模块类型错误{type_before_parametrizations(conv)}{type_before_parametrizations(bn)}{type_before_parametrizations(relu)}" # noqa: B950
super().__init__(conv, bn, relu)
[docs]class ConvBnReLU2d(_FusedModule):
r"""这是一个调用 Conv 2d、Batch Norm 2d 和 ReLU 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。"""
def __init__(self, conv, bn, relu):
assert (
type_before_parametrizations(conv) == Conv2d
and type_before_parametrizations(bn) == BatchNorm2d
and type_before_parametrizations(relu) == ReLU
), f"输入模块类型错误{type_before_parametrizations(conv)}{type_before_parametrizations(bn)}{type_before_parametrizations(relu)}" # noqa: B950
super().__init__(conv, bn, relu)
[文档]class ConvBn3d(_FusedModule):
r"""这是一个调用 Conv 3d 和 Batch Norm 3d 模块的顺序容器。
在量化过程中,这将被替换为相应的融合模块。"""
def __init__(self, conv, bn):
assert (
type_before_parametrizations(conv) == Conv3d
并且 type_before_parametrizations(bn) == BatchNorm3d
), f"输入模块类型错误{type_before_parametrizations(conv)}{type_before_parametrizations(bn)}"
super().__init__(conv, bn)
[文档]class ConvBnReLU3d(_FusedModule):
这是一个调用 3D 卷积、3D 批量归一化和 ReLU 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。
def __init__(self, conv, bn, relu):
assert (
type_before_parametrizations(conv) == Conv3d
and type_before_parametrizations(bn) == BatchNorm3d
and type_before_parametrizations(relu) == ReLU
), f"Incorrect types for input modules{type_before_parametrizations(conv)}{type_before_parametrizations(bn)}{type_before_parametrizations(relu)}" # noqa: B950
super().__init__(conv, bn, relu)
[文档]class BNReLU2d(_FusedModule):
r"""这是一个调用批归一化 2d 和 ReLU 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。"""
def __init__(self, batch_norm, relu):
assert (
type_before_parametrizations(batch_norm) == BatchNorm2d
and type_before_parametrizations(relu) == ReLU
), f"输入模块类型错误{type_before_parametrizations(batch_norm)}{type_before_parametrizations(relu)}"
super().__init__(batch_norm, relu)
[文档]class BNReLU3d(_FusedModule):
r"""这是一个调用 BatchNorm 3d 和 ReLU 模块的顺序容器。
在量化过程中,这将被替换为相应的融合模块。"""
def __init__(self, batch_norm, relu):
assert (
type_before_parametrizations(batch_norm) == BatchNorm3d
并且 type_before_parametrizations(relu) == ReLU
), f"输入模块类型错误{type_before_parametrizations(batch_norm)}{type_before_parametrizations(relu)}"
super().__init__(batch_norm, relu)
类 LinearBn1d(
混合模块):
r这是一个调用线性模块和 BatchNorm1d 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。
def 初始化(
自身,
线性, bn):
断言 (
参数化前的类型(
线性) ==
线性
和
参数化前的类型(bn) == BatchNorm1d
), f输入模块类型错误{
参数化前的类型(
线性)}{
参数化前的类型(bn)}"
超级().
初始化(
线性, bn)
类
线性 LeakyReLU(
混合模块):
r这是一个调用线性 LeakyReLU 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。
def 初始化(
自身,
线性,
漏波 ReLU):
断言 (
类型(
线性) ==
线性
和
类型(
漏波 ReLU) ==
火炬.nn.LeakyReLU
), f输入模块类型不正确{
类型(
线性)}{
类型(
漏波 ReLU)}"
超级().
初始化(
线性,
漏波 ReLU)
类
线性双曲正切函数(
混合模块):
r这是一个调用线性模块和 Tanh 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。
def 初始化(
自身,
线性,
双曲正切):
断言 (
类型(
线性) ==
线性
和
类型(
双曲正切) ==
火炬.nn.
双曲正切函数
), f输入模块类型不正确{
类型(
线性)}{
类型(
双曲正切)}"
超级().
初始化(
线性,
双曲正切)
类 ConvAdd2d(
混合模块):
r这是一个带有额外 Add 的 Conv2d 模块的顺序容器。
在量化过程中,这将替换为相应的融合模块。
def 初始化(
自身,
卷积,
添加):
超级().
初始化(conv)
自身.
添加 =
添加
def 前向(
自身, x1, x2):
# 类型:忽略[覆盖]
返回
自身.
添加(
自身[0
]x1), x2)
类 ConvAddReLU2d(
混合模块):
r这是一个调用 Conv2d、add、Relu 的顺序容器。
在量化过程中,这将替换为相应的融合模块。
def 初始化(
自身,
沟通,
添加, relu):
超级().
初始化(
沟通)
自身.
添加 =
添加
自身.relu = relu
def 前向(
自身, x1, x2):
# 类型:忽略[覆盖]
返回
自身.relu(
自身.
添加(
自身[0
]x1), x2))