CMakeLists.txt 9.4 KB
Newer Older
1 2 3
set(LIBMEGDNN_DEF)
file(GLOB_RECURSE SOURCES common/*.cpp naive/*.cpp)

4 5 6
# Build configure
list(APPEND SOURCES ${PROJECT_BINARY_DIR}/genfiles/megbrain_build_config.h)

7
if(NOT ${MGE_ARCH} STREQUAL "naive")
8 9 10 11 12 13
  file(GLOB_RECURSE SOURCES_ fallback/*.cpp)
  list(APPEND SOURCES ${SOURCES_})
  if(${MGE_ARCH} STREQUAL "fallback")
    message(WARNING "build only with fallback")
  elseif(${MGE_ARCH} STREQUAL "x86_64" OR ${MGE_ARCH} STREQUAL "i386")
    file(GLOB_RECURSE SOURCES_ x86/*.cpp)
14
    list(APPEND SOURCES ${SOURCES_})
15 16 17 18
    if(NOT MSVC)
      file(GLOB_RECURSE SOURCES_ x86/*.S)
      set_source_files_properties(${SOURCES_} PROPERTIES LANGUAGE C)
      list(APPEND SOURCES ${SOURCES_})
19
    endif()
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  elseif(${MGE_ARCH} STREQUAL "armv7")
    file(GLOB_RECURSE SOURCES_ armv7/*.cpp)
    list(APPEND SOURCES ${SOURCES_})
    file(GLOB_RECURSE SOURCES_ arm_common/*.cpp)
    list(APPEND SOURCES ${SOURCES_})
    file(GLOB_RECURSE SOURCES_ armv7/*.S)
    set_source_files_properties(${SOURCES_} PROPERTIES LANGUAGE C)
    list(APPEND SOURCES ${SOURCES_})
  elseif(${MGE_ARCH} STREQUAL "aarch64")
    file(GLOB_RECURSE SOURCES_ aarch64/*.cpp)
    list(APPEND SOURCES ${SOURCES_})
    file(GLOB_RECURSE SOURCES_ arm_common/*.cpp)
    list(APPEND SOURCES ${SOURCES_})
    file(GLOB_RECURSE SOURCES_ aarch64/*.S)
    set_source_files_properties(${SOURCES_} PROPERTIES LANGUAGE C)
    list(APPEND SOURCES ${SOURCES_})
  endif()
37 38
endif()

39
if(MGE_WITH_MIDOUT_PROFILE)
40
  list(APPEND SOURCES ${PROJECT_SOURCE_DIR}/third_party/midout/src/midout.cpp)
41
endif()
42

43
# ######################################################################################
44
# HIP_COMPILE
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
# ######################################################################################
macro(HIP_COMPILE _hip_target _hip_objs)
  # Separate the sources from the options
  hip_get_sources_and_options(_sources _cmake_options _hipcc_options _hcc_options
                              _nvcc_options ${ARGN})
  hip_prepare_target_commands(
    ${_hip_target}
    OBJ
    _generated_files
    _source_files
    ${_sources}
    ${_cmake_options}
    HIPCC_OPTIONS
    ${_hipcc_options}
    HCC_OPTIONS
    ${_hcc_options}
    NVCC_OPTIONS
    ${_nvcc_options})
  if(_source_files)
    list(REMOVE_ITEM _sources ${_source_files})
  endif()
66

67
  add_custom_target(${_hip_target})
68

69 70
  # set return value
  set(${_hip_objs} ${_generated_files})
71 72
endmacro()

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
if(MGE_WITH_ROCM)
  file(GLOB_RECURSE SOURCES_ rocm/*.cpp)
  list(APPEND SOURCES ${SOURCES_})

  # FIXME rocm may lost the first hip file, so currently we just create an empty file to
  # bypass this error.
  file(GLOB start.cpp.hip "")
  list(APPEND HIP_SOURCES start.cpp.hip)
  configure_file(${PROJECT_SOURCE_DIR}/dnn/include/hcc_detail/hcc_defs_prologue.h.in
                 ${PROJECT_BINARY_DIR}/dnn/include/hcc_detail/hcc_defs_prologue.h)

  configure_file(${PROJECT_SOURCE_DIR}/dnn/include/hcc_detail/hcc_defs_epilogue.h.in
                 ${PROJECT_BINARY_DIR}/dnn/include/hcc_detail/hcc_defs_epilogue.h)

  file(GLOB_RECURSE HIP_SOURCES_ rocm/*.cpp.hip)
  set(HIP_TARGET_NAME megdnn_hip_kernel)
  set(_HIPCC_OPTIONS "-fPIC")
  set(_HCC_OPTIONS "-fPIC")
  set(_NVCC_OPTIONS "-fPIC")

  list(APPEND HIP_SOURCES ${HIP_SOURCES_})
  set_source_files_properties(${HIP_SOURCES} PROPERTIES HIP_SOURCE_PROPERTY_FORMAT 1)
  hip_include_directories(
    ${PROJECT_SOURCE_DIR}/dnn
    ${PROJECT_SOURCE_DIR}/dnn/include
    ${PROJECT_BINARY_DIR}/dnn
    ${PROJECT_BINARY_DIR}/genfiles
    ${PROJECT_BINARY_DIR}/dnn/include
    ${HIP_INCLUDE_DIR}
    ${MIOPEN_INCLUDE_DIR}
    ${ROCBLAS_INCLUDE_DIR}
    ${ROCRAND_INCLUDE_DIR}
    ${AMDOCL_INCLUDE_DIR})
  hip_compile(
    ${HIP_TARGET_NAME}
    HIPOBJS
    ${HIP_SOURCES}
    HIPCC_OPTIONS
    ${_HIPCC_OPTIONS}
    HCC_OPTIONS
    ${_HCC_OPTIONS}
    NVCC_OPTIONS
    ${_NVCC_OPTIONS})
  list(APPEND SOURCES ${HIPOBJS})
endif()
118

119
if(MGE_WITH_CUDA)
120 121
  file(GLOB_RECURSE SOURCES_ cuda/*.cpp)
  list(APPEND SOURCES ${SOURCES_})
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  file(GLOB_RECURSE CUSOURCES cuda/*.cu)

  set(CUTLASS_GEN_SCRIPT
      ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/cutlass_generator/generator.py)
  set(CUTLASS_GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/cuda/cutlass/generated)
  set(CUTLASS_SOURCES "")
  function(gen_cutlass_kimpl op type gen_files)
    set(CURRENT_CUTLASS_STAGE_DIR ${CUTLASS_GEN_DIR}/${op}_${type}.stage)
    set(CURRENT_CUTLASS_GEN_DIR ${CUTLASS_GEN_DIR}/${op}_${type})

    set_directory_properties(PROPERTIES CMAKE_CONFIGURE_DEPENDS ${CUTLASS_GEN_SCRIPT})

    file(REMOVE_RECURSE ${CURRENT_CUTLASS_STAGE_DIR})
    file(MAKE_DIRECTORY ${CURRENT_CUTLASS_STAGE_DIR})
    file(MAKE_DIRECTORY ${CURRENT_CUTLASS_GEN_DIR})
    execute_process(
      COMMAND ${PYTHON3_EXECUTABLE_WITHOUT_VERSION} ${CUTLASS_GEN_SCRIPT} --operations
              ${op} --type ${type} ${CURRENT_CUTLASS_STAGE_DIR}
      RESULT_VARIABLE gen_cutlass_result
      OUTPUT_FILE ${CURRENT_CUTLASS_GEN_DIR}/gen_cutlass.log
      ERROR_FILE ${CURRENT_CUTLASS_GEN_DIR}/gen_cutlass.log)
    if(NOT gen_cutlass_result EQUAL 0)
      message(
        FATAL_ERROR
          "Error generating library instances. See ${CURRENT_CUTLASS_GEN_DIR}/gen_cutlass.log"
      )
    endif()
    file(
      GLOB CUTLASS_GEN_FILES
      RELATIVE "${CURRENT_CUTLASS_GEN_DIR}/"
      "${CURRENT_CUTLASS_GEN_DIR}/*.cu")
    foreach(FILE ${CUTLASS_GEN_FILES})
      if(NOT EXISTS "${CURRENT_CUTLASS_STAGE_DIR}/${FILE}")
        file(REMOVE "${CURRENT_CUTLASS_GEN_DIR}/${FILE}")
      endif()
    endforeach()
    file(
      GLOB CUTLASS_GEN_FILES
      RELATIVE "${CURRENT_CUTLASS_STAGE_DIR}"
      "${CURRENT_CUTLASS_STAGE_DIR}/*.cu")
    foreach(FILE ${CUTLASS_GEN_FILES})
      execute_process(
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${CURRENT_CUTLASS_STAGE_DIR}/${FILE}" "${CURRENT_CUTLASS_GEN_DIR}")
    endforeach()
    file(REMOVE_RECURSE ${CURRENT_CUTLASS_STAGE_DIR})
    file(GLOB_RECURSE CUTLASS_GEN_FILES "${CURRENT_CUTLASS_GEN_DIR}/*.cu")
    list(APPEND ${gen_files} ${CUTLASS_GEN_FILES})
    set(${gen_files}
        "${${gen_files}}"
        PARENT_SCOPE)
  endfunction()
  gen_cutlass_kimpl(gemm simt CUTLASS_SOURCES)
  gen_cutlass_kimpl(gemm tensorop884 CUTLASS_SOURCES)
  gen_cutlass_kimpl(gemm tensorop1688 CUTLASS_SOURCES)
  gen_cutlass_kimpl(gemv simt CUTLASS_SOURCES)
  gen_cutlass_kimpl(deconv simt CUTLASS_SOURCES)
  gen_cutlass_kimpl(deconv tensorop8816 CUTLASS_SOURCES)
  gen_cutlass_kimpl(conv2d simt CUTLASS_SOURCES)
  gen_cutlass_kimpl(conv2d tensorop8816 CUTLASS_SOURCES)
  gen_cutlass_kimpl(conv2d tensorop8832 CUTLASS_SOURCES)
184 185
  gen_cutlass_kimpl(dwconv2d_fprop simt CUTLASS_SOURCES)
  gen_cutlass_kimpl(dwconv2d_fprop tensorop884 CUTLASS_SOURCES)
186 187
  gen_cutlass_kimpl(dwconv2d_dgrad simt CUTLASS_SOURCES)
  gen_cutlass_kimpl(dwconv2d_dgrad tensorop884 CUTLASS_SOURCES)
188 189
  list(APPEND SOURCES ${CUTLASS_SOURCES})
  list(APPEND SOURCES ${CUSOURCES})
190 191
endif()

192
if(MGE_WITH_ATLAS)
193 194 195
  file(GLOB_RECURSE SOURCES_ atlas/*.cpp)
  list(APPEND SOURCES ${SOURCES_})
  list(APPEND LIBMEGDNN_DEF -DMEGDNN_WITH_ATLAS=1)
196
endif()
197 198

add_definitions(${LIBMEGDNN_DEF})
199
add_library(megdnn EXCLUDE_FROM_ALL OBJECT ${SOURCES})
200

201
target_link_libraries(megdnn PUBLIC opr_param_defs)
202
if(MGE_WITH_CUDA)
203 204
  target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:cutlass>)
  target_include_directories(megdnn PRIVATE ${CUDNN_INCLUDE_DIR})
205
endif()
206

207
if(MGE_WITH_ROCM)
208 209 210 211 212 213 214 215 216 217 218
  target_include_directories(
    megdnn PUBLIC ${HIP_INCLUDE_DIR} ${MIOPEN_INCLUDE_DIR} ${ROCBLAS_INCLUDE_DIR}
                  ${ROCRAND_INCLUDE_DIR} ${AMDOCL_INCLUDE_DIR})
  target_link_directories(
    megdnn
    PUBLIC
    ${HIP_LIBRARY_DIR}
    ${MIOPEN_LIBRARY_DIR}
    ${ROCBLAS_LIBRARY_DIR}
    ${ROCRAND_LIBRARY_DIR}
    ${AMDOCL_LIBRARY_DIR})
219 220
endif()

221 222 223 224 225 226 227
if(${MGE_ARCH} STREQUAL "x86_64"
   OR ${MGE_ARCH} STREQUAL "i386"
   OR ${MGE_ARCH} STREQUAL "armv7"
   OR ${MGE_ARCH} STREQUAL "aarch64")
  if(MGE_ENABLE_CPUINFO)
    target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:cpuinfo>)
  endif()
228 229
endif()

230 231 232 233 234 235
target_include_directories(
  megdnn
  PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/genfiles>
         $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/dnn/include>
         $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  PRIVATE ${PROJECT_SOURCE_DIR}/dnn ${PROJECT_SOURCE_DIR}/third_party/midout/src)
236

237 238 239 240 241
install(
  DIRECTORY ${PROJECT_SOURCE_DIR}/dnn/include
  DESTINATION .
  FILES_MATCHING
  PATTERN "*.h*")
242 243

if(CXX_SUPPORT_WCLASS_MEMACCESS)
244 245 246 247 248 249 250
  if(MGE_WITH_CUDA)
    target_compile_options(
      megdnn PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-Wno-class-memaccess>"
                     "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:-Wno-class-memaccess>")
  else()
    target_compile_options(megdnn PRIVATE "-Wno-class-memaccess")
  endif()
251 252 253 254
endif()
target_compile_definitions(megdnn INTERFACE ${LIBMEGDNN_DEF})

if(MGE_WITH_MKLDNN AND ${MGE_ARCH} STREQUAL "x86_64")
255 256 257 258 259
  if(BUILD_SHARED_LIBS)
    target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:dnnl>)
  else()
    target_link_libraries(megdnn PRIVATE dnnl)
  endif()
260
endif()
261 262
if(BUILD_SHARED_LIBS)
  target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:${MGE_BLAS_LIBS}>)
263
else()
264
  target_link_libraries(megdnn PRIVATE ${MGE_BLAS_LIBS})
265
endif()
266

267 268 269
if(MGE_WITH_ROCM)
  target_link_libraries(megdnn PRIVATE ${HIPOBJS} ${MGE_ROCM_LIBS})
endif()
270

271
if(MGE_WITH_ATLAS)
272 273 274 275 276
  if(BUILD_SHARED_LIBS)
    target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:${MGE_ATLAS_LIBS}>)
  else()
    target_link_libraries(megdnn PRIVATE ${MGE_ATLAS_LIBS})
  endif()
277 278 279

endif()

280
if(CMAKE_THREAD_LIBS_INIT)
281
  target_link_libraries(megdnn PRIVATE Threads::Threads)
282
endif()
283 284

install(TARGETS megdnn EXPORT ${MGE_EXPORT_TARGETS})