Skip to content

Commit

Permalink
* Force cast to fp32 to avoid atten layer overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Dango233 committed Dec 7, 2022
1 parent d7440ac commit 6e92cda
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ldm/modules/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,13 @@ def forward(self, x, context=None, mask=None):

q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v))

sim = einsum('b i d, b j d -> b i j', q, k) * self.scale
# force cast to fp32 to avoid overflowing
with torch.autocast(enabled=False, device_type = 'cuda'):
q, k = q.float(), k.float()
sim = einsum('b i d, b j d -> b i j', q, k) * self.scale

del q, k

if exists(mask):
mask = rearrange(mask, 'b ... -> b (...)')
max_neg_value = -torch.finfo(sim.dtype).max
Expand Down

0 comments on commit 6e92cda

Please sign in to comment.