MCPcopy Create free account
hub / github.com/CHENGY12/PLOT / forward

Method forward

plot-coop/trainers/plot.py:125–193  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

123
124
125 def forward(self):
126
127 ctx = self.ctx
128 if ctx.dim() == 3:
129 ctx = ctx.unsqueeze(0).expand(self.n_cls, -1, -1,-1)
130
131 ctx = ctx.permute(1, 0, 2, 3)
132 ctx = ctx.contiguous().view(self.N*self.n_cls,self.n_ctx,ctx.shape[3])
133
134 prefix = self.token_prefix
135 suffix = self.token_suffix
136
137 if self.class_token_position == "end":
138 prompts = torch.cat(
139 [
140 prefix, # (n_cls, 1, dim)
141 ctx, # (n_cls, n_ctx, dim)
142 suffix, # (n_cls, *, dim)
143 ],
144 dim=1,
145 )
146
147 elif self.class_token_position == "middle":
148 half_n_ctx = self.n_ctx // 2
149 prompts = []
150 for i in range(self.n_cls):
151 name_len = self.name_lens[i]
152 prefix_i = prefix[i : i + 1, :, :]
153 class_i = suffix[i : i + 1, :name_len, :]
154 suffix_i = suffix[i : i + 1, name_len:, :]
155 ctx_i_half1 = ctx[i : i + 1, :half_n_ctx, :]
156 ctx_i_half2 = ctx[i : i + 1, half_n_ctx:, :]
157 prompt = torch.cat(
158 [
159 prefix_i, # (1, 1, dim)
160 ctx_i_half1, # (1, n_ctx//2, dim)
161 class_i, # (1, name_len, dim)
162 ctx_i_half2, # (1, n_ctx//2, dim)
163 suffix_i, # (1, *, dim)
164 ],
165 dim=1,
166 )
167 prompts.append(prompt)
168 prompts = torch.cat(prompts, dim=0)
169
170 elif self.class_token_position == "front":
171 prompts = []
172 for i in range(self.n_cls):
173 name_len = self.name_lens[i]
174 prefix_i = prefix[i : i + 1, :, :]
175 class_i = suffix[i : i + 1, :name_len, :]
176 suffix_i = suffix[i : i + 1, name_len:, :]
177 ctx_i = ctx[i : i + 1, :, :]
178 prompt = torch.cat(
179 [
180 prefix_i, # (1, 1, dim)
181 class_i, # (1, name_len, dim)
182 ctx_i, # (1, n_ctx, dim)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected