-
Notifications
You must be signed in to change notification settings - Fork 3k
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 memory efficient attention from CUTLASS #14343
Merged
Merged
Conversation
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
yufenglee
reviewed
Jan 19, 2023
onnxruntime/contrib_ops/cuda/bert/cutlass_fmha/memory_efficient_attention.h
Outdated
Show resolved
Hide resolved
yufenglee
reviewed
Jan 19, 2023
onnxruntime/contrib_ops/cuda/bert/cutlass_fmha/memory_efficient_attention.cu
Outdated
Show resolved
Hide resolved
yufenglee
reviewed
Jan 19, 2023
onnxruntime/contrib_ops/cuda/bert/cutlass_fmha/memory_efficient_attention.cu
Outdated
Show resolved
Hide resolved
yufenglee
reviewed
Jan 19, 2023
onnxruntime/contrib_ops/cuda/bert/cutlass_fmha/memory_efficient_attention.cu
Outdated
Show resolved
Hide resolved
wangyems
reviewed
Jan 19, 2023
onnxruntime/contrib_ops/cuda/bert/cutlass_fmha/memory_efficient_attention.cu
Outdated
Show resolved
Hide resolved
wangyems
approved these changes
Jan 20, 2023
adrianlizarraga
added a commit
that referenced
this pull request
Jan 24, 2023
…14404) ### Description Fixes unused `use_memory_efficient_attention` variable in contrib_ops/cuda/bert/attention_impl.cu. ### Motivation and Context ORT with CUDA version < 11.6 fails to build for release configurations due to an unused variable. ```shell c:\...\onnxruntime\onnxruntime\contrib_ops\cuda\bert\attention_impl.cu(420): error : variable "use_memory_efficient_attention" was declared but never referenced [C:\...\onnxruntime\build\Windows\RelWithDebInfo\onnx runtime_providers_cuda.vcxproj] detected during instantiation of "onnxruntime::common::Status onnxruntime::contrib::cuda::QkvToContext(const cudaDeviceProp &, cublasHandle_t &, cudaStream_t, onnxruntime::contrib::AttentionParameters &, onnxruntime::contrib::cuda::AttentionData<T> &) [wit h T=float]" (923): here ``` This happens for CUDA < 11.6. Our cmake script turns off onnxruntime_USE_FLASH_ATTENTION for CUDA < 11.6, which leaves the aforementioned variable unused outside of asserts (which are removed in release builds). The USE_FLASH_ATTENTION option was added by #14343
tianleiwu
added a commit
that referenced
this pull request
Jan 25, 2023
…s FMHA (#14401) ### Description Add sequence length threshold for triggering cutlass FMHA in FP32. See performance test results in #14343 to see how this threshold is selected. Upgrade cutlass to v2.11 and update deps.txt and cgmanifest for nuget pipeline build (test build: https://aiinfra.visualstudio.com/Lotus/_build/results?buildId=268574&view=results)
rui-ren
pushed a commit
that referenced
this pull request
Jan 27, 2023
### Description Add memory efficient attention from CUTLASS. TODO (in next pull request): (1) Need performance tests on different GPUs, then add a sequence length threshold (only activate it for long sequence length). (2) Merge changes from NVIDIA/cutlass#773 when it is in cutlass master.
rui-ren
pushed a commit
that referenced
this pull request
Jan 27, 2023
…s FMHA (#14401) ### Description Add sequence length threshold for triggering cutlass FMHA in FP32. See performance test results in #14343 to see how this threshold is selected. Upgrade cutlass to v2.11 and update deps.txt and cgmanifest for nuget pipeline build (test build: https://aiinfra.visualstudio.com/Lotus/_build/results?buildId=268574&view=results)
rui-ren
pushed a commit
that referenced
this pull request
Jan 27, 2023
### Description Add memory efficient attention from CUTLASS. TODO (in next pull request): (1) Need performance tests on different GPUs, then add a sequence length threshold (only activate it for long sequence length). (2) Merge changes from NVIDIA/cutlass#773 when it is in cutlass master.
rui-ren
pushed a commit
that referenced
this pull request
Feb 3, 2023
…14404) ### Description Fixes unused `use_memory_efficient_attention` variable in contrib_ops/cuda/bert/attention_impl.cu. ### Motivation and Context ORT with CUDA version < 11.6 fails to build for release configurations due to an unused variable. ```shell c:\...\onnxruntime\onnxruntime\contrib_ops\cuda\bert\attention_impl.cu(420): error : variable "use_memory_efficient_attention" was declared but never referenced [C:\...\onnxruntime\build\Windows\RelWithDebInfo\onnx runtime_providers_cuda.vcxproj] detected during instantiation of "onnxruntime::common::Status onnxruntime::contrib::cuda::QkvToContext(const cudaDeviceProp &, cublasHandle_t &, cudaStream_t, onnxruntime::contrib::AttentionParameters &, onnxruntime::contrib::cuda::AttentionData<T> &) [wit h T=float]" (923): here ``` This happens for CUDA < 11.6. Our cmake script turns off onnxruntime_USE_FLASH_ATTENTION for CUDA < 11.6, which leaves the aforementioned variable unused outside of asserts (which are removed in release builds). The USE_FLASH_ATTENTION option was added by #14343
rui-ren
pushed a commit
that referenced
this pull request
Feb 3, 2023
…14404) ### Description Fixes unused `use_memory_efficient_attention` variable in contrib_ops/cuda/bert/attention_impl.cu. ### Motivation and Context ORT with CUDA version < 11.6 fails to build for release configurations due to an unused variable. ```shell c:\...\onnxruntime\onnxruntime\contrib_ops\cuda\bert\attention_impl.cu(420): error : variable "use_memory_efficient_attention" was declared but never referenced [C:\...\onnxruntime\build\Windows\RelWithDebInfo\onnx runtime_providers_cuda.vcxproj] detected during instantiation of "onnxruntime::common::Status onnxruntime::contrib::cuda::QkvToContext(const cudaDeviceProp &, cublasHandle_t &, cudaStream_t, onnxruntime::contrib::AttentionParameters &, onnxruntime::contrib::cuda::AttentionData<T> &) [wit h T=float]" (923): here ``` This happens for CUDA < 11.6. Our cmake script turns off onnxruntime_USE_FLASH_ATTENTION for CUDA < 11.6, which leaves the aforementioned variable unused outside of asserts (which are removed in release builds). The USE_FLASH_ATTENTION option was added by #14343
snnn
added a commit
that referenced
this pull request
Apr 13, 2023
### Description The following three lines are needed before including some cutlass header files, because cutlass uses "and"/"or" keywords. Generally it should not be a problem without this header, but nvcc is not strictly compliant to C++ standard. ```c++ #ifdef __cplusplus #include <ciso646> #endif ``` We didn't hit this problem because the above code exists in absl. We always include absl headers first. However, ABSL recently deleted them! abseil/abseil-cpp#1246 The cutlass dependency was introduced in #14343 , after we had abseil.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Add memory efficient attention from CUTLASS.
TODO (in next pull request):
(1) Need performance tests on different GPUs, then add a sequence length threshold (only activate it for long sequence length).
(2) Merge changes from NVIDIA/cutlass#773 when it is in cutlass master.
Average latency of bert-base-cased with FP16 in T4 GPU
In this test, no attention mask is used, and latency might change if you apply attention mask.
Using TRT kernel is slightly faster then using Cutlass kernel in most cases (except a combination of large batch and large sequence length). Note that it is end to end latency, so other parts (like add bias transpose) might also contribute the gap in latency.
Average latency of bert-base-cased with FP32 in T4 GPU
When sequence length is not multiply of 16, unfused kernel performance is worse except for very short sequence (<16). For unfused kernel, proper padding for inputs is important, while padding seems not help fused kernel.
For fp32, a good threshold seems to be: use unfused kernel when sequence length < 256, otherwise use cutlass fmha for longer sequence. This threshold 256 also considers other GPU like V100 and RT 1070 etc.
Average latency of bert-base-cased with FP32 in V100 GPU
Average latency of bert-base-cased with FP16 in V100 GPU
Average latency of bert-base-cased with FP32 on RTX 1070 GPU
Average latency of bert-base-cased with FP16 on RTX 1070 GPU
Average latency of stable diffusion v1.5 pipeline (50 steps, 512x512 images) with FP16 in T4 GPU
Motivation and Context