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

Class CrossAttention

ldm/modules/attention.py:145–194  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

143
144
145class CrossAttention(nn.Module):
146 def __init__(self, query_dim, context_dim=None, heads=8, dim_head=64, dropout=0.):
147 super().__init__()
148 inner_dim = dim_head * heads
149 context_dim = default(context_dim, query_dim)
150
151 self.scale = dim_head ** -0.5
152 self.heads = heads
153
154 self.to_q = nn.Linear(query_dim, inner_dim, bias=False)
155 self.to_k = nn.Linear(context_dim, inner_dim, bias=False)
156 self.to_v = nn.Linear(context_dim, inner_dim, bias=False)
157
158 self.to_out = nn.Sequential(
159 nn.Linear(inner_dim, query_dim),
160 nn.Dropout(dropout)
161 )
162
163 def forward(self, x, context=None, mask=None):
164 h = self.heads
165
166 q = self.to_q(x)
167 context = default(context, x)
168 k = self.to_k(context)
169 v = self.to_v(context)
170
171 q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v))
172
173 # force cast to fp32 to avoid overflowing
174 if _ATTN_PRECISION =="fp32":
175 with torch.autocast(enabled=False, device_type = 'cuda'):
176 q, k = q.float(), k.float()
177 sim = einsum('b i d, b j d -> b i j', q, k) * self.scale
178 else:
179 sim = einsum('b i d, b j d -> b i j', q, k) * self.scale
180
181 del q, k
182
183 if exists(mask):
184 mask = rearrange(mask, 'b ... -> b (...)')
185 max_neg_value = -torch.finfo(sim.dtype).max
186 mask = repeat(mask, 'b j -> (b h) () j', h=h)
187 sim.masked_fill_(~mask, max_neg_value)
188
189 # attention, what we cannot get enough of
190 sim = sim.softmax(dim=-1)
191
192 out = einsum('b i j, b j d -> b i d', sim, v)
193 out = rearrange(out, '(b h) n d -> b n (h d)', h=h)
194 return self.to_out(out)
195
196
197class MemoryEfficientCrossAttention(nn.Module):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected