Skip to content
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

[WIP] Code merge | Merge CINN into Paddle #54405

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
36 changes: 32 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ 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)
option(WITH_SETUP_INSTALL "Compile PaddlePaddle with setup.py" OFF)
# Note(zhouwei): It use option above, so put here
include(init)
include(generic) # simplify cmake module
include(experimental) # experimental build options

if(WITH_GPU AND WITH_XPU)
message(FATAL_ERROR "Error when compile GPU and XPU at the same time")
Expand Down Expand Up @@ -308,6 +304,38 @@ option(WITH_CUDNN_FRONTEND
option(WITH_CUDNN_DSO "Compile PaddlePaddle with cuDNN dynamic-link libraries"
OFF)
option(WITH_NEWIR "Compile PaddlePaddle with NEWIR" ON)
option(CINN_ONLY "Compile CINN only in Paddle" OFF)

#------------- cinn cmake config start --------------

if(WITH_CINN)
message(STATUS "Compile Paddle with CINN.")
include(cmake/cinn.cmake)
add_definitions(-DPADDLE_WITH_CINN)
if(WITH_GPU)
add_definitions(-DCINN_WITH_CUDA)
add_definitions(-DCINN_WITH_CUDNN)
endif()
if(WITH_MKL)
add_definitions(-DCINN_WITH_MKL_CBLAS)
add_definitions(-DCINN_WITH_MKLDNN)
endif()

if(CINN_ONLY)
if(WITH_PYTHON)
add_subdirectory(python)
endif()
add_subdirectory(test)
return()
endif()
endif()

#------------- cinn cmake config end --------------

# Note(zhouwei): It use option above, so put here
include(init)
include(generic) # simplify cmake module
include(experimental) # experimental build options

if(WITH_RECORD_BUILDTIME)
set_property(
Expand Down
305 changes: 305 additions & 0 deletions cmake/cinn.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY TRUE)

set(CINN_THIRD_PARTY_PATH "${CMAKE_BINARY_DIR}/thirds")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(DOWNLOAD_MODEL_DIR "${CINN_THIRD_PARTY_PATH}/model")

if(NOT DEFINED ENV{runtime_include_dir})
message(
STATUS
"set runtime_include_dir: ${CMAKE_SOURCE_DIR}/paddle/cinn/runtime/cuda")
set(ENV{runtime_include_dir} "${CMAKE_SOURCE_DIR}/paddle/cinn/runtime/cuda")
add_definitions(
-DRUNTIME_INCLUDE_DIR="${CMAKE_SOURCE_DIR}/paddle/cinn/runtime/cuda")
endif()

if(WITH_TESTING)
add_definitions(-DCINN_WITH_TEST)
endif()
if(WITH_DEBUG)
add_definitions(-DCINN_WITH_DEBUG)
endif()

set(WITH_MKLDNN ON)

# TODO(zhhsplendid): CINN has lots of warnings during early development.
# They will be treated as errors under paddle. We set no-error now and we will
# clean the code in the future.
add_definitions(-w)

include(cmake/cinn/version.cmake)
# include the customized configures
if(EXISTS ${CMAKE_BINARY_DIR}/config.cmake)
include(${CMAKE_BINARY_DIR}/config.cmake)
endif()

if(WITH_GPU)
message(STATUS "Enable CINN CUDA")
add_definitions(-DCINN_WITH_CUDA)
message(STATUS "Enable CINN CUDNN")
add_definitions(-DCINN_WITH_CUDNN)
enable_language(CUDA)
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
include_directories(${CMAKE_SOURCE_DIR}/paddle/cinn/runtime/cuda)
include_directories(/usr/lib/x86_64-linux-gnu)
set(CUDA_SEPARABLE_COMPILATION ON)

cuda_select_nvcc_arch_flags(ARCH_FLAGS Auto)
list(APPEND CUDA_NVCC_FLAGS ${ARCH_FLAGS})

message(
STATUS
"copy paddle/cinn/common/float16.h paddle/cinn/common/bfloat16.h to $ENV{runtime_include_dir}"
)
file(COPY paddle/cinn/common/float16.h paddle/cinn/common/bfloat16.h
DESTINATION $ENV{runtime_include_dir})

find_library(CUDASTUB libcuda.so HINTS ${CUDA_TOOLKIT_ROOT_DIR}/lib64/stubs/
REQUIRED)
find_library(CUBLAS libcublas.so HINTS ${CUDA_TOOLKIT_ROOT_DIR}/lib64
/usr/lib REQUIRED)
find_library(CUDNN libcudnn.so HINTS ${CUDA_TOOLKIT_ROOT_DIR}/lib64 /usr/lib
REQUIRED)
find_library(CURAND libcurand.so HINTS ${CUDA_TOOLKIT_ROOT_DIR}/lib64
/usr/lib REQUIRED)
find_library(CUSOLVER libcusolver.so HINTS ${CUDA_TOOLKIT_ROOT_DIR}/lib64
/usr/lib REQUIRED)
endif()

set(cinnapi_src CACHE INTERNAL "" FORCE)
set(core_src CACHE INTERNAL "" FORCE)
set(core_includes CACHE INTERNAL "" FORCE)
set(core_proto_includes CACHE INTERNAL "" FORCE)
include(cmake/cinn/system.cmake)
include(cmake/cinn/core.cmake)

include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_BINARY_DIR})

include(cmake/cinn/external/pybind11.cmake)
include(cmake/cinn/external/gflags.cmake)
include(cmake/cinn/external/glog.cmake)
include(cmake/cinn/external/gtest.cmake)
include(cmake/cinn/external/absl.cmake)
include(cmake/cinn/nvrtc.cmake)
include(cmake/cinn/nvtx.cmake)
include(cmake/cinn/external/llvm.cmake)
include(cmake/cinn/external/isl.cmake)
include(cmake/cinn/external/ginac.cmake)
include(cmake/cinn/external/protobuf.cmake)
include(cmake/cinn/external/mklml.cmake)
include(cmake/cinn/external/mkldnn.cmake)
include(cmake/cinn/external/openmp.cmake)
include(cmake/cinn/external/jitify.cmake)

set(LINK_FLAGS
"-Wl,--version-script ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cinn/export.map"
CACHE INTERNAL "")
set(global_test_args
"--cinn_x86_builtin_code_root=${CMAKE_SOURCE_DIR}/paddle/cinn/backends")

set(Python_VIRTUALENV FIRST)

if(NOT PYTHON_EXECUTABLE)
find_package(PythonInterp ${PY_VERSION} REQUIRED)
endif()

if(NOT PYTHON_LIBRARIES)
find_package(PythonLibs ${PY_VERSION} REQUIRED)
endif()

message(STATUS "PYTHON_LIBRARIES: ${PYTHON_LIBRARIES}")
message(STATUS "PYTHON_INCLUDE_DIR: ${PYTHON_INCLUDE_DIR}")

include_directories(${PYTHON_INCLUDE_DIR})

set(core_deps CACHE INTERNAL "" FORCE)
set(hlir_src CACHE INTERNAL "" FORCE)

# TODO(chenweihang): The logic later depends adding cinn subdirectory here,
# but better to move to paddle/CMakeLists.txt
add_subdirectory(paddle/cinn)

set(core_src "${cinnapi_src}")

cinn_cc_library(
cinnapi
SHARED
SRCS
${cinnapi_src}
DEPS
glog
${llvm_libs}
framework_proto
param_proto
auto_schedule_proto
schedule_desc_proto
absl
isl
ginac
pybind
${jitify_deps})
add_dependencies(cinnapi GEN_LLVM_RUNTIME_IR_HEADER ZLIB::ZLIB)
add_dependencies(cinnapi GEN_LLVM_RUNTIME_IR_HEADER ${core_deps})

target_link_libraries(cinnapi ${PYTHON_LIBRARIES})

if(WITH_MKL)
target_link_libraries(cinnapi mklml)
add_dependencies(cinnapi mklml)
if(WITH_MKLDNN)
target_link_libraries(cinnapi mkldnn)
add_dependencies(cinnapi mkldnn)
endif()
endif()

if(WITH_GPU)
target_link_libraries(
cinnapi
${CUDA_NVRTC_LIB}
${CUDA_LIBRARIES}
${CUDASTUB}
${CUBLAS}
${CUDNN}
${CURAND}
${CUSOLVER})
if(NVTX_FOUND)
target_link_libraries(cinnapi ${CUDA_NVTX_LIB})
endif()
endif()

function(gen_cinncore LINKTYPE)
set(CINNCORE_TARGET cinncore)
if(${LINKTYPE} STREQUAL "STATIC")
set(CINNCORE_TARGET cinncore_static)
endif()
cinn_cc_library(
${CINNCORE_TARGET}
${LINKTYPE}
SRCS
${core_src}
DEPS
glog
${llvm_libs}
framework_proto
param_proto
auto_schedule_proto
schedule_desc_proto
absl
isl
ginac)
add_dependencies(${CINNCORE_TARGET} GEN_LLVM_RUNTIME_IR_HEADER ZLIB::ZLIB)
add_dependencies(${CINNCORE_TARGET} GEN_LLVM_RUNTIME_IR_HEADER ${core_deps})

add_dependencies(${CINNCORE_TARGET} pybind)
target_link_libraries(${CINNCORE_TARGET} ${PYTHON_LIBRARIES})

if(WITH_MKL)
target_link_libraries(${CINNCORE_TARGET} mklml)
add_dependencies(${CINNCORE_TARGET} mklml)
if(WITH_MKLDNN)
target_link_libraries(${CINNCORE_TARGET} mkldnn)
add_dependencies(${CINNCORE_TARGET} mkldnn)
endif()
endif()

if(WITH_GPU)
target_link_libraries(
${CINNCORE_TARGET}
${CUDA_NVRTC_LIB}
${CUDA_LIBRARIES}
${CUDASTUB}
${CUBLAS}
${CUDNN}
${CURAND}
${CUSOLVER}
${jitify_deps})
if(NVTX_FOUND)
target_link_libraries(${CINNCORE_TARGET} ${CUDA_NVTX_LIB})
endif()
endif()
endfunction()

gen_cinncore(STATIC)
gen_cinncore(SHARED)

# --------distribute cinncore lib and include begin--------
set(PUBLISH_LIBS ON)
if(PUBLISH_LIBS)
set(core_includes
"${core_includes};paddle/cinn/runtime/cuda/cinn_cuda_runtime_source.cuh")
foreach(header ${core_includes})
get_filename_component(prefix ${header} DIRECTORY)
file(COPY ${header}
DESTINATION ${CMAKE_BINARY_DIR}/dist/cinn/include/${prefix})
endforeach()

foreach(proto_header ${core_proto_includes})
string(REPLACE ${CMAKE_BINARY_DIR}/ "" relname ${proto_header})
get_filename_component(prefix ${relname} DIRECTORY)
set(target_name ${CMAKE_BINARY_DIR}/dist/cinn/include/${relname})
add_custom_command(
TARGET cinnapi
POST_BUILD
COMMENT "copy generated proto header '${relname}' to dist"
COMMAND cmake -E copy ${proto_header} ${target_name} DEPENDS cinnapi)
endforeach()

add_custom_command(
TARGET cinnapi
POST_BUILD
COMMAND cmake -E copy ${CMAKE_BINARY_DIR}/libcinnapi.so
${CMAKE_BINARY_DIR}/dist/cinn/lib/libcinnapi.so
COMMAND cmake -E copy_directory ${CMAKE_BINARY_DIR}/thirds/install
${CMAKE_BINARY_DIR}/dist/third_party DEPENDS cinnapi)
add_custom_command(
TARGET cinncore_static
POST_BUILD
COMMAND cmake -E copy ${CMAKE_BINARY_DIR}/libcinncore_static.a
${CMAKE_BINARY_DIR}/dist/cinn/lib/libcinncore_static.a
COMMAND
cmake -E copy
${CMAKE_BINARY_DIR}/paddle/cinn/frontend/paddle/libframework_proto.a
${CMAKE_BINARY_DIR}/dist/cinn/lib/libframework_proto.a
COMMAND
cmake -E copy ${CMAKE_BINARY_DIR}/paddle/cinn/hlir/pe/libparam_proto.a
${CMAKE_BINARY_DIR}/dist/cinn/lib/libparam_proto.a
COMMAND
cmake -E copy
${CMAKE_BINARY_DIR}/paddle/cinn/auto_schedule/libauto_schedule_proto.a
${CMAKE_BINARY_DIR}/dist/cinn/lib/libauto_schedule_proto.a
COMMAND
cmake -E copy ${CMAKE_BINARY_DIR}/paddle/cinn/ir/libschedule_desc_proto.a
${CMAKE_BINARY_DIR}/dist/cinn/lib/libschedule_desc_proto.a
COMMENT "distribute libcinncore_static.a and related header files." DEPENDS
cinncore_static)
endif()
# --------distribute cinncore lib and include end--------

set(CINN_LIB_NAME "libcinnapi.so")
set(CINN_LIB_LOCATION "${CMAKE_BINARY_DIR}/dist/cinn/lib")
set(CINN_LIB "${CINN_LIB_LOCATION}/${CINN_LIB_NAME}")

######################################
# Add CINN's dependencies header files
######################################

# Add absl
set(ABSL_INCLUDE_DIR "${CMAKE_BINARY_DIR}/dist/third_party/absl/include")
include_directories(${ABSL_INCLUDE_DIR})

# Add isl
set(ISL_INCLUDE_DIR "${CMAKE_BINARY_DIR}/dist/third_party/isl/include")
include_directories(${ISL_INCLUDE_DIR})

# Add LLVM
set(LLVM_INCLUDE_DIR "${CMAKE_BINARY_DIR}/dist/third_party/llvm/include")
include_directories(${LLVM_INCLUDE_DIR})

######################################################
# Put external_cinn and dependencies together as a lib
######################################################

set(CINN_INCLUDE_DIR "${CMAKE_BINARY_DIR}/dist/cinn/include")
include_directories(${CINN_INCLUDE_DIR})
11 changes: 11 additions & 0 deletions cmake/cinn/config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# The home path of ISL
# Required!
set(ISL_HOME "")

# Whether enable NVidia CUDA support.
# Possible values: ON, OFF
set(WITH_GPU ON)

set(WITH_MKL ON)
set(WITH_MKLDNN ON)
set(USE_OPENMP "intel")
Loading