| 52 | self._models[name].load_state_dict(state_dict, strict=False) |
| 53 | |
| 54 | class TextEncoder(nn.Module): |
| 55 | def __init__(self, clip_model): |
| 56 | super().__init__() |
| 57 | self.transformer = clip_model.transformer |
| 58 | self.positional_embedding = clip_model.positional_embedding |
| 59 | self.ln_final = clip_model.ln_final |
| 60 | self.text_projection = clip_model.text_projection |
| 61 | self.dtype = clip_model.dtype |
| 62 | |
| 63 | def forward(self, prompts, tokenized_prompts): |
| 64 | |
| 65 | x = prompts + self.positional_embedding.type(self.dtype) |
| 66 | |
| 67 | x = x.permute(1, 0, 2) # NLD -> LND |
| 68 | x = self.transformer(x) |
| 69 | x = x.permute(1, 0, 2) # LND -> NLD |
| 70 | x = self.ln_final(x).type(self.dtype) |
| 71 | |
| 72 | x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection |
| 73 | |
| 74 | return x |
| 75 | |
| 76 | |
| 77 | class PromptLearner(nn.Module): |