(self)
| 139 | |
| 140 | |
| 141 | def forward(self): |
| 142 | |
| 143 | ctx = self.ctx |
| 144 | if ctx.dim() == 3: |
| 145 | ctx = ctx.unsqueeze(0).expand(self.n_cls, -1, -1,-1) |
| 146 | |
| 147 | ctx = ctx.permute(1, 0, 2, 3) |
| 148 | ctx = ctx.contiguous().view(self.N*self.n_cls,self.n_ctx,ctx.shape[3]) |
| 149 | |
| 150 | prefix = self.token_prefix |
| 151 | suffix = self.token_suffix |
| 152 | |
| 153 | if self.class_token_position == "end": |
| 154 | prompts = torch.cat( |
| 155 | [ |
| 156 | prefix, # (n_cls, 1, dim) |
| 157 | ctx, # (n_cls, n_ctx, dim) |
| 158 | suffix, # (n_cls, *, dim) |
| 159 | ], |
| 160 | dim=1, |
| 161 | ) |
| 162 | |
| 163 | elif self.class_token_position == "middle": |
| 164 | half_n_ctx = self.n_ctx // 2 |
| 165 | prompts = [] |
| 166 | for i in range(self.n_cls): |
| 167 | name_len = self.name_lens[i] |
| 168 | prefix_i = prefix[i : i + 1, :, :] |
| 169 | class_i = suffix[i : i + 1, :name_len, :] |
| 170 | suffix_i = suffix[i : i + 1, name_len:, :] |
| 171 | ctx_i_half1 = ctx[i : i + 1, :half_n_ctx, :] |
| 172 | ctx_i_half2 = ctx[i : i + 1, half_n_ctx:, :] |
| 173 | prompt = torch.cat( |
| 174 | [ |
| 175 | prefix_i, # (1, 1, dim) |
| 176 | ctx_i_half1, # (1, n_ctx//2, dim) |
| 177 | class_i, # (1, name_len, dim) |
| 178 | ctx_i_half2, # (1, n_ctx//2, dim) |
| 179 | suffix_i, # (1, *, dim) |
| 180 | ], |
| 181 | dim=1, |
| 182 | ) |
| 183 | prompts.append(prompt) |
| 184 | prompts = torch.cat(prompts, dim=0) |
| 185 | |
| 186 | elif self.class_token_position == "front": |
| 187 | prompts = [] |
| 188 | for i in range(self.n_cls): |
| 189 | name_len = self.name_lens[i] |
| 190 | prefix_i = prefix[i : i + 1, :, :] |
| 191 | class_i = suffix[i : i + 1, :name_len, :] |
| 192 | suffix_i = suffix[i : i + 1, name_len:, :] |
| 193 | ctx_i = ctx[i : i + 1, :, :] |
| 194 | prompt = torch.cat( |
| 195 | [ |
| 196 | prefix_i, # (1, 1, dim) |
| 197 | class_i, # (1, name_len, dim) |
| 198 | ctx_i, # (1, n_ctx, dim) |
nothing calls this directly
no outgoing calls
no test coverage detected