| 35 | |
| 36 | |
| 37 | class TextEncoder(nn.Module): |
| 38 | def __init__(self, clip_model): |
| 39 | super().__init__() |
| 40 | self.transformer = clip_model.transformer |
| 41 | self.positional_embedding = clip_model.positional_embedding |
| 42 | self.ln_final = clip_model.ln_final |
| 43 | self.text_projection = clip_model.text_projection |
| 44 | self.dtype = clip_model.dtype |
| 45 | |
| 46 | def forward(self, prompts, tokenized_prompts): |
| 47 | |
| 48 | x = prompts + self.positional_embedding.type(self.dtype) |
| 49 | |
| 50 | x = x.permute(1, 0, 2) # NLD -> LND |
| 51 | x = self.transformer(x) |
| 52 | x = x.permute(1, 0, 2) # LND -> NLD |
| 53 | x = self.ln_final(x).type(self.dtype) |
| 54 | |
| 55 | x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection |
| 56 | |
| 57 | return x |
| 58 | |
| 59 | |
| 60 | class PromptLearner(nn.Module): |