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

Class TextEncoder

trainers/independentVL.py:42–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40
41
42class TextEncoder(nn.Module):
43 def __init__(self, clip_model):
44 super().__init__()
45 self.transformer = clip_model.transformer
46 self.positional_embedding = clip_model.positional_embedding
47 self.ln_final = clip_model.ln_final
48 self.text_projection = clip_model.text_projection
49 self.dtype = clip_model.dtype
50
51 def forward(self, prompts, tokenized_prompts):
52 x = prompts + self.positional_embedding.type(self.dtype)
53 x = x.permute(1, 0, 2) # NLD -> LND
54 x = self.transformer(x)
55 x = x.permute(1, 0, 2) # LND -> NLD
56 x = self.ln_final(x).type(self.dtype)
57
58 # x.shape = [batch_size, n_ctx, transformer.width]
59 # take features from the eot embedding (eot_token is the highest number in each sequence)
60 x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection
61
62 return x
63
64
65class VLPromptLearner(nn.Module):

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected