cpu_extension.cmake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  2. set(CMAKE_CXX_STANDARD 17)
  3. #
  4. # Define environment variables for special configurations
  5. #
  6. if(DEFINED ENV{APHRODITE_CPU_AVX512BF16})
  7. set(ENABLE_AVX512BF16 ON)
  8. endif()
  9. include_directories("${CMAKE_SOURCE_DIR}/kernels")
  10. #
  11. # Check the compile flags
  12. #
  13. list(APPEND CXX_COMPILE_FLAGS
  14. "-fopenmp"
  15. "-DAPHRODITE_CPU_EXTENSION")
  16. execute_process(COMMAND cat /proc/cpuinfo
  17. RESULT_VARIABLE CPUINFO_RET
  18. OUTPUT_VARIABLE CPUINFO)
  19. if (NOT CPUINFO_RET EQUAL 0)
  20. message(FATAL_ERROR "Failed to check CPU features via /proc/cpuinfo")
  21. endif()
  22. function (find_isa CPUINFO TARGET OUT)
  23. string(FIND ${CPUINFO} ${TARGET} ISA_FOUND)
  24. if(NOT ISA_FOUND EQUAL -1)
  25. set(${OUT} ON PARENT_SCOPE)
  26. else()
  27. set(${OUT} OFF PARENT_SCOPE)
  28. endif()
  29. endfunction()
  30. function (is_avx512_disabled OUT)
  31. set(DISABLE_AVX512 $ENV{APHRODITE_CPU_DISABLE_AVX512})
  32. if(DISABLE_AVX512 AND DISABLE_AVX512 STREQUAL "true")
  33. set(${OUT} ON PARENT_SCOPE)
  34. else()
  35. set(${OUT} OFF PARENT_SCOPE)
  36. endif()
  37. endfunction()
  38. is_avx512_disabled(AVX512_DISABLED)
  39. find_isa(${CPUINFO} "avx2" AVX2_FOUND)
  40. find_isa(${CPUINFO} "avx512f" AVX512_FOUND)
  41. find_isa(${CPUINFO} "POWER10" POWER10_FOUND)
  42. find_isa(${CPUINFO} "POWER9" POWER9_FOUND)
  43. if (AVX512_FOUND AND NOT AVX512_DISABLED)
  44. list(APPEND CXX_COMPILE_FLAGS
  45. "-mavx512f"
  46. "-mavx512vl"
  47. "-mavx512bw"
  48. "-mavx512dq")
  49. find_isa(${CPUINFO} "avx512_bf16" AVX512BF16_FOUND)
  50. if (AVX512BF16_FOUND OR ENABLE_AVX512BF16)
  51. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
  52. CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12.3)
  53. list(APPEND CXX_COMPILE_FLAGS "-mavx512bf16")
  54. else()
  55. message(WARNING "Disable AVX512-BF16 ISA support, requires gcc/g++ >= 12.3")
  56. endif()
  57. else()
  58. message(WARNING "Disable AVX512-BF16 ISA support, no avx512_bf16 found in local CPU flags." " If cross-compilation is required, please set env APHRODITE_CPU_AVX512BF16=1.")
  59. endif()
  60. elseif (AVX2_FOUND)
  61. list(APPEND CXX_COMPILE_FLAGS "-mavx2")
  62. message(WARNING "Aphrodite CPU backend using AVX2 ISA")
  63. elseif (POWER9_FOUND OR POWER10_FOUND)
  64. message(STATUS "PowerPC detected")
  65. # Check for PowerPC VSX support
  66. list(APPEND CXX_COMPILE_FLAGS
  67. "-mvsx"
  68. "-mcpu=native"
  69. "-mtune=native")
  70. else()
  71. message(FATAL_ERROR "Aphrodite CPU backend requires AVX512 or AVX2 or Power9+ ISA support.")
  72. endif()
  73. message(STATUS "CPU extension compile flags: ${CXX_COMPILE_FLAGS}")
  74. list(APPEND LIBS dnnl numa)
  75. #
  76. # _C extension
  77. #
  78. set(APHRODITE_EXT_SRC
  79. "kernels/cpu/activation.cpp"
  80. "kernels/cpu/attention.cpp"
  81. "kernels/cpu/cache.cpp"
  82. "kernels/cpu/utils.cpp"
  83. "kernels/cpu/layernorm.cpp"
  84. "kernels/cpu/pos_encoding.cpp"
  85. "kernels/cpu/torch_bindings.cpp")
  86. if (AVX512_FOUND AND NOT AVX512_DISABLED)
  87. set(APHRODITE_EXT_SRC
  88. "kernels/cpu/quant.cpp"
  89. ${APHRODITE_EXT_SRC})
  90. endif()
  91. #
  92. # Define extension targets
  93. #
  94. define_gpu_extension_target(
  95. _C
  96. DESTINATION aphrodite
  97. LANGUAGE CXX
  98. SOURCES ${APHRODITE_EXT_SRC}
  99. LIBRARIES ${LIBS}
  100. COMPILE_FLAGS ${CXX_COMPILE_FLAGS}
  101. USE_SABI 3
  102. WITH_SOABI
  103. )
  104. message(STATUS "Enabling C extension.")
  105. add_dependencies(default _C)