| 148 | self.num_heads = num_heads |
| 149 | |
| 150 | def forward(self, x): |
| 151 | x = x.permute(1, 0, 2) # NLC -> LNC |
| 152 | x = torch.cat([x.mean(dim=0, keepdim=True), x], dim=0) # (L+1)NC |
| 153 | x = x + self.positional_embedding[:, None, :].to(x.dtype) # (L+1)NC |
| 154 | x, _ = torch.nn.functional.multi_head_attention_forward( |
| 155 | query=x[:1], key=x, value=x, |
| 156 | embed_dim_to_check=x.shape[-1], |
| 157 | num_heads=self.num_heads, |
| 158 | q_proj_weight=self.q_proj.weight, |
| 159 | k_proj_weight=self.k_proj.weight, |
| 160 | v_proj_weight=self.v_proj.weight, |
| 161 | in_proj_weight=None, |
| 162 | in_proj_bias=torch.cat([self.q_proj.bias, self.k_proj.bias, self.v_proj.bias]), |
| 163 | bias_k=None, |
| 164 | bias_v=None, |
| 165 | add_zero_attn=False, |
| 166 | dropout_p=0, |
| 167 | out_proj_weight=self.c_proj.weight, |
| 168 | out_proj_bias=self.c_proj.bias, |
| 169 | use_separate_proj_weight=True, |
| 170 | training=self.training, |
| 171 | need_weights=False |
| 172 | ) |
| 173 | return x.squeeze(0) |
| 174 | |
| 175 | |
| 176 | class PatchEmbed(torch.nn.Module): |