MCPcopy Create free account
hub / github.com/buaacxf/VIPTR / forward

Method forward

modules/VIPTRv2.py:158–204  ·  view source on GitHub ↗

x: (b h w c) mask_h: (n h h) mask_w: (n w w)

(self, x: torch.Tensor, rel_pos, chunkwise_recurrent=False, incremental_state=None)

Source from the content-addressed store, hash-verified

156 nn.init.constant_(self.out_proj.bias, 0.0)
157
158 def forward(self, x: torch.Tensor, rel_pos, chunkwise_recurrent=False, incremental_state=None):
159 '''
160 x: (b h w c)
161 mask_h: (n h h)
162 mask_w: (n w w)
163 '''
164 bsz, h, w, _ = x.size()
165
166 mask_h, mask_w = rel_pos
167
168 q = self.q_proj(x)
169 k = self.k_proj(x)
170 v = self.v_proj(x)
171 lepe = self.lepe(v)
172
173 k *= self.scaling
174 qr = q.view(bsz, h, w, self.num_heads, self.key_dim).permute(0, 3, 1, 2, 4) # (b n h w d1)
175 kr = k.view(bsz, h, w, self.num_heads, self.key_dim).permute(0, 3, 1, 2, 4) # (b n h w d1)
176
177 '''
178 qr: (b n h w d1)
179 kr: (b n h w d1)
180 v: (b h w n*d2)
181 '''
182
183 qr_w = qr.transpose(1, 2) # (b h n w d1)
184 kr_w = kr.transpose(1, 2) # (b h n w d1)
185 v = v.reshape(bsz, h, w, self.num_heads, -1).permute(0, 1, 3, 2, 4) # (b h n w d2)
186
187 qk_mat_w = qr_w @ kr_w.transpose(-1, -2) # (b h n w w)
188 qk_mat_w = qk_mat_w + mask_w # (b h n w w)
189 qk_mat_w = torch.softmax(qk_mat_w, -1) # (b h n w w)
190 v = torch.matmul(qk_mat_w, v) # (b h n w d2)
191
192 qr_h = qr.permute(0, 3, 1, 2, 4) # (b w n h d1)
193 kr_h = kr.permute(0, 3, 1, 2, 4) # (b w n h d1)
194 v = v.permute(0, 3, 2, 1, 4) # (b w n h d2)
195
196 qk_mat_h = qr_h @ kr_h.transpose(-1, -2) # (b w n h h)
197 qk_mat_h = qk_mat_h + mask_h # (b w n h h)
198 qk_mat_h = torch.softmax(qk_mat_h, -1) # (b w n h h)
199 output = torch.matmul(qk_mat_h, v) # (b w n h d2)
200
201 output = output.permute(0, 3, 1, 2, 4).flatten(-2, -1) # (b h w n*d2)
202 output = output + lepe
203 output = self.out_proj(output)
204 return output
205
206class MaSA(nn.Module):
207

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected