@fugood/llama.node 1.1.5 → 1.1.7

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 (64) hide show
  1. package/lib/binding.ts +4 -0
  2. package/lib/index.js +6 -1
  3. package/lib/index.ts +6 -0
  4. package/lib/version.js +5 -0
  5. package/lib/version.ts +2 -0
  6. package/package.json +14 -14
  7. package/scripts/llama.cpp.patch +19 -15
  8. package/src/LlamaCompletionWorker.cpp +73 -18
  9. package/src/LlamaCompletionWorker.h +8 -0
  10. package/src/llama.cpp/CMakeLists.txt +2 -0
  11. package/src/llama.cpp/common/arg.cpp +147 -46
  12. package/src/llama.cpp/common/chat-parser.cpp +9 -1
  13. package/src/llama.cpp/common/chat.cpp +350 -3
  14. package/src/llama.cpp/common/chat.h +11 -3
  15. package/src/llama.cpp/common/common.cpp +54 -0
  16. package/src/llama.cpp/common/common.h +44 -9
  17. package/src/llama.cpp/ggml/CMakeLists.txt +5 -2
  18. package/src/llama.cpp/ggml/include/ggml-opt.h +25 -6
  19. package/src/llama.cpp/ggml/include/ggml-zdnn.h +16 -0
  20. package/src/llama.cpp/ggml/include/ggml.h +65 -3
  21. package/src/llama.cpp/ggml/src/CMakeLists.txt +13 -1
  22. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +1 -1
  23. package/src/llama.cpp/ggml/src/ggml-cpu/arch/arm/quants.c +61 -0
  24. package/src/llama.cpp/ggml/src/ggml-cpu/arch/x86/quants.c +96 -8
  25. package/src/llama.cpp/ggml/src/ggml-cpu/arch/x86/repack.cpp +1136 -1077
  26. package/src/llama.cpp/ggml/src/ggml-cpu/arch-fallback.h +20 -0
  27. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +20 -1
  28. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +21 -24
  29. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +16 -7
  30. package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +270 -11
  31. package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +3 -8
  32. package/src/llama.cpp/ggml/src/ggml-cpu/quants.c +35 -0
  33. package/src/llama.cpp/ggml/src/ggml-cpu/quants.h +8 -0
  34. package/src/llama.cpp/ggml/src/ggml-cpu/repack.cpp +200 -51
  35. package/src/llama.cpp/ggml/src/ggml-cpu/repack.h +11 -0
  36. package/src/llama.cpp/ggml/src/ggml-cpu/traits.cpp +2 -2
  37. package/src/llama.cpp/ggml/src/ggml-cpu/traits.h +1 -1
  38. package/src/llama.cpp/ggml/src/ggml-cpu/vec.h +19 -4
  39. package/src/llama.cpp/include/llama.h +26 -0
  40. package/src/llama.cpp/src/llama-arch.cpp +65 -0
  41. package/src/llama.cpp/src/llama-arch.h +10 -0
  42. package/src/llama.cpp/src/llama-batch.cpp +1 -1
  43. package/src/llama.cpp/src/llama-chat.cpp +15 -4
  44. package/src/llama.cpp/src/llama-chat.h +1 -0
  45. package/src/llama.cpp/src/llama-context.cpp +37 -25
  46. package/src/llama.cpp/src/llama-context.h +6 -5
  47. package/src/llama.cpp/src/llama-graph.cpp +118 -9
  48. package/src/llama.cpp/src/llama-graph.h +38 -0
  49. package/src/llama.cpp/src/llama-hparams.h +5 -3
  50. package/src/llama.cpp/src/llama-kv-cache-unified-iswa.cpp +12 -6
  51. package/src/llama.cpp/src/llama-kv-cache-unified-iswa.h +2 -2
  52. package/src/llama.cpp/src/llama-kv-cache-unified.cpp +93 -69
  53. package/src/llama.cpp/src/llama-kv-cache-unified.h +2 -2
  54. package/src/llama.cpp/src/llama-memory-hybrid.cpp +6 -2
  55. package/src/llama.cpp/src/llama-memory-hybrid.h +2 -2
  56. package/src/llama.cpp/src/llama-memory-recurrent.cpp +6 -2
  57. package/src/llama.cpp/src/llama-memory-recurrent.h +2 -2
  58. package/src/llama.cpp/src/llama-memory.h +2 -2
  59. package/src/llama.cpp/src/llama-model-loader.cpp +1 -0
  60. package/src/llama.cpp/src/llama-model-loader.h +3 -2
  61. package/src/llama.cpp/src/llama-model.cpp +500 -4
  62. package/src/llama.cpp/src/llama-model.h +25 -4
  63. package/src/llama.cpp/src/llama-quant.cpp +37 -1
  64. package/src/llama.cpp/src/llama-vocab.cpp +43 -0
@@ -8,6 +8,7 @@
8
8
  #include "vec.h"
9
9
 
10
10
  #include <float.h>
11
+ #include <algorithm>
11
12
 
12
13
  // ggml_compute_forward_dup
13
14
 
@@ -1283,6 +1284,7 @@ void ggml_compute_forward_add(
1283
1284
  case GGML_TYPE_Q5_0:
1284
1285
  case GGML_TYPE_Q5_1:
1285
1286
  case GGML_TYPE_Q8_0:
1287
+ case GGML_TYPE_MXFP4:
1286
1288
  case GGML_TYPE_Q2_K:
1287
1289
  case GGML_TYPE_Q3_K:
1288
1290
  case GGML_TYPE_Q4_K:
@@ -1309,6 +1311,77 @@ void ggml_compute_forward_add(
1309
1311
  }
1310
1312
  }
1311
1313
 
1314
+ // ggml_compute_forward_add_id
1315
+
1316
+ static void ggml_compute_forward_add_id_f32(
1317
+ const ggml_compute_params * params,
1318
+ ggml_tensor * dst) {
1319
+
1320
+ const ggml_tensor * src0 = dst->src[0];
1321
+ const ggml_tensor * src1 = dst->src[1];
1322
+ const ggml_tensor * src2 = dst->src[2];
1323
+
1324
+ GGML_ASSERT(dst->type == GGML_TYPE_F32);
1325
+ GGML_ASSERT(src0->type == GGML_TYPE_F32);
1326
+ GGML_ASSERT(src1->type == GGML_TYPE_F32);
1327
+ GGML_ASSERT(src2->type == GGML_TYPE_I32);
1328
+
1329
+ GGML_ASSERT(src0->nb[0] == sizeof(float));
1330
+ GGML_ASSERT(src1->nb[0] == sizeof(float));
1331
+
1332
+ const int ith = params->ith;
1333
+ const int nth = params->nth;
1334
+
1335
+ const int nr = ggml_nrows(src0);
1336
+
1337
+ GGML_TENSOR_TERNARY_OP_LOCALS
1338
+
1339
+ GGML_ASSERT( nb0 == sizeof(float));
1340
+ GGML_ASSERT(nb10 == sizeof(float));
1341
+
1342
+ // rows per thread
1343
+ const int dr = (nr + nth - 1)/nth;
1344
+
1345
+ // row range for this thread
1346
+ const int ir0 = dr*ith;
1347
+ const int ir1 = MIN(ir0 + dr, nr);
1348
+
1349
+ for (int ir = ir0; ir < ir1; ++ir) {
1350
+ // src0 indices
1351
+ const int i3 = ir/(ne2*ne1);
1352
+ const int i2 = (ir - i3*ne2*ne1)/ne1;
1353
+ const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
1354
+
1355
+ // src1 indices
1356
+ const int i11 = *(int32_t *) ((char *) src2->data + i1*nb20 + i2*nb21);
1357
+
1358
+ GGML_ASSERT(i11 >= 0 && i11 < ne11);
1359
+
1360
+ ggml_vec_add_f32(ne0,
1361
+ (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ),
1362
+ (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01),
1363
+ (float *) ((char *) src1->data + i11*nb11));
1364
+ }
1365
+ }
1366
+
1367
+ void ggml_compute_forward_add_id(
1368
+ const ggml_compute_params * params,
1369
+ ggml_tensor * dst) {
1370
+
1371
+ const ggml_tensor * src0 = dst->src[0];
1372
+
1373
+ switch (src0->type) {
1374
+ case GGML_TYPE_F32:
1375
+ {
1376
+ ggml_compute_forward_add_id_f32(params, dst);
1377
+ } break;
1378
+ default:
1379
+ {
1380
+ GGML_ABORT("unsupported type for ggml_compute_forward_add_id: %s", ggml_type_name(src0->type));
1381
+ }
1382
+ }
1383
+ }
1384
+
1312
1385
  // ggml_compute_forward_add1
1313
1386
 
1314
1387
  static void ggml_compute_forward_add1_f32(
@@ -1660,6 +1733,7 @@ void ggml_compute_forward_add1(
1660
1733
  case GGML_TYPE_Q5_1:
1661
1734
  case GGML_TYPE_Q8_0:
1662
1735
  case GGML_TYPE_Q8_1:
1736
+ case GGML_TYPE_MXFP4:
1663
1737
  case GGML_TYPE_Q2_K:
1664
1738
  case GGML_TYPE_Q3_K:
1665
1739
  case GGML_TYPE_Q4_K:
@@ -1787,6 +1861,7 @@ void ggml_compute_forward_acc(
1787
1861
  case GGML_TYPE_Q5_1:
1788
1862
  case GGML_TYPE_Q8_0:
1789
1863
  case GGML_TYPE_Q8_1:
1864
+ case GGML_TYPE_MXFP4:
1790
1865
  case GGML_TYPE_Q2_K:
1791
1866
  case GGML_TYPE_Q3_K:
1792
1867
  case GGML_TYPE_Q4_K:
@@ -3614,6 +3689,93 @@ static void ggml_compute_forward_swiglu(
3614
3689
  }
3615
3690
  }
3616
3691
 
3692
+ // ggml_compute_forward_swiglu_oai
3693
+
3694
+ static void ggml_compute_forward_swiglu_oai_f32(
3695
+ const ggml_compute_params * params,
3696
+ ggml_tensor * dst) {
3697
+
3698
+ const ggml_tensor * src0 = dst->src[0];
3699
+ const ggml_tensor * src1 = dst->src[1];
3700
+ char * src0_d = (char *) src0->data;
3701
+ char * src1_d = (char *) (src1 ? src1->data : src0->data);
3702
+ const size_t src0_o = src0->nb[1];
3703
+ const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1];
3704
+
3705
+ GGML_ASSERT(ggml_is_contiguous_1(src0));
3706
+ GGML_ASSERT(ggml_is_contiguous_1(dst));
3707
+
3708
+ if (src1) {
3709
+ GGML_ASSERT(ggml_is_contiguous_1(src1));
3710
+ GGML_ASSERT(src0->type == src1->type);
3711
+ }
3712
+
3713
+ const int ith = params->ith;
3714
+ const int nth = params->nth;
3715
+
3716
+ const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2;
3717
+ const int nr = ggml_nrows(src0);
3718
+
3719
+ GGML_ASSERT(dst->ne[0] == nc);
3720
+ GGML_ASSERT(ggml_nrows(dst) == nr);
3721
+
3722
+ const int32_t swapped = ggml_get_op_params_i32(dst, 1);
3723
+ const float alpha = ggml_get_op_params_f32(dst, 2);
3724
+ const float limit = ggml_get_op_params_f32(dst, 3);
3725
+
3726
+ // rows per thread
3727
+ const int dr = (nr + nth - 1)/nth;
3728
+
3729
+ // row range for this thread
3730
+ const int ir0 = dr*ith;
3731
+ const int ir1 = MIN(ir0 + dr, nr);
3732
+
3733
+ for (int i1 = ir0; i1 < ir1; i1++) {
3734
+ float * src0_p = (float *) (src0_d + i1*src0_o);
3735
+ float * src1_p = (float *) (src1_d + i1*src1_o);
3736
+ float * dst_p = (float *) ((char *) dst->data + i1*(dst->nb[1]));
3737
+
3738
+ if (!src1) {
3739
+ src0_p += swapped ? nc : 0;
3740
+ src1_p += swapped ? 0 : nc;
3741
+ }
3742
+
3743
+ for (int k = 0; k < nc; k++) {
3744
+ const float x = std::min(src0_p[k], limit);
3745
+ const float y = std::clamp(src1_p[k], -limit, limit);
3746
+ const float out_glu = x / (1.f + expf(alpha * (-x)));
3747
+ dst_p[k] = out_glu * (y + 1.f);
3748
+ }
3749
+
3750
+ #ifndef NDEBUG
3751
+ for (int k = 0; k < nc; k++) {
3752
+ const float x = dst_p[k];
3753
+ GGML_UNUSED(x);
3754
+ assert(!isnan(x));
3755
+ assert(!isinf(x));
3756
+ }
3757
+ #endif
3758
+ }
3759
+ }
3760
+
3761
+ static void ggml_compute_forward_swiglu_oai(
3762
+ const ggml_compute_params * params,
3763
+ ggml_tensor * dst) {
3764
+
3765
+ const ggml_tensor * src0 = dst->src[0];
3766
+
3767
+ switch (src0->type) {
3768
+ case GGML_TYPE_F32:
3769
+ {
3770
+ ggml_compute_forward_swiglu_oai_f32(params, dst);
3771
+ } break;
3772
+ default:
3773
+ {
3774
+ GGML_ABORT("fatal error");
3775
+ }
3776
+ }
3777
+ }
3778
+
3617
3779
  // ggml_compute_forward_geglu_erf
3618
3780
 
3619
3781
  static void ggml_compute_forward_geglu_erf_f32(
@@ -4599,6 +4761,7 @@ void ggml_compute_forward_out_prod(
4599
4761
  case GGML_TYPE_Q5_0:
4600
4762
  case GGML_TYPE_Q5_1:
4601
4763
  case GGML_TYPE_Q8_0:
4764
+ case GGML_TYPE_MXFP4:
4602
4765
  case GGML_TYPE_Q2_K:
4603
4766
  case GGML_TYPE_Q3_K:
4604
4767
  case GGML_TYPE_Q4_K:
@@ -4873,6 +5036,7 @@ void ggml_compute_forward_set(
4873
5036
  case GGML_TYPE_Q5_1:
4874
5037
  case GGML_TYPE_Q8_0:
4875
5038
  case GGML_TYPE_Q8_1:
5039
+ case GGML_TYPE_MXFP4:
4876
5040
  case GGML_TYPE_Q2_K:
4877
5041
  case GGML_TYPE_Q3_K:
4878
5042
  case GGML_TYPE_Q4_K:
@@ -5134,6 +5298,7 @@ void ggml_compute_forward_get_rows(
5134
5298
  case GGML_TYPE_Q5_1:
5135
5299
  case GGML_TYPE_Q8_0:
5136
5300
  case GGML_TYPE_Q8_1:
5301
+ case GGML_TYPE_MXFP4:
5137
5302
  case GGML_TYPE_Q2_K:
5138
5303
  case GGML_TYPE_Q3_K:
5139
5304
  case GGML_TYPE_Q4_K:
@@ -5523,6 +5688,7 @@ static void ggml_compute_forward_soft_max_f32(
5523
5688
 
5524
5689
  const ggml_tensor * src0 = dst->src[0];
5525
5690
  const ggml_tensor * src1 = dst->src[1];
5691
+ const ggml_tensor * src2 = dst->src[2];
5526
5692
 
5527
5693
  assert(ggml_is_contiguous(dst));
5528
5694
  assert(ggml_are_same_shape(src0, dst));
@@ -5557,6 +5723,9 @@ static void ggml_compute_forward_soft_max_f32(
5557
5723
 
5558
5724
  const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16);
5559
5725
 
5726
+ // sinks
5727
+ const float * sk = src2 ? (float *)((char *) src2->data) : nullptr;
5728
+
5560
5729
  for (int64_t i03 = 0; i03 < ne03; i03++) {
5561
5730
  for (int64_t i02 = 0; i02 < ne02; i02++) {
5562
5731
  for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
@@ -5599,9 +5768,18 @@ static void ggml_compute_forward_soft_max_f32(
5599
5768
  float max = -INFINITY;
5600
5769
  ggml_vec_max_f32(ne00, &max, wp);
5601
5770
 
5771
+ // if we have sinks, make a correction as if they were included in the softmax
5772
+ if (sk) {
5773
+ max = MAX(max, sk[i02]);
5774
+ }
5775
+
5602
5776
  ggml_float sum = ggml_vec_soft_max_f32(ne00, dp, wp, max);
5603
5777
  assert(sum > 0.0);
5604
5778
 
5779
+ if (sk) {
5780
+ sum += (ggml_float) expf(sk[i02] - max);
5781
+ }
5782
+
5605
5783
  sum = 1.0/sum;
5606
5784
  ggml_vec_scale_f32(ne00, dp, sum);
5607
5785
 
@@ -5836,6 +6014,7 @@ void ggml_compute_forward_clamp(
5836
6014
  case GGML_TYPE_Q5_1:
5837
6015
  case GGML_TYPE_Q8_0:
5838
6016
  case GGML_TYPE_Q8_1:
6017
+ case GGML_TYPE_MXFP4:
5839
6018
  case GGML_TYPE_Q2_K:
5840
6019
  case GGML_TYPE_Q3_K:
5841
6020
  case GGML_TYPE_Q4_K:
@@ -7989,12 +8168,14 @@ void ggml_compute_forward_argsort(
7989
8168
 
7990
8169
  static void ggml_compute_forward_flash_attn_ext_f16(
7991
8170
  const ggml_compute_params * params,
7992
- const ggml_tensor * q,
7993
- const ggml_tensor * k,
7994
- const ggml_tensor * v,
7995
- const ggml_tensor * mask,
7996
8171
  ggml_tensor * dst) {
7997
8172
 
8173
+ const ggml_tensor * q = dst->src[0];
8174
+ const ggml_tensor * k = dst->src[1];
8175
+ const ggml_tensor * v = dst->src[2];
8176
+ const ggml_tensor * mask = dst->src[3];
8177
+ const ggml_tensor * sinks = dst->src[4];
8178
+
7998
8179
  GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
7999
8180
  GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
8000
8181
  GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
@@ -8189,6 +8370,23 @@ static void ggml_compute_forward_flash_attn_ext_f16(
8189
8370
  }
8190
8371
  }
8191
8372
 
8373
+ // sinks
8374
+ if (sinks) {
8375
+ const float s = ((float *)((char *) sinks->data))[h];
8376
+
8377
+ float ms = 1.0f;
8378
+ float vs = 1.0f;
8379
+
8380
+ if (s > M) {
8381
+ ms = expf(M - s);
8382
+ ggml_vec_scale_f32(DV, VKQ32, ms);
8383
+ } else {
8384
+ vs = expf(s - M);
8385
+ }
8386
+
8387
+ S = S*ms + vs;
8388
+ }
8389
+
8192
8390
  // V /= S
8193
8391
  const float S_inv = 1.0f/S;
8194
8392
  ggml_vec_scale_f32(DV, VKQ32, S_inv);
@@ -8208,17 +8406,13 @@ static void ggml_compute_forward_flash_attn_ext_f16(
8208
8406
 
8209
8407
  void ggml_compute_forward_flash_attn_ext(
8210
8408
  const ggml_compute_params * params,
8211
- const ggml_tensor * q,
8212
- const ggml_tensor * k,
8213
- const ggml_tensor * v,
8214
- const ggml_tensor * mask,
8215
8409
  ggml_tensor * dst) {
8216
8410
  switch (dst->op_params[3]) {
8217
8411
  case GGML_PREC_DEFAULT:
8218
8412
  case GGML_PREC_F32:
8219
8413
  {
8220
8414
  // uses F32 accumulators
8221
- ggml_compute_forward_flash_attn_ext_f16(params, q, k, v, mask, dst);
8415
+ ggml_compute_forward_flash_attn_ext_f16(params, dst);
8222
8416
  } break;
8223
8417
  default:
8224
8418
  {
@@ -9080,6 +9274,10 @@ void ggml_compute_forward_glu(
9080
9274
  {
9081
9275
  ggml_compute_forward_swiglu(params, dst);
9082
9276
  } break;
9277
+ case GGML_GLU_OP_SWIGLU_OAI:
9278
+ {
9279
+ ggml_compute_forward_swiglu_oai(params, dst);
9280
+ } break;
9083
9281
  case GGML_GLU_OP_GEGLU_ERF:
9084
9282
  {
9085
9283
  ggml_compute_forward_geglu_erf(params, dst);
@@ -10132,6 +10330,7 @@ static void ggml_compute_forward_opt_step_adamw_f32(
10132
10330
  const int ir1 = MIN(ir0 + dr, nr);
10133
10331
 
10134
10332
  const float * adamw_params_ptr = ggml_get_data_f32(adamw_params);
10333
+
10135
10334
  const float alpha = adamw_params_ptr[0];
10136
10335
  const float beta1 = adamw_params_ptr[1];
10137
10336
  const float beta2 = adamw_params_ptr[2];
@@ -10139,7 +10338,7 @@ static void ggml_compute_forward_opt_step_adamw_f32(
10139
10338
  const float wd = adamw_params_ptr[4];
10140
10339
  const float beta1h = adamw_params_ptr[5];
10141
10340
  const float beta2h = adamw_params_ptr[6];
10142
-
10341
+ const float keep = 1.f - alpha * wd;
10143
10342
  for (int ir = ir0; ir < ir1; ++ir) {
10144
10343
  const int64_t i03 = ir/(ne02*ne01);
10145
10344
  const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
@@ -10162,7 +10361,7 @@ static void ggml_compute_forward_opt_step_adamw_f32(
10162
10361
  // The weight decay is applied independently of the Adam momenta m and v.
10163
10362
  // This is NOT equivalent to l2 regularization that adds w[i00]*w[i00] to the loss.
10164
10363
  // See: https://arxiv.org/pdf/1711.05101v3.pdf
10165
- w[i00] = w[i00]*(1.0f - alpha*wd) - alpha*mh/vh;
10364
+ w[i00] = w[i00] * keep - alpha * mh / vh;
10166
10365
  }
10167
10366
  }
10168
10367
  }
@@ -10184,3 +10383,63 @@ void ggml_compute_forward_opt_step_adamw(
10184
10383
  }
10185
10384
  }
10186
10385
  }
10386
+
10387
+ static void ggml_compute_forward_opt_step_sgd_f32(const ggml_compute_params * params, ggml_tensor * dst) {
10388
+ const ggml_tensor * src0 = dst->src[0];
10389
+ const ggml_tensor * src0_grad = dst->src[1];
10390
+ const ggml_tensor * sgd_params = dst->src[2];
10391
+
10392
+ GGML_ASSERT(ggml_are_same_shape(src0, src0_grad));
10393
+ GGML_ASSERT(ggml_nelements(sgd_params) == 2);
10394
+
10395
+ const int ith = params->ith;
10396
+ const int nth = params->nth;
10397
+
10398
+ const int nr = ggml_nrows(src0);
10399
+
10400
+ GGML_TENSOR_UNARY_OP_LOCALS
10401
+ GGML_ASSERT(nb00 == sizeof(float));
10402
+
10403
+ // rows per thread
10404
+ const int dr = (nr + nth - 1) / nth;
10405
+
10406
+ // row range for this thread
10407
+ const int ir0 = dr * ith;
10408
+ const int ir1 = MIN(ir0 + dr, nr);
10409
+
10410
+ // using adamw param subset we care about - alpha, wd - could have a separate struct
10411
+ const float * sgd_params_ptr = ggml_get_data_f32(sgd_params);
10412
+ const float alpha = sgd_params_ptr[0];
10413
+ const float keep = 1.f - alpha * sgd_params_ptr[1];
10414
+
10415
+ for (int ir = ir0; ir < ir1; ++ir) {
10416
+ const int64_t i03 = ir / (ne02 * ne01);
10417
+ const int64_t i02 = (ir - i03 * ne02 * ne01) / ne01;
10418
+ const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01);
10419
+
10420
+ const size_t offset = i03 * nb03 + i02 * nb02 + i01 * nb01;
10421
+
10422
+ float * w = (float *) ((char *) src0->data + offset); // weight
10423
+ const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad
10424
+
10425
+ for (int i00 = 0; i00 < ne00; ++i00) {
10426
+ w[i00] = w[i00] * keep - alpha * g[i00];
10427
+ }
10428
+ }
10429
+ }
10430
+
10431
+ void ggml_compute_forward_opt_step_sgd(const ggml_compute_params * params, ggml_tensor * dst) {
10432
+ const ggml_tensor * src0 = dst->src[0];
10433
+
10434
+ switch (src0->type) {
10435
+ case GGML_TYPE_F32:
10436
+ {
10437
+ ggml_compute_forward_opt_step_sgd_f32(params, dst);
10438
+ }
10439
+ break;
10440
+ default:
10441
+ {
10442
+ GGML_ABORT("fatal error - sgd is F32 only");
10443
+ }
10444
+ }
10445
+ }
@@ -29,6 +29,7 @@ extern "C" {
29
29
 
30
30
  void ggml_compute_forward_dup(const struct ggml_compute_params * params, struct ggml_tensor * dst);
31
31
  void ggml_compute_forward_add(const struct ggml_compute_params * params, struct ggml_tensor * dst);
32
+ void ggml_compute_forward_add_id(const struct ggml_compute_params * params, struct ggml_tensor * dst);
32
33
  void ggml_compute_forward_add1(const struct ggml_compute_params * params, struct ggml_tensor * dst);
33
34
  void ggml_compute_forward_acc(const struct ggml_compute_params * params, struct ggml_tensor * dst);
34
35
  void ggml_compute_forward_sum(const struct ggml_compute_params * params, struct ggml_tensor * dst);
@@ -82,13 +83,7 @@ void ggml_compute_forward_arange(const struct ggml_compute_params * params, stru
82
83
  void ggml_compute_forward_timestep_embedding(const struct ggml_compute_params * params, struct ggml_tensor * dst);
83
84
  void ggml_compute_forward_argsort(const struct ggml_compute_params * params, struct ggml_tensor * dst);
84
85
  void ggml_compute_forward_leaky_relu(const struct ggml_compute_params * params, struct ggml_tensor * dst);
85
- void ggml_compute_forward_flash_attn_ext(
86
- const struct ggml_compute_params * params,
87
- const struct ggml_tensor * q,
88
- const struct ggml_tensor * k,
89
- const struct ggml_tensor * v,
90
- const struct ggml_tensor * mask,
91
- struct ggml_tensor * dst);
86
+ void ggml_compute_forward_flash_attn_ext(const struct ggml_compute_params * params, struct ggml_tensor * dst);
92
87
  void ggml_compute_forward_flash_attn_back(
93
88
  const struct ggml_compute_params * params,
94
89
  const bool masked,
@@ -112,7 +107,7 @@ void ggml_compute_forward_cross_entropy_loss(const struct ggml_compute_params *
112
107
  void ggml_compute_forward_cross_entropy_loss_back(const struct ggml_compute_params * params, struct ggml_tensor * dst);
113
108
  void ggml_compute_forward_opt_step_adamw(const struct ggml_compute_params * params, struct ggml_tensor * dst);
114
109
  void ggml_compute_forward_mul_mat(const struct ggml_compute_params * params, struct ggml_tensor * dst);
115
-
110
+ void ggml_compute_forward_opt_step_sgd(const struct ggml_compute_params * params, struct ggml_tensor * dst);
116
111
  #ifdef __cplusplus
117
112
  }
118
113
  #endif
@@ -46,6 +46,10 @@ void quantize_row_q8_1_generic(const float * GGML_RESTRICT x, void * GGML_RESTRI
46
46
  quantize_row_q8_1_ref(x, y, k);
47
47
  }
48
48
 
49
+ void quantize_row_mxfp4(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
50
+ quantize_row_mxfp4_ref(x, y, k);
51
+ }
52
+
49
53
  //
50
54
  // 2-6 bit quantization in super-blocks
51
55
  //
@@ -181,6 +185,37 @@ void ggml_vec_dot_q4_1_q8_1_generic(int n, float * GGML_RESTRICT s, size_t bs, c
181
185
  *s = sumf;
182
186
  }
183
187
 
188
+ void ggml_vec_dot_mxfp4_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
189
+ assert(nrc == 1);
190
+ UNUSED(nrc);
191
+ UNUSED(bx);
192
+ UNUSED(by);
193
+ UNUSED(bs);
194
+ assert(n % QK_MXFP4 == 0);
195
+ static_assert(QK_MXFP4 == QK8_0, "QK_MXFP4 and QK8_0 must be the same");
196
+
197
+ const block_mxfp4 * GGML_RESTRICT x = vx;
198
+ const block_q8_0 * GGML_RESTRICT y = vy;
199
+
200
+ const int nb = n / QK_MXFP4;
201
+
202
+ int ib = 0;
203
+ float sumf = 0;
204
+
205
+ for (; ib < nb; ++ib) {
206
+ const float d = GGML_CPU_FP16_TO_FP32(y[ib].d)*GGML_E8M0_TO_FP32_HALF(x[ib].e);
207
+
208
+ int sumi1 = 0;
209
+ int sumi2 = 0;
210
+ for (int j = 0; j < QK_MXFP4/2; ++j) {
211
+ sumi1 += y[ib].qs[j + 0] * kvalues_mxfp4[x[ib].qs[j] & 0xf];
212
+ sumi2 += y[ib].qs[j + QK_MXFP4/2] * kvalues_mxfp4[x[ib].qs[j] >> 4];
213
+ }
214
+ sumf += d * (sumi1 + sumi2);
215
+ }
216
+ *s = sumf;
217
+ }
218
+
184
219
  void ggml_vec_dot_q5_0_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
185
220
  const int qk = QK8_0;
186
221
  const int nb = n / qk;
@@ -19,6 +19,8 @@ void quantize_row_q5_1(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, in
19
19
  void quantize_row_q8_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
20
20
  void quantize_row_q8_1(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
21
21
 
22
+ void quantize_row_mxfp4(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
23
+
22
24
  void quantize_row_q2_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
23
25
  void quantize_row_q3_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
24
26
  void quantize_row_q4_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
@@ -39,6 +41,8 @@ void ggml_vec_dot_q5_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const voi
39
41
  void ggml_vec_dot_q5_1_q8_1(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
40
42
  void ggml_vec_dot_q8_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
41
43
 
44
+ void ggml_vec_dot_mxfp4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
45
+
42
46
  void ggml_vec_dot_q2_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
43
47
  void ggml_vec_dot_q3_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
44
48
  void ggml_vec_dot_q4_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
@@ -67,8 +71,12 @@ void ggml_vec_dot_q4_1_q8_1_generic(int n, float * GGML_RESTRICT s, size_t bs, c
67
71
  void ggml_vec_dot_q5_0_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
68
72
  void ggml_vec_dot_q5_1_q8_1_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
69
73
  void ggml_vec_dot_q8_0_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
74
+
75
+ void ggml_vec_dot_mxfp4_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
76
+
70
77
  void ggml_vec_dot_tq1_0_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
71
78
  void ggml_vec_dot_tq2_0_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
79
+
72
80
  void ggml_vec_dot_q2_K_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
73
81
  void ggml_vec_dot_q3_K_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);
74
82
  void ggml_vec_dot_q4_K_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc);