llama_cpp 0.16.2 → 0.17.0

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 (177) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/README.md +7 -12
  4. data/ext/llama_cpp/extconf.rb +2 -43
  5. data/ext/llama_cpp/llama_cpp.cpp +8 -0
  6. data/lib/llama_cpp/version.rb +3 -3
  7. data/sig/llama_cpp.rbs +3 -0
  8. metadata +2 -171
  9. data/vendor/include/.gitkeep +0 -0
  10. data/vendor/lib/.gitkeep +0 -0
  11. data/vendor/tmp/llama.cpp/LICENSE +0 -21
  12. data/vendor/tmp/llama.cpp/Makefile +0 -1124
  13. data/vendor/tmp/llama.cpp/ggml-alloc.c +0 -1041
  14. data/vendor/tmp/llama.cpp/ggml-alloc.h +0 -76
  15. data/vendor/tmp/llama.cpp/ggml-backend-impl.h +0 -153
  16. data/vendor/tmp/llama.cpp/ggml-backend.c +0 -2225
  17. data/vendor/tmp/llama.cpp/ggml-backend.h +0 -236
  18. data/vendor/tmp/llama.cpp/ggml-blas.cpp +0 -363
  19. data/vendor/tmp/llama.cpp/ggml-blas.h +0 -23
  20. data/vendor/tmp/llama.cpp/ggml-common.h +0 -1805
  21. data/vendor/tmp/llama.cpp/ggml-cuda/acc.cu +0 -47
  22. data/vendor/tmp/llama.cpp/ggml-cuda/arange.cu +0 -34
  23. data/vendor/tmp/llama.cpp/ggml-cuda/argsort.cu +0 -104
  24. data/vendor/tmp/llama.cpp/ggml-cuda/binbcast.cu +0 -280
  25. data/vendor/tmp/llama.cpp/ggml-cuda/clamp.cu +0 -34
  26. data/vendor/tmp/llama.cpp/ggml-cuda/concat.cu +0 -196
  27. data/vendor/tmp/llama.cpp/ggml-cuda/convert.cu +0 -686
  28. data/vendor/tmp/llama.cpp/ggml-cuda/cpy.cu +0 -490
  29. data/vendor/tmp/llama.cpp/ggml-cuda/diagmask.cu +0 -40
  30. data/vendor/tmp/llama.cpp/ggml-cuda/dmmv.cu +0 -674
  31. data/vendor/tmp/llama.cpp/ggml-cuda/fattn-tile-f16.cu +0 -319
  32. data/vendor/tmp/llama.cpp/ggml-cuda/fattn-tile-f32.cu +0 -312
  33. data/vendor/tmp/llama.cpp/ggml-cuda/fattn.cu +0 -345
  34. data/vendor/tmp/llama.cpp/ggml-cuda/getrows.cu +0 -178
  35. data/vendor/tmp/llama.cpp/ggml-cuda/im2col.cu +0 -104
  36. data/vendor/tmp/llama.cpp/ggml-cuda/mmq.cu +0 -88
  37. data/vendor/tmp/llama.cpp/ggml-cuda/mmvq.cu +0 -419
  38. data/vendor/tmp/llama.cpp/ggml-cuda/norm.cu +0 -221
  39. data/vendor/tmp/llama.cpp/ggml-cuda/pad.cu +0 -49
  40. data/vendor/tmp/llama.cpp/ggml-cuda/pool2d.cu +0 -94
  41. data/vendor/tmp/llama.cpp/ggml-cuda/quantize.cu +0 -112
  42. data/vendor/tmp/llama.cpp/ggml-cuda/rope.cu +0 -271
  43. data/vendor/tmp/llama.cpp/ggml-cuda/scale.cu +0 -31
  44. data/vendor/tmp/llama.cpp/ggml-cuda/softmax.cu +0 -206
  45. data/vendor/tmp/llama.cpp/ggml-cuda/sumrows.cu +0 -40
  46. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-f16.cu +0 -5
  47. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_0.cu +0 -5
  48. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_1.cu +0 -5
  49. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_0.cu +0 -5
  50. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_1.cu +0 -5
  51. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q8_0.cu +0 -5
  52. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-f16.cu +0 -5
  53. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_0.cu +0 -5
  54. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_1.cu +0 -5
  55. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_0.cu +0 -5
  56. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_1.cu +0 -5
  57. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q8_0.cu +0 -5
  58. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-f16.cu +0 -5
  59. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_0.cu +0 -5
  60. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_1.cu +0 -5
  61. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_0.cu +0 -5
  62. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_1.cu +0 -5
  63. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q8_0.cu +0 -5
  64. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-f16.cu +0 -5
  65. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_0.cu +0 -5
  66. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_1.cu +0 -5
  67. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_0.cu +0 -5
  68. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_1.cu +0 -5
  69. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q8_0.cu +0 -5
  70. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-f16.cu +0 -5
  71. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_0.cu +0 -5
  72. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_1.cu +0 -5
  73. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_0.cu +0 -5
  74. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_1.cu +0 -5
  75. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q8_0.cu +0 -5
  76. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-f16.cu +0 -5
  77. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_0.cu +0 -5
  78. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_1.cu +0 -5
  79. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_0.cu +0 -5
  80. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_1.cu +0 -5
  81. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q8_0.cu +0 -5
  82. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs256-f16-f16.cu +0 -5
  83. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-f16.cu +0 -5
  84. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_0.cu +0 -5
  85. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_1.cu +0 -5
  86. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_0.cu +0 -5
  87. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_1.cu +0 -5
  88. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q8_0.cu +0 -5
  89. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-f16.cu +0 -5
  90. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_0.cu +0 -5
  91. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_1.cu +0 -5
  92. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_0.cu +0 -5
  93. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_1.cu +0 -5
  94. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q8_0.cu +0 -5
  95. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-f16.cu +0 -5
  96. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_0.cu +0 -5
  97. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_1.cu +0 -5
  98. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_0.cu +0 -5
  99. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_1.cu +0 -5
  100. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q8_0.cu +0 -5
  101. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-f16.cu +0 -5
  102. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_0.cu +0 -5
  103. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_1.cu +0 -5
  104. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_0.cu +0 -5
  105. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_1.cu +0 -5
  106. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q8_0.cu +0 -5
  107. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-f16.cu +0 -5
  108. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_0.cu +0 -5
  109. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_1.cu +0 -5
  110. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_0.cu +0 -5
  111. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_1.cu +0 -5
  112. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q8_0.cu +0 -5
  113. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-f16.cu +0 -5
  114. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_0.cu +0 -5
  115. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_1.cu +0 -5
  116. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_0.cu +0 -5
  117. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_1.cu +0 -5
  118. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q8_0.cu +0 -5
  119. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-f16.cu +0 -5
  120. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_0.cu +0 -5
  121. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_1.cu +0 -5
  122. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_0.cu +0 -5
  123. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_1.cu +0 -5
  124. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q8_0.cu +0 -5
  125. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs256-f16-f16.cu +0 -5
  126. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-f16.cu +0 -5
  127. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_0.cu +0 -5
  128. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_1.cu +0 -5
  129. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_0.cu +0 -5
  130. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_1.cu +0 -5
  131. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q8_0.cu +0 -5
  132. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-wmma-f16-instance-kqfloat-cpb16.cu +0 -10
  133. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-wmma-f16-instance-kqfloat-cpb32.cu +0 -9
  134. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-wmma-f16-instance-kqhalf-cpb16.cu +0 -10
  135. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-wmma-f16-instance-kqhalf-cpb32.cu +0 -10
  136. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/fattn-wmma-f16-instance-kqhalf-cpb8.cu +0 -8
  137. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q2_k.cu +0 -5
  138. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q3_k.cu +0 -5
  139. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q4_0.cu +0 -5
  140. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q4_1.cu +0 -5
  141. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q4_k.cu +0 -5
  142. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q5_0.cu +0 -5
  143. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q5_1.cu +0 -5
  144. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q5_k.cu +0 -5
  145. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q6_k.cu +0 -5
  146. data/vendor/tmp/llama.cpp/ggml-cuda/template-instances/mmq-instance-q8_0.cu +0 -5
  147. data/vendor/tmp/llama.cpp/ggml-cuda/tsembd.cu +0 -47
  148. data/vendor/tmp/llama.cpp/ggml-cuda/unary.cu +0 -314
  149. data/vendor/tmp/llama.cpp/ggml-cuda/upscale.cu +0 -51
  150. data/vendor/tmp/llama.cpp/ggml-cuda.cu +0 -3069
  151. data/vendor/tmp/llama.cpp/ggml-cuda.h +0 -44
  152. data/vendor/tmp/llama.cpp/ggml-impl.h +0 -651
  153. data/vendor/tmp/llama.cpp/ggml-kompute.cpp +0 -2038
  154. data/vendor/tmp/llama.cpp/ggml-kompute.h +0 -46
  155. data/vendor/tmp/llama.cpp/ggml-metal.h +0 -66
  156. data/vendor/tmp/llama.cpp/ggml-metal.m +0 -3273
  157. data/vendor/tmp/llama.cpp/ggml-metal.metal +0 -6540
  158. data/vendor/tmp/llama.cpp/ggml-quants.c +0 -14994
  159. data/vendor/tmp/llama.cpp/ggml-quants.h +0 -133
  160. data/vendor/tmp/llama.cpp/ggml-rpc.cpp +0 -1178
  161. data/vendor/tmp/llama.cpp/ggml-rpc.h +0 -24
  162. data/vendor/tmp/llama.cpp/ggml-sycl.cpp +0 -6351
  163. data/vendor/tmp/llama.cpp/ggml-sycl.h +0 -40
  164. data/vendor/tmp/llama.cpp/ggml-vulkan-shaders.hpp +0 -144508
  165. data/vendor/tmp/llama.cpp/ggml-vulkan.cpp +0 -7183
  166. data/vendor/tmp/llama.cpp/ggml-vulkan.h +0 -29
  167. data/vendor/tmp/llama.cpp/ggml.c +0 -22506
  168. data/vendor/tmp/llama.cpp/ggml.h +0 -2458
  169. data/vendor/tmp/llama.cpp/llama.cpp +0 -18985
  170. data/vendor/tmp/llama.cpp/llama.h +0 -1147
  171. data/vendor/tmp/llama.cpp/scripts/get-flags.mk +0 -38
  172. data/vendor/tmp/llama.cpp/sgemm.cpp +0 -1032
  173. data/vendor/tmp/llama.cpp/sgemm.h +0 -14
  174. data/vendor/tmp/llama.cpp/unicode-data.cpp +0 -7033
  175. data/vendor/tmp/llama.cpp/unicode-data.h +0 -20
  176. data/vendor/tmp/llama.cpp/unicode.cpp +0 -810
  177. data/vendor/tmp/llama.cpp/unicode.h +0 -63
@@ -1,810 +0,0 @@
1
- #include "unicode.h"
2
- #include "unicode-data.h"
3
-
4
- #include <cassert>
5
- #include <cstddef>
6
- #include <cstdint>
7
- #include <map>
8
- #include <regex>
9
- #include <stdexcept>
10
- #include <string>
11
- #include <unordered_map>
12
- #include <unordered_set>
13
- #include <utility>
14
- #include <vector>
15
- #include <locale>
16
- #include <codecvt>
17
-
18
- static std::string unicode_cpts_to_utf8(const std::vector<uint32_t> & cps) {
19
- std::string result;
20
- for (size_t i = 0; i < cps.size(); ++i) {
21
- result.append(unicode_cpt_to_utf8(cps[i]));
22
- }
23
- return result;
24
- }
25
-
26
- static uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset) {
27
- assert(offset < utf8.size());
28
- if (!(utf8[offset + 0] & 0x80)) {
29
- auto result = utf8[offset + 0];
30
- offset += 1;
31
- return result;
32
- }
33
- if (!(utf8[offset + 0] & 0x40)) {
34
- throw std::invalid_argument("invalid character");
35
- }
36
- if (!(utf8[offset + 0] & 0x20)) {
37
- if (offset + 1 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80)) {
38
- throw std::invalid_argument("invalid character");
39
- }
40
- auto result = ((utf8[offset + 0] & 0x1f) << 6) | (utf8[offset + 1] & 0x3f);
41
- offset += 2;
42
- return result;
43
- }
44
- if (!(utf8[offset + 0] & 0x10)) {
45
- if (offset + 2 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80)) {
46
- throw std::invalid_argument("invalid character");
47
- }
48
- auto result = ((utf8[offset + 0] & 0x0f) << 12) | ((utf8[offset + 1] & 0x3f) << 6) | (utf8[offset + 2] & 0x3f);
49
- offset += 3;
50
- return result;
51
- }
52
- if (!(utf8[offset + 0] & 0x08)) {
53
- if (offset + 3 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80) || !((utf8[offset + 3] & 0xc0) == 0x80)) {
54
- throw std::invalid_argument("invalid character");
55
- }
56
- auto result = ((utf8[offset + 0] & 0x07) << 18) | ((utf8[offset + 1] & 0x3f) << 12) | ((utf8[offset + 2] & 0x3f) << 6) | (utf8[offset + 3] & 0x3f);
57
- offset += 4;
58
- return result;
59
- }
60
- throw std::invalid_argument("failed to convert utf8 to codepoint");
61
- }
62
-
63
- //static std::vector<uint16_t> unicode_cpt_to_utf16(uint32_t cp) {
64
- // std::vector<uint16_t> result;
65
- // if (/* 0x0000 <= cp && */ cp <= 0xffff) {
66
- // result.emplace_back(cp);
67
- // return result;
68
- // }
69
- // if (0x10000 <= cp && cp <= 0x10ffff) {
70
- // result.emplace_back(0xd800 | ((cp - 0x10000) >> 10));
71
- // result.emplace_back(0xdc00 | ((cp - 0x10000) & 0x03ff));
72
- // return result;
73
- // }
74
- // throw std::invalid_argument("failed to convert codepoint to utf16");
75
- //}
76
-
77
- //static std::vector<uint16_t> unicode_cpts_to_utf16(const std::vector<uint32_t> & cps) {
78
- // std::vector<uint16_t> result;
79
- // for (size_t i = 0; i < cps.size(); ++i) {
80
- // auto temp = unicode_cpt_to_utf16(cps[i]);
81
- // result.insert(result.end(), temp.begin(), temp.end());
82
- // }
83
- // return result;
84
- //}
85
-
86
- //static uint32_t unicode_cpt_from_utf16(const std::vector<uint16_t> & utf16, size_t & offset) {
87
- // assert(offset < utf16.size());
88
- // if (((utf16[0] >> 10) << 10) != 0xd800) {
89
- // auto result = utf16[offset + 0];
90
- // offset += 1;
91
- // return result;
92
- // }
93
- //
94
- // if (offset + 1 >= utf16.size() || !((utf16[1] & 0xdc00) == 0xdc00)) {
95
- // throw std::invalid_argument("invalid character");
96
- // }
97
- //
98
- // auto result = 0x10000 + (((utf16[0] & 0x03ff) << 10) | (utf16[1] & 0x03ff));
99
- // offset += 2;
100
- // return result;
101
- //}
102
-
103
- //static std::vector<uint32_t> unicode_cpts_from_utf16(const std::vector<uint16_t> & utf16) {
104
- // std::vector<uint32_t> result;
105
- // size_t offset = 0;
106
- // while (offset < utf16.size()) {
107
- // result.push_back(unicode_cpt_from_utf16(utf16, offset));
108
- // }
109
- // return result;
110
- //}
111
-
112
- static std::vector<codepoint_flags> unicode_cpt_flags_array() {
113
- std::vector<codepoint_flags> cpt_flags(MAX_CODEPOINTS, codepoint_flags::UNDEFINED);
114
-
115
- assert (unicode_ranges_flags.front().first == 0);
116
- assert (unicode_ranges_flags.back().first == MAX_CODEPOINTS);
117
- for (size_t i = 1; i < unicode_ranges_flags.size(); ++i) {
118
- const auto range_ini = unicode_ranges_flags[i-1]; // codepoint_ini, flags
119
- const auto range_end = unicode_ranges_flags[i]; // codepoint_end, flags
120
- for (uint32_t cpt = range_ini.first; cpt < range_end.first; ++cpt) {
121
- cpt_flags[cpt] = range_ini.second;
122
- }
123
- }
124
-
125
- for (auto cpt : unicode_set_whitespace) {
126
- cpt_flags[cpt].is_whitespace = true;
127
- }
128
-
129
- for (auto p : unicode_map_lowercase) {
130
- cpt_flags[p.second].is_lowercase = true;
131
- }
132
-
133
- for (auto p : unicode_map_uppercase) {
134
- cpt_flags[p.second].is_uppercase = true;
135
- }
136
-
137
- for (auto &range : unicode_ranges_nfd) { // start, last, nfd
138
- cpt_flags[range.nfd].is_nfd = true;
139
- }
140
-
141
- return cpt_flags;
142
- }
143
-
144
- static std::unordered_map<uint8_t, std::string> unicode_byte_to_utf8_map() {
145
- std::unordered_map<uint8_t, std::string> map;
146
- for (int ch = 0x21; ch <= 0x7E; ++ch) { // u'!' to u'~'
147
- assert(0 <= ch && ch < 256);
148
- map[ch] = unicode_cpt_to_utf8(ch);
149
- }
150
- for (int ch = 0xA1; ch <= 0xAC; ++ch) { // u'¡' to u'¬'
151
- assert(0 <= ch && ch < 256);
152
- map[ch] = unicode_cpt_to_utf8(ch);
153
- }
154
- for (int ch = 0xAE; ch <= 0xFF; ++ch) { // u'®' to u'ÿ'
155
- assert(0 <= ch && ch < 256);
156
- map[ch] = unicode_cpt_to_utf8(ch);
157
- }
158
- auto n = 0;
159
- for (int ch = 0; ch < 256; ++ch) {
160
- if (map.find(ch) == map.end()) {
161
- map[ch] = unicode_cpt_to_utf8(256 + n);
162
- ++n;
163
- }
164
- }
165
- return map;
166
- }
167
-
168
- static std::unordered_map<std::string, uint8_t> unicode_utf8_to_byte_map() {
169
- std::unordered_map<std::string, uint8_t> map;
170
- for (int ch = 0x21; ch <= 0x7E; ++ch) { // u'!' to u'~'
171
- assert(0 <= ch && ch < 256);
172
- map[unicode_cpt_to_utf8(ch)] = ch;
173
- }
174
- for (int ch = 0xA1; ch <= 0xAC; ++ch) { // u'¡' to u'¬'
175
- assert(0 <= ch && ch < 256);
176
- map[unicode_cpt_to_utf8(ch)] = ch;
177
- }
178
- for (int ch = 0xAE; ch <= 0xFF; ++ch) { // u'®' to u'ÿ'
179
- assert(0 <= ch && ch < 256);
180
- map[unicode_cpt_to_utf8(ch)] = ch;
181
- }
182
- auto n = 0;
183
- for (int ch = 0; ch < 256; ++ch) {
184
- if (map.find(unicode_cpt_to_utf8(ch)) == map.end()) {
185
- map[unicode_cpt_to_utf8(256 + n)] = ch;
186
- ++n;
187
- }
188
- }
189
- return map;
190
- }
191
-
192
- static inline std::wstring unicode_wstring_from_utf8(const std::string & s) {
193
- std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
194
- return conv.from_bytes(s);
195
- }
196
-
197
- static std::vector<std::string> unicode_byte_encoding_process(const std::vector<std::string> & bpe_words) {
198
- std::vector<std::string> bpe_encoded_words;
199
- for (const auto & word : bpe_words) {
200
- std::string text_utf;
201
- auto utf_word = unicode_cpts_from_utf8(word);
202
- for (size_t i = 0; i < utf_word.size(); ++i) {
203
- text_utf += unicode_cpt_to_utf8(utf_word[i]);
204
- }
205
-
206
- std::string encoded_token;
207
- for (char & c : text_utf) {
208
- encoded_token += unicode_byte_to_utf8(c);
209
- }
210
- bpe_encoded_words.emplace_back(encoded_token);
211
- }
212
- return bpe_encoded_words;
213
- }
214
-
215
- // GPT2 system regex: 's|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+
216
- static std::vector<size_t> unicode_regex_split_custom_gpt2(const std::string & text, const std::vector<size_t> & offsets) {
217
- std::vector<size_t> bpe_offsets; // store the offset of each word
218
- bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size
219
-
220
- const auto cpts = unicode_cpts_from_utf8(text);
221
-
222
- size_t start = 0;
223
- for (auto offset : offsets) {
224
- const size_t offset_ini = start;
225
- const size_t offset_end = start + offset;
226
- assert(offset_end <= cpts.size());
227
- start = offset_end;
228
-
229
- static const uint32_t OUT_OF_RANGE = 0xFFFFFFFF;
230
- auto _get_cpt = [&] (const size_t pos) -> uint32_t {
231
- return (offset_ini <= pos && pos < offset_end) ? cpts[pos] : OUT_OF_RANGE;
232
- };
233
-
234
- auto _get_flags = [&] (const size_t pos) -> codepoint_flags {
235
- static const codepoint_flags undef(codepoint_flags::UNDEFINED);
236
- return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags(cpts[pos]) : undef;
237
- };
238
-
239
- size_t _prev_end = offset_ini;
240
- auto _add_token = [&] (const size_t end) -> size_t {
241
- assert(_prev_end <= end && end <= offset_end);
242
- size_t len = end - _prev_end;
243
- if (len > 0) {
244
- bpe_offsets.push_back(len);
245
- }
246
- _prev_end = end;
247
- //if (len > 0) {
248
- // std::string s = "";
249
- // for(size_t p = end-len; p < end; p++)
250
- // s += unicode_cpt_to_utf8(cpts[p]);
251
- // printf(">>> '%s'\n", s.c_str());
252
- //}
253
- return len;
254
- };
255
-
256
- for (size_t pos = offset_ini; pos < offset_end; /*pos++*/ ) {
257
- const uint32_t cpt = _get_cpt(pos);
258
- const auto flags = _get_flags(pos);
259
-
260
- // regex: 's|'t|'re|'ve|'m|'ll|'d
261
- if (cpt == '\'' && pos+1 < offset_end) {
262
- uint32_t cpt_next = _get_cpt(pos+1);
263
- if (cpt_next == 's' || cpt_next == 't' || cpt_next == 'm' || cpt_next == 'd') {
264
- pos += _add_token(pos+2);
265
- continue;
266
- }
267
- if (pos+2 < offset_end) {
268
- uint32_t cpt_next_next = _get_cpt(pos+2);
269
- if ((cpt_next == 'r' && cpt_next_next == 'e') ||
270
- (cpt_next == 'v' && cpt_next_next == 'e') ||
271
- (cpt_next == 'l' && cpt_next_next == 'l')) {
272
- pos += _add_token(pos+3);
273
- continue;
274
- }
275
- }
276
- }
277
-
278
- auto flags2 = (cpt == ' ' ? _get_flags(pos+1) : flags);
279
- // regex: <space>?\p{L}+
280
- if (flags2.is_letter) {
281
- pos += (cpt == ' ');
282
- while (flags2.is_letter) {
283
- flags2 = _get_flags(++pos);
284
- }
285
- _add_token(pos);
286
- continue;
287
- }
288
- // regex: <space>?\p{N}+
289
- if (flags2.is_number) {
290
- pos += (cpt == ' ');
291
- while (flags2.is_number) {
292
- flags2 = _get_flags(++pos);
293
- }
294
- _add_token(pos);
295
- continue;
296
- }
297
- // regex: <space>?[^\s\p{L}\p{N}]+
298
- if (!(flags2.is_whitespace || flags2.is_letter || flags2.is_number || flags2.is_undefined)) {
299
- pos += (cpt == ' ');
300
- while (!(flags2.is_whitespace || flags2.is_letter || flags2.is_number || flags2.is_undefined)) {
301
- flags2 = _get_flags(++pos);
302
- }
303
- _add_token(pos);
304
- continue;
305
- }
306
-
307
- size_t num_whitespaces = 0;
308
- while (_get_flags(pos+num_whitespaces).is_whitespace) {
309
- num_whitespaces++;
310
- }
311
-
312
- // regex: \s+(?!\S)
313
- if (num_whitespaces > 1 && _get_cpt(pos+num_whitespaces) != OUT_OF_RANGE) {
314
- pos += num_whitespaces - 1;
315
- _add_token(pos);
316
- continue;
317
- }
318
-
319
- // regex: \s+
320
- if (num_whitespaces > 0) {
321
- pos += num_whitespaces;
322
- _add_token(pos);
323
- continue;
324
- }
325
-
326
- // no matches
327
- _add_token(++pos);
328
- }
329
- }
330
-
331
- return bpe_offsets;
332
- }
333
-
334
- // LLAMA3 system regex: "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"
335
- static std::vector<size_t> unicode_regex_split_custom_llama3(const std::string & text, const std::vector<size_t> & offsets) {
336
- std::vector<size_t> bpe_offsets; // store the offset of each word
337
- bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size
338
-
339
- const auto cpts = unicode_cpts_from_utf8(text);
340
-
341
- size_t start = 0;
342
- for (auto offset : offsets) {
343
- const size_t offset_ini = start;
344
- const size_t offset_end = start + offset;
345
- assert(offset_end <= cpts.size());
346
- start = offset_end;
347
-
348
- static const uint32_t OUT_OF_RANGE = 0xFFFFFFFF;
349
- auto _get_cpt = [&] (const size_t pos) -> uint32_t {
350
- return (offset_ini <= pos && pos < offset_end) ? cpts[pos] : OUT_OF_RANGE;
351
- };
352
-
353
- auto _get_flags = [&] (const size_t pos) -> codepoint_flags {
354
- static const codepoint_flags undef(codepoint_flags::UNDEFINED);
355
- return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags(cpts[pos]) : undef;
356
- };
357
-
358
- size_t _prev_end = offset_ini;
359
- auto _add_token = [&] (const size_t end) -> size_t {
360
- assert(_prev_end <= end && end <= offset_end);
361
- size_t len = end - _prev_end;
362
- if (len > 0) {
363
- bpe_offsets.push_back(len);
364
- }
365
- _prev_end = end;
366
- //if (len > 0) {
367
- // std::string s = "";
368
- // for(size_t p = end-len; p < end; p++)
369
- // s += unicode_cpt_to_utf8(cpts[p]);
370
- // printf(">>> '%s'\n", s.c_str());
371
- //}
372
- return len;
373
- };
374
-
375
- for (size_t pos = offset_ini; pos < offset_end; /*pos++*/ ) {
376
- const uint32_t cpt = _get_cpt(pos);
377
- const auto flags = _get_flags(pos);
378
-
379
- // regex: (?i:'s|'t|'re|'ve|'m|'ll|'d) // case insensitive
380
- if (cpt == '\'' && pos+1 < offset_end) {
381
- uint32_t cpt_next = unicode_tolower(_get_cpt(pos+1));
382
- if (cpt_next == 's' || cpt_next == 't' || cpt_next == 'm' || cpt_next == 'd') {
383
- pos += _add_token(pos+2);
384
- continue;
385
- }
386
- if (pos+2 < offset_end) {
387
- uint32_t cpt_next_next = unicode_tolower(_get_cpt(pos+2));
388
- if ((cpt_next == 'r' && cpt_next_next == 'e') ||
389
- (cpt_next == 'v' && cpt_next_next == 'e') ||
390
- (cpt_next == 'l' && cpt_next_next == 'l')) {
391
- pos += _add_token(pos+3);
392
- continue;
393
- }
394
- }
395
- }
396
-
397
- // regex: [^\r\n\p{L}\p{N}]?\p{L}+ //####FIXME: the first \p{L} is correct?
398
- if (!(cpt == '\r' || cpt == '\n' || /*flags.is_letter |*/ flags.is_number)) {
399
- if (flags.is_letter || _get_flags(pos+1).is_letter) { // one or more letters
400
- pos++;
401
- while (_get_flags(pos).is_letter) {
402
- pos++;
403
- }
404
- _add_token(pos);
405
- continue;
406
- }
407
- }
408
-
409
- // regex: \p{N}{1,3}
410
- if (flags.is_number) {
411
- size_t ini = pos;
412
- while (_get_flags(pos).is_number) {
413
- if (++pos - ini >= 3 ) {
414
- _add_token(pos);
415
- ini = pos;
416
- }
417
- }
418
- _add_token(pos);
419
- continue;
420
- }
421
-
422
- // regex: <space>?[^\s\p{L}\p{N}]+[\r\n]*
423
- auto flags2 = (cpt == ' ' ? _get_flags(pos+1) : flags);
424
- if (!(flags2.is_whitespace || flags2.is_letter || flags2.is_number || flags2.is_undefined)) {
425
- pos += (cpt == ' ');
426
- while (!(flags2.is_whitespace || flags2.is_letter || flags2.is_number || flags2.is_undefined)) {
427
- flags2 = _get_flags(++pos);
428
- }
429
- uint32_t cpt2 = _get_cpt(pos);
430
- while (cpt2 == '\r' || cpt2 == '\n') {
431
- cpt2 = _get_cpt(++pos);
432
- }
433
- _add_token(pos);
434
- continue;
435
- }
436
-
437
- size_t num_whitespaces = 0;
438
- size_t last_end_r_or_n = 0;
439
- while (_get_flags(pos+num_whitespaces).is_whitespace) {
440
- uint32_t cpt2 = _get_cpt(pos+num_whitespaces);
441
- if (cpt2 == '\r' || cpt2 == '\n') {
442
- last_end_r_or_n = pos + num_whitespaces + 1;
443
- }
444
- num_whitespaces++;
445
- }
446
-
447
- // regex: \s*[\r\n]+
448
- if (last_end_r_or_n > 0) {
449
- pos = last_end_r_or_n;
450
- _add_token(pos);
451
- continue;
452
- }
453
-
454
- // regex: \s+(?!\S)
455
- if (num_whitespaces > 1 && _get_cpt(pos+num_whitespaces) != OUT_OF_RANGE) {
456
- pos += num_whitespaces - 1;
457
- _add_token(pos);
458
- continue;
459
- }
460
-
461
- // regex: \s+
462
- if (num_whitespaces > 0) {
463
- pos += num_whitespaces;
464
- _add_token(pos);
465
- continue;
466
- }
467
-
468
- // no matches
469
- _add_token(++pos);
470
- }
471
- }
472
-
473
- return bpe_offsets;
474
- }
475
-
476
- // use std::wregex to split the text
477
- static std::vector<size_t> unicode_regex_split_stl(const std::wstring & wtext, const std::wstring & regex_expr, const std::vector<size_t> & offsets) {
478
- std::wregex expr(regex_expr);
479
- std::vector<size_t> bpe_offsets; // store the offset of each word
480
- bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size
481
- size_t start = 0;
482
- for (auto offset : offsets) {
483
- std::wcregex_iterator it(wtext.data() + start, wtext.data() + start + offset, expr);
484
- std::wcregex_iterator end;
485
-
486
- int64_t start_idx = 0;
487
- while (it != end) {
488
- std::wcmatch match = *it;
489
- if (match.position() > start_idx) {
490
- bpe_offsets.emplace_back(match.position() - start_idx);
491
- }
492
- bpe_offsets.emplace_back(match.length());
493
- start_idx = match.position() + match.length();
494
- ++it;
495
- }
496
-
497
- if (start_idx < (int64_t) offset) {
498
- bpe_offsets.emplace_back(offset - start_idx);
499
- }
500
- start += offset;
501
- }
502
-
503
- return bpe_offsets;
504
- }
505
-
506
- // use std::regex to split the text
507
- static std::vector<size_t> unicode_regex_split_stl(const std::string & text, const std::string & regex_expr, const std::vector<size_t> & offsets) {
508
- std::regex expr(regex_expr);
509
- std::vector<size_t> bpe_offsets; // store the offset of each word
510
- bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size
511
- size_t start = 0;
512
- for (auto offset : offsets) {
513
- std::cregex_iterator it(text.data() + start, text.data() + start + offset, expr);
514
- std::cregex_iterator end;
515
-
516
- int64_t start_idx = 0;
517
- while (it != end) {
518
- std::cmatch match = *it;
519
- if (match.position() > start_idx) {
520
- bpe_offsets.emplace_back(match.position() - start_idx);
521
- }
522
- bpe_offsets.emplace_back(match.length());
523
- start_idx = match.position() + match.length();
524
- ++it;
525
- }
526
-
527
- if (start_idx < (int64_t) offset) {
528
- bpe_offsets.emplace_back(offset - start_idx);
529
- }
530
- start += offset;
531
- }
532
-
533
- return bpe_offsets;
534
- }
535
-
536
- static std::vector<size_t> unicode_regex_split_custom(const std::string & text, const std::string & regex_expr, const std::vector<size_t> & offsets) {
537
- std::vector<size_t> bpe_offsets;
538
-
539
- if (regex_expr == "'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)") {
540
- bpe_offsets = unicode_regex_split_custom_gpt2(text, offsets);
541
- } else if (
542
- regex_expr == "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+" ||
543
- regex_expr == "(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+") {
544
-
545
- bpe_offsets = unicode_regex_split_custom_llama3(text, offsets);
546
- }
547
-
548
- return bpe_offsets;
549
- }
550
-
551
- //
552
- // interface
553
- //
554
-
555
- std::string unicode_cpt_to_utf8(uint32_t cp) {
556
- std::string result;
557
-
558
- if (/* 0x00 <= cp && */ cp <= 0x7f) {
559
- result.push_back(cp);
560
- return result;
561
- }
562
- if (0x80 <= cp && cp <= 0x7ff) {
563
- result.push_back(0xc0 | ((cp >> 6) & 0x1f));
564
- result.push_back(0x80 | (cp & 0x3f));
565
- return result;
566
- }
567
- if (0x800 <= cp && cp <= 0xffff) {
568
- result.push_back(0xe0 | ((cp >> 12) & 0x0f));
569
- result.push_back(0x80 | ((cp >> 6) & 0x3f));
570
- result.push_back(0x80 | (cp & 0x3f));
571
- return result;
572
- }
573
- if (0x10000 <= cp && cp <= 0x10ffff) {
574
- result.push_back(0xf0 | ((cp >> 18) & 0x07));
575
- result.push_back(0x80 | ((cp >> 12) & 0x3f));
576
- result.push_back(0x80 | ((cp >> 6) & 0x3f));
577
- result.push_back(0x80 | (cp & 0x3f));
578
- return result;
579
- }
580
-
581
- throw std::invalid_argument("invalid codepoint");
582
- }
583
-
584
- std::vector<uint32_t> unicode_cpts_normalize_nfd(const std::vector<uint32_t> & cpts) {
585
- auto comp = [] (const uint32_t cpt, const range_nfd & range) {
586
- return cpt < range.first;
587
- };
588
- std::vector<uint32_t> result(cpts.size());
589
- for (size_t i = 0; i < cpts.size(); ++i) {
590
- const uint32_t cpt = cpts[i];
591
- auto it = std::upper_bound(unicode_ranges_nfd.cbegin(), unicode_ranges_nfd.cend(), cpt, comp) - 1;
592
- result[i] = (it->first <= cpt && cpt <= it->last) ? it->nfd : cpt;
593
- }
594
- return result;
595
- }
596
-
597
- std::vector<uint32_t> unicode_cpts_from_utf8(const std::string & utf8) {
598
- std::vector<uint32_t> result;
599
- result.reserve(utf8.size());
600
- size_t offset = 0;
601
- while (offset < utf8.size()) {
602
- result.push_back(unicode_cpt_from_utf8(utf8, offset));
603
- }
604
- return result;
605
- }
606
-
607
- codepoint_flags unicode_cpt_flags(const uint32_t cp) {
608
- static const codepoint_flags undef(codepoint_flags::UNDEFINED);
609
- static const auto cpt_flags = unicode_cpt_flags_array();
610
- return cp < cpt_flags.size() ? cpt_flags[cp] : undef;
611
- }
612
-
613
- codepoint_flags unicode_cpt_flags(const std::string & utf8) {
614
- static const codepoint_flags undef(codepoint_flags::UNDEFINED);
615
- if (utf8.empty()) {
616
- return undef; // undefined
617
- }
618
- size_t offset = 0;
619
- return unicode_cpt_flags(unicode_cpt_from_utf8(utf8, offset));
620
- }
621
-
622
- std::string unicode_byte_to_utf8(uint8_t byte) {
623
- static std::unordered_map<uint8_t, std::string> map = unicode_byte_to_utf8_map();
624
- return map.at(byte);
625
- }
626
-
627
- uint8_t unicode_utf8_to_byte(const std::string & utf8) {
628
- static std::unordered_map<std::string, uint8_t> map = unicode_utf8_to_byte_map();
629
- return map.at(utf8);
630
- }
631
-
632
- uint32_t unicode_tolower(uint32_t cp) {
633
- auto it = unicode_map_lowercase.find(cp);
634
- return it == unicode_map_lowercase.end() ? cp : it->second;
635
- }
636
-
637
- std::vector<std::string> unicode_regex_split(const std::string & text, const std::vector<std::string> & regex_exprs) {
638
- // unicode categories
639
- static const std::map<std::string, int> k_ucat_enum = {
640
- { "\\p{N}", codepoint_flags::NUMBER },
641
- { "\\p{L}", codepoint_flags::LETTER },
642
- { "\\p{P}", codepoint_flags::PUNCTUATION },
643
- };
644
-
645
- static const std::map<int, int> k_ucat_cpt = {
646
- { codepoint_flags::NUMBER, 0xD1 },
647
- { codepoint_flags::LETTER, 0xD2 },
648
- { codepoint_flags::PUNCTUATION, 0xD3 },
649
- };
650
-
651
- static const std::map<int, std::string> k_ucat_map = {
652
- { codepoint_flags::NUMBER, "\x30-\x39" }, // 0-9
653
- { codepoint_flags::LETTER, "\x41-\x5A\x61-\x7A" }, // A-Za-z
654
- { codepoint_flags::PUNCTUATION, "\x21-\x23\x25-\x2A\x2C-\x2F\x3A-\x3B\x3F-\x40\\\x5B-\\\x5D\x5F\\\x7B\\\x7D" }, // !-#%-*,-/:-;?-@\[-\]_\{\}
655
- };
656
-
657
- // compute collapsed codepoints only if needed by at least one regex
658
- bool need_collapse = false;
659
- for (auto & regex_expr : regex_exprs) {
660
- // search for unicode categories
661
- for (const auto & ucat : k_ucat_enum) {
662
- if (std::string::npos != regex_expr.find(ucat.first)) {
663
- need_collapse = true;
664
- break;
665
- }
666
- }
667
- }
668
-
669
- const auto cpts = unicode_cpts_from_utf8(text);
670
-
671
- // generate a "collapsed" representation of the text, where all codepoints are replaced by a single byte
672
- // ref: https://github.com/ggerganov/llama.cpp/pull/6920#issuecomment-2081479935
673
- std::string text_collapsed;
674
- if (need_collapse) {
675
- // collapse all unicode categories
676
- text_collapsed.resize(cpts.size());
677
-
678
- for (size_t i = 0; i < cpts.size(); ++i) {
679
- // keep single-byte codepoints as is
680
- if (cpts[i] < 128) {
681
- text_collapsed[i] = cpts[i];
682
- continue;
683
- }
684
-
685
- const auto flags = unicode_cpt_flags(cpts[i]);
686
-
687
- if (flags.is_whitespace) {
688
- //NOTE: C++ std::regex \s does not mach 0x85, Rust and Python regex does.
689
- //text_collapsed[i] = (char) 0x85; // <Next Line> as whitespace fallback
690
- text_collapsed[i] = (char) 0x0B; // <vertical tab> as whitespace fallback
691
- } else if (k_ucat_cpt.find(flags.category_flag()) != k_ucat_cpt.end()) {
692
- text_collapsed[i] = k_ucat_cpt.at(flags.category_flag());
693
- } else {
694
- text_collapsed[i] = (char) 0xD0; // fallback
695
- }
696
- }
697
- }
698
-
699
- std::vector<size_t> bpe_offsets = { cpts.size() };
700
-
701
- for (auto & regex_expr : regex_exprs) {
702
- // first, see if we have an efficient custom regex implementation
703
- auto tmp = unicode_regex_split_custom(text, regex_expr, bpe_offsets);
704
-
705
- if (!tmp.empty()) {
706
- bpe_offsets = std::move(tmp);
707
- continue;
708
- }
709
-
710
- // fallback to general-purpose std::regex / std::wregex
711
- try {
712
- // if a unicode category is used in the regex, we use the collapsed text and replace the unicode category
713
- // with the corresponding collapsed representation
714
- bool use_collapsed = false;
715
- for (auto & ucat : k_ucat_enum) {
716
- if (std::string::npos != regex_expr.find(ucat.first)) {
717
- use_collapsed = true;
718
- break;
719
- }
720
- }
721
-
722
- if (use_collapsed) {
723
- // sanity-check that the original regex does not contain any non-ASCII characters
724
- const auto cpts_regex = unicode_cpts_from_utf8(regex_expr);
725
- for (size_t i = 0; i < cpts_regex.size(); ++i) {
726
- if (cpts_regex[i] >= 128) {
727
- throw std::runtime_error("Regex includes both unicode categories and non-ASCII characters - not supported");
728
- }
729
- }
730
-
731
- // generate a collapsed representation of the regex
732
- std::string regex_expr_collapsed;
733
-
734
- // track if we are inside [], because nested [] are not allowed
735
- bool inside = false;
736
- for (size_t i = 0; i < regex_expr.size(); ++i) {
737
- if (regex_expr[i] == '[' && (i == 0 || regex_expr[i - 1] != '\\')) {
738
- regex_expr_collapsed += '[';
739
- inside = true;
740
- continue;
741
- }
742
-
743
- if (inside && regex_expr[i] == ']' && regex_expr[i - 1] != '\\') {
744
- regex_expr_collapsed += ']';
745
- inside = false;
746
- continue;
747
- }
748
-
749
- if (regex_expr[i + 0] == '\\' && i + 4 < regex_expr.size() &&
750
- regex_expr[i + 1] == 'p' &&
751
- regex_expr[i + 2] == '{' &&
752
- regex_expr[i + 4] == '}') {
753
- const std::string pat = regex_expr.substr(i, 5);
754
- if (k_ucat_enum.find(pat) != k_ucat_enum.end()) {
755
- if (!inside) {
756
- regex_expr_collapsed += '[';
757
- }
758
- regex_expr_collapsed += k_ucat_cpt.at(k_ucat_enum.at(pat));
759
- regex_expr_collapsed += k_ucat_map.at(k_ucat_enum.at(pat));
760
- if (!inside) {
761
- regex_expr_collapsed += ']';
762
- }
763
- i += 4;
764
- continue;
765
- }
766
- }
767
-
768
- regex_expr_collapsed += regex_expr[i];
769
- }
770
-
771
- //printf("text_collapsed: %s\n", text_collapsed.c_str());
772
- //printf("regex_expr_collapsed: %s\n", regex_expr_collapsed.c_str());
773
- bpe_offsets = unicode_regex_split_stl(text_collapsed, regex_expr_collapsed, bpe_offsets);
774
- } else {
775
- // no unicode category used, we can use std::wregex directly
776
- const std::wstring wregex_expr = unicode_wstring_from_utf8(regex_expr);
777
-
778
- // std::wregex \s does not mach non-ASCII whitespaces, using 0x0B as fallback
779
- std::wstring wtext(cpts.begin(), cpts.end());
780
- for (size_t i = 0; i < wtext.size(); ++i) {
781
- if (wtext[i] > 0x7F && unicode_cpt_flags(wtext[i]).is_whitespace) {
782
- wtext[i] = 0x0B;
783
- }
784
- }
785
-
786
- //printf("text: %s\n", text.c_str());
787
- //printf("regex_expr: %s\n", regex_expr.c_str());
788
- bpe_offsets = unicode_regex_split_stl(wtext, wregex_expr, bpe_offsets);
789
- }
790
- } catch (std::regex_error & e) {
791
- fprintf(stderr, "Failed to process regex: '%s'\n", regex_expr.c_str());
792
- fprintf(stderr, "Regex error: %s\n", e.what());
793
- throw std::runtime_error("Failed to process regex");
794
- }
795
- }
796
-
797
- std::vector<std::string> bpe_words;
798
- bpe_words.reserve(bpe_offsets.size()); // reserve memory for the approximate size
799
-
800
- size_t start = 0;
801
- for (size_t & offset : bpe_offsets) {
802
- bpe_words.emplace_back();
803
- for (size_t i = start; i < start + offset; ++i) {
804
- bpe_words.back() += unicode_cpt_to_utf8(cpts[i]);
805
- }
806
- start += offset;
807
- }
808
-
809
- return unicode_byte_encoding_process(bpe_words);
810
- }