| 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 |
| 125 | elif name == "Conv2d_0": |
| 126 | self.conv = conv |
| 127 | else: |
| 128 | self.conv = conv |
| 129 | |
| 130 | def forward(self, hidden_states: torch.Tensor, *args, **kwargs) -> torch.Tensor: |
| 131 | if len(args) > 0 or kwargs.get("scale", None) is not None: |