-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft for supporting flash attention
- Loading branch information
Showing
4 changed files
with
217 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright (c) 2024, DeepLink. | ||
|
||
import torch | ||
import deeplink_ext.cpp_extensions as ext | ||
|
||
assert hasattr(ext, "fa_fwd") and hasattr(ext, "fa_bwd") | ||
|
||
|
||
class DeepLinkFlashAttentionKVPackedFunc(torch.autograd.Function): | ||
@staticmethod | ||
def forward(ctx, q, kv, dropout_p, softmax_scale, causal): | ||
if softmax_scale is None: | ||
softmax_scale = q.shape[-1] ** (-0.5) | ||
out, softmax_max, softmax_sum, softmax_out, rng = ext.fa_fwd( | ||
q, kv[:, :, 0], kv[:, :, 1], dropout_p, softmax_scale, causal | ||
) | ||
ctx.save_for_backward( | ||
q, kv, out, softmax_max, softmax_sum, softmax_out, rng.get_state() | ||
) | ||
ctx.dropout_p = dropout_p | ||
ctx.softmax_scale = softmax_scale | ||
ctx.causal = causal | ||
return out | ||
|
||
@staticmethod | ||
def backward(ctx, dout): | ||
q, kv, out, softmax_max, softmax_sum, softmax_out, rng_state = ctx.saved_tensors | ||
dq = torch.empty_like(q) | ||
dkv = torch.empty_like(kv) | ||
rng = torch.Generator(device=q.device) | ||
rng.set_state(rng_state) | ||
ext.fa_bwd( | ||
dq, | ||
dkv[:, :, 0], | ||
dkv[:, :, 1], | ||
dout, | ||
q, | ||
kv[:, :, 0], | ||
kv[:, :, 1], | ||
out, | ||
softmax_max, | ||
softmax_sum, | ||
softmax_out, | ||
rng, | ||
ctx.dropout_p, | ||
ctx.softmax_scale, | ||
ctx.causal, | ||
) | ||
return dq, dkv, None, None, None, None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright (c) 2024, DeepLink. | ||
|
||
import torch | ||
import deeplink_ext.cpp_extensions as ext | ||
|
||
assert hasattr(ext, "fa_fwd") and hasattr(ext, "fa_bwd") | ||
|
||
|
||
class DeepLinkFlashAttentionQKVPackedFunc(torch.autograd.Function): | ||
@staticmethod | ||
def forward(ctx, qkv, dropout_p, softmax_scale, causal): | ||
if softmax_scale is None: | ||
softmax_scale = qkv.shape[-1] ** (-0.5) | ||
out, softmax_max, softmax_sum, softmax_out, rng = ext.fa_fwd( | ||
qkv[:, :, 0], qkv[:, :, 1], qkv[:, :, 2], dropout_p, softmax_scale, causal | ||
) | ||
ctx.save_for_backward( | ||
qkv, out, softmax_max, softmax_sum, softmax_out, rng.get_state() | ||
) | ||
ctx.dropout_p = dropout_p | ||
ctx.softmax_scale = softmax_scale | ||
ctx.causal = causal | ||
return out | ||
|
||
@staticmethod | ||
def backward(ctx, dout): | ||
qkv, out, softmax_max, softmax_sum, softmax_out, rng_state = ctx.saved_tensors | ||
dqkv = torch.empty_like(qkv) | ||
rng = torch.Generator(device=qkv.device) | ||
rng.set_state(rng_state) | ||
ext.fa_bwd( | ||
dqkv[:, :, 0], | ||
dqkv[:, :, 1], | ||
dqkv[:, :, 2], | ||
dout, | ||
qkv[:, :, 0], | ||
qkv[:, :, 1], | ||
qkv[:, :, 2], | ||
out, | ||
softmax_max, | ||
softmax_sum, | ||
softmax_out, | ||
rng, | ||
ctx.dropout_p, | ||
ctx.softmax_scale, | ||
ctx.causal, | ||
) | ||
return dqkv, None, None, None, None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters