A 2D downsampling layer with an optional convolution. Parameters: channels (`int`): number of channels in the inputs and outputs. use_conv (`bool`, default `False`): option to use a convolution. out_channels (`int`, optional): number o
| 67 | |
| 68 | |
| 69 | class Downsample2D(nn.Module): |
| 70 | """A 2D downsampling layer with an optional convolution. |
| 71 | |
| 72 | Parameters: |
| 73 | channels (`int`): |
| 74 | number of channels in the inputs and outputs. |
| 75 | use_conv (`bool`, default `False`): |
| 76 | option to use a convolution. |
| 77 | out_channels (`int`, optional): |
| 78 | number of output channels. Defaults to `channels`. |
| 79 | padding (`int`, default `1`): |
| 80 | padding for the convolution. |
| 81 | name (`str`, default `conv`): |
| 82 | name of the downsampling 2D layer. |
| 83 | """ |
| 84 | |
| 85 | def __init__( |
| 86 | self, |
| 87 | channels: int, |
| 88 | use_conv: bool = False, |
| 89 | out_channels: Optional[int] = None, |
| 90 | padding: int = 1, |
| 91 | name: str = "conv", |
| 92 | kernel_size=3, |
| 93 | norm_type=None, |
| 94 | eps=None, |
| 95 | elementwise_affine=None, |
| 96 | bias=True, |
| 97 | ): |
| 98 | super().__init__() |
| 99 | self.channels = channels |
| 100 | self.out_channels = out_channels or channels |
| 101 | self.use_conv = use_conv |
| 102 | self.padding = padding |
| 103 | stride = 2 |
| 104 | self.name = name |
| 105 | |
| 106 | if norm_type == "ln_norm": |
| 107 | self.norm = nn.LayerNorm(channels, eps, elementwise_affine) |
| 108 | elif norm_type == "rms_norm": |
| 109 | self.norm = RMSNorm(channels, eps, elementwise_affine) |
| 110 | elif norm_type is None: |
| 111 | self.norm = None |
| 112 | else: |
| 113 | raise ValueError(f"unknown norm_type: {norm_type}") |
| 114 | |
| 115 | if use_conv: |
| 116 | conv = nn.Conv2d( |
| 117 | self.channels, self.out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias |
| 118 | ) |
| 119 | else: |
| 120 | assert self.channels == self.out_channels |
| 121 | conv = nn.AvgPool2d(kernel_size=stride, stride=stride) |
| 122 | |
| 123 | # TODO(Suraj, Patrick) - clean up after weight dicts are correctly renamed |
| 124 | if name == "conv": |
| 125 | self.Conv2d_0 = conv |
| 126 | self.conv = conv |
no outgoing calls