2022-09-22 18:06:45 +08:00
cmake_minimum_required ( VERSION 3.17 ) # FindCUDAToolkit
2022-07-31 21:43:26 +08:00
include ( CMakeDependentOption )
project ( InfiniTensor C CXX )
# Do not change these options in this file. Use cmake.config, cmake -DOPTION=VALUE, or ccmake to specify them.
2022-09-22 16:57:39 +08:00
option ( USE_CUDA "Support CUDA GPU" OFF )
option ( USE_BANG "Support BANG MLU" OFF )
2022-09-01 20:30:12 +08:00
option ( USE_BACKTRACE "Print backtrace on exception and segmentation fault" ON )
2022-09-13 11:27:41 +08:00
option ( USE_PROTOBUF "Serialize and deserialize tensors" ON )
2022-08-08 16:02:07 +08:00
option ( BUILD_TEST "Build tests" ON )
2022-09-01 20:30:12 +08:00
2022-07-31 21:43:26 +08:00
cmake_dependent_option ( BUILD_TEST_CORE "Build tests for core components" ON BUILD_TEST OFF )
2022-08-08 16:02:07 +08:00
cmake_dependent_option ( BUILD_TEST_PET "Build tests for PET" OFF BUILD_TEST OFF )
cmake_dependent_option ( BUILD_TEST_EINNET "Build tests for EINNET" OFF BUILD_TEST OFF )
2022-07-31 21:43:26 +08:00
set ( DEFAULT_BUILD_TYPE "RelWithDebInfo" )
set ( CMAKE_CXX_STANDARD 17 )
set ( CMAKE_CXX_EXTENSIONS OFF ) # -std=gnu++11 when on, -std=c++11 when off
2022-09-08 21:31:35 +08:00
2022-08-31 14:44:53 +08:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Werror -Wno-error=deprecated-declarations" )
2022-07-31 21:43:26 +08:00
set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -UNDEBUG" ) # Enable assertion
set ( CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -UNDEBUG" ) # Enable assertion
find_package (
P y t h o n
C O M P O N E N T S I n t e r p r e t e r D e v e l o p m e n t
R E Q U I R E D )
# OpenMP
find_package ( OpenMP )
if ( OpenMP_C_FOUND )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}" )
endif ( )
if ( OpenMP_CXX_FOUND )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}" )
endif ( )
2022-09-13 11:27:41 +08:00
#Protobuf
if ( USE_PROTOBUF )
add_definitions ( -D TENSOR_PROTOBUF )
find_package ( Protobuf REQUIRED )
message ( STATUS "protobuf include: " ${ PROTOBUF_INCLUDE_DIRS } )
message ( STATUS "protobuf libraries: " ${ PROTOBUF_LIBRARIES } )
message ( STATUS "protoc executable: " ${ PROTOBUF_PROTOC_EXECUTABLE } )
include_directories ( ${ PROTOBUF_INCLUDE_DIR } )
include_directories ( ${ CMAKE_CURRENT_BINARY_DIR } )
set ( PROTO_PATH "${CMAKE_CURRENT_SOURCE_DIR}/proto" )
file ( GLOB PROTO_FILES "${PROTO_PATH}/data.proto" )
protobuf_generate_cpp ( PROTO_SRCS PROTO_HDRS ${ PROTO_FILES } )
message ( ${ PROTO_SRCS } "-----------" ${ PROTO_FILES } )
message ( ${ PROTO_HDRS } "-----------" ${ PROTO_FILES } )
add_library ( tensor_proto SHARED ${ PROTO_SRCS } ${ PROTO_HDRS } )
target_link_libraries ( tensor_proto PUBLIC ${ PROTOBUF_LIBRARIES } )
endif ( )
2022-07-31 21:43:26 +08:00
include_directories ( include )
2022-08-08 16:02:07 +08:00
2022-09-22 18:06:45 +08:00
# Pybind11
add_subdirectory ( 3rd-party/pybind11 )
include_directories ( 3rd-party/pybind11/include )
2022-08-08 16:02:07 +08:00
# nlohmann_json
add_subdirectory ( 3rd-party/nlohmann_json_cmake_fetchcontent )
include_directories ( 3rd-party/nlohmann_json_cmake_fetchcontent/single_include )
2022-07-31 21:43:26 +08:00
if ( BUILD_TEST )
2022-08-08 16:02:07 +08:00
set ( BUILD_GMOCK
O F F
C A C H E B O O L " D o n o t b u i l d g m o c k " F O R C E )
set ( INSTALL_GTEST
O F F
C A C H E B O O L " D o n o t i n s t a l l g t e s t " F O R C E )
2022-07-31 21:43:26 +08:00
add_subdirectory ( 3rd-party/googletest )
include_directories ( 3rd-party/googletest/googletest/include )
endif ( )
2022-09-21 12:28:00 +08:00
# Source files
2022-09-22 18:06:45 +08:00
file ( GLOB_RECURSE SRC src/ffi/*.cc src/core/*.cc src/kernels/cpu/*.cc src/nnet/*.cc src/operators/*.cc src/utils/*.cc )
2022-07-31 21:43:26 +08:00
2022-09-21 12:28:00 +08:00
if ( USE_CUDA )
file ( GLOB_RECURSE SRC_CUDA src/cuda/*.cc src/cuda/*.cu src/kernels/cuda/*.cc src/kernels/cuda/*.cu )
list ( APPEND SRC ${ SRC_CUDA } )
endif ( )
2022-09-22 16:57:39 +08:00
if ( USE_BANG )
file ( GLOB_RECURSE SRC_BANG src/bang/*.cc src/kernels/bang/*.cc )
list ( APPEND SRC ${ SRC_BANG } )
endif ( )
2022-09-21 12:28:00 +08:00
# Libraries
2022-07-31 21:43:26 +08:00
add_library ( InfiniTensor SHARED ${ SRC } )
2022-09-13 11:27:41 +08:00
if ( USE_PROTOBUF )
target_link_libraries ( InfiniTensor tensor_proto )
endif ( )
2022-08-22 15:01:03 +08:00
2022-09-22 18:06:45 +08:00
target_link_libraries ( InfiniTensor pybind11::embed )
2022-09-27 10:41:12 +08:00
# Python bindings
file ( GLOB_RECURSE FFIS src/ffi/ffi_infinitensor.cc )
pybind11_add_module ( pyinfinitensor MODULE ${ FFIS } )
target_link_libraries ( pyinfinitensor PRIVATE InfiniTensor )
2022-09-01 20:30:12 +08:00
if ( USE_BACKTRACE )
add_definitions ( -D BACKWARD_TRACE )
add_subdirectory ( 3rd-party/backward-cpp )
include_directories ( 3rd-party/backward-cpp )
add_backward ( InfiniTensor )
target_link_libraries ( InfiniTensor dw )
endif ( )
2022-08-22 15:01:03 +08:00
if ( USE_CUDA )
2022-09-27 10:41:12 +08:00
add_compile_definitions ( USE_CUDA=1 )
2022-09-08 21:31:35 +08:00
# Since enable_language only executes once, rerun cmake is required if CMAKE_CUDA_HOST_COMPILER is wrong
set ( CMAKE_CUDA_HOST_COMPILER
$ { C M A K E _ C X X _ C O M P I L E R }
C A C H E S T R I N G " S e t c u d a h o s t c o m p i l e r p a t h " )
2022-09-21 12:28:00 +08:00
# CMP0104 requires CUDA_ARCHITECTURES
set_target_properties ( InfiniTensor PROPERTIES CUDA_ARCHITECTURES "70;80" )
2022-08-22 15:01:03 +08:00
enable_language ( CUDA )
2022-09-22 18:06:45 +08:00
find_package ( CUDAToolkit ) # For nvrtc and cuda driver
2022-09-27 10:41:12 +08:00
target_link_libraries ( InfiniTensor cudnn CUDA::curand CUDA::cublas CUDA::nvrtc CUDA::cudart CUDA::cuda_driver )
2022-08-22 15:01:03 +08:00
endif ( )
2022-07-31 21:43:26 +08:00
2022-09-22 16:57:39 +08:00
if ( USE_BANG )
################################################################################
# Neuware Evironment
################################################################################
# cnrt cndrv cnnl
if ( ( NOT DEFINED NEUWARE_HOME ) AND ( NOT DEFINED ENV{NEUWARE_HOME} ) )
message ( FATAL_ERROR "NEUWARE_HOME is not defined from cmake or env" )
elseif ( DEFINED NEUWARE_HOME )
set ( NEUWARE_HOME ${ NEUWARE_HOME } CACHE STRING "NEUWARE_HOME directory for Cambricon Neuware development" )
else ( )
set ( NEUWARE_HOME $ENV{ NEUWARE_HOME } CACHE STRING "NEUWARE_HOME directory for Cambricon Neuware development" )
endif ( )
message ( STATUS "NEUWARE_HOME: ${NEUWARE_HOME}" )
include_directories ( "${NEUWARE_HOME}/include" )
find_library ( CAMBRICON_CNNL libcnnl.so "${NEUWARE_HOME}/lib64" )
find_library ( CAMBRICON_CNRT libcnrt.so "${NEUWARE_HOME}/lib64" )
find_library ( CAMBRICON_CNDRV libcndrv.so "${NEUWARE_HOME}/lib64" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lstdc++ -Wall -Werror" )
if ( ( NOT DEFINED TARGET_CPU_ARCH ) AND ( NOT DEFINED ENV{TARGET_CPU_ARCH} ) )
execute_process ( COMMAND uname -m OUTPUT_VARIABLE _uname_m OUTPUT_STRIP_TRAILING_WHITESPACE )
set ( TARGET_CPU_ARCH "${_uname_m}" CACHE STRING "Target CPU ARCH" )
elseif ( DEFINED TARGET_CPU_ARCH )
set ( TARGET_CPU_ARCH ${ TARGET_CPU_ARCH } CACHE STRING "Target CPU ARCH" )
else ( )
set ( TARGET_CPU_ARCH $ENV{ TARGET_CPU_ARCH } CACHE STRING "Target CPU ARCH" )
endif ( )
message ( STATUS "TARGET_CPU_ARCH: ${TARGET_CPU_ARCH}" )
################################################################################
# Sample Kernels
################################################################################
set ( CMAKE_MODULE_PATH ${ CMAKE_MODULE_PATH } "$ENV{NEUWARE_HOME}/cmake" "$ENV{NEUWARE_HOME}/cmake/modules" )
find_package ( BANG )
if ( NOT BANG_FOUND )
message ( FATAL_ERROR "BANG cannot be found." )
elseif ( NOT BANG_CNCC_EXECUTABLE )
message ( FATAL_ERROR "cncc not found, please ensure cncc is in your PATH env or set variable BANG_CNCC_EXECUTABLE from cmake. Otherwise you should check path used by find_program(BANG_CNCC_EXECUTABLE) in FindBANG.cmake" )
endif ( )
set ( BANG_CNCC_FLAGS "-Wall -Werror -fPIC -std=c++11 --target=${TARGET_CPU_ARCH} -O3" )
set ( BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS}"
" - - b a n g - a r c h = c o m p u t e _ 2 0 "
" - - b a n g - a r c h = c o m p u t e _ 3 0 "
" - - b a n g - m l u - a r c h = m t p _ 3 2 2 "
" - - b a n g - w r a m - a l i g n 6 4 "
)
if ( ${ TARGET_CPU_ARCH } MATCHES "aarch64-linux-gnu" )
set ( BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=1" )
add_definitions ( -D_GLIBCXX_USE_CXX11_ABI=1 )
execute_process (
C O M M A N D u n a m e - m
O U T P U T _ V A R I A B L E _ u n a m e _ m
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
)
if ( NOT ( "${TARGET_CPU_ARCH}" MATCHES ".*${_uname_m}.*" AND "${_uname_m}" MATCHES "aarch64" ) )
execute_process (
C O M M A N D " $ { C M A K E _ C X X _ C O M P I L E R } " " - v " " - c " " - x " " c + + " " / d e v / n u l l " " - M "
E R R O R _ V A R I A B L E _ c x x _ v e r b o s e
)
execute_process (
C O M M A N D " e c h o " " $ { _ c x x _ v e r b o s e } "
C O M M A N D " s e d " " - n " " / i n c l u d e . * s e a r c h s t a r t s h e r e / , / E n d o f s e a r c h l i s t / { s / ^ / / p } "
C O M M A N D " t r " " ' \ n ' " " ; "
O U T P U T _ V A R I A B L E _ c x x _ i n c l u d e s
)
list ( REMOVE_ITEM _cxx_includes "/usr/include" )
foreach ( _include ${ _cxx_includes } )
message ( STATUS "add include path: ${_include}" )
set ( BANG_CNCC_FLAGS "${BANG_CNCC_FLAGS} -idirafter ${_include}" )
endforeach ( )
endif ( )
endif ( )
#bang_add_library(bangops SHARED ${SRC_BANG})
#target_link_libraries(bangops ${CAMBRICON_CNDRV})
target_link_libraries ( InfiniTensor ${ CAMBRICON_CNNL } ${ CAMBRICON_CNRT } ${ CAMBRICON_CNDRV } stdc++ )
#target_link_libraries(InfiniTensor bangops)
endif ( )
2022-07-31 21:43:26 +08:00
# # Python bindings
# pybind11_add_module(infini MODULE ${FFI})
# target_link_libraries(infini PRIVATE infini_cpp)
2022-08-08 16:02:07 +08:00
function ( build_test files )
# Non-recursive glob for skip failed tests
file ( GLOB TEST_SOURCES ${ files } )
2022-07-31 21:43:26 +08:00
foreach ( testsourcefile ${ TEST_SOURCES } )
get_filename_component ( testname ${ testsourcefile } NAME_WE )
add_executable ( ${ testname } ${ testsourcefile } )
2022-08-08 16:02:07 +08:00
target_link_libraries ( ${ testname } InfiniTensor GTest::gtest_main )
2022-07-31 21:43:26 +08:00
add_test ( NAME ${ testname } COMMAND ${ testname } )
endforeach ( testsourcefile ${ TEST_SOURCES } )
2022-08-08 16:02:07 +08:00
endfunction ( )
if ( BUILD_TEST )
enable_testing ( )
2022-09-01 20:30:12 +08:00
if ( USE_TRACE )
build_test ( test/trace/*.cc )
endif ( )
2022-08-08 16:02:07 +08:00
if ( BUILD_TEST_CORE )
build_test ( test/core/*.cc )
2022-08-17 14:16:01 +08:00
build_test ( test/operators/*.cc )
2022-09-21 12:28:00 +08:00
if ( USE_CUDA )
build_test ( test/kernels/cuda/*.cc )
endif ( )
2022-09-22 16:57:39 +08:00
if ( USE_BANG )
build_test ( test/kernels/bang/*.cc )
endif ( )
2022-08-08 16:02:07 +08:00
endif ( )
if ( BUILD_TEST_PET )
build_test ( test/pet/*.cc )
endif ( )
if ( BUILD_TEST_EINNET )
2022-09-13 15:17:22 +08:00
build_test ( test/nnet/test_*.cc )
2022-08-08 16:02:07 +08:00
endif ( )
2022-07-31 21:43:26 +08:00
endif ( )