A 2D upsampling 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. use_conv_transpose (`bool`, default `False`):
| 72 | |
| 73 | |
| 74 | class Upsample2D(nn.Module): |
| 75 | """A 2D upsampling layer with an optional convolution. |
| 76 | |
| 77 | Parameters: |
| 78 | channels (`int`): |
| 79 | number of channels in the inputs and outputs. |
| 80 | use_conv (`bool`, default `False`): |
| 81 | option to use a convolution. |
| 82 | use_conv_transpose (`bool`, default `False`): |
| 83 | option to use a convolution transpose. |
| 84 | out_channels (`int`, optional): |
| 85 | number of output channels. Defaults to `channels`. |
| 86 | name (`str`, default `conv`): |
| 87 | name of the upsampling 2D layer. |
| 88 | """ |
| 89 | |
| 90 | def __init__( |
| 91 | self, |
| 92 | channels: int, |
| 93 | use_conv: bool = False, |
| 94 | use_conv_transpose: bool = False, |
| 95 | out_channels: int | None = None, |
| 96 | name: str = "conv", |
| 97 | kernel_size: int | None = None, |
| 98 | padding=1, |
| 99 | norm_type=None, |
| 100 | eps=None, |
| 101 | elementwise_affine=None, |
| 102 | bias=True, |
| 103 | interpolate=True, |
| 104 | ): |
| 105 | super().__init__() |
| 106 | self.channels = channels |
| 107 | self.out_channels = out_channels or channels |
| 108 | self.use_conv = use_conv |
| 109 | self.use_conv_transpose = use_conv_transpose |
| 110 | self.name = name |
| 111 | self.interpolate = interpolate |
| 112 | |
| 113 | if norm_type == "ln_norm": |
| 114 | self.norm = nn.LayerNorm(channels, eps, elementwise_affine) |
| 115 | elif norm_type == "rms_norm": |
| 116 | self.norm = RMSNorm(channels, eps, elementwise_affine) |
| 117 | elif norm_type is None: |
| 118 | self.norm = None |
| 119 | else: |
| 120 | raise ValueError(f"unknown norm_type: {norm_type}") |
| 121 | |
| 122 | conv = None |
| 123 | if use_conv_transpose: |
| 124 | if kernel_size is None: |
| 125 | kernel_size = 4 |
| 126 | conv = nn.ConvTranspose2d( |
| 127 | channels, self.out_channels, kernel_size=kernel_size, stride=2, padding=padding, bias=bias |
| 128 | ) |
| 129 | elif use_conv: |
| 130 | if kernel_size is None: |
| 131 | kernel_size = 3 |
no outgoing calls
searching dependent graphs…