CMakeLists.txt 9.5 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 8 9 10 11 12 13 14 15 16 17 18 19
if(NOT ${MGE_ARCH} STREQUAL "naive")
    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)
        list(APPEND SOURCES ${SOURCES_})
        if(NOT MSVC)
            file(GLOB_RECURSE SOURCES_ x86/*.S)
            set_source_files_properties(${SOURCES_} PROPERTIES LANGUAGE C)
            list(APPEND SOURCES ${SOURCES_})
        endif()
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    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_})
36 37 38
    endif()
endif()

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

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
###############################################################################
# HIP_COMPILE
###############################################################################
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()

    add_custom_target(${_hip_target})

    # set return value
66
    set(${_hip_objs} ${_generated_files})
67 68 69 70 71 72 73 74 75 76
endmacro()

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)
77 78 79
    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)
80

81 82 83 84 85 86
    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)
87 88 89 90
    set(_HIPCC_OPTIONS "-fPIC")
    set(_HCC_OPTIONS "-fPIC")
    set(_NVCC_OPTIONS "-fPIC")

91
    list(APPEND HIP_SOURCES ${HIP_SOURCES_})
92 93 94 95 96 97 98 99 100
    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}
101 102
        ${ROCRAND_INCLUDE_DIR}
        ${AMDOCL_INCLUDE_DIR})
103
    hip_compile(
104 105 106 107 108
        ${HIP_TARGET_NAME} HIPOBJS ${HIP_SOURCES}
        HIPCC_OPTIONS ${_HIPCC_OPTIONS}
        HCC_OPTIONS ${_HCC_OPTIONS}
        NVCC_OPTIONS ${_NVCC_OPTIONS})
    list(APPEND SOURCES ${HIPOBJS})
109 110
endif ()

111 112 113 114 115
if(MGE_WITH_CUDA)
    file(GLOB_RECURSE SOURCES_ cuda/*.cpp)
    list(APPEND SOURCES ${SOURCES_})

    file(GLOB_RECURSE CUSOURCES cuda/*.cu)
116 117 118

    set(CUTLASS_GEN_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/cutlass_generator/generator.py)
    set(CUTLASS_GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/cuda/cutlass/generated)
119 120 121
    set(CUTLASS_SOURCES "")
    function(gen_cutlass_kimpl op type gen_files)
        set(CURRENT_CUTLASS_STAGE_DIR ${CUTLASS_GEN_DIR}/${op}_${type}.stage)
122
        set(CURRENT_CUTLASS_GEN_DIR ${CUTLASS_GEN_DIR}/${op}_${type})
123 124 125 126 127
        
        set_directory_properties(PROPERTIES CMAKE_CONFIGURE_DEPENDS ${CUTLASS_GEN_SCRIPT})
        
        file(REMOVE_RECURSE ${CURRENT_CUTLASS_STAGE_DIR})
        file(MAKE_DIRECTORY ${CURRENT_CUTLASS_STAGE_DIR})
128 129
        file(MAKE_DIRECTORY ${CURRENT_CUTLASS_GEN_DIR})
        execute_process(
130
            COMMAND ${PYTHON3_EXECUTABLE_WITHOUT_VERSION} ${CUTLASS_GEN_SCRIPT} --operations ${op} --type ${type} ${CURRENT_CUTLASS_STAGE_DIR}
131 132 133 134 135 136 137
            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()
138 139 140 141 142 143 144 145 146 147 148 149 150 151
        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)
152
    endfunction()
153
    gen_cutlass_kimpl(gemm simt CUTLASS_SOURCES)
154 155
    gen_cutlass_kimpl(gemm tensorop884 CUTLASS_SOURCES)
    gen_cutlass_kimpl(gemm tensorop1688 CUTLASS_SOURCES)
156 157
    gen_cutlass_kimpl(gemv simt CUTLASS_SOURCES)
    gen_cutlass_kimpl(deconv simt CUTLASS_SOURCES)
158
    gen_cutlass_kimpl(deconv tensorop8816 CUTLASS_SOURCES)
159 160 161
    gen_cutlass_kimpl(conv2d simt CUTLASS_SOURCES)
    gen_cutlass_kimpl(conv2d tensorop8816 CUTLASS_SOURCES)
    gen_cutlass_kimpl(conv2d tensorop8832 CUTLASS_SOURCES)
162
    list(APPEND SOURCES ${CUTLASS_SOURCES})
163 164 165
    list(APPEND SOURCES ${CUSOURCES})
endif()

166 167 168 169 170
if(MGE_WITH_ATLAS)
    file(GLOB_RECURSE SOURCES_ atlas/*.cpp)
    list(APPEND SOURCES ${SOURCES_})
    list(APPEND LIBMEGDNN_DEF -DMEGDNN_WITH_ATLAS=1)
endif()
171 172

add_definitions(${LIBMEGDNN_DEF})
173
add_library(megdnn EXCLUDE_FROM_ALL OBJECT ${SOURCES})
174

175
target_link_libraries(megdnn PUBLIC opr_param_defs)
176
if(MGE_WITH_CUDA)
177
    target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:cutlass>)
178
    target_include_directories(megdnn PRIVATE ${CUDNN_INCLUDE_DIR})
179
endif()
180

181
if(MGE_WITH_ROCM)
182
    target_include_directories(megdnn PUBLIC
183 184 185
        ${HIP_INCLUDE_DIR}
        ${MIOPEN_INCLUDE_DIR}
        ${ROCBLAS_INCLUDE_DIR}
186 187 188
        ${ROCRAND_INCLUDE_DIR}
        ${AMDOCL_INCLUDE_DIR})
    target_link_directories(megdnn PUBLIC
189 190 191
        ${HIP_LIBRARY_DIR}
        ${MIOPEN_LIBRARY_DIR}
        ${ROCBLAS_LIBRARY_DIR}
192 193
        ${ROCRAND_LIBRARY_DIR}
        ${AMDOCL_LIBRARY_DIR})
194 195
endif()

196
if(${MGE_ARCH} STREQUAL "x86_64" OR ${MGE_ARCH} STREQUAL "i386" OR ${MGE_ARCH} STREQUAL "armv7" OR ${MGE_ARCH} STREQUAL "aarch64")
197
    if(MGE_ENABLE_CPUINFO)
198 199 200 201
        target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:cpuinfo>)
    endif()
endif()

202 203 204 205 206 207 208 209 210
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
)
211 212 213 214 215 216 217 218 219 220 221 222 223 224

install(DIRECTORY ${PROJECT_SOURCE_DIR}/dnn/include DESTINATION . FILES_MATCHING PATTERN "*.h*")

if(CXX_SUPPORT_WCLASS_MEMACCESS)
    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()
endif()
target_compile_definitions(megdnn INTERFACE ${LIBMEGDNN_DEF})

if(MGE_WITH_MKLDNN AND ${MGE_ARCH} STREQUAL "x86_64")
225 226 227 228 229 230 231 232 233 234
    if (BUILD_SHARED_LIBS)
        target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:dnnl>)
    else()
        target_link_libraries(megdnn PRIVATE dnnl)
    endif()
endif()
if (BUILD_SHARED_LIBS)
    target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:${MGE_BLAS_LIBS}>)
else()
    target_link_libraries(megdnn PRIVATE ${MGE_BLAS_LIBS})
235
endif()
236

237 238 239 240
if (MGE_WITH_ROCM)
    target_link_libraries(megdnn PRIVATE ${HIPOBJS} ${MGE_ROCM_LIBS})
endif ()

241 242 243 244 245 246 247 248 249
if(MGE_WITH_ATLAS)
    if (BUILD_SHARED_LIBS)
        target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:${MGE_ATLAS_LIBS}>)
    else()
        target_link_libraries(megdnn PRIVATE ${MGE_ATLAS_LIBS})
    endif()

endif()

250
if(CMAKE_THREAD_LIBS_INIT)
251
    target_link_libraries(megdnn PRIVATE Threads::Threads)
252
endif()
253 254

install(TARGETS megdnn EXPORT ${MGE_EXPORT_TARGETS})