1
0

cpu_extension.cmake 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  2. #
  3. # Define environment variables for special configurations
  4. #
  5. if(DEFINED ENV{APHRODITE_CPU_AVX512BF16})
  6. set(ENABLE_AVX512BF16 ON)
  7. endif()
  8. include_directories("${CMAKE_SOURCE_DIR}/kernels")
  9. #
  10. # Check the compile flags
  11. #
  12. list(APPEND CXX_COMPILE_FLAGS
  13. "-fopenmp"
  14. "-DAPHRODITE_CPU_EXTENSION")
  15. execute_process(COMMAND cat /proc/cpuinfo
  16. RESULT_VARIABLE CPUINFO_RET
  17. OUTPUT_VARIABLE CPUINFO)
  18. if (NOT CPUINFO_RET EQUAL 0)
  19. message(FATAL_ERROR "Failed to check CPU features via /proc/cpuinfo")
  20. endif()
  21. function (find_isa CPUINFO TARGET OUT)
  22. string(FIND ${CPUINFO} ${TARGET} ISA_FOUND)
  23. if(NOT ISA_FOUND EQUAL -1)
  24. set(${OUT} ON PARENT_SCOPE)
  25. else()
  26. set(${OUT} OFF PARENT_SCOPE)
  27. endif()
  28. endfunction()
  29. find_isa(${CPUINFO} "avx512f" AVX512_FOUND)
  30. if (AVX512_FOUND)
  31. list(APPEND CXX_COMPILE_FLAGS
  32. "-mavx512f"
  33. "-mavx512vl"
  34. "-mavx512bw"
  35. "-mavx512dq")
  36. find_isa(${CPUINFO} "avx512_bf16" AVX512BF16_FOUND)
  37. if (AVX512BF16_FOUND OR ENABLE_AVX512BF16)
  38. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
  39. CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12.3)
  40. list(APPEND CXX_COMPILE_FLAGS "-mavx512bf16")
  41. else()
  42. message(WARNING "Disable AVX512-BF16 ISA support, requires gcc/g++ >= 12.3")
  43. endif()
  44. else()
  45. 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.")
  46. endif()
  47. else()
  48. message(FATAL_ERROR "Aphrodite CPU backend requires AVX512 ISA support.")
  49. endif()
  50. message(STATUS "CPU extension compile flags: ${CXX_COMPILE_FLAGS}")
  51. #
  52. # Define extension targets
  53. #
  54. #
  55. # _C extension
  56. #
  57. set(APHRODITE_EXT_SRC
  58. "kernels/cpu/activation.cpp"
  59. "kernels/cpu/attention.cpp"
  60. "kernels/cpu/cache.cpp"
  61. "kernels/cpu/layernorm.cpp"
  62. "kernels/cpu/pos_encoding.cpp"
  63. "kernels/cpu/pybind.cpp")
  64. define_gpu_extension_target(
  65. _C
  66. DESTINATION aphrodite
  67. LANGUAGE CXX
  68. SOURCES ${APHRODITE_EXT_SRC}
  69. COMPILE_FLAGS ${CXX_COMPILE_FLAGS}
  70. WITH_SOABI
  71. )
  72. add_custom_target(default)
  73. message(STATUS "Enabling C extension.")
  74. add_dependencies(default _C)