|
| 1 | + |
| 2 | +find_package(OpenCL) |
| 3 | + |
| 4 | +if (OpenCL_FOUND) |
| 5 | + find_package(Python3 REQUIRED) |
| 6 | + |
| 7 | + set(TARGET_NAME ggml-opencl2) |
| 8 | + |
| 9 | + add_library(${TARGET_NAME} |
| 10 | + ggml-opencl2.cpp |
| 11 | + ../../include/ggml-opencl2.h) |
| 12 | + target_link_libraries(${TARGET_NAME} PRIVATE ggml-base ${OpenCL_LIBRARIES}) |
| 13 | + target_include_directories(${TARGET_NAME} PRIVATE . .. ${OpenCL_INCLUDE_DIRS}) |
| 14 | + |
| 15 | + # TODO - this is kind of strange. We have been calling this backend OpenCL2, |
| 16 | + # so everything (function names, folder name, etc) except macro switches |
| 17 | + # has been OpenCL2. Now, the backend frameworke enforces the use of the folder |
| 18 | + # name as the backend name and switch. So, GGML_USE_OPENCL2 is used in |
| 19 | + # ggml-backend-reg.cpp, but the rest still uses GGML_USE_OPENCL. |
| 20 | + add_compile_definitions(GGML_USE_OPENCL) |
| 21 | + |
| 22 | + if (GGML_OPENCL_PROFILING) |
| 23 | + message(STATUS "OpenCL profiling enabled (increases CPU overhead)") |
| 24 | + add_compile_definitions(GGML_OPENCL_PROFILING) |
| 25 | + endif () |
| 26 | + |
| 27 | + add_compile_definitions(GGML_OPENCL_SOA_Q) |
| 28 | + |
| 29 | + if (GGML_OPENCL_SMALL_ALLOC) |
| 30 | + message(STATUS "OpenCL will allocate a separate buffer for each tensor. " |
| 31 | + "The default behavior allocates a large buffer to hold multiple tensors.") |
| 32 | + add_compile_definitions(GGML_OPENCL_SMALL_ALLOC) |
| 33 | + endif () |
| 34 | + |
| 35 | + if (GGML_OPENCL_USE_ADRENO_KERNELS) |
| 36 | + message(STATUS "OpenCL will use matmul kernels optimized for Adreno") |
| 37 | + add_compile_definitions(GGML_OPENCL_USE_ADRENO_KERNELS) |
| 38 | + endif () |
| 39 | + |
| 40 | + if (GGML_OPENCL_EMBED_KERNELS) |
| 41 | + add_compile_definitions(GGML_OPENCL_EMBED_KERNELS) |
| 42 | + |
| 43 | + set(OPENCL2_CL_SOURCE_EMBED "${CMAKE_BINARY_DIR}/autogenerated/ggml-opencl2.cl.h") |
| 44 | + set(OPENCL2_MM_CL_SOURCE_EMBED "${CMAKE_BINARY_DIR}/autogenerated/ggml-opencl2_mm.cl.h") |
| 45 | + set(OPENCL2_CVT_CL_SOURCE_EMBED "${CMAKE_BINARY_DIR}/autogenerated/ggml-opencl2_cvt.cl.h") |
| 46 | + |
| 47 | + set(OPENCL2_GEMV_NOSHUFFLE_SOURCE_EMBED "${CMAKE_BINARY_DIR}/autogenerated/ggml-opencl2_gemv_noshuffle.cl.h") |
| 48 | + set(OPENCL2_GEMV_NOSHUFFLE_GENERAL_SOURCE_EMBED "${CMAKE_BINARY_DIR}/autogenerated/ggml-opencl2_gemv_noshuffle_general.cl.h") |
| 49 | + set(OPENCL2_MUL_MAT_Ab_Bi_8x4_SOURCE_EMBED "${CMAKE_BINARY_DIR}/autogenerated/ggml-opencl2_mul_mat_Ab_Bi_8x4.cl.h") |
| 50 | + set(OPENCL2_TRANSPOSE_16_SOURCE_EMBED "${CMAKE_BINARY_DIR}/autogenerated/ggml-opencl2_transpose_16.cl.h") |
| 51 | + set(OPENCL2_TRANSPOSE_32_SOURCE_EMBED "${CMAKE_BINARY_DIR}/autogenerated/ggml-opencl2_transpose_32.cl.h") |
| 52 | + set(OPENCL2_TRANSPOSE_32_16_SOURCE_EMBED "${CMAKE_BINARY_DIR}/autogenerated/ggml-opencl2_transpose_32_16.cl.h") |
| 53 | + |
| 54 | + set(EMBED_KERNEL_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/kernels/embed_kernel.py") |
| 55 | + file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/autogenerated") |
| 56 | + |
| 57 | + include_directories("${CMAKE_BINARY_DIR}/autogenerated") |
| 58 | + |
| 59 | + # Python must be accessible from command line |
| 60 | + add_custom_command( |
| 61 | + OUTPUT ${OPENCL2_CL_SOURCE_EMBED} |
| 62 | + COMMAND ${Python3_EXECUTABLE} ${EMBED_KERNEL_SCRIPT} |
| 63 | + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ggml-opencl2.cl |
| 64 | + ${OPENCL2_CL_SOURCE_EMBED} |
| 65 | + DEPENDS kernels/ggml-opencl2.cl ${EMBED_KERNEL_SCRIPT} |
| 66 | + COMMENT "Generate ggml-opencl2.cl.h" |
| 67 | + ) |
| 68 | + |
| 69 | + add_custom_command( |
| 70 | + OUTPUT ${OPENCL2_MM_CL_SOURCE_EMBED} |
| 71 | + COMMAND ${Python3_EXECUTABLE} ${EMBED_KERNEL_SCRIPT} |
| 72 | + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ggml-opencl2_mm.cl |
| 73 | + ${OPENCL2_MM_CL_SOURCE_EMBED} |
| 74 | + DEPENDS kernels/ggml-opencl2_mm.cl ${EMBED_KERNEL_SCRIPT} |
| 75 | + COMMENT "Generate ggml-opencl2_mm.cl.h" |
| 76 | + ) |
| 77 | + |
| 78 | + add_custom_command( |
| 79 | + OUTPUT ${OPENCL2_CVT_CL_SOURCE_EMBED} |
| 80 | + COMMAND ${Python3_EXECUTABLE} ${EMBED_KERNEL_SCRIPT} |
| 81 | + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ggml-opencl2_cvt.cl |
| 82 | + ${OPENCL2_CVT_CL_SOURCE_EMBED} |
| 83 | + DEPENDS kernels/ggml-opencl2_cvt.cl ${EMBED_KERNEL_SCRIPT} |
| 84 | + COMMENT "Generate ggml-opencl2_cvt.cl.h" |
| 85 | + ) |
| 86 | + |
| 87 | + add_custom_command( |
| 88 | + OUTPUT ${OPENCL2_GEMV_NOSHUFFLE_SOURCE_EMBED} |
| 89 | + COMMAND python ${EMBED_KERNEL_SCRIPT} |
| 90 | + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ggml-opencl2_gemv_noshuffle.cl |
| 91 | + ${OPENCL2_GEMV_NOSHUFFLE_SOURCE_EMBED} |
| 92 | + DEPENDS kernels/ggml-opencl2_gemv_noshuffle.cl ${EMBED_KERNEL_SCRIPT} |
| 93 | + COMMENT "Generate ggml-opencl2_gemv_noshuffle.cl.h" |
| 94 | + ) |
| 95 | + |
| 96 | + add_custom_command( |
| 97 | + OUTPUT ${OPENCL2_GEMV_NOSHUFFLE_GENERAL_SOURCE_EMBED} |
| 98 | + COMMAND python ${EMBED_KERNEL_SCRIPT} |
| 99 | + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ggml-opencl2_gemv_noshuffle_general.cl |
| 100 | + ${OPENCL2_GEMV_NOSHUFFLE_GENERAL_SOURCE_EMBED} |
| 101 | + DEPENDS kernels/ggml-opencl2_gemv_noshuffle_general.cl ${EMBED_KERNEL_SCRIPT} |
| 102 | + COMMENT "Generate ggml-opencl2_gemv_noshuffle_general.cl.h" |
| 103 | + ) |
| 104 | + |
| 105 | + add_custom_command( |
| 106 | + OUTPUT ${OPENCL2_MUL_MAT_Ab_Bi_8x4_SOURCE_EMBED} |
| 107 | + COMMAND python ${EMBED_KERNEL_SCRIPT} |
| 108 | + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ggml-opencl2_mul_mat_Ab_Bi_8x4.cl |
| 109 | + ${OPENCL2_MUL_MAT_Ab_Bi_8x4_SOURCE_EMBED} |
| 110 | + DEPENDS kernels/ggml-opencl2_mul_mat_Ab_Bi_8x4.cl ${EMBED_KERNEL_SCRIPT} |
| 111 | + COMMENT "Generate ggml-opencl2_mul_mat_Ab_Bi_8x4.cl.cl.h" |
| 112 | + ) |
| 113 | + |
| 114 | + add_custom_command( |
| 115 | + OUTPUT ${OPENCL2_TRANSPOSE_16_SOURCE_EMBED} |
| 116 | + COMMAND python ${EMBED_KERNEL_SCRIPT} |
| 117 | + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ggml-opencl2_transpose_16.cl |
| 118 | + ${OPENCL2_TRANSPOSE_16_SOURCE_EMBED} |
| 119 | + DEPENDS kernels/ggml-opencl2_transpose_16.cl ${EMBED_KERNEL_SCRIPT} |
| 120 | + COMMENT "Generate ggml-opencl2_transpose_16.cl.h" |
| 121 | + ) |
| 122 | + |
| 123 | + add_custom_command( |
| 124 | + OUTPUT ${OPENCL2_TRANSPOSE_32_SOURCE_EMBED} |
| 125 | + COMMAND python ${EMBED_KERNEL_SCRIPT} |
| 126 | + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ggml-opencl2_transpose_32.cl |
| 127 | + ${OPENCL2_TRANSPOSE_32_SOURCE_EMBED} |
| 128 | + DEPENDS kernels/ggml-opencl2_transpose_32.cl ${EMBED_KERNEL_SCRIPT} |
| 129 | + COMMENT "Generate ggml-opencl2_transpose_32.cl.h" |
| 130 | + ) |
| 131 | + |
| 132 | + add_custom_command( |
| 133 | + OUTPUT ${OPENCL2_TRANSPOSE_32_16_SOURCE_EMBED} |
| 134 | + COMMAND python ${EMBED_KERNEL_SCRIPT} |
| 135 | + ${CMAKE_CURRENT_SOURCE_DIR}/kernels/ggml-opencl2_transpose_32_16.cl |
| 136 | + ${OPENCL2_TRANSPOSE_32_16_SOURCE_EMBED} |
| 137 | + DEPENDS kernels/ggml-opencl2_transpose_32_16.cl ${EMBED_KERNEL_SCRIPT} |
| 138 | + COMMENT "Generate ggml-opencl2_transpose_32_16.cl.h" |
| 139 | + ) |
| 140 | + |
| 141 | + target_sources(${TARGET_NAME} PRIVATE |
| 142 | + ${OPENCL2_CL_SOURCE_EMBED} |
| 143 | + ${OPENCL2_MM_CL_SOURCE_EMBED} |
| 144 | + ${OPENCL2_CVT_CL_SOURCE_EMBED} |
| 145 | + ${OPENCL2_GEMV_NOSHUFFLE_SOURCE_EMBED} |
| 146 | + ${OPENCL2_GEMV_NOSHUFFLE_GENERAL_SOURCE_EMBED} |
| 147 | + ${OPENCL2_MUL_MAT_Ab_Bi_8x4_SOURCE_EMBED} |
| 148 | + ${OPENCL2_TRANSPOSE_16_SOURCE_EMBED} |
| 149 | + ${OPENCL2_TRANSPOSE_32_SOURCE_EMBED} |
| 150 | + ${OPENCL2_TRANSPOSE_32_16_SOURCE_EMBED}) |
| 151 | + else () |
| 152 | + # copy ggml-opencl.cl to bin directory |
| 153 | + configure_file(kernels/ggml-opencl2.cl ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml-opencl2.cl COPYONLY) |
| 154 | + configure_file(kernels/ggml-opencl2_mm.cl ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml-opencl2_mm.cl COPYONLY) |
| 155 | + configure_file(kernels/ggml-opencl2_cvt.cl ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ggml-opencl2_cvt.cl COPYONLY) |
| 156 | + endif () |
| 157 | +else () |
| 158 | + message(WARNING "OpenCL not found") |
| 159 | +endif () |
0 commit comments