Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlashAttention needs contiguous gradients #548

Merged
merged 2 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion tests/test_mem_eff_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,9 @@ def test_backward(

grad_out = torch.ones_like(out)
if grad_out_contiguous is False:
grad_out = torch.tensor([1.0], device=device)[None, None, :].expand_as(out)
grad_out = torch.tensor([1.0], dtype=query.dtype, device=device)[
None, None, :
].expand_as(out)

out.backward(grad_out)
del out
Expand Down
2 changes: 1 addition & 1 deletion xformers/ops/memory_efficient_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def _backward(cls, ctx, grad, saved_tensors):

assert grad.dtype in cls.SUPPORTED_DTYPES
cls._flash_attn_backward(
grad.reshape(ctx.kernel_output_shape),
grad.reshape(ctx.kernel_output_shape).contiguous(),
q,
k,
v,
Expand Down