| 84 | """ |
| 85 | |
| 86 | def __init__( |
| 87 | self, |
| 88 | channels: int, |
| 89 | use_conv: bool = False, |
| 90 | out_channels: Optional[int] = None, |
| 91 | padding: int = 1, |
| 92 | name: str = "conv", |
| 93 | kernel_size=3, |
| 94 | norm_type=None, |
| 95 | eps=None, |
| 96 | elementwise_affine=None, |
| 97 | bias=True, |
| 98 | ): |
| 99 | super().__init__() |
| 100 | self.channels = channels |
| 101 | self.out_channels = out_channels or channels |
| 102 | self.use_conv = use_conv |
| 103 | self.padding = padding |
| 104 | stride = 2 |
| 105 | self.name = name |
| 106 | conv_cls = nn.Conv2d if USE_PEFT_BACKEND else LoRACompatibleConv |
| 107 | |
| 108 | if norm_type == "ln_norm": |
| 109 | self.norm = nn.LayerNorm(channels, eps, elementwise_affine) |
| 110 | elif norm_type == "rms_norm": |
| 111 | self.norm = RMSNorm(channels, eps, elementwise_affine) |
| 112 | elif norm_type is None: |
| 113 | self.norm = None |
| 114 | else: |
| 115 | raise ValueError(f"unknown norm_type: {norm_type}") |
| 116 | |
| 117 | if use_conv: |
| 118 | conv = conv_cls( |
| 119 | self.channels, self.out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias |
| 120 | ) |
| 121 | else: |
| 122 | assert self.channels == self.out_channels |
| 123 | conv = nn.AvgPool2d(kernel_size=stride, stride=stride) |
| 124 | |
| 125 | # TODO(Suraj, Patrick) - clean up after weight dicts are correctly renamed |
| 126 | if name == "conv": |
| 127 | self.Conv2d_0 = conv |
| 128 | self.conv = conv |
| 129 | elif name == "Conv2d_0": |
| 130 | self.conv = conv |
| 131 | else: |
| 132 | self.conv = conv |
| 133 | |
| 134 | def forward(self, hidden_states: torch.FloatTensor, scale: float = 1.0) -> torch.FloatTensor: |
| 135 | assert hidden_states.shape[1] == self.channels |