MCPcopy Create free account
hub / github.com/caoql98/DCPL / TextEncoder

Class TextEncoder

trainers/DCPL.py:43–66  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41
42
43class TextEncoder(nn.Module):
44 def __init__(self, clip_model):
45 super().__init__()
46 self.transformer = clip_model.transformer
47 self.positional_embedding = clip_model.positional_embedding
48 self.ln_final = clip_model.ln_final
49 self.text_projection = clip_model.text_projection
50 self.dtype = clip_model.dtype
51
52 def forward(self, prompts, tokenized_prompts, compound_prompts_deeper_text):
53 x = prompts + self.positional_embedding.type(self.dtype)
54 x = x.permute(1, 0, 2) # NLD -> LND
55 # Pass as the list, as nn.sequential cannot process multiple arguments in the forward pass
56 combined = [x, compound_prompts_deeper_text, 0] # third argument is the counter which denotes depth of prompt
57 outputs = self.transformer(combined)
58 x = outputs[0] # extract the x back from here
59 x = x.permute(1, 0, 2) # LND -> NLD
60 x = self.ln_final(x).type(self.dtype)
61
62 # x.shape = [batch_size, n_ctx, transformer.width]
63 # take features from the eot embedding (eot_token is the highest number in each sequence)
64 x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection
65
66 return x
67
68
69class MultiModalPromptLearner(nn.Module):

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected