-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
add num_splist for flash_attn_bwd and FlashAttnUnpaddedGradKernel #56363
Merged
Xreki
merged 10 commits into
PaddlePaddle:develop
from
AnnaTrainingG:flash_bwd_num_splits
Sep 4, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b742184
add num_splist for flash_attn_bwd and FlashAttnUnpaddedGradKernel
AnnaTrainingG 5a5c307
update
AnnaTrainingG 7d9ce2f
update
AnnaTrainingG 814b9c5
Add assertTrue
AnnaTrainingG ded6af8
Update submodule to a specific commit
AnnaTrainingG 063f8f3
update test
AnnaTrainingG 51817ec
update
AnnaTrainingG 7dd1f71
update
AnnaTrainingG 75ff6f8
update
AnnaTrainingG 8b585ab
update
AnnaTrainingG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,208 @@ | ||
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个单测挪出来了吗?那需要加到GPUPS CI跑的单测列表里面去,下个PR加下吧。 |
||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import re | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
import paddle | ||
import paddle.nn.functional as F | ||
from paddle.device import core | ||
from paddle.nn.functional.flash_attention import ( | ||
flash_attention, | ||
scaled_dot_product_attention, | ||
) | ||
|
||
|
||
def get_cuda_version(): | ||
result = os.popen("nvcc --version").read() | ||
regex = r'release (\S+),' | ||
match = re.search(regex, result) | ||
if match: | ||
num = str(match.group(1)) | ||
integer, decimal = num.split('.') | ||
return int(integer) * 1000 + int(float(decimal) * 10) | ||
else: | ||
return -1 | ||
|
||
|
||
def attention_naive(q, k, v, causal=False): | ||
qt = paddle.transpose(q, [0, 2, 1, 3]) | ||
kt = paddle.transpose(k, [0, 2, 1, 3]) | ||
vt = paddle.transpose(v, [0, 2, 1, 3]) | ||
scale = 1.0 / np.sqrt(q.shape[-1]) | ||
s = paddle.matmul(qt, paddle.transpose(kt, [0, 1, 3, 2])) | ||
s = paddle.scale(s, scale) | ||
p = ( | ||
paddle.incubate.softmax_mask_fuse_upper_triangle(s) | ||
if causal | ||
else F.softmax(s) | ||
) | ||
o = paddle.matmul(p, vt) | ||
return paddle.transpose(o, [0, 2, 1, 3]) | ||
|
||
|
||
is_sm8x = ( | ||
core.is_compiled_with_cuda() | ||
and paddle.device.cuda.get_device_capability()[0] == 8 | ||
and paddle.device.cuda.get_device_capability()[1] >= 0 | ||
) | ||
|
||
is_sm90 = ( | ||
core.is_compiled_with_cuda() | ||
and paddle.device.cuda.get_device_capability()[0] == 9 | ||
and paddle.device.cuda.get_device_capability()[1] == 0 | ||
) | ||
|
||
is_sm_supported = is_sm8x or is_sm90 | ||
|
||
|
||
@unittest.skipIf( | ||
not core.is_compiled_with_cuda() | ||
or get_cuda_version() < 11040 | ||
or not is_sm_supported, | ||
"core is not compiled with CUDA and cuda version need larger than or equal to 11.4" | ||
"and device's compute capability must be 8.x or 90", | ||
) | ||
class TestFlashAttentionAPIFlag(unittest.TestCase): | ||
def setUp(self): | ||
self.place = paddle.CUDAPlace(0) | ||
self.shape = (2, 128, 8, 16) | ||
self.dtype = 'float16' | ||
self.dropout = 0.0 | ||
self.causal = False | ||
self.return_softmax = False | ||
self.use_sdp_kernel = False | ||
self.use_sdp_api = False | ||
|
||
def flash_attn_compute(self, query, key, value): | ||
# test dynamic | ||
paddle.disable_static() | ||
|
||
q = paddle.to_tensor( | ||
query, place=self.place, dtype=self.dtype, stop_gradient=False | ||
) | ||
k = paddle.to_tensor( | ||
key, place=self.place, dtype=self.dtype, stop_gradient=False | ||
) | ||
v = paddle.to_tensor( | ||
value, place=self.place, dtype=self.dtype, stop_gradient=False | ||
) | ||
|
||
q_ = paddle.to_tensor( | ||
query, place=self.place, dtype=self.dtype, stop_gradient=False | ||
) | ||
k_ = paddle.to_tensor( | ||
key, place=self.place, dtype=self.dtype, stop_gradient=False | ||
) | ||
v_ = paddle.to_tensor( | ||
value, place=self.place, dtype=self.dtype, stop_gradient=False | ||
) | ||
|
||
if self.use_sdp_kernel: | ||
with paddle.nn.functional.sdp_kernel( | ||
enable_math=self.enable_math, | ||
enable_flash=self.enable_flash, | ||
enable_mem_efficient=self.enable_mem_efficient, | ||
): | ||
if self.use_sdp_api: | ||
out = scaled_dot_product_attention( | ||
q, k, v, None, self.dropout, self.causal | ||
) | ||
else: | ||
out, _ = flash_attention( | ||
q, k, v, self.dropout, self.causal, self.return_softmax | ||
) | ||
|
||
else: | ||
out, _ = flash_attention( | ||
q, k, v, self.dropout, self.causal, self.return_softmax | ||
) | ||
out_ = attention_naive(q_, k_, v_, self.causal) | ||
|
||
out.backward() | ||
out_.backward() | ||
|
||
self.assertEqual(q.grad.shape, q.shape) | ||
self.assertEqual(q_.grad.shape, q.shape) | ||
|
||
np.testing.assert_allclose( | ||
q.grad.numpy(), q_.grad.numpy(), rtol=5e-03, atol=1e-03 | ||
) | ||
|
||
return out, out_, q.grad.numpy(), k.grad.numpy(), v.grad.numpy() | ||
|
||
def test_all_flag(self): | ||
paddle.set_flags({'FLAGS_cudnn_deterministic': 1}) | ||
query = np.random.random(self.shape) | ||
key = np.random.random(self.shape) | ||
value = np.random.random(self.shape) | ||
|
||
out1, out1_, q_grad1, k_grad1, v_grad1 = self.flash_attn_compute( | ||
query, key, value | ||
) | ||
|
||
np.testing.assert_allclose(out1.numpy(), out1_, rtol=5e-03, atol=1e-03) | ||
|
||
out2, out2_, q_grad2, k_grad2, v_grad2 = self.flash_attn_compute( | ||
query, key, value | ||
) | ||
self.assertTrue(np.equal(out1.numpy(), out2.numpy()).all()) | ||
self.assertTrue(np.equal(q_grad1, q_grad2).all()) | ||
self.assertTrue(np.equal(k_grad1, k_grad2).all()) | ||
self.assertTrue(np.equal(v_grad1, v_grad2).all()) | ||
paddle.set_flags({'FLAGS_cudnn_deterministic': 0}) | ||
|
||
|
||
class TestFlashAttentionAPIFlagTest1(TestFlashAttentionAPIFlag): | ||
def setUp(self): | ||
self.place = paddle.CUDAPlace(0) | ||
self.shape = (2, 128, 8, 16) | ||
self.dtype = paddle.float16 | ||
self.dropout = 0.0 | ||
self.causal = False | ||
self.return_softmax = False | ||
self.use_sdp_kernel = False | ||
|
||
|
||
class TestFlashAttentionAPIFlagTest2(TestFlashAttentionAPIFlag): | ||
def setUp(self): | ||
self.place = paddle.CUDAPlace(0) | ||
self.shape = (8, 1024, 16, 256) | ||
self.dtype = paddle.float16 | ||
self.dropout = 0.0 | ||
self.causal = False | ||
self.return_softmax = False | ||
self.use_sdp_kernel = False | ||
|
||
|
||
class TestSDPAttentionAPIFlagTest(TestFlashAttentionAPIFlag): | ||
def setUp(self): | ||
self.place = paddle.CUDAPlace(0) | ||
self.shape = (8, 1024, 16, 128) | ||
self.dtype = paddle.float16 | ||
self.dropout = 0.0 | ||
self.causal = False | ||
self.return_softmax = False | ||
self.use_sdp_kernel = True | ||
self.use_sdp_api = True | ||
self.enable_math = True | ||
self.enable_flash = False | ||
self.enable_mem_efficient = False | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Submodule flashattn
updated
5 files
+21 −11 | csrc/capi/flash_attn.cu | |
+2 −0 | csrc/capi/flash_attn.h | |
+1 −0 | csrc/flash_attn/src/flash.h | |
+10 −2 | csrc/flash_attn/src/flash_bwd_kernel.h | |
+1 −1 | csrc/flash_attn/src/flash_bwd_launch_template.h |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个函数,以及kernel中两处
int num_splits = get_num_split();
建议直接封装在FlashAttnBwdParamsV2里面。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
下个PR再改