llama_cpp 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1034 @@
1
+ #include "ggml-opencl.h"
2
+
3
+ #include <array>
4
+ #include <atomic>
5
+ #include <sstream>
6
+
7
+ #define CL_TARGET_OPENCL_VERSION 110
8
+ #include <clblast.h>
9
+
10
+ #include <stdlib.h>
11
+ #include <stdio.h>
12
+ #include <string.h>
13
+
14
+ #include "ggml.h"
15
+
16
+ #define CL_DMMV_BLOCK_SIZE 32;
17
+
18
+ #define MULTILINE_QUOTE(...) #__VA_ARGS__
19
+ static std::string program_source = MULTILINE_QUOTE(
20
+
21
+ typedef char int8_t;
22
+ typedef uchar uint8_t;
23
+ typedef int int32_t;
24
+ typedef uint uint32_t;
25
+
26
+ struct __attribute__ ((packed)) block_q4_0
27
+ {
28
+ half d;
29
+ uint8_t qs[QK4_0 / 2];
30
+ };
31
+
32
+ struct __attribute__ ((packed)) block_q4_1
33
+ {
34
+ half d;
35
+ half m;
36
+ uint8_t qs[QK4_1 / 2];
37
+ };
38
+
39
+ struct __attribute__ ((packed)) block_q5_0
40
+ {
41
+ half d;
42
+ uint32_t qh;
43
+ uint8_t qs[QK5_0 / 2];
44
+ };
45
+
46
+ struct __attribute__ ((packed)) block_q5_1
47
+ {
48
+ half d;
49
+ half m;
50
+ uint32_t qh;
51
+ uint8_t qs[QK5_1 / 2];
52
+ };
53
+
54
+ struct __attribute__ ((packed)) block_q8_0
55
+ {
56
+ half d;
57
+ int8_t qs[QK8_0];
58
+ };
59
+
60
+
61
+ __kernel void convert_fp16_to_fp32(__global half* x, __global float* y) {
62
+ const uint i = get_global_id(0);
63
+
64
+ y[i] = vload_half(0, &x[i]);
65
+ }
66
+
67
+ void dequantize_q4_0(__global const struct block_q4_0* x, const int ib, const int iqs, float* v0, float* v1) {
68
+ const float d = vload_half(0, &x[ib].d);
69
+
70
+ const uint8_t vui = x[ib].qs[iqs];
71
+
72
+ const int8_t vi0 = vui & 0xF;
73
+ const int8_t vi1 = vui >> 4;
74
+
75
+ *v0 = (vi0 - 8)*d;
76
+ *v1 = (vi1 - 8)*d;
77
+ }
78
+ void dequantize_q4_1(__global const struct block_q4_1* x, const int ib, const int iqs, float* v0, float* v1) {
79
+ const float d = vload_half(0, &x[ib].d);
80
+ const float m = vload_half(0, &x[ib].m);
81
+
82
+ const uint8_t vui = x[ib].qs[iqs];
83
+
84
+ const int8_t vi0 = vui & 0xF;
85
+ const int8_t vi1 = vui >> 4;
86
+
87
+ *v0 = vi0*d + m;
88
+ *v1 = vi1*d + m;
89
+ }
90
+ void dequantize_q5_0(__global const struct block_q5_0* x, const int ib, const int iqs, float* v0, float* v1) {
91
+ const float d = vload_half(0, &x[ib].d);
92
+
93
+ uint32_t qh = x[ib].qh;
94
+
95
+ const uint8_t xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
96
+ const uint8_t xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
97
+
98
+ const int32_t x0 = ((x[ib].qs[iqs] & 0xf) | xh_0) - 16;
99
+ const int32_t x1 = ((x[ib].qs[iqs] >> 4) | xh_1) - 16;
100
+
101
+ *v0 = x0*d;
102
+ *v1 = x1*d;
103
+ }
104
+ void dequantize_q5_1(__global const struct block_q5_1* x, const int ib, const int iqs, float* v0, float* v1) {
105
+ const float d = vload_half(0, &x[ib].d);
106
+ const float m = vload_half(0, &x[ib].m);
107
+
108
+ uint32_t qh = x[ib].qh;
109
+
110
+ const uint8_t xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
111
+ const uint8_t xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
112
+
113
+ const int32_t x0 = ((x[ib].qs[iqs] & 0xf) | xh_0);
114
+ const int32_t x1 = ((x[ib].qs[iqs] >> 4) | xh_1);
115
+
116
+ *v0 = x0*d + m;
117
+ *v1 = x1*d + m;
118
+ }
119
+ void dequantize_q8_0(__global const struct block_q8_0* x, const int ib, const int iqs, float* v0, float* v1) {
120
+ const float d = vload_half(0, &x[ib].d);
121
+
122
+ const int8_t vi0 = x[ib].qs[iqs + 0];
123
+ const int8_t vi1 = x[ib].qs[iqs + 1];
124
+
125
+ *v0 = vi0*d;
126
+ *v1 = vi1*d;
127
+ }
128
+ void convert_f16(__global half* x, const int ib, const int iqs, float* v0, float* v1){
129
+ *v0 = vload_half(0, &x[ib + 0]);
130
+ *v1 = vload_half(0, &x[ib + 1]);
131
+ }
132
+ );
133
+
134
+ std::string dequant_template = MULTILINE_QUOTE(
135
+ __kernel void KERNEL_NAME(__global X_TYPE* x, __global float* y) {
136
+ const int i = get_group_id(0)*get_local_size(0) + get_local_id(0)*2;
137
+
138
+ if (i >= get_global_size(0)) {
139
+ return;
140
+ }
141
+
142
+ const uint qk = QUANT_K;
143
+ const uint qr = QUANT_R;
144
+
145
+ const int ib = i/qk; // block index
146
+ const int iqs = (i%qk)/qr; // quant index
147
+ const int iybs = i - i%qk; // y block start index
148
+ const int y_offset = qr == 1 ? 1 : qk/2;
149
+
150
+ // dequantize
151
+ float v0, v1;
152
+ DEQUANT_FUNC(x, ib, iqs, &v0, &v1);
153
+ y[iybs + iqs + 0] = v0;
154
+ y[iybs + iqs + y_offset] = v1;
155
+ }
156
+ );
157
+
158
+ std::string dequant_mul_mat_vec_template = MULTILINE_QUOTE(
159
+ __kernel void KERNEL_NAME(__global X_TYPE* x, __local float* tmp, __global float* y, __global float* dst, const int ncols) {
160
+ const int block_size = get_local_size(0);
161
+ const int row = get_global_id(0) / block_size;
162
+ const int tid = get_local_id(0);
163
+
164
+ const uint qk = QUANT_K;
165
+ const uint qr = QUANT_R;
166
+
167
+ const int y_offset = qr == 1 ? 1 : qk/2;
168
+
169
+ tmp[tid] = 0;
170
+
171
+ for (int i = 0; i < ncols/block_size; i += 2) {
172
+ const int col = i*block_size + 2*tid;
173
+ const int ib = (row*ncols + col)/qk; // block index
174
+ const int iqs = (col%qk)/qr; // quant index
175
+ const int iybs = col - col%qk; // y block start index
176
+
177
+ // dequantize
178
+ float v0, v1;
179
+ DEQUANT_FUNC(x, ib, iqs, &v0, &v1);
180
+
181
+ // matrix multiplication
182
+ tmp[tid] += v0 * y[iybs + iqs + 0];
183
+ tmp[tid] += v1 * y[iybs + iqs + y_offset];
184
+ }
185
+
186
+ // sum up partial sums and write back result
187
+ barrier(CLK_LOCAL_MEM_FENCE);
188
+ for (int s=block_size/2; s>0; s>>=1) {
189
+ if (tid < s) {
190
+ tmp[tid] += tmp[tid + s];
191
+ }
192
+ barrier(CLK_LOCAL_MEM_FENCE);
193
+ }
194
+ if (tid == 0) {
195
+ dst[row] = tmp[0];
196
+ }
197
+ }
198
+ );
199
+
200
+ #define CL_CHECK(err) \
201
+ do { \
202
+ cl_int err_ = (err); \
203
+ if (err_ != CL_SUCCESS) { \
204
+ fprintf(stderr, "ggml_opencl: %s error %d at %s:%d\n", \
205
+ #err, err_, __FILE__, __LINE__); \
206
+ exit(1); \
207
+ } \
208
+ } while (0)
209
+
210
+ #define CLBLAST_CHECK(err) \
211
+ do { \
212
+ CLBlastStatusCode err_ = (err); \
213
+ if (err_ != CLBlastSuccess) { \
214
+ fprintf(stderr, "ggml_opencl: %s error %d at %s:%d\n", \
215
+ #err, err_, __FILE__, __LINE__); \
216
+ exit(1); \
217
+ } \
218
+ } while (0)
219
+
220
+ std::array<std::string, 5> dequant_str_keys = {
221
+ "KERNEL_NAME", "X_TYPE", "QUANT_K", "QUANT_R", "DEQUANT_FUNC"
222
+ };
223
+
224
+ std::array<std::string, 30> dequant_str_values = {
225
+ "dequantize_row_q4_0", "struct block_q4_0", "QK4_0", "QR4_0", "dequantize_q4_0",
226
+ "dequantize_row_q4_1", "struct block_q4_1", "QK4_1", "QR4_1", "dequantize_q4_1",
227
+ "dequantize_row_q5_0", "struct block_q5_0", "QK5_0", "QR5_0", "dequantize_q5_0",
228
+ "dequantize_row_q5_1", "struct block_q5_1", "QK5_1", "QR5_1", "dequantize_q5_1",
229
+ "dequantize_row_q8_0", "struct block_q8_0", "QK8_0", "QR8_0", "dequantize_q8_0",
230
+ "convert_row_f16", "half", "1", "1", "convert_f16"
231
+ };
232
+
233
+ std::array<std::string, 30> dequant_mul_mat_vec_str_values = {
234
+ "dequantize_mul_mat_vec_q4_0", "struct block_q4_0", "QK4_0", "QR4_0", "dequantize_q4_0",
235
+ "dequantize_mul_mat_vec_q4_1", "struct block_q4_1", "QK4_1", "QR4_1", "dequantize_q4_1",
236
+ "dequantize_mul_mat_vec_q5_0", "struct block_q5_0", "QK5_0", "QR5_0", "dequantize_q5_0",
237
+ "dequantize_mul_mat_vec_q5_1", "struct block_q5_1", "QK5_1", "QR5_1", "dequantize_q5_1",
238
+ "dequantize_mul_mat_vec_q8_0", "struct block_q8_0", "QK8_0", "QR8_0", "dequantize_q8_0",
239
+ "convert_mul_mat_vec_f16", "half", "1", "1", "convert_f16"
240
+ };
241
+
242
+ std::string& replace(std::string& s, const std::string& from, const std::string& to) {
243
+ size_t pos = 0;
244
+ while ((pos = s.find(from, pos)) != std::string::npos) {
245
+ s.replace(pos, from.length(), to);
246
+ pos += to.length();
247
+ }
248
+ return s;
249
+ }
250
+
251
+ std::string generate_kernels() {
252
+ std::stringstream src;
253
+ src << program_source << '\n';
254
+ for (size_t i = 0; i < dequant_str_values.size(); i += dequant_str_keys.size()) {
255
+ std::string dequant_kernel = dequant_template;
256
+ std::string dmmv_kernel = dequant_mul_mat_vec_template;
257
+ for (size_t j = 0; j < dequant_str_keys.size(); j++) {
258
+ replace(dequant_kernel, dequant_str_keys[j], dequant_str_values[i + j]);
259
+ replace(dmmv_kernel, dequant_str_keys[j], dequant_mul_mat_vec_str_values[i + j]);
260
+ }
261
+ src << dequant_kernel << '\n';
262
+ src << dmmv_kernel << '\n';
263
+ }
264
+ return src.str();
265
+ }
266
+
267
+ static cl_platform_id platform;
268
+ static cl_device_id device;
269
+ static cl_context context;
270
+ static cl_command_queue queue;
271
+ static cl_program program;
272
+ static cl_kernel convert_row_f16_cl;
273
+ static cl_kernel dequantize_row_q4_0_cl, dequantize_row_q4_1_cl, dequantize_row_q5_0_cl, dequantize_row_q5_1_cl, dequantize_row_q8_0_cl;
274
+ static cl_kernel dequantize_mul_mat_vec_q4_0_cl, dequantize_mul_mat_vec_q4_1_cl, dequantize_mul_mat_vec_q5_0_cl, dequantize_mul_mat_vec_q5_1_cl, dequantize_mul_mat_vec_q8_0_cl, convert_mul_mat_vec_f16_cl;
275
+ static bool fp16_support;
276
+
277
+ static cl_program build_program_from_source(cl_context ctx, cl_device_id dev, const char* program_buffer) {
278
+ cl_program p;
279
+ char *program_log;
280
+ size_t program_size;
281
+ size_t log_size;
282
+ int err;
283
+
284
+ program_size = strlen(program_buffer);
285
+
286
+ p = clCreateProgramWithSource(ctx, 1, (const char**)&program_buffer, &program_size, &err);
287
+ if(err < 0) {
288
+ fprintf(stderr, "OpenCL error creating program");
289
+ exit(1);
290
+ }
291
+
292
+ const char* compile_opts = "-cl-mad-enable -cl-unsafe-math-optimizations -cl-finite-math-only -cl-fast-relaxed-math "
293
+ "-DQK4_0=32 -DQR4_0=2 -DQK4_1=32 -DQR4_1=2 -DQK5_0=32 -DQR5_0=2 -DQK5_1=32 -DQR5_1=2 -DQK8_0=32 -DQR8_0=1";
294
+
295
+ err = clBuildProgram(p, 0, NULL, compile_opts, NULL, NULL);
296
+ if(err < 0) {
297
+
298
+ clGetProgramBuildInfo(p, dev, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
299
+ program_log = (char*) malloc(log_size + 1);
300
+ program_log[log_size] = '\0';
301
+ clGetProgramBuildInfo(p, dev, CL_PROGRAM_BUILD_LOG, log_size + 1, program_log, NULL);
302
+ fprintf(stderr, "ggml_opencl: kernel compile error:\n\n%s\n", program_log);
303
+ free(program_log);
304
+ exit(1);
305
+ }
306
+
307
+ return p;
308
+ }
309
+
310
+ void ggml_cl_init(void) {
311
+ cl_int err;
312
+
313
+ struct cl_device;
314
+ struct cl_platform {
315
+ cl_platform_id id;
316
+ unsigned number;
317
+ char name[128];
318
+ char vendor[128];
319
+ struct cl_device * devices;
320
+ unsigned n_devices;
321
+ struct cl_device * default_device;
322
+ };
323
+
324
+ struct cl_device {
325
+ struct cl_platform * platform;
326
+ cl_device_id id;
327
+ unsigned number;
328
+ cl_device_type type;
329
+ char name[128];
330
+ };
331
+
332
+ enum { NPLAT = 16, NDEV = 16 };
333
+
334
+ struct cl_platform platforms[NPLAT];
335
+ unsigned n_platforms = 0;
336
+ struct cl_device devices[NDEV];
337
+ unsigned n_devices = 0;
338
+ struct cl_device * default_device = NULL;
339
+
340
+ platform = NULL;
341
+ device = NULL;
342
+
343
+ cl_platform_id platform_ids[NPLAT];
344
+ CL_CHECK(clGetPlatformIDs(NPLAT, platform_ids, &n_platforms));
345
+
346
+ for (unsigned i = 0; i < n_platforms; i++) {
347
+ struct cl_platform * p = &platforms[i];
348
+ p->number = i;
349
+ p->id = platform_ids[i];
350
+ CL_CHECK(clGetPlatformInfo(p->id, CL_PLATFORM_NAME, sizeof(p->name), &p->name, NULL));
351
+ CL_CHECK(clGetPlatformInfo(p->id, CL_PLATFORM_VENDOR, sizeof(p->vendor), &p->vendor, NULL));
352
+
353
+ cl_device_id device_ids[NDEV];
354
+ cl_int clGetDeviceIDsError = clGetDeviceIDs(p->id, CL_DEVICE_TYPE_ALL, NDEV, device_ids, &p->n_devices);
355
+ if (clGetDeviceIDsError == CL_DEVICE_NOT_FOUND) {
356
+ p->n_devices = 0;
357
+ } else {
358
+ CL_CHECK(clGetDeviceIDsError);
359
+ }
360
+ p->devices = p->n_devices > 0 ? &devices[n_devices] : NULL;
361
+ p->default_device = NULL;
362
+
363
+ for (unsigned j = 0; j < p->n_devices; j++) {
364
+ struct cl_device * d = &devices[n_devices];
365
+ d->number = n_devices++;
366
+ d->id = device_ids[j];
367
+ d->platform = p;
368
+ CL_CHECK(clGetDeviceInfo(d->id, CL_DEVICE_NAME, sizeof(d->name), &d->name, NULL));
369
+ CL_CHECK(clGetDeviceInfo(d->id, CL_DEVICE_TYPE, sizeof(d->type), &d->type, NULL));
370
+
371
+ if (p->default_device == NULL && d->type == CL_DEVICE_TYPE_GPU) {
372
+ p->default_device = d;
373
+ }
374
+ }
375
+
376
+ if (default_device == NULL && p->default_device != NULL) {
377
+ default_device = p->default_device;
378
+ }
379
+ }
380
+
381
+ if (n_devices == 0) {
382
+ fprintf(stderr, "ggml_opencl: could find any OpenCL devices.\n");
383
+ exit(1);
384
+ }
385
+
386
+ char * user_platform_string = getenv("GGML_OPENCL_PLATFORM");
387
+ char * user_device_string = getenv("GGML_OPENCL_DEVICE");
388
+ int user_platform_number = -1;
389
+ int user_device_number = -1;
390
+
391
+ unsigned n;
392
+ if (user_platform_string != NULL && sscanf(user_platform_string, " %u", &n) == 1 && n < n_platforms) {
393
+ user_platform_number = (int)n;
394
+ }
395
+ if (user_device_string != NULL && sscanf(user_device_string, " %u", &n) == 1 && n < n_devices) {
396
+ user_device_number = (int)n;
397
+ }
398
+ if (user_platform_number != -1 && user_device_number != -1) {
399
+ cl_platform* platform = &platforms[user_platform_number];
400
+ if ((unsigned)user_device_number >= platform->n_devices) {
401
+ fprintf(stderr, "ggml_opencl: invalid device number %d\n", user_device_number);
402
+ exit(1);
403
+ }
404
+ default_device = &platform->devices[user_device_number];
405
+ } else {
406
+
407
+ struct cl_device * selected_devices = devices;
408
+ unsigned n_selected_devices = n_devices;
409
+
410
+ if (user_platform_number == -1 && user_platform_string != NULL && user_platform_string[0] != 0) {
411
+ for (unsigned i = 0; i < n_platforms; i++) {
412
+ struct cl_platform * p = &platforms[i];
413
+ if (strstr(p->name, user_platform_string) != NULL ||
414
+ strstr(p->vendor, user_platform_string) != NULL) {
415
+ user_platform_number = (int)i;
416
+ break;
417
+ }
418
+ }
419
+ if (user_platform_number == -1) {
420
+ fprintf(stderr, "ggml_opencl: no platform matching '%s' was found.\n", user_platform_string);
421
+ exit(1);
422
+ }
423
+ }
424
+ if (user_platform_number != -1) {
425
+ struct cl_platform * p = &platforms[user_platform_number];
426
+ selected_devices = p->devices;
427
+ n_selected_devices = p->n_devices;
428
+ default_device = p->default_device;
429
+ if (n_selected_devices == 0) {
430
+ fprintf(stderr, "ggml_opencl: selected platform '%s' does not have any devices.\n", p->name);
431
+ exit(1);
432
+ }
433
+ }
434
+
435
+ if (user_device_number == -1 && user_device_string != NULL && user_device_string[0] != 0) {
436
+ for (unsigned i = 0; i < n_selected_devices; i++) {
437
+ struct cl_device * d = &selected_devices[i];
438
+ if (strstr(d->name, user_device_string) != NULL) {
439
+ user_device_number = d->number;
440
+ break;
441
+ }
442
+ }
443
+ if (user_device_number == -1) {
444
+ fprintf(stderr, "ggml_opencl: no device matching '%s' was found.\n", user_device_string);
445
+ exit(1);
446
+ }
447
+ }
448
+ if (user_device_number != -1) {
449
+ selected_devices = &devices[user_device_number];
450
+ n_selected_devices = 1;
451
+ default_device = &selected_devices[0];
452
+ }
453
+
454
+ GGML_ASSERT(n_selected_devices > 0);
455
+
456
+ if (default_device == NULL) {
457
+ default_device = &selected_devices[0];
458
+ }
459
+ }
460
+
461
+ fprintf(stderr, "ggml_opencl: selecting platform: '%s'\n", default_device->platform->name);
462
+ fprintf(stderr, "ggml_opencl: selecting device: '%s'\n", default_device->name);
463
+ if (default_device->type != CL_DEVICE_TYPE_GPU) {
464
+ fprintf(stderr, "ggml_opencl: warning, not a GPU: '%s'.\n", default_device->name);
465
+ }
466
+
467
+ platform = default_device->platform->id;
468
+ device = default_device->id;
469
+
470
+ size_t ext_str_size;
471
+ clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, 0, NULL, &ext_str_size);
472
+ char* ext_buffer = (char*) malloc(sizeof(char) * ext_str_size);
473
+ clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, ext_str_size, ext_buffer, NULL);
474
+ // Check if ext_buffer contains cl_khr_fp16
475
+ for (size_t i = 0; i < ext_str_size - 12; i++) {
476
+ if (memcmp(ext_buffer + i, "cl_khr_fp16", 11) == 0) {
477
+ fp16_support = true;
478
+ break;
479
+ }
480
+ }
481
+ free(ext_buffer);
482
+ fprintf(stderr, "ggml_opencl: device FP16 support: %s\n", fp16_support ? "true" : "false");
483
+
484
+ cl_context_properties properties[] = {
485
+ (intptr_t)CL_CONTEXT_PLATFORM, (intptr_t)platform, 0
486
+ };
487
+
488
+ CL_CHECK((context = clCreateContext(properties, 1, &device, NULL, NULL, &err), err));
489
+
490
+ CL_CHECK((queue = clCreateCommandQueue(context, device, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &err),
491
+ (err != CL_INVALID_QUEUE_PROPERTIES && err != CL_INVALID_VALUE ? err :
492
+ (queue = clCreateCommandQueue(context, device, 0, &err), err)
493
+ )));
494
+
495
+ const std::string kernel_src = generate_kernels();
496
+
497
+ program = build_program_from_source(context, device, kernel_src.c_str());
498
+
499
+ // FP16 to FP32 kernel
500
+ CL_CHECK((convert_row_f16_cl = clCreateKernel(program, "convert_row_f16", &err), err));
501
+
502
+ // Dequantize kernels
503
+ CL_CHECK((dequantize_row_q4_0_cl = clCreateKernel(program, "dequantize_row_q4_0", &err), err));
504
+ CL_CHECK((dequantize_row_q4_1_cl = clCreateKernel(program, "dequantize_row_q4_1", &err), err));
505
+ CL_CHECK((dequantize_row_q5_0_cl = clCreateKernel(program, "dequantize_row_q5_0", &err), err));
506
+ CL_CHECK((dequantize_row_q5_1_cl = clCreateKernel(program, "dequantize_row_q5_1", &err), err));
507
+ CL_CHECK((dequantize_row_q8_0_cl = clCreateKernel(program, "dequantize_row_q8_0", &err), err));
508
+
509
+ // dequant mul mat kernel
510
+ CL_CHECK((dequantize_mul_mat_vec_q4_0_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q4_0", &err), err));
511
+ CL_CHECK((dequantize_mul_mat_vec_q4_1_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q4_1", &err), err));
512
+ CL_CHECK((dequantize_mul_mat_vec_q5_0_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q5_0", &err), err));
513
+ CL_CHECK((dequantize_mul_mat_vec_q5_1_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q5_1", &err), err));
514
+ CL_CHECK((dequantize_mul_mat_vec_q8_0_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q8_0", &err), err));
515
+ CL_CHECK((convert_mul_mat_vec_f16_cl = clCreateKernel(program, "convert_mul_mat_vec_f16", &err), err));
516
+ }
517
+
518
+ static cl_kernel* ggml_get_to_fp32_cl(ggml_type type) {
519
+ switch (type) {
520
+ case GGML_TYPE_Q4_0:
521
+ return &dequantize_row_q4_0_cl;
522
+ case GGML_TYPE_Q4_1:
523
+ return &dequantize_row_q4_1_cl;
524
+ case GGML_TYPE_Q5_0:
525
+ return &dequantize_row_q5_0_cl;
526
+ case GGML_TYPE_Q5_1:
527
+ return &dequantize_row_q5_1_cl;
528
+ case GGML_TYPE_Q8_0:
529
+ return &dequantize_row_q8_0_cl;
530
+ case GGML_TYPE_F16:
531
+ return &convert_row_f16_cl;
532
+ default:
533
+ return nullptr;
534
+ }
535
+ }
536
+
537
+ static cl_kernel* ggml_get_dequantize_mul_mat_vec_cl(ggml_type type) {
538
+ switch (type) {
539
+ case GGML_TYPE_Q4_0:
540
+ return &dequantize_mul_mat_vec_q4_0_cl;
541
+ case GGML_TYPE_Q4_1:
542
+ return &dequantize_mul_mat_vec_q4_1_cl;
543
+ case GGML_TYPE_Q5_0:
544
+ return &dequantize_mul_mat_vec_q5_0_cl;
545
+ case GGML_TYPE_Q5_1:
546
+ return &dequantize_mul_mat_vec_q5_1_cl;
547
+ case GGML_TYPE_Q8_0:
548
+ return &dequantize_mul_mat_vec_q8_0_cl;
549
+ case GGML_TYPE_F16:
550
+ return &convert_mul_mat_vec_f16_cl;
551
+ default:
552
+ return nullptr;
553
+ }
554
+ }
555
+
556
+ // buffer pool for cl
557
+ #define MAX_CL_BUFFERS 256
558
+
559
+ struct scoped_spin_lock {
560
+ std::atomic_flag& lock;
561
+ scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
562
+ while (lock.test_and_set(std::memory_order_acquire)) {
563
+ ; // spin
564
+ }
565
+ }
566
+ ~scoped_spin_lock() {
567
+ lock.clear(std::memory_order_release);
568
+ }
569
+ scoped_spin_lock(const scoped_spin_lock&) = delete;
570
+ scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
571
+ };
572
+
573
+ struct cl_buffer {
574
+ cl_mem mem;
575
+ size_t size = 0;
576
+ };
577
+
578
+ static cl_buffer g_cl_buffer_pool[MAX_CL_BUFFERS];
579
+ static std::atomic_flag g_cl_pool_lock = ATOMIC_FLAG_INIT;
580
+
581
+ static cl_mem ggml_cl_pool_malloc(size_t size, size_t * actual_size, cl_mem_flags flags) {
582
+ scoped_spin_lock lock(g_cl_pool_lock);
583
+ cl_int err;
584
+
585
+ for (int i = 0; i < MAX_CL_BUFFERS; ++i) {
586
+ cl_buffer& b = g_cl_buffer_pool[i];
587
+ if (b.size > 0 && b.size >= size) {
588
+ cl_mem mem = b.mem;
589
+ *actual_size = b.size;
590
+ b.size = 0;
591
+ return mem;
592
+ }
593
+ }
594
+ cl_mem mem;
595
+ CL_CHECK((mem = clCreateBuffer(context, flags, size, NULL, &err), err));
596
+ *actual_size = size;
597
+ return mem;
598
+ }
599
+
600
+ static void ggml_cl_pool_free(cl_mem mem, size_t size) {
601
+ scoped_spin_lock lock(g_cl_pool_lock);
602
+
603
+ for (int i = 0; i < MAX_CL_BUFFERS; ++i) {
604
+ cl_buffer& b = g_cl_buffer_pool[i];
605
+ if (b.size == 0) {
606
+ b.mem = mem;
607
+ b.size = size;
608
+ return;
609
+ }
610
+ }
611
+ fprintf(stderr, "WARNING: cl buffer pool full, increase MAX_CL_BUFFERS\n");
612
+ clReleaseMemObject(mem);
613
+ }
614
+
615
+ static cl_int ggml_cl_h2d_tensor_2d(cl_command_queue queue, cl_mem dst, size_t offset, const struct ggml_tensor * src, uint64_t i3, uint64_t i2, cl_event* ev) {
616
+ cl_int err;
617
+ const uint64_t ne0 = src->ne[0];
618
+ const uint64_t ne1 = src->ne[1];
619
+ const uint64_t nb0 = src->nb[0];
620
+ const uint64_t nb1 = src->nb[1];
621
+ const uint64_t nb2 = src->nb[2];
622
+ const uint64_t nb3 = src->nb[3];
623
+ const enum ggml_type type = src->type;
624
+ const size_t ts = ggml_type_size(type);
625
+ const size_t bs = ggml_blck_size(type);
626
+
627
+ const void * x = (const void *) ((const char *) src->data + i2*nb2 + i3*nb3);
628
+ if (nb0 == ts && nb1 == ts*ne0/bs) {
629
+ err = clEnqueueWriteBuffer(queue, dst, CL_FALSE, offset, ne1*nb1, x, 0, NULL, ev);
630
+ return err;
631
+ }
632
+ if (nb0 == ts) {
633
+ const size_t buffer_origin[3] = { offset, 0, 0 };
634
+ const size_t host_origin[3] = { 0, 0, 0 };
635
+ const size_t region[3] = { ts*ne0/bs, ne1, 1 };
636
+ err = clEnqueueWriteBufferRect(queue, dst, CL_FALSE, buffer_origin, host_origin, region, ts*ne0/bs, 0, nb1, 0, x, 0, NULL, ev);
637
+ return err;
638
+ }
639
+ for (uint64_t i1 = 0; i1 < ne1; i1++) {
640
+ // pretend the row is a matrix with cols=1
641
+ const size_t buffer_origin[3] = { offset, i1, 0 };
642
+ const size_t host_origin[3] = { 0, 0, 0 };
643
+ const size_t region[3] = { ts/bs, ne0, 1 };
644
+ err = clEnqueueWriteBufferRect(queue, dst, CL_FALSE, buffer_origin, host_origin, region, 0, 0, nb0, 0, ((const char *)x) + i1*nb0, 0, NULL, ev);
645
+ if (err != CL_SUCCESS) {
646
+ break;
647
+ }
648
+ }
649
+ return err;
650
+ }
651
+
652
+ static void ggml_cl_mul_mat_f32(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
653
+ const int64_t ne00 = src0->ne[0];
654
+ const int64_t ne01 = src0->ne[1];
655
+ const int64_t ne02 = src0->ne[2];
656
+ const int64_t ne03 = src0->ne[3];
657
+
658
+ const int64_t ne10 = src1->ne[0];
659
+ const int64_t ne11 = src1->ne[1];
660
+
661
+ const int nb2 = dst->nb[2];
662
+ const int nb3 = dst->nb[3];
663
+
664
+ const float alpha = 1.0f;
665
+ const float beta = 0.0f;
666
+ const int x_ne = ne01 * ne00;
667
+ const int y_ne = ne11 * ne10;
668
+ const int d_ne = ne11 * ne01;
669
+
670
+ size_t x_size;
671
+ size_t y_size;
672
+ size_t d_size;
673
+ cl_mem d_X;
674
+ if (src0->backend == GGML_BACKEND_CL) {
675
+ d_X = *(cl_mem*) src0->data;
676
+ } else {
677
+ d_X = ggml_cl_pool_malloc(sizeof(ggml_fp16_t) * x_ne, &x_size, CL_MEM_READ_ONLY);
678
+ }
679
+ cl_mem d_Y = ggml_cl_pool_malloc(sizeof(float) * y_ne, &y_size, CL_MEM_READ_ONLY);
680
+ cl_mem d_D = ggml_cl_pool_malloc(sizeof(float) * d_ne, &d_size, CL_MEM_WRITE_ONLY);
681
+
682
+ for (int64_t i03 = 0; i03 < ne03; i03++) {
683
+ for (int64_t i02 = 0; i02 < ne02; i02++) {
684
+ // copy data to device
685
+ if (src0->backend != GGML_BACKEND_CL) {
686
+ CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_X, 0, src0, i03, i02, NULL));
687
+ }
688
+ CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_Y, 0, src1, i03, i02, NULL));
689
+
690
+ CL_CHECK(clFinish(queue));
691
+
692
+ // compute
693
+ cl_event ev_sgemm;
694
+ clblast::StatusCode status = clblast::Gemm<cl_float>(clblast::Layout::kColMajor,
695
+ clblast::Transpose::kYes, clblast::Transpose::kNo,
696
+ ne01, ne11, ne10,
697
+ alpha,
698
+ d_X, 0, ne00,
699
+ d_Y, 0, ne10,
700
+ beta,
701
+ d_D, 0, ne01,
702
+ &queue, &ev_sgemm);
703
+
704
+ if (status != clblast::StatusCode::kSuccess) {
705
+ GGML_ASSERT(false);
706
+ }
707
+
708
+ // copy dst to host
709
+ float * d = (float *) ((char *) dst->data + i02*nb2 + i03*nb3);
710
+ CL_CHECK(clEnqueueReadBuffer(queue, d_D, true, 0, sizeof(float) * d_ne, d, 1, &ev_sgemm, NULL));
711
+ }
712
+ }
713
+
714
+ if (src0->backend != GGML_BACKEND_CL) {
715
+ ggml_cl_pool_free(d_X, x_size);
716
+ }
717
+ ggml_cl_pool_free(d_Y, y_size);
718
+ ggml_cl_pool_free(d_D, d_size);
719
+ }
720
+
721
+ static void ggml_cl_mul_mat_f16(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, void * wdata, size_t /* wsize */) {
722
+ GGML_ASSERT(fp16_support);
723
+
724
+ const int64_t ne00 = src0->ne[0];
725
+ const int64_t ne01 = src0->ne[1];
726
+ const int64_t ne02 = src0->ne[2];
727
+ const int64_t ne03 = src0->ne[3];
728
+
729
+ const int64_t ne10 = src1->ne[0];
730
+ const int64_t ne11 = src1->ne[1];
731
+
732
+ const int nb10 = src1->nb[0];
733
+ const int nb11 = src1->nb[1];
734
+ const int nb12 = src1->nb[2];
735
+ const int nb13 = src1->nb[3];
736
+
737
+ const int nb2 = dst->nb[2];
738
+ const int nb3 = dst->nb[3];
739
+
740
+ const ggml_fp16_t alpha = ggml_fp32_to_fp16(1.0f);
741
+ const ggml_fp16_t beta = ggml_fp32_to_fp16(0.0f);
742
+ const int x_ne = ne01 * ne00;
743
+ const int y_ne = ne11 * ne10;
744
+ const int d_ne = ne11 * ne01;
745
+
746
+ size_t x_size;
747
+ size_t y_size;
748
+ size_t d_size;
749
+ cl_mem d_X;
750
+ if (src0->backend == GGML_BACKEND_CL) {
751
+ d_X = *(cl_mem*) src0->data;
752
+ } else {
753
+ d_X = ggml_cl_pool_malloc(sizeof(ggml_fp16_t) * x_ne, &x_size, CL_MEM_READ_ONLY);
754
+ }
755
+ cl_mem d_Y = ggml_cl_pool_malloc(sizeof(ggml_fp16_t) * y_ne, &y_size, CL_MEM_READ_ONLY);
756
+ cl_mem d_D = ggml_cl_pool_malloc(sizeof(ggml_fp16_t) * d_ne, &d_size, CL_MEM_WRITE_ONLY);
757
+
758
+ bool src1_cont_rows = nb10 == sizeof(float);
759
+ bool src1_cont_cols = (size_t)nb11 == ne11*sizeof(float);
760
+
761
+ for (int64_t i03 = 0; i03 < ne03; i03++) {
762
+ for (int64_t i02 = 0; i02 < ne02; i02++) {
763
+ // copy src0 to device
764
+ if (src0->backend != GGML_BACKEND_CL) {
765
+ CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_X, 0, src0, i03, i02, NULL));
766
+ }
767
+
768
+ // convert src1 to fp16
769
+ // TODO: use multiple threads
770
+ ggml_fp16_t * const tmp = (ggml_fp16_t *) wdata + (ne11 * ne10) * (i03 * ne02 + i02);
771
+ char * src1i = (char *) src1->data + i03*nb13 + i02*nb12;
772
+ if (src1_cont_rows) {
773
+ if (src1_cont_cols) {
774
+ ggml_fp32_to_fp16_row((float *) src1i, tmp, ne10*ne11);
775
+ }
776
+ else {
777
+ for (int64_t i01 = 0; i01 < ne11; i01++) {
778
+ ggml_fp32_to_fp16_row((float *) (src1i + i01*nb11), tmp + i01*ne10, ne10);
779
+ }
780
+ }
781
+ }
782
+ else {
783
+ for (int64_t i01 = 0; i01 < ne11; i01++) {
784
+ for (int64_t i00 = 0; i00 < ne10; i00++) {
785
+ // very slow due to no inlining
786
+ tmp[i01*ne10 + i00] = ggml_fp32_to_fp16(*(float *) (src1i + i01*nb11 + i00*nb10));
787
+ }
788
+ }
789
+ }
790
+
791
+ // copy src1 to device
792
+ CL_CHECK(clEnqueueWriteBuffer(queue, d_Y, false, 0, sizeof(ggml_fp16_t) * y_ne, tmp, 0, NULL, NULL));
793
+
794
+ CL_CHECK(clFinish(queue));
795
+
796
+ // compute
797
+ cl_event ev_sgemm;
798
+ clblast::StatusCode status = clblast::Gemm<cl_half>(clblast::Layout::kColMajor,
799
+ clblast::Transpose::kYes, clblast::Transpose::kNo,
800
+ ne01, ne11, ne10,
801
+ alpha,
802
+ d_X, 0, ne00,
803
+ d_Y, 0, ne10,
804
+ beta,
805
+ d_D, 0, ne01,
806
+ &queue, &ev_sgemm);
807
+
808
+ if (status != clblast::StatusCode::kSuccess) {
809
+ GGML_ASSERT(false);
810
+ }
811
+
812
+ // copy dst to host, then convert to float
813
+ CL_CHECK(clEnqueueReadBuffer(queue, d_D, true, 0, sizeof(ggml_fp16_t) * d_ne, tmp, 1, &ev_sgemm, NULL));
814
+
815
+ float * d = (float *) ((char *) dst->data + i02*nb2 + i03*nb3);
816
+
817
+ ggml_fp16_to_fp32_row(tmp, d, d_ne);
818
+ }
819
+ }
820
+
821
+ if (src0->backend != GGML_BACKEND_CL) {
822
+ ggml_cl_pool_free(d_X, x_size);
823
+ }
824
+ ggml_cl_pool_free(d_Y, y_size);
825
+ ggml_cl_pool_free(d_D, d_size);
826
+ }
827
+
828
+ static void ggml_cl_mul_mat_q_f32(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
829
+ const int64_t ne00 = src0->ne[0];
830
+ const int64_t ne01 = src0->ne[1];
831
+ const int64_t ne02 = src0->ne[2];
832
+ const int64_t ne03 = src0->ne[3];
833
+
834
+ const int64_t ne10 = src1->ne[0];
835
+ const int64_t ne11 = src1->ne[1];
836
+
837
+ const int nb2 = dst->nb[2];
838
+ const int nb3 = dst->nb[3];
839
+ const ggml_type type = src0->type;
840
+ const bool mul_mat_vec = ne11 == 1;
841
+
842
+ const float alpha = 1.0f;
843
+ const float beta = 0.0f;
844
+ const int x_ne = ne01 * ne00;
845
+ const int y_ne = ne11 * ne10;
846
+ const int d_ne = ne11 * ne01;
847
+ const size_t q_sz = ggml_type_size(type) * x_ne / ggml_blck_size(type);
848
+
849
+ size_t x_size;
850
+ size_t y_size;
851
+ size_t d_size;
852
+ size_t q_size;
853
+ cl_mem d_X;
854
+ if (!mul_mat_vec) {
855
+ d_X = ggml_cl_pool_malloc(sizeof(float) * x_ne, &x_size, CL_MEM_READ_WRITE);
856
+ }
857
+ cl_mem d_Y = ggml_cl_pool_malloc(sizeof(float) * y_ne, &y_size, CL_MEM_READ_ONLY);
858
+ cl_mem d_D = ggml_cl_pool_malloc(sizeof(float) * d_ne, &d_size, CL_MEM_WRITE_ONLY);
859
+ cl_mem d_Q;
860
+ if (src0->backend == GGML_BACKEND_CPU) {
861
+ d_Q = ggml_cl_pool_malloc(q_sz, &q_size, CL_MEM_READ_ONLY);
862
+ }
863
+
864
+ cl_kernel* to_fp32_cl = ggml_get_to_fp32_cl(type);
865
+ cl_kernel* dmmv = ggml_get_dequantize_mul_mat_vec_cl(type);
866
+ GGML_ASSERT(to_fp32_cl != nullptr);
867
+
868
+ for (int64_t i03 = 0; i03 < ne03; i03++) {
869
+ for (int64_t i02 = 0; i02 < ne02; i02++) {
870
+ cl_event ev_sgemm;
871
+
872
+ // copy src0 to device if necessary
873
+ if (src0->backend == GGML_BACKEND_CPU) {
874
+ CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_Q, 0, src0, i03, i02, NULL));
875
+ } else if (src0->backend == GGML_BACKEND_CL) {
876
+ d_Q = *(cl_mem*) src0->data;
877
+ } else {
878
+ GGML_ASSERT(false);
879
+ }
880
+ if (mul_mat_vec) { // specialized dequantize_mul_mat_vec kernel
881
+ // copy src1 to device
882
+ CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_Y, 0, src1, i03, i02, NULL));
883
+
884
+ // compute
885
+ const size_t global = ne01 * CL_DMMV_BLOCK_SIZE;
886
+ const size_t local = CL_DMMV_BLOCK_SIZE;
887
+ const cl_int ncols = ne00;
888
+ CL_CHECK(clSetKernelArg(*dmmv, 0, sizeof(cl_mem), &d_Q));
889
+ CL_CHECK(clSetKernelArg(*dmmv, 1, sizeof(float) * local, NULL));
890
+ CL_CHECK(clSetKernelArg(*dmmv, 2, sizeof(cl_mem), &d_Y));
891
+ CL_CHECK(clSetKernelArg(*dmmv, 3, sizeof(cl_mem), &d_D));
892
+ CL_CHECK(clSetKernelArg(*dmmv, 4, sizeof(cl_int), &ncols));
893
+ CL_CHECK(clFinish(queue));
894
+ CL_CHECK(clEnqueueNDRangeKernel(queue, *dmmv, 1, NULL, &global, &local, 0, NULL, &ev_sgemm));
895
+ } else { // general dequantization kernel + CLBlast matrix matrix multiplication
896
+ // convert src0 to fp32 on device
897
+ const size_t global = x_ne;
898
+ CL_CHECK(clSetKernelArg(*to_fp32_cl, 0, sizeof(cl_mem), &d_Q));
899
+ CL_CHECK(clSetKernelArg(*to_fp32_cl, 1, sizeof(cl_mem), &d_X));
900
+ CL_CHECK(clFinish(queue));
901
+ CL_CHECK(clEnqueueNDRangeKernel(queue, *to_fp32_cl, 1, NULL, &global, NULL, 0, NULL, NULL));
902
+
903
+ // copy src1 to device
904
+ CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_Y, 0, src1, i03, i02, NULL));
905
+
906
+ // wait for conversion
907
+ CL_CHECK(clFinish(queue));
908
+
909
+ // compute
910
+ clblast::StatusCode status = clblast::Gemm<cl_float>(clblast::Layout::kColMajor,
911
+ clblast::Transpose::kYes, clblast::Transpose::kNo,
912
+ ne01, ne11, ne10,
913
+ alpha,
914
+ d_X, 0, ne00,
915
+ d_Y, 0, ne10,
916
+ beta,
917
+ d_D, 0, ne01,
918
+ &queue, &ev_sgemm);
919
+
920
+ if (status != clblast::StatusCode::kSuccess) {
921
+ GGML_ASSERT(false);
922
+ }
923
+ }
924
+
925
+ // copy dst to host
926
+ float * d = (float *) ((char *) dst->data + i02*nb2 + i03*nb3);
927
+ CL_CHECK(clEnqueueReadBuffer(queue, d_D, true, 0, sizeof(float) * d_ne, d, 1, &ev_sgemm, NULL));
928
+ clReleaseEvent(ev_sgemm);
929
+ }
930
+ }
931
+
932
+ if (!mul_mat_vec) {
933
+ ggml_cl_pool_free(d_X, x_size);
934
+ }
935
+ ggml_cl_pool_free(d_Y, y_size);
936
+ ggml_cl_pool_free(d_D, d_size);
937
+ if (src0->backend == GGML_BACKEND_CPU) {
938
+ ggml_cl_pool_free(d_Q, q_size);
939
+ }
940
+ }
941
+
942
+
943
+ bool ggml_cl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
944
+ const int64_t ne10 = src1->ne[0];
945
+
946
+ const int64_t ne0 = dst->ne[0];
947
+ const int64_t ne1 = dst->ne[1];
948
+
949
+ // TODO: find the optimal values for these
950
+ if ((src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
951
+ src1->type == GGML_TYPE_F32 &&
952
+ dst->type == GGML_TYPE_F32 &&
953
+ ((ne0 >= 32 && ne1 >= 32 && ne10 >= 32) || src0->backend == GGML_BACKEND_CL)) {
954
+ return true;
955
+ }
956
+
957
+ return false;
958
+ }
959
+
960
+ bool ggml_cl_mul_mat_use_f16(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * /* dst */) {
961
+ // If device doesn't support FP16
962
+ if (!fp16_support) {
963
+ return false;
964
+ }
965
+
966
+ size_t src0_sz = ggml_nbytes(src0);
967
+ size_t src1_sz = ggml_nbytes(src1);
968
+
969
+ // mul_mat_q: src0 is converted to fp32 on device
970
+ size_t mul_mat_q_transfer = src0_sz + src1_sz;
971
+
972
+ // mul_mat_f16: src1 is converted to fp16 on cpu
973
+ size_t mul_mat_f16_transfer = src0_sz + sizeof(ggml_fp16_t) * ggml_nelements(src1);
974
+
975
+ // choose the smaller one to transfer to the device
976
+ // TODO: this is not always the best choice due to the overhead of converting to fp16
977
+ return mul_mat_f16_transfer < mul_mat_q_transfer;
978
+ }
979
+
980
+ void ggml_cl_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst, void * wdata, size_t wsize) {
981
+ GGML_ASSERT(ggml_cl_can_mul_mat(src0, src1, dst));
982
+
983
+ if (src0->type == GGML_TYPE_F32) {
984
+ ggml_cl_mul_mat_f32(src0, src1, dst);
985
+ }
986
+ else if (src0->type == GGML_TYPE_F16) {
987
+ if (ggml_cl_mul_mat_use_f16(src0, src1, dst)) {
988
+ ggml_cl_mul_mat_f16(src0, src1, dst, wdata, wsize);
989
+ }
990
+ else {
991
+ ggml_cl_mul_mat_q_f32(src0, src1, dst);
992
+ }
993
+ }
994
+ else if (ggml_is_quantized(src0->type)) {
995
+ ggml_cl_mul_mat_q_f32(src0, src1, dst);
996
+ }
997
+ else {
998
+ GGML_ASSERT(false);
999
+ }
1000
+ }
1001
+
1002
+ size_t ggml_cl_mul_mat_get_wsize(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
1003
+ if (ggml_cl_mul_mat_use_f16(src0, src1, dst)) {
1004
+ return ggml_nelements(src1) * sizeof(ggml_fp16_t);
1005
+ }
1006
+ return 0;
1007
+ }
1008
+
1009
+ void ggml_cl_transform_tensor(ggml_tensor * tensor) {
1010
+ const int64_t ne0 = tensor->ne[0];
1011
+ const int64_t ne1 = tensor->ne[1];
1012
+ const int64_t ne2 = tensor->ne[2];
1013
+ const int64_t ne3 = tensor->ne[3];
1014
+
1015
+ const ggml_type type = tensor->type;
1016
+ const size_t q_sz = ggml_type_size(type) * ne0 * ne1 * ne2 * ne3 / ggml_blck_size(type);
1017
+
1018
+ size_t q_size;
1019
+ cl_mem* dst = (cl_mem*) malloc(sizeof(cl_mem));
1020
+ *dst = ggml_cl_pool_malloc(q_sz, &q_size, CL_MEM_READ_ONLY);
1021
+
1022
+ // copy tensor to device
1023
+ for (int64_t i3 = 0; i3 < ne3; i3++) {
1024
+ for (int64_t i2 = 0; i2 < ne2; i2++) {
1025
+ int i = i3*ne2 + i2;
1026
+ CL_CHECK(ggml_cl_h2d_tensor_2d(queue, *dst, i*ne0*ne1, tensor, i3, i2, NULL));
1027
+ }
1028
+ }
1029
+
1030
+ CL_CHECK(clFinish(queue));
1031
+
1032
+ tensor->data = dst;
1033
+ tensor->backend = GGML_BACKEND_CL;
1034
+ }