@novastera-oss/llamarn 0.2.7 → 0.2.9

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 (186) hide show
  1. package/android/src/main/cpp/include/llama.h +8 -3
  2. package/android/src/main/jniLibs/arm64-v8a/libggml-base.so +0 -0
  3. package/android/src/main/jniLibs/arm64-v8a/libggml-cpu.so +0 -0
  4. package/android/src/main/jniLibs/arm64-v8a/libggml.so +0 -0
  5. package/android/src/main/jniLibs/arm64-v8a/libllama.so +0 -0
  6. package/android/src/main/jniLibs/x86_64/libggml-base.so +0 -0
  7. package/android/src/main/jniLibs/x86_64/libggml-cpu.so +0 -0
  8. package/android/src/main/jniLibs/x86_64/libggml.so +0 -0
  9. package/android/src/main/jniLibs/x86_64/libllama.so +0 -0
  10. package/cpp/LlamaCppModel.cpp +56 -22
  11. package/cpp/build-info.cpp +2 -2
  12. package/cpp/llama.cpp/CMakeLists.txt +1 -1
  13. package/cpp/llama.cpp/common/arg.cpp +7 -0
  14. package/cpp/llama.cpp/common/common.cpp +3 -0
  15. package/cpp/llama.cpp/common/common.h +1 -0
  16. package/cpp/llama.cpp/common/json-schema-to-grammar.cpp +3 -46
  17. package/cpp/llama.cpp/convert_hf_to_gguf.py +118 -20
  18. package/cpp/llama.cpp/ggml/CMakeLists.txt +1 -0
  19. package/cpp/llama.cpp/ggml/include/ggml-cpu.h +2 -0
  20. package/cpp/llama.cpp/ggml/include/ggml.h +33 -0
  21. package/cpp/llama.cpp/ggml/src/CMakeLists.txt +17 -0
  22. package/cpp/llama.cpp/ggml/src/ggml-cann/common.h +1 -1
  23. package/cpp/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +31 -2
  24. package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/mmq.cpp +10 -9
  25. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/arm/quants.c +109 -108
  26. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/arm/repack.cpp +1027 -1038
  27. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +53 -52
  28. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/powerpc/cpu-feats.cpp +82 -0
  29. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/powerpc/quants.c +56 -55
  30. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +42 -41
  31. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/riscv/repack.cpp +24 -23
  32. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/s390/quants.c +29 -28
  33. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/wasm/quants.c +30 -29
  34. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/x86/quants.c +83 -82
  35. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/x86/repack.cpp +20 -19
  36. package/cpp/llama.cpp/ggml/src/ggml-cpu/common.h +3 -2
  37. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +9 -3
  38. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +83 -102
  39. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +4 -0
  40. package/cpp/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +3 -2
  41. package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.cpp +192 -67
  42. package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.h +2 -0
  43. package/cpp/llama.cpp/ggml/src/ggml-cpu/quants.c +25 -24
  44. package/cpp/llama.cpp/ggml/src/ggml-cpu/repack.cpp +56 -40
  45. package/cpp/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +211 -33
  46. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.cpp +2 -2
  47. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.h +45 -45
  48. package/cpp/llama.cpp/ggml/src/ggml-cuda/common.cuh +54 -29
  49. package/cpp/llama.cpp/ggml/src/ggml-cuda/conv2d-dw.cu +161 -0
  50. package/cpp/llama.cpp/ggml/src/ggml-cuda/conv2d-dw.cuh +5 -0
  51. package/cpp/llama.cpp/ggml/src/ggml-cuda/conv2d-transpose.cu +91 -0
  52. package/cpp/llama.cpp/ggml/src/ggml-cuda/conv2d-transpose.cuh +4 -0
  53. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-wmma-f16.cu +4 -0
  54. package/cpp/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu +84 -31
  55. package/cpp/llama.cpp/ggml/src/ggml-cuda/mean.cu +19 -0
  56. package/cpp/llama.cpp/ggml/src/ggml-cuda/mean.cuh +3 -0
  57. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmv.cu +257 -87
  58. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmv.cuh +2 -3
  59. package/cpp/llama.cpp/ggml/src/ggml-cuda/sumrows.cu +5 -18
  60. package/cpp/llama.cpp/ggml/src/ggml-cuda/sumrows.cuh +0 -1
  61. package/cpp/llama.cpp/ggml/src/ggml-impl.h +61 -183
  62. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +16 -0
  63. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.m +227 -41
  64. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.metal +362 -182
  65. package/cpp/llama.cpp/ggml/src/ggml-musa/mudnn.cuh +2 -2
  66. package/cpp/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +240 -535
  67. package/cpp/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +5 -6
  68. package/cpp/llama.cpp/ggml/src/ggml-sycl/common.hpp +1 -24
  69. package/cpp/llama.cpp/ggml/src/ggml-sycl/concat.cpp +28 -41
  70. package/cpp/llama.cpp/ggml/src/ggml-sycl/conv.cpp +4 -10
  71. package/cpp/llama.cpp/ggml/src/ggml-sycl/convert.cpp +99 -166
  72. package/cpp/llama.cpp/ggml/src/ggml-sycl/cpy.cpp +94 -72
  73. package/cpp/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +49 -67
  74. package/cpp/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +31 -1
  75. package/cpp/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +99 -159
  76. package/cpp/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +6 -9
  77. package/cpp/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +45 -54
  78. package/cpp/llama.cpp/ggml/src/ggml-sycl/gla.cpp +2 -2
  79. package/cpp/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +1 -1
  80. package/cpp/llama.cpp/ggml/src/ggml-sycl/mmq.cpp +60 -80
  81. package/cpp/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +132 -201
  82. package/cpp/llama.cpp/ggml/src/ggml-sycl/norm.cpp +55 -74
  83. package/cpp/llama.cpp/ggml/src/ggml-sycl/rope.cpp +24 -20
  84. package/cpp/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +3 -3
  85. package/cpp/llama.cpp/ggml/src/ggml-sycl/sycl_hw.cpp +3 -1
  86. package/cpp/llama.cpp/ggml/src/ggml-sycl/sycl_hw.hpp +3 -0
  87. package/cpp/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +3 -8
  88. package/cpp/llama.cpp/ggml/src/ggml-sycl/wkv.cpp +12 -16
  89. package/cpp/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +12 -1
  90. package/cpp/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +57 -1
  91. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +4 -0
  92. package/cpp/llama.cpp/ggml/src/ggml.c +69 -13
  93. package/cpp/llama.cpp/ggml/src/gguf.cpp +5 -1
  94. package/cpp/llama.cpp/gguf-py/gguf/constants.py +76 -0
  95. package/cpp/llama.cpp/gguf-py/gguf/gguf_writer.py +21 -0
  96. package/cpp/llama.cpp/gguf-py/gguf/tensor_mapping.py +64 -0
  97. package/cpp/llama.cpp/gguf-py/gguf/vocab.py +97 -4
  98. package/cpp/llama.cpp/gguf-py/pyproject.toml +2 -2
  99. package/cpp/llama.cpp/include/llama.h +8 -3
  100. package/cpp/llama.cpp/models/templates/Mistral-Small-3.2-24B-Instruct-2506.jinja +124 -0
  101. package/cpp/llama.cpp/src/llama-arch.cpp +55 -0
  102. package/cpp/llama.cpp/src/llama-arch.h +18 -0
  103. package/cpp/llama.cpp/src/llama-batch.cpp +570 -359
  104. package/cpp/llama.cpp/src/llama-batch.h +98 -70
  105. package/cpp/llama.cpp/src/llama-chat.cpp +11 -6
  106. package/cpp/llama.cpp/src/llama-context.cpp +101 -107
  107. package/cpp/llama.cpp/src/llama-context.h +13 -13
  108. package/cpp/llama.cpp/src/llama-graph.cpp +199 -252
  109. package/cpp/llama.cpp/src/llama-graph.h +44 -32
  110. package/cpp/llama.cpp/src/llama-hparams.cpp +4 -0
  111. package/cpp/llama.cpp/src/llama-hparams.h +8 -0
  112. package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.cpp +51 -53
  113. package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.h +19 -24
  114. package/cpp/llama.cpp/src/llama-kv-cache-unified.cpp +110 -104
  115. package/cpp/llama.cpp/src/llama-kv-cache-unified.h +17 -22
  116. package/cpp/llama.cpp/src/llama-kv-cells.h +35 -11
  117. package/cpp/llama.cpp/src/llama-memory-hybrid.cpp +66 -67
  118. package/cpp/llama.cpp/src/llama-memory-hybrid.h +16 -21
  119. package/cpp/llama.cpp/src/llama-memory-recurrent.cpp +69 -68
  120. package/cpp/llama.cpp/src/llama-memory-recurrent.h +15 -20
  121. package/cpp/llama.cpp/src/llama-memory.h +18 -22
  122. package/cpp/llama.cpp/src/llama-model-saver.cpp +1 -0
  123. package/cpp/llama.cpp/src/llama-model.cpp +1006 -472
  124. package/cpp/llama.cpp/src/llama-model.h +22 -0
  125. package/cpp/llama.cpp/src/llama-quant.cpp +87 -5
  126. package/cpp/llama.cpp/src/llama-vocab.cpp +26 -3
  127. package/cpp/llama.cpp/src/llama-vocab.h +1 -0
  128. package/cpp/rn-utils.h +3 -0
  129. package/ios/include/common.h +1 -0
  130. package/ios/include/llama.h +8 -3
  131. package/ios/libs/llama.xcframework/Info.plist +19 -19
  132. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  133. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4890 -4863
  134. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml-cpu.h +2 -0
  135. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml.h +33 -0
  136. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/llama.h +8 -3
  137. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/llama +0 -0
  138. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  139. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4861 -4834
  140. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3764 -3742
  141. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml-cpu.h +2 -0
  142. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +33 -0
  143. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/llama.h +8 -3
  144. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/llama +0 -0
  145. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  146. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4861 -4834
  147. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3766 -3744
  148. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml-cpu.h +2 -0
  149. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml.h +33 -0
  150. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/llama.h +8 -3
  151. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml-cpu.h +2 -0
  152. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml.h +33 -0
  153. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/llama.h +8 -3
  154. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/llama +0 -0
  155. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml-cpu.h +2 -0
  156. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml.h +33 -0
  157. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/llama.h +8 -3
  158. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/llama +0 -0
  159. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/llama +0 -0
  160. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  161. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4890 -4863
  162. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml-cpu.h +2 -0
  163. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml.h +33 -0
  164. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/llama.h +8 -3
  165. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/llama +0 -0
  166. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  167. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4861 -4834
  168. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3764 -3742
  169. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml-cpu.h +2 -0
  170. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +33 -0
  171. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/llama.h +8 -3
  172. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/llama +0 -0
  173. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  174. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4926 -4900
  175. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml-cpu.h +2 -0
  176. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml.h +33 -0
  177. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/llama.h +8 -3
  178. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/llama +0 -0
  179. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  180. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4897 -4871
  181. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3794 -3773
  182. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml-cpu.h +2 -0
  183. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +33 -0
  184. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/llama.h +8 -3
  185. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/llama +0 -0
  186. package/package.json +1 -1
@@ -317,203 +317,81 @@ struct ggml_cgraph ggml_graph_view(struct ggml_cgraph * cgraph, int i0, int i1);
317
317
  GGML_API void * ggml_aligned_malloc(size_t size);
318
318
  GGML_API void ggml_aligned_free(void * ptr, size_t size);
319
319
 
320
- // FP16 to FP32 conversion
320
+ // FP16 <-> FP32
321
+ // ref: https://github.com/Maratyszcza/FP16
321
322
 
322
- // 16-bit float
323
- // on Arm, we use __fp16
324
- // on x86, we use uint16_t
325
- //
326
- // for old CUDA compilers (<= 11), we use uint16_t: ref https://github.com/ggml-org/llama.cpp/pull/10616
327
- // for MUSA compilers , we use uint16_t: ref https://github.com/ggml-org/llama.cpp/pull/11843
328
- //
329
- #if defined(__ARM_NEON) && !(defined(__CUDACC__) && __CUDACC_VER_MAJOR__ <= 11) && !defined(__MUSACC__)
330
- #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
331
- #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
332
-
333
- #define GGML_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
334
-
335
- static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
336
- __fp16 tmp;
337
- memcpy(&tmp, &h, sizeof(ggml_fp16_t));
338
- return (float)tmp;
339
- }
340
-
341
- static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
342
- ggml_fp16_t res;
343
- __fp16 tmp = f;
344
- memcpy(&res, &tmp, sizeof(ggml_fp16_t));
345
- return res;
346
- }
347
-
348
- #elif defined(__F16C__)
349
-
350
- #ifdef _MSC_VER
351
- #define GGML_COMPUTE_FP16_TO_FP32(x) _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(x)))
352
- #define GGML_COMPUTE_FP32_TO_FP16(x) _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(x), 0), 0)
353
- #else
354
- #define GGML_COMPUTE_FP16_TO_FP32(x) _cvtsh_ss(x)
355
- #define GGML_COMPUTE_FP32_TO_FP16(x) _cvtss_sh(x, 0)
356
- #endif
357
-
358
- #elif defined(__POWER9_VECTOR__)
359
-
360
- #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
361
- #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
362
- /* the inline asm below is about 12% faster than the lookup method */
363
- #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
364
- #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
365
-
366
- static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
367
- float f;
368
- double d;
369
- __asm__(
370
- "mtfprd %0,%2\n"
371
- "xscvhpdp %0,%0\n"
372
- "frsp %1,%0\n" :
373
- /* temp */ "=d"(d),
374
- /* out */ "=f"(f):
375
- /* in */ "r"(h));
376
- return f;
377
- }
378
-
379
- static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
380
- double d;
381
- ggml_fp16_t r;
382
- __asm__( /* xscvdphp can work on double or single precision */
383
- "xscvdphp %0,%2\n"
384
- "mffprd %1,%0\n" :
385
- /* temp */ "=d"(d),
386
- /* out */ "=r"(r):
387
- /* in */ "f"(f));
388
- return r;
389
- }
390
-
391
- #elif defined(__riscv) && defined(__riscv_zfhmin)
392
-
393
- static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
394
- float f;
395
- __asm__(
396
- "fmv.h.x %[f], %[h]\n\t"
397
- "fcvt.s.h %[f], %[f]"
398
- : [f] "=&f" (f)
399
- : [h] "r" (h)
400
- );
401
- return f;
402
- }
323
+ static inline float fp32_from_bits(uint32_t w) {
324
+ union {
325
+ uint32_t as_bits;
326
+ float as_value;
327
+ } fp32;
328
+ fp32.as_bits = w;
329
+ return fp32.as_value;
330
+ }
403
331
 
404
- static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
405
- ggml_fp16_t res;
406
- __asm__(
407
- "fcvt.h.s %[f], %[f]\n\t"
408
- "fmv.x.h %[h], %[f]"
409
- : [h] "=&r" (res)
410
- : [f] "f" (f)
411
- );
412
- return res;
413
- }
332
+ static inline uint32_t fp32_to_bits(float f) {
333
+ union {
334
+ float as_value;
335
+ uint32_t as_bits;
336
+ } fp32;
337
+ fp32.as_value = f;
338
+ return fp32.as_bits;
339
+ }
414
340
 
415
- #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
416
- #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
417
- #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
418
- #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
341
+ static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
342
+ const uint32_t w = (uint32_t) h << 16;
343
+ const uint32_t sign = w & UINT32_C(0x80000000);
344
+ const uint32_t two_w = w + w;
419
345
 
346
+ const uint32_t exp_offset = UINT32_C(0xE0) << 23;
347
+ #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
348
+ const float exp_scale = 0x1.0p-112f;
420
349
  #else
350
+ const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
351
+ #endif
352
+ const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
421
353
 
422
- // FP16 <-> FP32
423
- // ref: https://github.com/Maratyszcza/FP16
424
-
425
- static inline float fp32_from_bits(uint32_t w) {
426
- union {
427
- uint32_t as_bits;
428
- float as_value;
429
- } fp32;
430
- fp32.as_bits = w;
431
- return fp32.as_value;
432
- }
433
-
434
- static inline uint32_t fp32_to_bits(float f) {
435
- union {
436
- float as_value;
437
- uint32_t as_bits;
438
- } fp32;
439
- fp32.as_value = f;
440
- return fp32.as_bits;
441
- }
442
-
443
- static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
444
- const uint32_t w = (uint32_t) h << 16;
445
- const uint32_t sign = w & UINT32_C(0x80000000);
446
- const uint32_t two_w = w + w;
447
-
448
- const uint32_t exp_offset = UINT32_C(0xE0) << 23;
449
- #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
450
- const float exp_scale = 0x1.0p-112f;
451
- #else
452
- const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
453
- #endif
454
- const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
455
-
456
- const uint32_t magic_mask = UINT32_C(126) << 23;
457
- const float magic_bias = 0.5f;
458
- const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
354
+ const uint32_t magic_mask = UINT32_C(126) << 23;
355
+ const float magic_bias = 0.5f;
356
+ const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
459
357
 
460
- const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
461
- const uint32_t result = sign |
462
- (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
463
- return fp32_from_bits(result);
464
- }
465
-
466
- static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
467
- #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
468
- const float scale_to_inf = 0x1.0p+112f;
469
- const float scale_to_zero = 0x1.0p-110f;
470
- #else
471
- const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
472
- const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
473
- #endif
474
- float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
475
-
476
- const uint32_t w = fp32_to_bits(f);
477
- const uint32_t shl1_w = w + w;
478
- const uint32_t sign = w & UINT32_C(0x80000000);
479
- uint32_t bias = shl1_w & UINT32_C(0xFF000000);
480
- if (bias < UINT32_C(0x71000000)) {
481
- bias = UINT32_C(0x71000000);
482
- }
358
+ const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
359
+ const uint32_t result = sign |
360
+ (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
361
+ return fp32_from_bits(result);
362
+ }
483
363
 
484
- base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
485
- const uint32_t bits = fp32_to_bits(base);
486
- const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
487
- const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
488
- const uint32_t nonsign = exp_bits + mantissa_bits;
489
- return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
364
+ static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
365
+ #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
366
+ const float scale_to_inf = 0x1.0p+112f;
367
+ const float scale_to_zero = 0x1.0p-110f;
368
+ #else
369
+ const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
370
+ const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
371
+ #endif
372
+ float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
373
+
374
+ const uint32_t w = fp32_to_bits(f);
375
+ const uint32_t shl1_w = w + w;
376
+ const uint32_t sign = w & UINT32_C(0x80000000);
377
+ uint32_t bias = shl1_w & UINT32_C(0xFF000000);
378
+ if (bias < UINT32_C(0x71000000)) {
379
+ bias = UINT32_C(0x71000000);
490
380
  }
491
381
 
492
- #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
493
- #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
494
-
495
- #endif // defined(__ARM_NEON) && !(defined(__CUDACC__) && __CUDACC_VER_MAJOR__ <= 11) && !defined(__MUSACC__)
496
-
497
- // precomputed f32 table for f16 (256 KB)
498
- // defined in ggml.c, initialized in ggml_init()
499
- GGML_API float ggml_table_f32_f16[1 << 16];
500
-
501
- // On ARM NEON, it's quicker to directly convert x -> x instead of calling into ggml_lookup_fp16_to_fp32,
502
- // so we define GGML_FP16_TO_FP32 and GGML_FP32_TO_FP16 elsewhere for NEON.
503
- // This is also true for POWER9.
504
- #if !defined(GGML_FP16_TO_FP32)
505
- inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
506
- uint16_t s;
507
- memcpy(&s, &f, sizeof(uint16_t));
508
- return ggml_table_f32_f16[s];
382
+ base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
383
+ const uint32_t bits = fp32_to_bits(base);
384
+ const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
385
+ const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
386
+ const uint32_t nonsign = exp_bits + mantissa_bits;
387
+ return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
509
388
  }
510
389
 
511
- #define GGML_FP16_TO_FP32(x) ggml_lookup_fp16_to_fp32(x)
512
- #endif
390
+ #define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)
391
+ #define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)
513
392
 
514
- #if !defined(GGML_FP32_TO_FP16)
393
+ #define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)
515
394
  #define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)
516
- #endif
517
395
 
518
396
  /**
519
397
  * Converts brain16 to float32.
@@ -521,6 +521,22 @@ typedef struct {
521
521
  uint64_t nb2;
522
522
  } ggml_metal_kargs_get_rows;
523
523
 
524
+ typedef struct {
525
+ int32_t nk0;
526
+ int32_t ne01;
527
+ uint64_t nb01;
528
+ uint64_t nb02;
529
+ uint64_t nb03;
530
+ int32_t ne11;
531
+ int32_t ne12;
532
+ uint64_t nb10;
533
+ uint64_t nb11;
534
+ uint64_t nb12;
535
+ uint64_t nb1;
536
+ uint64_t nb2;
537
+ uint64_t nb3;
538
+ } ggml_metal_kargs_set_rows;
539
+
524
540
  typedef struct {
525
541
  int64_t ne00;
526
542
  int64_t ne01;