|
| 1 | +# Copyright (c) 2025 Moore Threads Technology Co., Ltd("Moore Threads"). All |
| 2 | +# rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | +# use this file except in compliance with the License. You may obtain a copy of |
| 6 | +# the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations under |
| 14 | +# the License |
| 15 | + |
| 16 | +cmake_minimum_required(VERSION 3.10) |
| 17 | + |
| 18 | +project(paddle-musa CXX C) |
| 19 | + |
| 20 | +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") |
| 21 | + |
| 22 | +set(MUSA_PATH |
| 23 | + "/usr/local/musa" |
| 24 | + CACHE PATH "Path to which musa has been installed") |
| 25 | +list(APPEND CMAKE_MODULE_PATH "${MUSA_PATH}/cmake") |
| 26 | +find_package(MUSA REQUIRED) |
| 27 | + |
| 28 | +set(PLUGIN_NAME "paddle-musa") |
| 29 | +set(PLUGIN_VERSION "1.0.0") |
| 30 | +set(PADDLE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../Paddle/") |
| 31 | +set(THIRD_PARTY_PATH |
| 32 | + "${PADDLE_SOURCE_DIR}/build/third_party" |
| 33 | + CACHE PATH "Third party libraries directory.") |
| 34 | + |
| 35 | +option(WITH_TESTING "compile with unit testing" ON) |
| 36 | +option(ON_INFER "compile with inference c++ lib" OFF) |
| 37 | +option(WITH_MKL "compile with mkl support" ON) |
| 38 | +option(WITH_COVERAGE "Compile PaddlePaddle with code coverage" OFF) |
| 39 | +option(WITH_MUSA "Compile PaddlePaddle with MUSA" ON) |
| 40 | +option(WITH_MCCL "Compile PaddlePaddle with MCCL support" ON) |
| 41 | + |
| 42 | +message(STATUS "CXX compiler: ${CMAKE_CXX_COMPILER}, version: " |
| 43 | + "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}") |
| 44 | +message(STATUS "C compiler: ${CMAKE_C_COMPILER}, version: " |
| 45 | + "${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}") |
| 46 | +message(STATUS "AR tools: ${CMAKE_AR}") |
| 47 | + |
| 48 | +if(NOT WITH_TESTING) |
| 49 | + set(NO_PADDLE_SUBMODULE ON) |
| 50 | +endif() |
| 51 | + |
| 52 | +if(NOT WITH_MUSA AND WITH_MCCL) |
| 53 | + message( |
| 54 | + WARNING "Disable MCCL when compiling without MUSA. Force WITH_MCCL=OFF.") |
| 55 | + set(WITH_MCCL |
| 56 | + OFF |
| 57 | + CACHE STRING "Disable MCCL when compiling without MUSA" FORCE) |
| 58 | +endif() |
| 59 | + |
| 60 | +# build shared library |
| 61 | +include(utils) |
| 62 | +include(paddle) |
| 63 | +include(external/eigen) |
| 64 | +include(version) |
| 65 | +include(generic) |
| 66 | +include_directories(${CMAKE_SOURCE_DIR}) |
| 67 | +include_directories( |
| 68 | + ${PADDLE_INC_DIR} |
| 69 | + ${CMAKE_SOURCE_DIR} |
| 70 | + ${CMAKE_SOURCE_DIR}/kernels |
| 71 | + ${CMAKE_SOURCE_DIR}/kernels/paddle_kernels |
| 72 | + ${CMAKE_SOURCE_DIR}/hack/cuda_hack |
| 73 | + ${CMAKE_SOURCE_DIR}/hack/thrust_hack |
| 74 | + ${CMAKE_SOURCE_DIR}/hack/paddle_backend_dyload_hack |
| 75 | + ${CMAKE_SOURCE_DIR}/hack/paddle_platform_dyload_hack) |
| 76 | +include_directories(${PADDLE_SOURCE_DIR}) |
| 77 | +file( |
| 78 | + GLOB |
| 79 | + PADDLE_KERNEL_SRCS |
| 80 | + ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/abs_kernel.cu |
| 81 | + ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/abs_grad_kernel.cu |
| 82 | + # ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/cross_entropy2_kernel.cu |
| 83 | + # ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/cross_entropy2_grad_kernel.cu |
| 84 | + ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/cross_kernel.cu |
| 85 | + ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/cross_grad_kernel.cu |
| 86 | + # ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/activation_kernel.cu |
| 87 | + # ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/selected_rows/activation_kernel.cc |
| 88 | + # ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/activation_grad_kernel.cu |
| 89 | + ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/adam_kernel.cu |
| 90 | + ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/adamw_kernel.cu |
| 91 | + ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/selected_rows/gpu/adamw_kernel.cu |
| 92 | + ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/adamax_kernel.cu |
| 93 | + # ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/angle_grad_kernel.cu |
| 94 | + ${PADDLE_SOURCE_DIR}/paddle/phi/kernels/gpu/angle_kernel.cu) |
| 95 | +file( |
| 96 | + GLOB_RECURSE PLUGIN_SRCS |
| 97 | + RELATIVE ${CMAKE_SOURCE_DIR} |
| 98 | + ${CMAKE_SOURCE_DIR}/runtime/*.cc kernels/*.cc kernels/paddle_kernels/*.cc |
| 99 | + # kernels/paddle_kernels/*.mu |
| 100 | + hack/paddle_backend_dyload_hack/*.cc hack/paddle_platform_dyload_hack/*.cc) |
| 101 | +set(CUSTOM_DEVICE_SRCS ${PLUGIN_SRCS} ${PADDLE_KERNEL_SRCS}) |
| 102 | +# target_include_directories(${PLUGIN_NAME} PUBLIC |
| 103 | +# ${CMAKE_SOURCE_DIR}/hack/paddle_backend_dyload_hack) |
| 104 | +include(musa) |
| 105 | +include(mudnn) |
| 106 | +musa_add_library(${PLUGIN_NAME} SHARED ${CUSTOM_DEVICE_SRCS}) |
| 107 | +target_include_directories( |
| 108 | + ${PLUGIN_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/hack/cuda_hack |
| 109 | + ${CMAKE_SOURCE_DIR}/hack/thrust_hack) |
| 110 | +link_directories(${PADDLE_LIB_DIR}) |
| 111 | + |
| 112 | +# link paddle core lib |
| 113 | +if(ON_INFER) |
| 114 | + target_link_directories(${PLUGIN_NAME} ${PADDLE_INFERENCE_LIB_DIR}) |
| 115 | + target_link_libraries(${PLUGIN_NAME} paddle_inference) |
| 116 | +else() |
| 117 | + target_link_libraries(${PLUGIN_NAME} ${PADDLE_CORE_LIB}) |
| 118 | + # target_link_libraries(${PLUGIN_NAME} PRIVATE pybind) |
| 119 | +endif() |
| 120 | + |
| 121 | +# link musa rt mudnn |
| 122 | +target_link_libraries(${PLUGIN_NAME} ${musart_lib} ${mudnn_lib}) |
| 123 | +# target_link_libraries(${PLUGIN_NAME} ${mudnn_lib}) |
| 124 | + |
| 125 | +# link mccl |
| 126 | +if(WITH_MCCL) |
| 127 | + add_definitions("-DPADDLE_WITH_MCCL") |
| 128 | + include(mccl) |
| 129 | + target_link_libraries(${PLUGIN_NAME} ${mccl_lib}) |
| 130 | +else() |
| 131 | + if(WITH_MUSA) |
| 132 | + message( |
| 133 | + WARNING |
| 134 | + "If the environment is multi-card, the WITH_MCCL option needs to be turned on, otherwise only a single card can be used." |
| 135 | + ) |
| 136 | + endif() |
| 137 | +endif() |
| 138 | + |
| 139 | +# link third party |
| 140 | +include(third_party) |
| 141 | +add_dependencies(${PLUGIN_NAME} third_party) |
| 142 | +target_link_libraries(${PLUGIN_NAME} eigen3 gflags glog) |
| 143 | +target_compile_definitions( |
| 144 | + ${PLUGIN_NAME} |
| 145 | + PUBLIC __NVCC__=__MUSACC__ |
| 146 | + __CUDACC_VER_MAJOR__=99 |
| 147 | + __CUDACC_VER_MINOR__=10000 |
| 148 | + MARCH_TYPE=310 # TODO: the version num should be obtained automatically |
| 149 | + # here |
| 150 | + GPUContext=CustomContext |
| 151 | + KPSContext=CustomContext |
| 152 | + STREAM_TYPE=musaStream_t |
| 153 | + EVENT_TYPE=musaEvent_t |
| 154 | + EIGEN_USE_GPU=1 |
| 155 | + PADDLE_WITH_MUSA=1 |
| 156 | + PADDLE_WITH_CUSTOM_DEVICE=1 |
| 157 | + EIGEN_USE_MUSA=1) |
| 158 | + |
| 159 | +# packing wheel package |
| 160 | +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in |
| 161 | + ${CMAKE_CURRENT_BINARY_DIR}/setup.py) |
| 162 | + |
| 163 | +add_custom_command( |
| 164 | + TARGET ${PLUGIN_NAME} |
| 165 | + POST_BUILD |
| 166 | + COMMAND ${CMAKE_COMMAND} -E remove -f ${CMAKE_CURRENT_BINARY_DIR}/python/ |
| 167 | + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/python/ |
| 168 | + COMMAND ${CMAKE_COMMAND} -E make_directory |
| 169 | + ${CMAKE_CURRENT_BINARY_DIR}/python/paddle_custom_device/ |
| 170 | + COMMAND |
| 171 | + ${CMAKE_COMMAND} -E copy_if_different |
| 172 | + ${CMAKE_CURRENT_BINARY_DIR}/lib${PLUGIN_NAME}.so |
| 173 | + ${CMAKE_CURRENT_BINARY_DIR}/python/paddle_custom_device/ |
| 174 | + COMMENT "Creating plugin directories------>>>") |
| 175 | + |
| 176 | +find_package( |
| 177 | + Python |
| 178 | + COMPONENTS Interpreter |
| 179 | + REQUIRED) |
| 180 | + |
| 181 | +add_custom_command( |
| 182 | + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/python/.timestamp |
| 183 | + COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py bdist_wheel |
| 184 | + DEPENDS ${PLUGIN_NAME} |
| 185 | + COMMENT "Packing whl packages------>>>") |
| 186 | + |
| 187 | +add_custom_target(python_package ALL |
| 188 | + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/python/.timestamp) |
| 189 | + |
| 190 | +if(WITH_TESTING) |
| 191 | + set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../Paddle") |
| 192 | + enable_testing() |
| 193 | + add_subdirectory(tests) |
| 194 | + add_custom_command( |
| 195 | + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tests/.timestamp |
| 196 | + COMMAND cp -r ${CMAKE_SOURCE_DIR}/tests ${CMAKE_CURRENT_BINARY_DIR}) |
| 197 | + add_custom_target(python_tests ALL |
| 198 | + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tests/.timestamp) |
| 199 | +endif() |
0 commit comments