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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
FlashAttention needs contiguous gradients
This was not visible before because the test was creating tensors of the wrong dtype, which when casted to the right dtype would yield contiguous tensors
  • Loading branch information
fmassa committed Nov 28, 2022
commit 5922cdd2a101461cbf86219d375f04e9a464246a
2 changes: 1 addition & 1 deletion tests/test_mem_eff_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ 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