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