@fugood/llama.node 1.2.0-rc.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/package.json +16 -15
  2. package/src/llama.cpp/CMakeLists.txt +7 -0
  3. package/src/llama.cpp/common/arg.cpp +141 -21
  4. package/src/llama.cpp/common/chat.cpp +139 -0
  5. package/src/llama.cpp/common/chat.h +1 -0
  6. package/src/llama.cpp/common/common.h +23 -8
  7. package/src/llama.cpp/common/json-schema-to-grammar.cpp +28 -7
  8. package/src/llama.cpp/ggml/CMakeLists.txt +0 -1
  9. package/src/llama.cpp/ggml/include/ggml-backend.h +12 -0
  10. package/src/llama.cpp/ggml/include/ggml-cpu.h +1 -0
  11. package/src/llama.cpp/ggml/include/ggml-metal.h +1 -6
  12. package/src/llama.cpp/ggml/include/ggml-zdnn.h +0 -2
  13. package/src/llama.cpp/ggml/include/ggml.h +10 -5
  14. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +7 -1
  15. package/src/llama.cpp/ggml/src/ggml-cpu/arch/s390/quants.c +57 -59
  16. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +6 -1
  17. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +14 -1
  18. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +1 -0
  19. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +0 -3
  20. package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +161 -1
  21. package/src/llama.cpp/src/llama-arch.cpp +44 -10
  22. package/src/llama.cpp/src/llama-arch.h +9 -0
  23. package/src/llama.cpp/src/llama-chat.cpp +17 -0
  24. package/src/llama.cpp/src/llama-chat.h +1 -0
  25. package/src/llama.cpp/src/llama-context.cpp +13 -11
  26. package/src/llama.cpp/src/llama-graph.cpp +6 -5
  27. package/src/llama.cpp/src/llama-hparams.h +14 -3
  28. package/src/llama.cpp/src/llama-kv-cache.cpp +55 -15
  29. package/src/llama.cpp/src/llama-kv-cache.h +8 -0
  30. package/src/llama.cpp/src/llama-model.cpp +386 -140
  31. package/src/llama.cpp/src/llama-model.h +3 -0
  32. package/src/llama.cpp/src/llama-quant.cpp +6 -4
  33. package/src/llama.cpp/src/llama-vocab.cpp +13 -1
  34. package/src/llama.cpp/src/llama-vocab.h +1 -0
  35. package/src/llama.cpp/src/llama.cpp +53 -10
@@ -132,6 +132,8 @@ extern "C" {
132
132
  GGML_BACKEND_DEVICE_TYPE_CPU,
133
133
  // GPU device using dedicated memory
134
134
  GGML_BACKEND_DEVICE_TYPE_GPU,
135
+ // integrated GPU device using host memory
136
+ GGML_BACKEND_DEVICE_TYPE_IGPU,
135
137
  // accelerator devices intended to be used together with the CPU backend (e.g. BLAS or AMX)
136
138
  GGML_BACKEND_DEVICE_TYPE_ACCEL
137
139
  };
@@ -150,11 +152,21 @@ extern "C" {
150
152
 
151
153
  // all the device properties
152
154
  struct ggml_backend_dev_props {
155
+ // device name
153
156
  const char * name;
157
+ // device description
154
158
  const char * description;
159
+ // device free memory in bytes
155
160
  size_t memory_free;
161
+ // device total memory in bytes
156
162
  size_t memory_total;
163
+ // device type
157
164
  enum ggml_backend_dev_type type;
165
+ // device id
166
+ // for PCI devices, this should be the PCI bus id formatted as "domain:bus:device.function" (e.g. "0000:01:00.0")
167
+ // if the id is unknown, this should be NULL
168
+ const char * device_id;
169
+ // device capabilities
158
170
  struct ggml_backend_dev_caps caps;
159
171
  };
160
172
 
@@ -134,6 +134,7 @@ extern "C" {
134
134
  GGML_BACKEND_API ggml_backend_reg_t ggml_backend_cpu_reg(void);
135
135
 
136
136
  GGML_BACKEND_API void ggml_cpu_fp32_to_fp32(const float *, float *, int64_t);
137
+ GGML_BACKEND_API void ggml_cpu_fp32_to_i32 (const float *, int32_t *, int64_t);
137
138
  GGML_BACKEND_API void ggml_cpu_fp32_to_fp16(const float *, ggml_fp16_t *, int64_t);
138
139
  GGML_BACKEND_API void ggml_cpu_fp16_to_fp32(const ggml_fp16_t *, float *, int64_t);
139
140
  GGML_BACKEND_API void ggml_cpu_fp32_to_bf16(const float *, ggml_bf16_t *, int64_t);
@@ -39,18 +39,13 @@ extern "C" {
39
39
  // user-code should use only these functions
40
40
  //
41
41
 
42
+ // TODO: remove in the future
42
43
  GGML_BACKEND_API ggml_backend_t ggml_backend_metal_init(void);
43
44
 
44
45
  GGML_BACKEND_API bool ggml_backend_is_metal(ggml_backend_t backend);
45
46
 
46
- GGML_DEPRECATED(
47
- GGML_BACKEND_API ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size),
48
- "obsoleted by the new device interface - https://github.com/ggml-org/llama.cpp/pull/9713");
49
-
50
47
  GGML_BACKEND_API void ggml_backend_metal_set_abort_callback(ggml_backend_t backend, ggml_abort_callback abort_callback, void * user_data);
51
48
 
52
- GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
53
-
54
49
  // helper to check if the device supports a specific family
55
50
  // ideally, the user code should be doing these checks
56
51
  // ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
@@ -7,8 +7,6 @@
7
7
  extern "C" {
8
8
  #endif
9
9
 
10
- GGML_BACKEND_API ggml_backend_t ggml_backend_zdnn_init(void);
11
-
12
10
  GGML_BACKEND_API ggml_backend_reg_t ggml_backend_zdnn_reg(void);
13
11
 
14
12
  #ifdef __cplusplus
@@ -284,19 +284,19 @@ __host__ __device__ constexpr inline void ggml_unused_vars_impl(Args&&...) noexc
284
284
  // GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
285
285
  //
286
286
  #define GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
287
- const type prefix##0 = (pointer)->array[0]; \
287
+ const type prefix##0 = (pointer) ? (pointer)->array[0] : 0; \
288
288
  GGML_UNUSED(prefix##0);
289
289
  #define GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
290
290
  GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
291
- const type prefix##1 = (pointer)->array[1]; \
291
+ const type prefix##1 = (pointer) ? (pointer)->array[1] : 0; \
292
292
  GGML_UNUSED(prefix##1);
293
293
  #define GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
294
294
  GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
295
- const type prefix##2 = (pointer)->array[2]; \
295
+ const type prefix##2 = (pointer) ? (pointer)->array[2] : 0; \
296
296
  GGML_UNUSED(prefix##2);
297
297
  #define GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
298
298
  GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
299
- const type prefix##3 = (pointer)->array[3]; \
299
+ const type prefix##3 = (pointer) ? (pointer)->array[3] : 0; \
300
300
  GGML_UNUSED(prefix##3);
301
301
 
302
302
  #define GGML_TENSOR_UNARY_OP_LOCALS \
@@ -1404,6 +1404,7 @@ extern "C" {
1404
1404
  struct ggml_tensor * a,
1405
1405
  struct ggml_tensor * b);
1406
1406
 
1407
+ // note: casting from f32 to i32 will discard the fractional part
1407
1408
  GGML_API struct ggml_tensor * ggml_cast(
1408
1409
  struct ggml_context * ctx,
1409
1410
  struct ggml_tensor * a,
@@ -1528,7 +1529,11 @@ extern "C" {
1528
1529
  struct ggml_context * ctx,
1529
1530
  struct ggml_tensor * a);
1530
1531
 
1531
- // supports 3D: a->ne[2] == b->ne[1]
1532
+ // supports 4D a:
1533
+ // a [n_embd, ne1, ne2, ne3]
1534
+ // b I32 [n_rows, ne2, ne3, 1]
1535
+ //
1536
+ // return [n_embd, n_rows, ne2, ne3]
1532
1537
  GGML_API struct ggml_tensor * ggml_get_rows(
1533
1538
  struct ggml_context * ctx,
1534
1539
  struct ggml_tensor * a, // data
@@ -224,7 +224,13 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
224
224
  foreach(feature DOTPROD SVE MATMUL_INT8 FMA FP16_VECTOR_ARITHMETIC SME)
225
225
  string(FIND "${ARM_FEATURE}" "__ARM_FEATURE_${feature} 1" feature_pos)
226
226
  if (NOT ${feature_pos} EQUAL -1)
227
- message(STATUS "ARM feature ${feature} enabled")
227
+ # Special handling for MATMUL_INT8 when machine doesn't support i8mm
228
+ if ("${feature}" STREQUAL "MATMUL_INT8" AND GGML_MACHINE_SUPPORTS_noi8mm)
229
+ message(STATUS "ARM feature ${feature} detected but unsetting due to machine not supporting i8mm")
230
+ list(APPEND ARCH_FLAGS -U__ARM_FEATURE_MATMUL_INT8)
231
+ else()
232
+ message(STATUS "ARM feature ${feature} enabled")
233
+ endif()
228
234
  endif()
229
235
  endforeach()
230
236
  endif()
@@ -53,9 +53,9 @@ void quantize_row_q8_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, i
53
53
 
54
54
  #if defined(__VXE__) || defined(__VXE2__)
55
55
  for (int i = 0; i < nb; i++) {
56
- __vector float srcv [8];
57
- __vector float asrcv[8];
58
- __vector float amaxv[8];
56
+ float32x4_t srcv [8];
57
+ float32x4_t asrcv[8];
58
+ float32x4_t amaxv[8];
59
59
 
60
60
  for (int j = 0; j < 8; j++) srcv[j] = vec_xl(0, x + i*32 + 4*j);
61
61
  for (int j = 0; j < 8; j++) asrcv[j] = vec_abs(srcv[j]);
@@ -74,8 +74,8 @@ void quantize_row_q8_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, i
74
74
  y[i].d = GGML_CPU_FP32_TO_FP16(d);
75
75
 
76
76
  for (int j = 0; j < 8; j++) {
77
- const __vector float v = vec_mul(srcv[j], vec_splats(id));
78
- const __vector int32_t vi = vec_signed(v);
77
+ const float32x4_t v = vec_mul(srcv[j], vec_splats(id));
78
+ const int32x4_t vi = vec_signed(v);
79
79
 
80
80
  y[i].qs[4*j + 0] = vec_extract(vi, 0);
81
81
  y[i].qs[4*j + 1] = vec_extract(vi, 1);
@@ -98,9 +98,9 @@ void quantize_row_q8_1(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, i
98
98
 
99
99
  #if defined(__VXE__) || defined(__VXE2__)
100
100
  for (int i = 0; i < nb; i++) {
101
- __vector float srcv [8];
102
- __vector float asrcv[8];
103
- __vector float amaxv[8];
101
+ float32x4_t srcv [8];
102
+ float32x4_t asrcv[8];
103
+ float32x4_t amaxv[8];
104
104
 
105
105
  for (int j = 0; j < 8; j++) srcv[j] = vec_xl(0, x + i*32 + 4*j);
106
106
  for (int j = 0; j < 8; j++) asrcv[j] = vec_abs(srcv[j]);
@@ -118,11 +118,11 @@ void quantize_row_q8_1(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, i
118
118
 
119
119
  y[i].d = GGML_CPU_FP32_TO_FP16(d);
120
120
 
121
- __vector int32_t acc = vec_splats(0);
121
+ int32x4_t acc = vec_splats(0);
122
122
 
123
123
  for (int j = 0; j < 8; j++) {
124
- const __vector float v = vec_mul(srcv[j], vec_splats(id));
125
- const __vector int32_t vi = vec_signed(v);
124
+ const float32x4_t v = vec_mul(srcv[j], vec_splats(id));
125
+ const int32x4_t vi = vec_signed(v);
126
126
 
127
127
  y[i].qs[4*j + 0] = vec_extract(vi, 0);
128
128
  y[i].qs[4*j + 1] = vec_extract(vi, 1);
@@ -162,37 +162,36 @@ void ggml_vec_dot_q4_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const voi
162
162
  float sumf = 0;
163
163
 
164
164
  #if defined(__VXE__) || defined(__VXE2__)
165
- __vector float acc = vec_splats(0.0f);
165
+ float32x4_t acc = vec_splats(0.0f);
166
166
 
167
- const __vector uint8_t v_m = vec_splats((const uint8_t)0x0F);
168
- const __vector int8_t v_s = vec_splats( (const int8_t)0x08);
167
+ const uint8x16_t v_m = vec_splats((const uint8_t)0x0F);
168
+ const int8x16_t v_s = vec_splats( (const int8_t)0x08);
169
169
 
170
170
  for (; ib < nb; ++ib) {
171
- const __vector uint8_t v_x = vec_xl(0, x[ib].qs);
172
- const __vector int8_t v_xl = (const __vector int8_t)(v_x & v_m);
173
- const __vector int8_t v_xh = (const __vector int8_t)(v_x >> 4);
171
+ const uint8x16_t v_x = vec_xl(0, x[ib].qs);
172
+ const int8x16_t v_xl = (const int8x16_t)(v_x & v_m);
173
+ const int8x16_t v_xh = (const int8x16_t)(v_x >> 4);
174
174
 
175
- const __vector int8_t v_xls = vec_sub(v_xl, v_s);
176
- const __vector int8_t v_xhs = vec_sub(v_xh, v_s);
175
+ const int8x16_t v_xls = vec_sub(v_xl, v_s);
176
+ const int8x16_t v_xhs = vec_sub(v_xh, v_s);
177
177
 
178
- const __vector int8_t v_yl = vec_xl(0 , y[ib].qs);
179
- const __vector int8_t v_yh = vec_xl(QK8_0/2, y[ib].qs);
178
+ const int8x16_t v_yl = vec_xl(0 , y[ib].qs);
179
+ const int8x16_t v_yh = vec_xl(QK8_0/2, y[ib].qs);
180
180
 
181
- const __vector int16_t v_xylso = vec_mulo(v_xls, v_yl);
182
- const __vector int16_t v_xylse = vec_mule(v_xls, v_yl);
183
- const __vector int16_t v_xyhso = vec_mulo(v_xhs, v_yh);
184
- const __vector int16_t v_xyhse = vec_mule(v_xhs, v_yh);
181
+ const int16x8_t v_xylso = vec_mulo(v_xls, v_yl);
182
+ const int16x8_t v_xylse = vec_mule(v_xls, v_yl);
183
+ const int16x8_t v_xyhso = vec_mulo(v_xhs, v_yh);
184
+ const int16x8_t v_xyhse = vec_mule(v_xhs, v_yh);
185
185
 
186
- __vector int16_t v_xy_ = v_xylso + v_xylse + v_xyhso + v_xyhse; v_xy_ += vec_reve(v_xy_);
186
+ int16x8_t v_xy_ = v_xylso + v_xylse + v_xyhso + v_xyhse; v_xy_ += vec_reve(v_xy_);
187
187
 
188
- const __vector float v_xy = vec_float(vec_unpackh(v_xy_));
189
- const __vector float v_d = vec_splats(GGML_CPU_FP16_TO_FP32(x[ib].d) * GGML_CPU_FP16_TO_FP32(y[ib].d));
188
+ const float32x4_t v_xy = vec_float(vec_unpackh(v_xy_));
189
+ const float32x4_t v_d = vec_splats(GGML_CPU_FP16_TO_FP32(x[ib].d) * GGML_CPU_FP16_TO_FP32(y[ib].d));
190
190
 
191
191
  acc = vec_madd(v_xy, v_d, acc);
192
192
  }
193
193
 
194
- sumf = acc[0] + acc[1] + acc[2] + acc[3];
195
-
194
+ sumf = vec_hsum_f32x4(acc);
196
195
  *s = sumf;
197
196
  #else
198
197
  UNUSED(nb);
@@ -249,8 +248,7 @@ void ggml_vec_dot_q4_1_q8_1(int n, float * GGML_RESTRICT s, size_t bs, const voi
249
248
  acc = vec_madd(v_xy, v_d, acc);
250
249
  }
251
250
 
252
- sumf = acc[0] + acc[1] + acc[2] + acc[3] + summs;
253
-
251
+ sumf = vec_hsum_f32x4(acc) + summs;
254
252
  *s = sumf;
255
253
  #else
256
254
  UNUSED(nb);
@@ -351,7 +349,7 @@ void ggml_vec_dot_q5_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const voi
351
349
  v_sum1 = vec_madd(v_xy1f, v_d1, v_sum1);
352
350
  }
353
351
 
354
- sumf += vec_hsum(v_sum0) + vec_hsum(v_sum1);
352
+ sumf += vec_hsum_f32x4(v_sum0) + vec_hsum_f32x4(v_sum1);
355
353
 
356
354
  #pragma GCC unroll 4
357
355
  for (; ib < nb; ++ib) {
@@ -390,7 +388,7 @@ void ggml_vec_dot_q5_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const voi
390
388
  const float32x4_t v_d = vec_splats(GGML_CPU_FP16_TO_FP32(x0->d) * GGML_CPU_FP16_TO_FP32(y0->d));
391
389
  const float32x4_t v_acc = vec_madd(v_xyf, v_d, vec_splats(0.0f));
392
390
 
393
- sumf += vec_hsum(v_acc);
391
+ sumf += vec_hsum_f32x4(v_acc);
394
392
  }
395
393
 
396
394
  *s = sumf;
@@ -502,7 +500,7 @@ void ggml_vec_dot_q5_1_q8_1(int n, float * GGML_RESTRICT s, size_t bs, const voi
502
500
  v_sum1 = vec_madd(v_xy1f, v_d1, v_sum1);
503
501
  }
504
502
 
505
- sumf += vec_hsum(v_sum0) + vec_hsum(v_sum1) + summs0 + summs1;
503
+ sumf += vec_hsum_f32x4(v_sum0) + vec_hsum_f32x4(v_sum1) + summs0 + summs1;
506
504
 
507
505
  #pragma GCC unroll 4
508
506
  for (; ib < nb; ++ib) {
@@ -543,7 +541,7 @@ void ggml_vec_dot_q5_1_q8_1(int n, float * GGML_RESTRICT s, size_t bs, const voi
543
541
  const float32x4_t v_d = vec_splats(GGML_CPU_FP16_TO_FP32(x0->d) * GGML_CPU_FP16_TO_FP32(y0->d));
544
542
  const float32x4_t v_acc = vec_madd(v_xyf, v_d, v_acc);
545
543
 
546
- sumf += vec_hsum(v_acc) + summs;
544
+ sumf += vec_hsum_f32x4(v_acc) + summs;
547
545
  }
548
546
 
549
547
  *s = sumf;
@@ -575,7 +573,7 @@ void ggml_vec_dot_q8_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const voi
575
573
  float sumf = 0;
576
574
 
577
575
  #if defined(__VXE__) || defined(__VXE2__)
578
- __vector float acc = vec_splats(0.0f);
576
+ float32x4_t acc = vec_splats(0.0f);
579
577
 
580
578
  #pragma GCC unroll 8
581
579
  for (; ib < nb; ++ib) {
@@ -594,7 +592,7 @@ void ggml_vec_dot_q8_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const voi
594
592
  acc = vec_madd(v_xy, v_d, acc);
595
593
  }
596
594
 
597
- sumf = acc[0] + acc[1] + acc[2] + acc[3];
595
+ sumf = vec_hsum_f32x4(acc);
598
596
 
599
597
  *s = sumf;
600
598
  #else
@@ -718,10 +716,10 @@ void ggml_vec_dot_q3_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const voi
718
716
  isum2 = ggml_vec_dot(v_z, q3bytes[2], q8bytes[6]);
719
717
  isum3 = ggml_vec_dot(v_z, q3bytes[3], q8bytes[7]);
720
718
 
721
- isum += (isum0[0] + isum0[1] + isum0[2] + isum0[3]) * scale[0];
722
- isum += (isum1[0] + isum1[1] + isum1[2] + isum1[3]) * scale[1];
723
- isum += (isum2[0] + isum2[1] + isum2[2] + isum2[3]) * scale[2];
724
- isum += (isum3[0] + isum3[1] + isum3[2] + isum3[3]) * scale[3];
719
+ isum += vec_hsum_i32x4(isum0) * scale[0];
720
+ isum += vec_hsum_i32x4(isum1) * scale[1];
721
+ isum += vec_hsum_i32x4(isum2) * scale[2];
722
+ isum += vec_hsum_i32x4(isum3) * scale[3];
725
723
 
726
724
  scale += 4;
727
725
 
@@ -819,7 +817,7 @@ void ggml_vec_dot_q4_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const voi
819
817
  v_xl[1] = (int8x16_t)vec_and(v_x[1], v_lm);
820
818
 
821
819
  const int32x4_t p1 = ggml_vec_dot(ggml_vec_dot(v_z, v_xl[0], v_y[0]), v_xl[1], v_y[1]);
822
- sumi1 += (p1[0] + p1[1] + p1[2] + p1[3]) * scales[2*j+0];
820
+ sumi1 += vec_hsum_i32x4(p1) * scales[2*j+0];
823
821
 
824
822
  v_y[0] = vec_xl(0 , y0);
825
823
  v_y[1] = vec_xl(16, y0);
@@ -829,7 +827,7 @@ void ggml_vec_dot_q4_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const voi
829
827
  v_xl[1] = (int8x16_t)vec_sr(v_x[1], 4);
830
828
 
831
829
  const int32x4_t p2 = ggml_vec_dot(ggml_vec_dot(v_z, v_xl[0], v_y[0]), v_xl[1], v_y[1]);
832
- sumi2 += (p2[0] + p2[1] + p2[2] + p2[3]) * scales[2*j+1];
830
+ sumi2 += vec_hsum_i32x4(p2) * scales[2*j+1];
833
831
  }
834
832
 
835
833
  sumf += d * (sumi1 + sumi2);
@@ -911,7 +909,7 @@ void ggml_vec_dot_q5_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const voi
911
909
  const int32x4_t v_minsho = vec_mulo(v_ysums, v_minsh);
912
910
  const int32x4_t v_minshe = vec_mule(v_ysums, v_minsh);
913
911
  const int32x4_t v_mins = vec_add(v_minsho, v_minshe);
914
- const int32_t mins = v_mins[0] + v_mins[1] + v_mins[2] + v_mins[3];
912
+ const int32_t mins = vec_hsum_i32x4(v_mins);
915
913
 
916
914
  const uint8_t * scales = (const uint8_t *)utmp;
917
915
  const uint8_t * GGML_RESTRICT x0l = x[i].qs;
@@ -948,8 +946,8 @@ void ggml_vec_dot_q5_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const voi
948
946
  int32x4_t sumi0 = ggml_vec_dot(ggml_vec_dot(v_z, q5b[0], v_y[0]), q5b[1], v_y[1]);
949
947
  int32x4_t sumi1 = ggml_vec_dot(ggml_vec_dot(v_z, q5b[2], v_y[2]), q5b[3], v_y[3]);
950
948
 
951
- sumi += (sumi0[0] + sumi0[1] + sumi0[2] + sumi0[3]) * *scales++;
952
- sumi += (sumi1[0] + sumi1[1] + sumi1[2] + sumi1[3]) * *scales++;
949
+ sumi += vec_hsum_i32x4(sumi0) * *scales++;
950
+ sumi += vec_hsum_i32x4(sumi1) * *scales++;
953
951
  }
954
952
 
955
953
  sumf += d * sumi - dmin * mins;
@@ -1020,7 +1018,7 @@ void ggml_vec_dot_q6_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const voi
1020
1018
  const int32x4_t v_minshe = vec_mule(v_ysumsh, v_scaleh);
1021
1019
  const int32x4_t v_mins = v_minslo + v_minsle + v_minsho + v_minshe;
1022
1020
 
1023
- const int32_t mins = v_mins[0] + v_mins[1] + v_mins[2] + v_mins[3];
1021
+ const int32_t mins = vec_hsum_i32x4(v_mins);
1024
1022
 
1025
1023
  int32_t isum = 0;
1026
1024
  for (int j = 0; j < QK_K/128; ++j) {
@@ -1060,10 +1058,10 @@ void ggml_vec_dot_q6_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const voi
1060
1058
  int32x4_t summs2 = ggml_vec_dot(v_z, q6b[2], v_y[2]);
1061
1059
  int32x4_t summs3 = ggml_vec_dot(v_z, q6b[3], v_y[3]);
1062
1060
 
1063
- isum += (summs0[0] + summs0[1] + summs0[2] + summs0[3]) * scale[0] +
1064
- (summs1[0] + summs1[1] + summs1[2] + summs1[3]) * scale[1] +
1065
- (summs2[0] + summs2[1] + summs2[2] + summs2[3]) * scale[2] +
1066
- (summs3[0] + summs3[1] + summs3[2] + summs3[3]) * scale[3];
1061
+ isum += vec_hsum_i32x4(summs0) * scale[0] +
1062
+ vec_hsum_i32x4(summs1) * scale[1] +
1063
+ vec_hsum_i32x4(summs2) * scale[2] +
1064
+ vec_hsum_i32x4(summs3) * scale[3];
1067
1065
 
1068
1066
  scale += 4;
1069
1067
 
@@ -1094,10 +1092,10 @@ void ggml_vec_dot_q6_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const voi
1094
1092
  summs2 = ggml_vec_dot(v_z, q6b[2], v_y[2]);
1095
1093
  summs3 = ggml_vec_dot(v_z, q6b[3], v_y[3]);
1096
1094
 
1097
- isum += (summs0[0] + summs0[1] + summs0[2] + summs0[3]) * scale[0] +
1098
- (summs1[0] + summs1[1] + summs1[2] + summs1[3]) * scale[1] +
1099
- (summs2[0] + summs2[1] + summs2[2] + summs2[3]) * scale[2] +
1100
- (summs3[0] + summs3[1] + summs3[2] + summs3[3]) * scale[3];
1095
+ isum += vec_hsum_i32x4(summs0) * scale[0] +
1096
+ vec_hsum_i32x4(summs1) * scale[1] +
1097
+ vec_hsum_i32x4(summs2) * scale[2] +
1098
+ vec_hsum_i32x4(summs3) * scale[3];
1101
1099
 
1102
1100
  scale += 4;
1103
1101
  }
@@ -1285,7 +1283,7 @@ void ggml_vec_dot_iq4_nl_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const v
1285
1283
  const int8x16_t v_yh = vec_xl(QK8_0/2, y0->qs);
1286
1284
  const int32x4_t v_xy = ggml_vec_dot(ggml_vec_dot(vec_splats(0), v_xl, v_yl), v_xh, v_yh);
1287
1285
 
1288
- sumf += GGML_CPU_FP16_TO_FP32(x0->d) * GGML_CPU_FP16_TO_FP32(y0->d) * (v_xy[0] + v_xy[1] + v_xy[2] + v_xy[3]);
1286
+ sumf += GGML_CPU_FP16_TO_FP32(x0->d) * GGML_CPU_FP16_TO_FP32(y0->d) * vec_hsum_i32x4(v_xy);
1289
1287
  }
1290
1288
 
1291
1289
  *s = sumf;
@@ -1354,8 +1352,8 @@ void ggml_vec_dot_iq4_xs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const v
1354
1352
 
1355
1353
  h >>= 4;
1356
1354
 
1357
- sumi1 += (vsumi0[0] + vsumi0[1] + vsumi0[2] + vsumi0[3]) * ls1;
1358
- sumi2 += (vsumi1[0] + vsumi1[1] + vsumi1[2] + vsumi1[3]) * ls2;
1355
+ sumi1 += vec_hsum_i32x4(vsumi0) * ls1;
1356
+ sumi2 += vec_hsum_i32x4(vsumi1) * ls2;
1359
1357
  }
1360
1358
 
1361
1359
  sumf += GGML_CPU_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
@@ -483,11 +483,16 @@ inline static int16x8_t vec_padd_s16(int16x8_t a, int16x8_t b) {
483
483
  /**
484
484
  * @see https://github.com/ggml-org/llama.cpp/pull/14037
485
485
  */
486
- inline static float vec_hsum(float32x4_t v) {
486
+ inline static float vec_hsum_f32x4(float32x4_t v) {
487
487
  float32x4_t v_temp = v + vec_reve(v);
488
488
  return v_temp[0] + v_temp[1];
489
489
  }
490
490
 
491
+ inline static int32_t vec_hsum_i32x4(int32x4_t v) {
492
+ int32x4_t v_temp = v + vec_reve(v);
493
+ return v_temp[0] + v_temp[1];
494
+ }
495
+
491
496
  inline static int32x4_t ggml_vec_dot(int32x4_t acc, int8x16_t a, int8x16_t b) {
492
497
  const int16x8_t p = vec_mule(a, b) + vec_mulo(a, b);
493
498
  return acc + (vec_unpackh(p) + vec_unpackl(p));
@@ -373,6 +373,9 @@ static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = {
373
373
  .vec_dot_type = GGML_TYPE_Q8_K,
374
374
  .nrows = 1,
375
375
  },
376
+ [GGML_TYPE_I32] = {
377
+ .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_i32,
378
+ },
376
379
  };
377
380
 
378
381
  const struct ggml_type_traits_cpu * ggml_get_type_traits_cpu(enum ggml_type type) {
@@ -2696,7 +2699,10 @@ struct ggml_cplan ggml_graph_plan(
2696
2699
  if (ggml_is_quantized(node->type) ||
2697
2700
  // F16 -> BF16 and BF16 -> F16 copies go through intermediate F32
2698
2701
  (node->src[0]->type == GGML_TYPE_F16 && node->src[1] && node->src[1]->type == GGML_TYPE_BF16) ||
2699
- (node->src[0]->type == GGML_TYPE_BF16 && node->src[1] && node->src[1]->type == GGML_TYPE_F16)) {
2702
+ (node->src[0]->type == GGML_TYPE_BF16 && node->src[1] && node->src[1]->type == GGML_TYPE_F16) ||
2703
+ // conversion between F32 and I32
2704
+ (node->src[0]->type == GGML_TYPE_F32 && node->src[1] && node->src[1]->type == GGML_TYPE_I32) ||
2705
+ (node->src[0]->type == GGML_TYPE_I32 && node->src[1] && node->src[1]->type == GGML_TYPE_F32)) {
2700
2706
  cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
2701
2707
  }
2702
2708
  } break;
@@ -3258,6 +3264,13 @@ void ggml_cpu_fp32_to_bf16(const float * x, ggml_bf16_t * y, int64_t n) {
3258
3264
  }
3259
3265
  }
3260
3266
 
3267
+ void ggml_cpu_fp32_to_i32(const float * x, int32_t * y, int64_t n) {
3268
+ int64_t i = 0;
3269
+ for (; i < n; ++i) {
3270
+ y[i] = x[i];
3271
+ }
3272
+ }
3273
+
3261
3274
  void ggml_cpu_bf16_to_fp32(const ggml_bf16_t * x, float * y, int64_t n) {
3262
3275
  int64_t i = 0;
3263
3276
  #if defined(__AVX2__)
@@ -190,6 +190,7 @@ static const struct ggml_backend_i ggml_backend_cpu_i = {
190
190
  /* .graph_compute = */ ggml_backend_cpu_graph_compute,
191
191
  /* .event_record = */ NULL,
192
192
  /* .event_wait = */ NULL,
193
+ /* .optimize_graph = */ NULL,
193
194
  };
194
195
 
195
196
  static ggml_guid_t ggml_backend_cpu_guid(void) {
@@ -515,9 +515,6 @@ class extra_buffer_type : ggml::cpu::extra_buffer_type {
515
515
  op->src[0]->buffer &&
516
516
  (ggml_n_dims(op->src[0]) == 2) &&
517
517
  op->src[0]->buffer->buft == ggml_backend_cpu_kleidiai_buffer_type() && ctx.kernels) {
518
- if (op->op == GGML_OP_GET_ROWS && op->src[1]->ne[0] != 8) {
519
- return false;
520
- }
521
518
  if (op->src[1]->buffer && !ggml_backend_buft_is_host(op->src[1]->buffer->buft)) {
522
519
  return false;
523
520
  }