MCPcopy Create free account
hub / github.com/CompVis/diff2flow / forward

Method forward

diff2flow/kl_autoencoder.py:201–228  ·  view source on GitHub ↗
(self, x)

Source from the content-addressed store, hash-verified

199 self.use_null_attention = use_null_attention
200
201 def forward(self, x):
202 h_ = x
203 h_ = self.norm(h_)
204 q = self.q(h_)
205 k = self.k(h_)
206 v = self.v(h_)
207
208 # compute null attention by discarding q,k
209 if self.use_null_attention:
210 out = self.proj_out(v)
211 return x+out
212
213 # compute attention
214 if NATTEN_IS_AVAILBLE and self.natten_kernel_size > -1:
215 q, k, v = map(lambda x: rearrange(x, 'b c h w -> b 1 h w c'), (q, k, v))
216 qk = natten.functional.natten2dqk(q, k, self.natten_kernel_size, 1)
217 a = torch.softmax(qk, dim=-1)
218 out = natten.functional.natten2dav(a, v, self.natten_kernel_size, 1)
219 out = rearrange(out, 'b 1 h w c -> b c h w')
220
221 else:
222 b,c,h,w = q.shape
223 q, k, v = map(lambda x: rearrange(x, 'b c h w -> b (h w) c'), (q, k, v))
224 out = nn.functional.scaled_dot_product_attention(q, k, v, dropout_p=0.0)
225 out = rearrange(out, 'b (h w) c -> b c h w', h=h, w=w)
226
227 out = self.proj_out(out)
228 return x+out
229
230
231def make_attn(in_channels, attn_type="vanilla", natten_kernel_size=-1, use_null_attention=False):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected