-
Couldn't load subscription status.
- Fork 5.9k
Integration flash attention #49869
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
Merged
Merged
Integration flash attention #49869
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8c27292
flash attn
kuizhiqing 02c4e0a
seed
kuizhiqing a737fbc
almost
kuizhiqing a196895
softmax
kuizhiqing fa9af3e
fix workspace
kuizhiqing 9cabe56
add unitest; linux only
kuizhiqing df96cee
fix setup
kuizhiqing d700caf
Merge branch 'PaddlePaddle:develop' into flashattn
kuizhiqing 59c3a77
fix datatype include
kuizhiqing 5053956
Merge remote-tracking branch 'baidu/flashattn' into flashattn
kuizhiqing 06bbf00
fix setup typo
kuizhiqing af7360f
fix def scope
kuizhiqing 48544e2
new error api
kuizhiqing d41dbf6
use paddle fork
kuizhiqing 9d4b829
Merge remote-tracking branch 'origin/develop' into flashattn
kuizhiqing 5290470
fix attr bug; complete ut
kuizhiqing 04e45a4
update flash hash
kuizhiqing 647b06f
fix rng reset
kuizhiqing 2467a4e
Merge remote-tracking branch 'pd/develop' into flashattn
kuizhiqing 7ad71a0
fix offset
kuizhiqing bb70b92
fix comments
kuizhiqing 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 hidden or 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,108 @@ | ||
| # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # 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. | ||
|
|
||
| include(ExternalProject) | ||
|
|
||
| add_definitions(-DPADDLE_WITH_FLASHATTN) | ||
|
|
||
| set(FLASHATTN_PREFIX_DIR ${THIRD_PARTY_PATH}/flashattn) | ||
| set(FLASHATTN_SOURCE_SUBDIR csrc/flash_attn) | ||
| set(FLASHATTN_INSTALL_DIR ${THIRD_PARTY_PATH}/install/flashattn) | ||
| set(FLASHATTN_REPOSITORY ${GIT_URL}/PaddlePaddle/flash-attention.git) | ||
| set(FLASHATTN_TAG f0edf243a813a65d05c75fcb331b2a95faf96bbc) | ||
|
|
||
| set(FLASHATTN_INCLUDE_DIR | ||
| "${FLASHATTN_INSTALL_DIR}/include" | ||
| CACHE PATH "flash-attn Directory" FORCE) | ||
| set(FLASHATTN_LIB_DIR | ||
| "${FLASHATTN_INSTALL_DIR}/lib" | ||
| CACHE PATH "flash-attn Library Directory" FORCE) | ||
|
|
||
| if(WIN32) | ||
| set(FLASHATTN_LIBRARIES | ||
| "${FLASHATTN_INSTALL_DIR}/bin/flashattn${CMAKE_SHARED_LIBRARY_SUFFIX}" | ||
| CACHE FILEPATH "flash-attn Library" FORCE) | ||
| else() | ||
| set(FLASHATTN_LIBRARIES | ||
| "${FLASHATTN_INSTALL_DIR}/lib/libflashattn${CMAKE_SHARED_LIBRARY_SUFFIX}" | ||
| CACHE FILEPATH "flash-attn Library" FORCE) | ||
| endif() | ||
|
|
||
| if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" | ||
| OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" | ||
| OR WIN32) | ||
| set(USE_OMP OFF) | ||
| else() | ||
| set(USE_OMP ON) | ||
| endif() | ||
|
|
||
| if(WIN32) | ||
| set(FLASHATTN_C_FLAGS $<FILTER:${CMAKE_C_FLAGS},EXCLUDE,/Zc:inline>) | ||
| set(FLASHATTN_C_FLAGS_DEBUG | ||
| $<FILTER:${CMAKE_C_FLAGS_DEBUG},EXCLUDE,/Zc:inline>) | ||
| set(FLASHATTN_C_FLAGS_RELEASE | ||
| $<FILTER:${CMAKE_C_FLAGS_RELEASE},EXCLUDE,/Zc:inline>) | ||
| set(FLASHATTN_CXX_FLAGS $<FILTER:${CMAKE_CXX_FLAGS},EXCLUDE,/Zc:inline>) | ||
| set(FLASHATTN_CXX_FLAGS_RELEASE | ||
| $<FILTER:${CMAKE_CXX_FLAGS_RELEASE},EXCLUDE,/Zc:inline>) | ||
| set(FLASHATTN_CXX_FLAGS_DEBUG | ||
| $<FILTER:${CMAKE_CXX_FLAGS_DEBUG},EXCLUDE,/Zc:inline>) | ||
| else() | ||
| set(FLASHATTN_C_FLAGS ${CMAKE_C_FLAGS}) | ||
| set(FLASHATTN_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG}) | ||
| set(FLASHATTN_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE}) | ||
| set(FLASHATTN_CXX_FLAGS ${CMAKE_CXX_FLAGS}) | ||
| set(FLASHATTN_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) | ||
| set(FLASHATTN_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) | ||
| endif() | ||
|
|
||
| ExternalProject_Add( | ||
| extern_flashattn | ||
| ${EXTERNAL_PROJECT_LOG_ARGS} ${SHALLOW_CLONE} | ||
| GIT_REPOSITORY ${FLASHATTN_REPOSITORY} | ||
| GIT_TAG ${FLASHATTN_TAG} | ||
| PREFIX ${FLASHATTN_PREFIX_DIR} | ||
| SOURCE_SUBDIR ${FLASHATTN_SOURCE_SUBDIR} | ||
| UPDATE_COMMAND "" | ||
| PATCH_COMMAND "" | ||
| #BUILD_ALWAYS 1 | ||
| CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} | ||
| -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} | ||
| -DCMAKE_C_FLAGS=${FLASHATTN_C_FLAGS} | ||
| -DCMAKE_C_FLAGS_DEBUG=${FLASHATTN_C_FLAGS_DEBUG} | ||
| -DCMAKE_C_FLAGS_RELEASE=${FLASHATTN_C_FLAGS_RELEASE} | ||
| -DCMAKE_CXX_FLAGS=${FLASHATTN_CXX_FLAGS} | ||
| -DCMAKE_CXX_FLAGS_RELEASE=${FLASHATTN_CXX_FLAGS_RELEASE} | ||
| -DCMAKE_CXX_FLAGS_DEBUG=${FLASHATTN_CXX_FLAGS_DEBUG} | ||
| -DCMAKE_INSTALL_PREFIX=${FLASHATTN_INSTALL_DIR} | ||
| -DWITH_GPU=${WITH_GPU} | ||
| -DWITH_ROCM=${WITH_ROCM} | ||
| -DWITH_OMP=${USE_OMP} | ||
| -DBUILD_SHARED=ON | ||
| -DCMAKE_POSITION_INDEPENDENT_CODE=ON | ||
| -DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE} | ||
| ${EXTERNAL_OPTIONAL_ARGS} | ||
| CMAKE_CACHE_ARGS | ||
| -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE} | ||
| -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON | ||
| -DCMAKE_INSTALL_PREFIX:PATH=${FLASHATTN_INSTALL_DIR} | ||
| BUILD_BYPRODUCTS ${FLASHATTN_LIBRARIES}) | ||
|
|
||
| message(STATUS "flash-attn library: ${FLASHATTN_LIBRARIES}") | ||
| get_filename_component(FLASHATTN_LIBRARY_PATH ${FLASHATTN_LIBRARIES} DIRECTORY) | ||
| include_directories(${FLASHATTN_INCLUDE_DIR}) | ||
|
|
||
| add_library(flashattn INTERFACE) | ||
| #set_property(TARGET flashattn PROPERTY IMPORTED_LOCATION ${FLASHATTN_LIBRARIES}) | ||
| add_dependencies(flashattn extern_flashattn) | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| /* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
kuizhiqing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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. */ | ||
|
|
||
| #include "paddle/phi/backends/dynload/flashattn.h" | ||
|
|
||
| namespace phi { | ||
| namespace dynload { | ||
|
|
||
| std::once_flag flashattn_dso_flag; | ||
| void* flashattn_dso_handle = nullptr; | ||
|
|
||
| #define DEFINE_WRAP(__name) DynLoad__##__name __name | ||
|
|
||
| FLASHATTN_ROUTINE_EACH(DEFINE_WRAP); | ||
|
|
||
| } // namespace dynload | ||
| } // namespace phi | ||
This file contains hidden or 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,56 @@ | ||
| /* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
|
|
||
| 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. */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <mutex> // NOLINT | ||
|
|
||
| #include "flashattn/include/flash_attn.h" | ||
| #include "paddle/phi/backends/dynload/dynamic_loader.h" | ||
| #include "paddle/phi/backends/dynload/port.h" | ||
|
|
||
| namespace phi { | ||
| namespace dynload { | ||
|
|
||
| extern std::once_flag flashattn_dso_flag; | ||
| extern void* flashattn_dso_handle; | ||
|
|
||
| #define DYNAMIC_LOAD_FLASHATTN_WRAP(__name) \ | ||
| struct DynLoad__##__name { \ | ||
| template <typename... Args> \ | ||
| auto operator()(Args... args) -> DECLARE_TYPE(__name, args...) { \ | ||
| using flashattnFunc = decltype(&::__name); \ | ||
| std::call_once(flashattn_dso_flag, []() { \ | ||
| flashattn_dso_handle = phi::dynload::GetFlashAttnDsoHandle(); \ | ||
| }); \ | ||
| static void* p_##__name = dlsym(flashattn_dso_handle, #__name); \ | ||
| return reinterpret_cast<flashattnFunc>(p_##__name)(args...); \ | ||
| } \ | ||
| }; \ | ||
| extern DynLoad__##__name __name | ||
|
|
||
| #define DECLARE_DYNAMIC_LOAD_FLASHATTN_WRAP(__name) \ | ||
| DYNAMIC_LOAD_FLASHATTN_WRAP(__name) | ||
|
|
||
| #define FLASHATTN_ROUTINE_EACH(__macro) \ | ||
| __macro(flash_attn_fwd); \ | ||
| __macro(flash_attn_bwd); \ | ||
| __macro(flash_attn_error); | ||
|
|
||
| FLASHATTN_ROUTINE_EACH(DECLARE_DYNAMIC_LOAD_FLASHATTN_WRAP); | ||
|
|
||
| #undef DYNAMIC_LOAD_FLASHATTN_WRAP | ||
|
|
||
| } // namespace dynload | ||
| } // namespace phi |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
| // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
kuizhiqing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "paddle/phi/core/dense_tensor.h" | ||
| #include "paddle/phi/core/device_context.h" | ||
|
|
||
| namespace phi { | ||
|
|
||
| template <typename T, typename Context> | ||
| void FlashAttnGradKernel(const Context& ctx, | ||
| const DenseTensor& q, | ||
| const DenseTensor& k, | ||
| const DenseTensor& v, | ||
| const DenseTensor& out, | ||
| const DenseTensor& softmax_lse, | ||
| const DenseTensor& seed_offset, | ||
| const DenseTensor& dout, | ||
| float dropout, | ||
| bool causal, | ||
| DenseTensor* dq, | ||
| DenseTensor* dk, | ||
| DenseTensor* dv); | ||
|
|
||
| } // namespace phi | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.