Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#4 from mthreads/build_musa
Browse files Browse the repository at this point in the history
[MTAI] build(system): enable build system in paddle for MUSA
  • Loading branch information
zhic-mt authored and mt-robot committed Aug 17, 2023
2 parents 2207d32 + 51c52af commit 9c96836
Show file tree
Hide file tree
Showing 555 changed files with 6,573 additions and 938 deletions.
3 changes: 2 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
ignore = dirty
[submodule "third_party/eigen3"]
path = third_party/eigen3
url = https://gitlab.com/libeigen/eigen.git
url = https://gitlab.com/paipinuo233/eigen.git
branch = support_musa
ignore = dirty
[submodule "third_party/snappy"]
path = third_party/snappy
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ repos:
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$
args:
- --extensions=c,cc,cxx,cpp,cu,cuh,h,hpp,hxx,kps
- --filter=-readability/fn_size,-build/include_what_you_use,-build/c++11,-whitespace/parens
- --filter=-readability/fn_size,-build/include_what_you_use,-build/c++11,-whitespace/parens,-whitespace/braces,-build/include
- --quiet
# Exclude third-party libraries
exclude: |
Expand Down
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ option(WITH_XPU_XFT "Compile PaddlePaddle with BAIDU XPU-XFT" OFF)
option(WITH_XPU_PLUGIN "Compile PaddlePaddle with BAIDU XPU plugin" OFF)
option(WITH_WIN_DUMP_DBG "Compile with windows core dump debug mode" OFF)
option(WITH_ROCM "Compile PaddlePaddle with ROCM platform" OFF)
option(WITH_MUSA "Compile PaddlePaddle with MUSA platform" OFF)
option(WITH_IPU "Compile PaddlePaddle with Graphcore IPU" OFF)
option(WITH_ONNXRUNTIME "Compile PaddlePaddle with ONNXRUNTIME" OFF)
option(WITH_CUSPARSELT "Compile PaddlePaddle with CUSPARSELT" OFF)
Expand Down Expand Up @@ -89,6 +90,9 @@ endif()
if(WITH_GPU AND WITH_ROCM)
message(FATAL_ERROR "Error when compile CUDA and ROCM at the same time")
endif()
if(WITH_GPU AND WITH_MUSA)
message(FATAL_ERROR "Error when compile CUDA and MUSA at the same time")
endif()

if(WITH_GPU AND NOT APPLE)
enable_language(CUDA)
Expand Down Expand Up @@ -346,6 +350,7 @@ if(LINUX
AND NOT WITH_CUSTOM_DEVICE
AND NOT WITH_GPU
AND NOT WITH_ROCM
AND NOT WITH_MUSA
AND NOT WITH_XPU
AND NOT WITH_XPU_KP
AND NOT WITH_XPU_XFT
Expand Down Expand Up @@ -503,6 +508,31 @@ else()
endif()
endif()

if(WITH_MUSA)
include(musa)
include(mudnn)
endif()

if(NOT WITH_MUSA AND WITH_MCCL)
message(
WARNING "Disable MCCL when compiling without MUSA. Force WITH_MCCL=OFF.")
set(WITH_MCCL
OFF
CACHE STRING "Disable MCCL when compiling without MUSA" FORCE)
endif()

if(WITH_MCCL)
add_definitions("-DPADDLE_WITH_MCCL")
include(mccl)
else()
if(WITH_MUSA)
message(
WARNING
"If the environment is multi-card, the WITH_MCCL option needs to be turned on, otherwise only a single card can be used."
)
endif()
endif()

if(WITH_HETERPS AND WITH_PSLIB)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
endif()
Expand Down
7 changes: 7 additions & 0 deletions cmake/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ elseif(WITH_ROCM)
if(${MIOPEN_VERSION} VERSION_LESS 2090)
message(FATAL_ERROR "Paddle needs MIOPEN >= 2.9 to compile")
endif()
elseif(WITH_MUSA)
add_definitions(-DPADDLE_WITH_MUSA)
add_definitions(-DEIGEN_USE_GPU)
add_definitions(-DEIGEN_USE_MUSA)
if(NOT MUDNN_FOUND)
message(FATAL_ERROR "Paddle needs MUDNN to compile")
endif()
else()
add_definitions(-DHPPL_STUB_FUNC)
list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS cu)
Expand Down
2 changes: 1 addition & 1 deletion cmake/external/eigen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ include(ExternalProject)
# update eigen to the commit id f612df27 on 03/16/2021
set(EIGEN_PREFIX_DIR ${THIRD_PARTY_PATH}/eigen3)
set(EIGEN_SOURCE_DIR ${THIRD_PARTY_PATH}/eigen3/src/extern_eigen3)
set(EIGEN_TAG f612df273689a19d25b45ca4f8269463207c4fee)
set(EIGEN_TAG 6ad1f10acbc311dd82b20cce7f5c305ae8c3eaa9)
set(SOURCE_DIR ${PADDLE_SOURCE_DIR}/third_party/eigen3)

if(WIN32)
Expand Down
120 changes: 120 additions & 0 deletions cmake/generic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ function(cc_binary TARGET_NAME)
if(WITH_ROCM)
target_link_libraries(${TARGET_NAME} ${ROCM_HIPRTC_LIB})
endif()
if(WITH_MUSA)
target_link_libraries(${TARGET_NAME} ${MUSARTC_LIB})
endif()

check_coverage_opt(${TARGET_NAME} ${cc_binary_SRCS})

Expand Down Expand Up @@ -452,6 +455,9 @@ function(cc_test_build TARGET_NAME)
if(WITH_ROCM)
target_link_libraries(${TARGET_NAME} ${ROCM_HIPRTC_LIB})
endif()
if(WITH_MUSA)
target_link_libraries(${TARGET_NAME} ${MUSARTC_LIB})
endif()
check_coverage_opt(${TARGET_NAME} ${cc_test_SRCS})
endif()
endfunction()
Expand Down Expand Up @@ -775,6 +781,111 @@ function(hip_test TARGET_NAME)
endif()
endfunction()

function(musa_library TARGET_NAME)
if(WITH_MUSA)
set(options STATIC static SHARED shared)
set(oneValueArgs "")
set(multiValueArgs SRCS DEPS)
cmake_parse_arguments(musa_library "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
if(musa_library_SRCS)
if(musa_library_SHARED OR musa_library_shared) # build *.so
musa_add_library(${TARGET_NAME} SHARED ${musa_library_SRCS})
else()
musa_add_library(${TARGET_NAME} STATIC ${musa_library_SRCS})
find_fluid_modules(${TARGET_NAME})
find_phi_modules(${TARGET_NAME})
endif()
if(musa_library_DEPS)
add_dependencies(${TARGET_NAME} ${musa_library_DEPS})
target_link_libraries(${TARGET_NAME} ${musa_library_DEPS})
endif()
# cpplint code style
foreach(source_file ${musa_library_SRCS})
string(REGEX REPLACE "\\.[^.]*$" "" source ${source_file})
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${source}.h)
list(APPEND musa_library_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/${source}.h)
endif()
endforeach()
else()
if(musa_library_DEPS)
list(REMOVE_DUPLICATES musa_library_DEPS)
generate_dummy_static_lib(
LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR
"generic.cmake:musa_library")

target_link_libraries(${TARGET_NAME} ${musa_library_DEPS})
add_dependencies(${TARGET_NAME} ${musa_library_DEPS})
else()
message(FATAL "Please specify source file or library in musa_library.")
endif()
endif()
endif()
endfunction()

function(musa_binary TARGET_NAME)
if(WITH_MUSA)
set(options "")
set(oneValueArgs "")
set(multiValueArgs SRCS DEPS)
cmake_parse_arguments(musa_binary "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
add_executable(${TARGET_NAME} ${musa_binary_SRCS})
if(musa_binary_DEPS)
target_link_libraries(${TARGET_NAME} ${musa_binary_DEPS})
add_dependencies(${TARGET_NAME} ${musa_binary_DEPS})
common_link(${TARGET_NAME})
endif()
endif()
endfunction()

function(musa_test TARGET_NAME)
if(WITH_MUSA AND WITH_TESTING)
set(oneValueArgs "")
set(multiValueArgs SRCS DEPS)
cmake_parse_arguments(musa_test "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
musa_add_executable(${TARGET_NAME} ${musa_test_SRCS})
# "-pthread -ldl -lrt" is defined in CMAKE_CXX_LINK_EXECUTABLE
target_link_options(${TARGET_NAME} PRIVATE -pthread -ldl -lrt)
get_property(os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES)
target_link_libraries(
${TARGET_NAME}
${musa_test_DEPS}
paddle_gtest_main
lod_tensor
memory
gtest
glog
phi
${os_dependency_modules})
add_dependencies(
${TARGET_NAME}
${musa_test_DEPS}
paddle_gtest_main
lod_tensor
memory
gtest
phi
glog)
common_link(${TARGET_NAME})
add_test(${TARGET_NAME} ${TARGET_NAME})
set_property(TEST ${TARGET_NAME} PROPERTY ENVIRONMENT
FLAGS_cpu_deterministic=true)
set_property(TEST ${TARGET_NAME} PROPERTY ENVIRONMENT
FLAGS_init_allocated_mem=true)
set_property(TEST ${TARGET_NAME} PROPERTY ENVIRONMENT
FLAGS_cudnn_deterministic=true)
set_property(
TEST ${TARGET_NAME}
PROPERTY
ENVIRONMENT
"LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/python/paddle/libs:$LD_LIBRARY_PATH"
)
endif()
endfunction()

function(xpu_library TARGET_NAME)
if(WITH_XPU_KP)
set(options STATIC static SHARED shared)
Expand Down Expand Up @@ -1274,6 +1385,15 @@ function(math_library TARGET)
${TARGET}
SRCS ${cc_srcs} ${cu_srcs}
DEPS ${math_library_DEPS} ${math_common_deps})
elseif(WITH_MUSA)
musa_library(
${TARGET}
SRCS
${cc_srcs}
${cu_srcs}
DEPS
${math_library_DEPS}
${math_common_deps})
elseif(${cc_srcs_len} GREATER 0)
cc_library(
${TARGET}
Expand Down
52 changes: 52 additions & 0 deletions cmake/mccl.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
if(NOT WITH_MUSA)
return()
endif()

# Now we don't support MCCL on windows
if(WIN32)
return()
endif()

if(WITH_MCCL)
set(MCCL_ROOT
"/usr/local/musa/"
CACHE PATH "MCCL ROOT")
find_path(
MCCL_INCLUDE_DIR mccl.h
PATHS ${MCCL_ROOT} ${MCCL_ROOT}/include ${MCCL_ROOT}/local/include
$ENV{MCCL_ROOT} $ENV{MCCL_ROOT}/include $ENV{MCCL_ROOT}/local/include
NO_DEFAULT_PATH)

if(MCCL_INCLUDE_DIR)
file(READ ${MCCL_INCLUDE_DIR}/mccl.h MCCL_VERSION_FILE_CONTENTS)

string(REGEX MATCH "define MCCL_MAJOR +([0-9]+)" MCCL_MAJOR_VERSION
"${MCCL_VERSION_FILE_CONTENTS}")
string(REGEX REPLACE "define MCCL_MAJOR +([0-9]+)" "\\1" MCCL_MAJOR_VERSION
"${MCCL_MAJOR_VERSION}")
string(REGEX MATCH "define MCCL_MINOR +([0-9]+)" MCCL_MINOR_VERSION
"${MCCL_VERSION_FILE_CONTENTS}")
string(REGEX REPLACE "define MCCL_MINOR +([0-9]+)" "\\1" MCCL_MINOR_VERSION
"${MCCL_MINOR_VERSION}")
string(REGEX MATCH "define MCCL_PATCH +([0-9]+)" MCCL_PATCH_VERSION
"${MCCL_VERSION_FILE_CONTENTS}")
string(REGEX REPLACE "define MCCL_PATCH +([0-9]+)" "\\1" MCCL_PATCH_VERSION
"${MCCL_PATCH_VERSION}")
if(NOT MCCL_MAJOR_VERSION)
set(MCCL_VERSION "???")
else()
math(EXPR MCCL_VERSION "${MCCL_MAJOR_VERSION} * 1000 +
${MCCL_MINOR_VERSION} * 100 + ${MCCL_PATCH_VERSION}")
endif()
add_definitions("-DMCCL_VERSION_CODE=$MCCL_VERSION")
include_directories(${MCCL_INCLUDE_DIR})

message(STATUS "Current MCCL header is ${MCCL_INCLUDE_DIR}/mccl.h. ")
message(
STATUS
"Current MCCL version is "
"v${MCCL_MAJOR_VERSION}.${MCCL_MINOR_VERSION}.${MCCL_PATCH_VERSION} ")
else()
message(FATAL_ERROR "WITH_MCCL is enabled but mccl.h file is not found!")
endif()
endif()
92 changes: 92 additions & 0 deletions cmake/mudnn.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
if(NOT WITH_MUSA)
return()
endif()

if(WIN32)
return()
else()
set(MUDNN_ROOT
"/usr/local/musa"
CACHE PATH "MUDNN ROOT")
endif()

find_path(
MUDNN_INCLUDE_DIR mudnn.h
PATHS ${MUDNN_ROOT} ${MUDNN_ROOT}/include $ENV{MUDNN_ROOT}
$ENV{MUDNN_ROOT}/include ${MUSA_TOOLKIT_INCLUDE}
NO_DEFAULT_PATH)

set(TARGET_ARCH "x86_64")
if(NOT ${CMAKE_SYSTEM_PROCESSOR})
set(TARGET_ARCH ${CMAKE_SYSTEM_PROCESSOR})
endif()

list(
APPEND
MUDNN_CHECK_LIBRARY_DIRS
${MUDNN_ROOT}
${MUDNN_ROOT}/lib64
${MUDNN_ROOT}/lib
${MUDNN_ROOT}/lib/x64
${MUDNN_ROOT}/lib/${TARGET_ARCH}-linux-gnu
$ENV{MUDNN_ROOT}
$ENV{MUDNN_ROOT}/lib64
$ENV{MUDNN_ROOT}/lib
$ENV{MUDNN_ROOT}/lib/x64
/usr/lib
${MUSA_TOOLKIT_ROOT_DIR}
${MUSA_TOOLKIT_ROOT_DIR}/lib/x64)
set(MUDNN_LIB_NAME "")

if(LINUX)
set(MUDNN_LIB_NAME "libmudnn.so")
endif()

find_library(
MUDNN_LIBRARY
NAMES ${MUDNN_LIB_NAME}
PATHS ${MUDNN_CHECK_LIBRARY_DIRS} ${MUDNN_INCLUDE_DIR}
NO_DEFAULT_PATH
DOC "Path to muDNN library.")

if(MUDNN_INCLUDE_DIR AND MUDNN_LIBRARY)
set(MUDNN_FOUND ON)
else()
set(MUDNN_FOUND OFF)
endif()

macro(find_mudnn_version mudnn_version_file)
file(READ ${mudnn_version_file} MUDNN_VERSION_FILE_CONTENTS)
get_filename_component(MUDNN_LIB_PATH ${MUDNN_LIBRARY} DIRECTORY)

string(REGEX MATCH "define MUDNN_VERSION_MAJOR +([0-9]+)" MUDNN_MAJOR_VERSION
"${MUDNN_VERSION_FILE_CONTENTS}")
string(REGEX REPLACE "define MUDNN_VERSION_MAJOR +([0-9]+)" "\\1"
MUDNN_MAJOR_VERSION "${MUDNN_MAJOR_VERSION}")
string(REGEX MATCH "define MUDNN_VERSION_MINOR +([0-9]+)" MUDNN_MINOR_VERSION
"${MUDNN_VERSION_FILE_CONTENTS}")
string(REGEX REPLACE "define MUDNN_VERSION_MINOR +([0-9]+)" "\\1"
MUDNN_MINOR_VERSION "${MUDNN_MINOR_VERSION}")
string(REGEX MATCH "define MUDNN_VERSION_PATCH +([0-9]+)" MUDNN_PATCH_VERSION
"${MUDNN_VERSION_FILE_CONTENTS}")
string(REGEX REPLACE "define MUDNN_VERSION_PATCH +([0-9]+)" "\\1"
MUDNN_PATCH_VERSION "${MUDNN_PATCH_VERSION}")

if(NOT MUDNN_MAJOR_VERSION)
set(MUDNN_VERSION "???")
else()
add_definitions("-DMUDNN_MAJOR_VERSION=\"${MUDNN_MAJOR_VERSION}\"")
math(EXPR MUDNN_VERSION "${MUDNN_MAJOR_VERSION} * 1000 +
${MUDNN_MINOR_VERSION} * 100 + ${MUDNN_PATCH_VERSION}")
message(STATUS "Current muDNN version file is ${mudnn_version_file} ")
message(
STATUS
"Current muDNN version is v${MUDNN_MAJOR_VERSION}.${MUDNN_MINOR_VERSION}.${MUDNN_PATCH_VERSION}. "
)
endif()
endmacro()

if(MUDNN_FOUND)
find_mudnn_version(${MUDNN_INCLUDE_DIR}/mudnn_version.h)
include_directories(${MUDNN_INCLUDE_DIR})
endif()
Loading

0 comments on commit 9c96836

Please sign in to comment.