cpu_extension.cmake 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. function (is_avx512_disabled OUT)
  30. set(DISABLE_AVX512 $ENV{APHRODITE_CPU_DISABLE_AVX512})
  31. if(DISABLE_AVX512 AND DISABLE_AVX512 STREQUAL "true")
  32. set(${OUT} ON PARENT_SCOPE)
  33. else()
  34. set(${OUT} OFF PARENT_SCOPE)
  35. endif()
  36. endfunction()
  37. is_avx512_disabled(AVX512_DISABLED)
  38. find_isa(${CPUINFO} "avx2" AVX2_FOUND)
  39. find_isa(${CPUINFO} "avx512f" AVX512_FOUND)
  40. find_isa(${CPUINFO} "POWER10" POWER10_FOUND)
  41. find_isa(${CPUINFO} "POWER9" POWER9_FOUND)
  42. if (AVX512_FOUND AND NOT AVX512_DISABLED)
  43. list(APPEND CXX_COMPILE_FLAGS
  44. "-mavx512f"
  45. "-mavx512vl"
  46. "-mavx512bw"
  47. "-mavx512dq")
  48. find_isa(${CPUINFO} "avx512_bf16" AVX512BF16_FOUND)
  49. if (AVX512BF16_FOUND OR ENABLE_AVX512BF16)
  50. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
  51. CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12.3)
  52. list(APPEND CXX_COMPILE_FLAGS "-mavx512bf16")
  53. else()
  54. message(WARNING "Disable AVX512-BF16 ISA support, requires gcc/g++ >= 12.3")
  55. endif()
  56. else()
  57. 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.")
  58. endif()
  59. elseif (AVX2_FOUND)
  60. list(APPEND CXX_COMPILE_FLAGS "-mavx2")
  61. message(WARNING "Aphrodite CPU backend using AVX2 ISA")
  62. elseif (POWER9_FOUND OR POWER10_FOUND)
  63. message(STATUS "PowerPC detected")
  64. # Check for PowerPC VSX support
  65. list(APPEND CXX_COMPILE_FLAGS
  66. "-mvsx"
  67. "-mcpu=native"
  68. "-mtune=native")
  69. else()
  70. message(FATAL_ERROR "Aphrodite CPU backend requires AVX512 or AVX2 or Power9+ ISA support.")
  71. endif()
  72. message(STATUS "CPU extension compile flags: ${CXX_COMPILE_FLAGS}")
  73. #
  74. # Define extension targets
  75. #
  76. #
  77. # _C extension
  78. #
  79. set(APHRODITE_EXT_SRC
  80. "kernels/cpu/activation.cpp"
  81. "kernels/cpu/attention.cpp"
  82. "kernels/cpu/cache.cpp"
  83. "kernels/cpu/layernorm.cpp"
  84. "kernels/cpu/pos_encoding.cpp"
  85. "kernels/cpu/torch_bindings.cpp")
  86. define_gpu_extension_target(
  87. _C
  88. DESTINATION aphrodite
  89. LANGUAGE CXX
  90. SOURCES ${APHRODITE_EXT_SRC}
  91. COMPILE_FLAGS ${CXX_COMPILE_FLAGS}
  92. USE_SABI 3
  93. WITH_SOABI
  94. )
  95. add_custom_target(default)
  96. message(STATUS "Enabling C extension.")
  97. add_dependencies(default _C)