MCPcopy Create free account
hub / github.com/VisionXLab/OF-Diff / forward

Method forward

ldm/modules/attention.py:119–142  ·  view source on GitHub ↗
(self, x)

Source from the content-addressed store, hash-verified

117 padding=0)
118
119 def forward(self, x):
120 h_ = x
121 h_ = self.norm(h_)
122 q = self.q(h_)
123 k = self.k(h_)
124 v = self.v(h_)
125
126 # compute attention
127 b,c,h,w = q.shape
128 q = rearrange(q, 'b c h w -> b (h w) c')
129 k = rearrange(k, 'b c h w -> b c (h w)')
130 w_ = torch.einsum('bij,bjk->bik', q, k)
131
132 w_ = w_ * (int(c)**(-0.5))
133 w_ = torch.nn.functional.softmax(w_, dim=2)
134
135 # attend to values
136 v = rearrange(v, 'b c h w -> b c (h w)')
137 w_ = rearrange(w_, 'b i j -> b j i')
138 h_ = torch.einsum('bij,bjk->bik', v, w_)
139 h_ = rearrange(h_, 'b c (h w) -> b c h w', h=h)
140 h_ = self.proj_out(h_)
141
142 return x+h_
143
144
145class CrossAttention(nn.Module):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected