ggml-opencl.c 13 KB

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