ggml-opencl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "ggml-opencl.h"
  2. #define CL_TARGET_OPENCL_VERSION 110
  3. #include <clblast_c.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "ggml.h"
  8. #define MULTILINE_QUOTE(...) #__VA_ARGS__
  9. const char * clblast_dequant = MULTILINE_QUOTE(
  10. struct block_q4_0
  11. {
  12. float d;
  13. uchar qs[16];
  14. };
  15. __kernel void dequantize_row_q4_0(__global struct block_q4_0* blocks, __global float* result) {
  16. const uint i = get_global_id(0) / 32;
  17. const uint l = get_local_id(0);
  18. const float d = blocks[i].d;
  19. const uchar vi = blocks[i].qs[l];
  20. const uint index = i*32 + l*2;
  21. result[index + 0] = ((vi & 0xf) - 8)*d;
  22. result[index + 1] = ((vi >> 4) - 8)*d;
  23. }
  24. struct block_q4_1
  25. {
  26. float d;
  27. float m;
  28. uchar qs[16];
  29. };
  30. __kernel void dequantize_row_q4_1(__global struct block_q4_1* blocks, __global float* result) {
  31. const uint i = get_global_id(0) / 32;
  32. const uint l = get_local_id(0);
  33. const float d = blocks[i].d;
  34. const float m = blocks[i].m;
  35. const uchar vi = blocks[i].qs[l];
  36. const uint index = i*32 + l*2;
  37. result[index + 0] = (vi & 0xf) * d + m;
  38. result[index + 1] = (vi >> 4) * d + m;
  39. }
  40. struct block_q5_0
  41. {
  42. float d;
  43. uint qh;
  44. uchar qs[16];
  45. };
  46. __kernel void dequantize_row_q5_0(__global struct block_q5_0* blocks, __global float* result) {
  47. const uint i = get_global_id(0) / 32;
  48. const uint l = get_local_id(0);
  49. const float d = blocks[i].d;
  50. const uchar vi = blocks[i].qs[l];
  51. const uint l2 = l * 2;
  52. const uchar vh0 = ((blocks[i].qh & (1 << (l2 + 0))) >> (l2 + 0)) << 4;
  53. const uchar vh1 = ((blocks[i].qh & (1 << (l2 + 1))) >> (l2 + 1)) << 4;
  54. const uint index = i*32 + l2;
  55. result[index + 0] = (((vi & 0xf) | vh0) - 16)*d;
  56. result[index + 1] = (((vi >> 4) | vh1) - 16)*d;
  57. }
  58. struct block_q5_1
  59. {
  60. ushort d;
  61. ushort m;
  62. uint qh;
  63. uchar qs[16];
  64. };
  65. __kernel void dequantize_row_q5_1(__global struct block_q5_1* blocks, __global float* result) {
  66. const uint i = get_global_id(0) / 32;
  67. const uint l = get_local_id(0);
  68. const float d = vload_half(0, (__global half*) &blocks[i].d);
  69. const float m = vload_half(0, (__global half*) &blocks[i].m);
  70. const uchar vi = blocks[i].qs[l];
  71. const uint l2 = l * 2;
  72. const uchar vh0 = ((blocks[i].qh & (1 << (l2 + 0))) >> (l2 + 0)) << 4;
  73. const uchar vh1 = ((blocks[i].qh & (1 << (l2 + 1))) >> (l2 + 1)) << 4;
  74. const uint index = i*32 + l2;
  75. result[index + 0] = ((vi & 0xf) | vh0)*d + m;
  76. result[index + 1] = ((vi >> 4) | vh1)*d + m;
  77. }
  78. struct block_q8_0
  79. {
  80. float d;
  81. char qs[32];
  82. };
  83. __kernel void dequantize_row_q8_0(__global struct block_q8_0* blocks, __global float* result) {
  84. const uint i = get_global_id(0) / 32;
  85. const uint l = get_local_id(0);
  86. result[i*32 + l] = blocks[i].qs[l] * blocks[i].d;
  87. }
  88. );
  89. #define CL_CHECK(err, name) \
  90. do { \
  91. cl_int err_ = (err); \
  92. if (err_ != CL_SUCCESS) { \
  93. fprintf(stderr, "OpenCL %s error %d at %s:%d\n", name, err_, __FILE__, __LINE__); \
  94. exit(1); \
  95. } \
  96. } while (0)
  97. #define QK5_0 32
  98. typedef struct {
  99. ggml_fp16_t d; // delta
  100. uint8_t qh[4]; // 5-th bit of quants
  101. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  102. } block_q5_0;
  103. typedef struct {
  104. float d; // delta
  105. uint32_t qh; // 5-th bit of quants
  106. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  107. } cl_block_q5_0;
  108. static cl_platform_id platform;
  109. static cl_device_id device;
  110. static cl_context context;
  111. static cl_command_queue queue;
  112. static cl_program program;
  113. static cl_kernel kernel_q4_0, kernel_q4_1, kernel_q5_0, kernel_q5_1, kernel_q8_0;
  114. static cl_mem cl_buffer_a, cl_buffer_qb, cl_buffer_b, cl_buffer_c;
  115. static size_t cl_size_a = 0, cl_size_qb = 0, cl_size_b = 0, cl_size_c = 0;
  116. static cl_program build_program_from_source(cl_context ctx, cl_device_id dev, const char* program_buffer) {
  117. cl_program p;
  118. char *program_log;
  119. size_t program_size, log_size;
  120. int err;
  121. program_size = strlen(program_buffer);
  122. p = clCreateProgramWithSource(ctx, 1, (const char**)&program_buffer, &program_size, &err);
  123. if(err < 0) {
  124. fprintf(stderr, "OpenCL error creating program");
  125. exit(1);
  126. }
  127. err = clBuildProgram(p, 0, NULL, NULL, NULL, NULL);
  128. if(err < 0) {
  129. clGetProgramBuildInfo(p, dev, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
  130. program_log = (char*) malloc(log_size + 1);
  131. program_log[log_size] = '\0';
  132. clGetProgramBuildInfo(p, dev, CL_PROGRAM_BUILD_LOG, log_size + 1, program_log, NULL);
  133. printf("%s\n", program_log);
  134. free(program_log);
  135. exit(1);
  136. }
  137. return p;
  138. }
  139. void ggml_cl_init(void) {
  140. cl_int err = 0;
  141. char * GGML_CLBLAST_PLATFORM = getenv("GGML_CLBLAST_PLATFORM");
  142. char * GGML_CLBLAST_DEVICE = getenv("GGML_CLBLAST_DEVICE");
  143. int plat_num = (GGML_CLBLAST_PLATFORM == NULL ? 0 : atoi(GGML_CLBLAST_PLATFORM));
  144. int dev_num = (GGML_CLBLAST_DEVICE == NULL ? 0 : atoi(GGML_CLBLAST_DEVICE));
  145. printf("\nInitializing CLBlast (First Run)...");
  146. printf("\nAttempting to use: Platform=%d, Device=%d (If invalid, program will crash)\n",plat_num,dev_num);
  147. cl_uint num_platforms;
  148. clGetPlatformIDs(0, NULL, &num_platforms);
  149. cl_platform_id* platforms = (cl_platform_id*)malloc(num_platforms*sizeof(cl_platform_id));
  150. clGetPlatformIDs(num_platforms, platforms, NULL);
  151. platform = platforms[plat_num];
  152. char platform_buffer[1024];
  153. clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(platform_buffer), &platform_buffer, NULL);
  154. cl_uint num_devices;
  155. clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
  156. cl_device_id* devices = (cl_device_id*)malloc(num_devices*sizeof(cl_device_id));
  157. clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
  158. device = devices[dev_num];
  159. char device_buffer[1024];
  160. clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(device_buffer), &device_buffer, NULL);
  161. printf("Using Platform: %s Device: %s\n", platform_buffer, device_buffer);
  162. context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
  163. CL_CHECK(err, "clCreateContext");
  164. queue = clCreateCommandQueue(context, device, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &err);
  165. CL_CHECK(err, "clCreateCommandQueue");
  166. free(platforms);
  167. free(devices);
  168. program = build_program_from_source(context, device, clblast_dequant);
  169. // Prepare dequantize kernels
  170. kernel_q4_0 = clCreateKernel(program, "dequantize_row_q4_0", &err);
  171. CL_CHECK(err, "clCreateKernel");
  172. kernel_q4_1 = clCreateKernel(program, "dequantize_row_q4_1", &err);
  173. CL_CHECK(err, "clCreateKernel");
  174. kernel_q5_0 = clCreateKernel(program, "dequantize_row_q5_0", &err);
  175. CL_CHECK(err, "clCreateKernel");
  176. kernel_q5_1 = clCreateKernel(program, "dequantize_row_q5_1", &err);
  177. CL_CHECK(err, "clCreateKernel");
  178. kernel_q8_0 = clCreateKernel(program, "dequantize_row_q8_0", &err);
  179. CL_CHECK(err, "clCreateKernel");
  180. }
  181. static void ggml_cl_malloc(size_t req_size, size_t* cur_size, cl_mem_flags flags, cl_mem* buf) {
  182. if (req_size <= *cur_size) {
  183. return;
  184. }
  185. // Reallocate buffer with enough space
  186. if (*cur_size > 0) {
  187. clReleaseMemObject(*buf);
  188. }
  189. cl_int err;
  190. *buf = clCreateBuffer(context, flags, req_size, NULL, &err);
  191. *cur_size = req_size;
  192. CL_CHECK(err, "clCreateBuffer");
  193. }
  194. void ggml_cl_sgemm_wrapper(
  195. const enum ggml_blas_order order, const enum ggml_blas_op trans_a, const enum ggml_blas_op trans_b,
  196. const int m, const int n, const int k,
  197. const float alpha, const void *host_a, const int lda,
  198. const float *host_b, const int ldb, const float beta,
  199. float *host_c, const int ldc, const int btype) {
  200. cl_int err = 0;
  201. cl_kernel kernel;
  202. size_t global = n * k, local, size_qb;
  203. bool dequant;
  204. cl_block_q5_0* cl_host_b;
  205. switch (btype) {
  206. case GGML_TYPE_F32:
  207. dequant = false;
  208. break;
  209. case GGML_TYPE_Q4_0:
  210. dequant = true;
  211. kernel = kernel_q4_0;
  212. local = 16;
  213. size_qb = global * (sizeof(float) + local) / 32;
  214. break;
  215. case GGML_TYPE_Q4_1:
  216. dequant = true;
  217. kernel = kernel_q4_1;
  218. local = 16;
  219. size_qb = global * (sizeof(float) * 2 + local) / 32;
  220. break;
  221. case GGML_TYPE_Q5_0:
  222. dequant = true;
  223. kernel = kernel_q5_0;
  224. local = 16;
  225. // For some reason OpenCL seems to be incapable of working with structs of size 22.
  226. // 20 and 24 bytes are fine. Workaround to do the fp16 to fp32 step on CPU...
  227. // TODO Find the reason, fix and remove workaround.
  228. const block_q5_0* b = (const block_q5_0*) host_b;
  229. cl_host_b = (cl_block_q5_0*) malloc(sizeof(cl_block_q5_0) * global / 32);
  230. for (size_t i = 0; i < global / 32; i++) {
  231. cl_host_b[i].d = ggml_fp16_to_fp32(b[i].d);
  232. memcpy(&cl_host_b[i].qh, b[i].qh, sizeof(uint32_t));
  233. memcpy(&cl_host_b[i].qs, b[i].qs, QK5_0 / 2);
  234. }
  235. host_b = (const float*) cl_host_b;
  236. size_qb = global * (sizeof(float) + sizeof(uint32_t) + local) / 32;
  237. break;
  238. case GGML_TYPE_Q5_1:
  239. dequant = true;
  240. kernel = kernel_q5_1;
  241. local = 16;
  242. size_qb = global * (sizeof(ggml_fp16_t) * 2 + sizeof(uint32_t) + local) / 32;
  243. break;
  244. case GGML_TYPE_Q8_0:
  245. dequant = true;
  246. kernel = kernel_q8_0;
  247. local = 32;
  248. size_qb = global * (sizeof(float) + local) / 32;
  249. break;
  250. default:
  251. fprintf(stderr, "Error: Unsupported OpenCL btype %d\n", btype);
  252. abort();
  253. }
  254. const size_t size_a = m * k * sizeof(float);
  255. const size_t size_b = n * k * sizeof(float);
  256. const size_t size_c = m * n * sizeof(float);
  257. // Prepare buffers
  258. ggml_cl_malloc(size_a, &cl_size_a, CL_MEM_READ_ONLY, &cl_buffer_a);
  259. if (dequant) {
  260. ggml_cl_malloc(size_qb, &cl_size_qb, CL_MEM_READ_ONLY, &cl_buffer_qb);
  261. }
  262. ggml_cl_malloc(size_b, &cl_size_b, CL_MEM_READ_WRITE, &cl_buffer_b);
  263. ggml_cl_malloc(size_c, &cl_size_c, CL_MEM_WRITE_ONLY, &cl_buffer_c);
  264. cl_event ev_a, ev_qb, ev_b;
  265. if (dequant) {
  266. err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &cl_buffer_qb);
  267. err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &cl_buffer_b);
  268. CL_CHECK(err, "clSetKernelArg");
  269. err = clEnqueueWriteBuffer(queue, cl_buffer_qb, CL_FALSE, 0, size_qb, host_b, 0, NULL, &ev_qb);
  270. CL_CHECK(err, "clEnqueueWriteBuffer qb");
  271. } else {
  272. err = clEnqueueWriteBuffer(queue, cl_buffer_b, CL_FALSE, 0, size_b, host_b, 0, NULL, &ev_b);
  273. CL_CHECK(err, "clEnqueueWriteBuffer b");
  274. }
  275. err = clEnqueueWriteBuffer(queue, cl_buffer_a, CL_FALSE, 0, size_a, host_a, 0, NULL, &ev_a);
  276. CL_CHECK(err, "clEnqueueWriteBuffer a");
  277. if (dequant) {
  278. err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, &local, 1, &ev_qb, &ev_b);
  279. CL_CHECK(err, "clEnqueueNDRangeKernel");
  280. clReleaseEvent(ev_qb);
  281. }
  282. clWaitForEvents(1, &ev_a);
  283. clWaitForEvents(1, &ev_b);
  284. clReleaseEvent(ev_a);
  285. clReleaseEvent(ev_b);
  286. cl_event ev_sgemm;
  287. CLBlastStatusCode status = CLBlastSgemm((CLBlastLayout)order,
  288. (CLBlastTranspose)trans_a, (CLBlastTranspose)trans_b,
  289. m, n, k,
  290. alpha,
  291. cl_buffer_a, 0, lda,
  292. cl_buffer_b, 0, ldb,
  293. beta,
  294. cl_buffer_c, 0, ldc,
  295. &queue, &ev_sgemm);
  296. if (status != CLBlastSuccess) {
  297. fprintf(stderr, "Error: CLBlast SGEMM %d\n", status);
  298. abort();
  299. }
  300. cl_event ev_c;
  301. clEnqueueReadBuffer(queue, cl_buffer_c, CL_TRUE, 0, size_c, host_c, 1, &ev_sgemm, &ev_c);
  302. // Wait for completion
  303. clWaitForEvents(1, &ev_c);
  304. clReleaseEvent(ev_sgemm);
  305. clReleaseEvent(ev_c);
  306. if (btype == GGML_TYPE_Q5_0) {
  307. free((void*) cl_host_b);
  308. }
  309. }