@fugood/llama.node 0.3.3 → 0.3.4

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 (225) hide show
  1. package/CMakeLists.txt +5 -0
  2. package/bin/darwin/arm64/llama-node.node +0 -0
  3. package/bin/darwin/x64/llama-node.node +0 -0
  4. package/bin/linux/arm64/llama-node.node +0 -0
  5. package/bin/linux/x64/llama-node.node +0 -0
  6. package/bin/linux-vulkan/arm64/llama-node.node +0 -0
  7. package/bin/linux-vulkan/x64/llama-node.node +0 -0
  8. package/bin/win32/arm64/llama-node.node +0 -0
  9. package/bin/win32/arm64/node.lib +0 -0
  10. package/bin/win32/x64/llama-node.node +0 -0
  11. package/bin/win32/x64/node.lib +0 -0
  12. package/bin/win32-vulkan/arm64/llama-node.node +0 -0
  13. package/bin/win32-vulkan/arm64/node.lib +0 -0
  14. package/bin/win32-vulkan/x64/llama-node.node +0 -0
  15. package/bin/win32-vulkan/x64/node.lib +0 -0
  16. package/lib/binding.ts +18 -1
  17. package/package.json +1 -1
  18. package/src/EmbeddingWorker.cpp +15 -5
  19. package/src/EmbeddingWorker.h +2 -1
  20. package/src/LlamaCompletionWorker.cpp +1 -1
  21. package/src/LlamaContext.cpp +81 -18
  22. package/src/LlamaContext.h +2 -0
  23. package/src/llama.cpp/.github/workflows/build.yml +197 -159
  24. package/src/llama.cpp/.github/workflows/docker.yml +5 -8
  25. package/src/llama.cpp/.github/workflows/python-lint.yml +8 -1
  26. package/src/llama.cpp/.github/workflows/server.yml +21 -14
  27. package/src/llama.cpp/CMakeLists.txt +11 -6
  28. package/src/llama.cpp/Sources/llama/llama.h +4 -0
  29. package/src/llama.cpp/cmake/common.cmake +33 -0
  30. package/src/llama.cpp/cmake/x64-windows-llvm.cmake +11 -0
  31. package/src/llama.cpp/common/CMakeLists.txt +6 -2
  32. package/src/llama.cpp/common/arg.cpp +426 -245
  33. package/src/llama.cpp/common/common.cpp +143 -80
  34. package/src/llama.cpp/common/common.h +81 -24
  35. package/src/llama.cpp/common/sampling.cpp +53 -19
  36. package/src/llama.cpp/common/sampling.h +22 -1
  37. package/src/llama.cpp/common/speculative.cpp +274 -0
  38. package/src/llama.cpp/common/speculative.h +28 -0
  39. package/src/llama.cpp/docs/build.md +101 -148
  40. package/src/llama.cpp/examples/CMakeLists.txt +32 -13
  41. package/src/llama.cpp/examples/batched/CMakeLists.txt +1 -1
  42. package/src/llama.cpp/examples/batched/batched.cpp +5 -4
  43. package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +1 -1
  44. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +1 -1
  45. package/src/llama.cpp/examples/cvector-generator/CMakeLists.txt +1 -1
  46. package/src/llama.cpp/examples/deprecation-warning/deprecation-warning.cpp +1 -1
  47. package/src/llama.cpp/examples/embedding/CMakeLists.txt +1 -1
  48. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +3 -2
  49. package/src/llama.cpp/examples/export-lora/CMakeLists.txt +1 -1
  50. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +1 -1
  51. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +4 -7
  52. package/src/llama.cpp/examples/gen-docs/CMakeLists.txt +1 -1
  53. package/src/llama.cpp/examples/gguf/CMakeLists.txt +1 -1
  54. package/src/llama.cpp/examples/gguf-hash/CMakeLists.txt +8 -1
  55. package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +1 -1
  56. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +2 -2
  57. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +1 -1
  58. package/src/llama.cpp/examples/gritlm/gritlm.cpp +1 -1
  59. package/src/llama.cpp/examples/imatrix/CMakeLists.txt +1 -1
  60. package/src/llama.cpp/examples/imatrix/imatrix.cpp +11 -2
  61. package/src/llama.cpp/examples/infill/CMakeLists.txt +1 -1
  62. package/src/llama.cpp/examples/infill/infill.cpp +1 -1
  63. package/src/llama.cpp/examples/llama-bench/CMakeLists.txt +1 -1
  64. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +405 -316
  65. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
  66. package/src/llama.cpp/examples/llava/CMakeLists.txt +10 -3
  67. package/src/llama.cpp/examples/llava/clip.cpp +262 -66
  68. package/src/llama.cpp/examples/llava/clip.h +8 -2
  69. package/src/llama.cpp/examples/llava/llava-cli.cpp +1 -1
  70. package/src/llama.cpp/examples/llava/llava.cpp +46 -19
  71. package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +1 -1
  72. package/src/llama.cpp/examples/llava/qwen2vl-cli.cpp +581 -0
  73. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +1 -1
  74. package/src/llama.cpp/examples/lookahead/lookahead.cpp +1 -1
  75. package/src/llama.cpp/examples/lookup/CMakeLists.txt +4 -4
  76. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +2 -1
  77. package/src/llama.cpp/examples/lookup/lookup.cpp +2 -2
  78. package/src/llama.cpp/examples/main/CMakeLists.txt +1 -1
  79. package/src/llama.cpp/examples/main/main.cpp +9 -5
  80. package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +1 -1
  81. package/src/llama.cpp/examples/parallel/CMakeLists.txt +1 -1
  82. package/src/llama.cpp/examples/parallel/parallel.cpp +1 -1
  83. package/src/llama.cpp/examples/passkey/CMakeLists.txt +1 -1
  84. package/src/llama.cpp/examples/perplexity/CMakeLists.txt +1 -1
  85. package/src/llama.cpp/examples/quantize/CMakeLists.txt +1 -1
  86. package/src/llama.cpp/examples/quantize/quantize.cpp +0 -3
  87. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +1 -1
  88. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +1 -1
  89. package/src/llama.cpp/examples/retrieval/retrieval.cpp +4 -4
  90. package/src/llama.cpp/examples/run/CMakeLists.txt +5 -0
  91. package/src/llama.cpp/examples/run/run.cpp +911 -0
  92. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +1 -1
  93. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +4 -4
  94. package/src/llama.cpp/examples/server/CMakeLists.txt +3 -7
  95. package/src/llama.cpp/examples/server/server.cpp +1758 -886
  96. package/src/llama.cpp/examples/server/tests/requirements.txt +2 -2
  97. package/src/llama.cpp/examples/server/utils.hpp +94 -304
  98. package/src/llama.cpp/examples/simple/CMakeLists.txt +1 -1
  99. package/src/llama.cpp/examples/simple/simple.cpp +4 -0
  100. package/src/llama.cpp/examples/simple-chat/CMakeLists.txt +1 -1
  101. package/src/llama.cpp/examples/simple-chat/simple-chat.cpp +3 -0
  102. package/src/llama.cpp/examples/speculative/CMakeLists.txt +1 -1
  103. package/src/llama.cpp/examples/speculative/speculative.cpp +16 -15
  104. package/src/llama.cpp/examples/speculative-simple/CMakeLists.txt +5 -0
  105. package/src/llama.cpp/examples/speculative-simple/speculative-simple.cpp +265 -0
  106. package/src/llama.cpp/examples/tokenize/CMakeLists.txt +1 -1
  107. package/src/llama.cpp/examples/tokenize/tokenize.cpp +1 -1
  108. package/src/llama.cpp/examples/tts/CMakeLists.txt +5 -0
  109. package/src/llama.cpp/examples/tts/tts.cpp +932 -0
  110. package/src/llama.cpp/ggml/CMakeLists.txt +46 -34
  111. package/src/llama.cpp/ggml/include/ggml-backend.h +16 -0
  112. package/src/llama.cpp/ggml/include/ggml-cpu.h +7 -49
  113. package/src/llama.cpp/ggml/include/ggml-opencl.h +26 -0
  114. package/src/llama.cpp/ggml/include/ggml.h +106 -24
  115. package/src/llama.cpp/ggml/src/CMakeLists.txt +73 -24
  116. package/src/llama.cpp/ggml/src/ggml-alloc.c +0 -1
  117. package/src/llama.cpp/ggml/src/ggml-backend-impl.h +51 -11
  118. package/src/llama.cpp/ggml/src/ggml-backend-reg.cpp +379 -22
  119. package/src/llama.cpp/ggml/src/ggml-backend.cpp +4 -4
  120. package/src/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +3 -7
  121. package/src/llama.cpp/ggml/src/ggml-blas/ggml-blas.cpp +5 -2
  122. package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +33 -3
  123. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +456 -111
  124. package/src/llama.cpp/ggml/src/ggml-cann/common.h +6 -3
  125. package/src/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +95 -35
  126. package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +2 -5
  127. package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +22 -9
  128. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +24 -13
  129. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +23 -13
  130. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +11 -0
  131. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +10 -0
  132. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +10 -0
  133. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +17 -0
  134. package/src/llama.cpp/ggml/src/ggml-common.h +42 -42
  135. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +288 -213
  136. package/src/llama.cpp/ggml/src/ggml-cpu/amx/amx.cpp +220 -0
  137. package/src/llama.cpp/ggml/src/ggml-cpu/amx/amx.h +8 -0
  138. package/src/llama.cpp/ggml/src/{ggml-amx → ggml-cpu/amx}/common.h +19 -22
  139. package/src/llama.cpp/ggml/src/{ggml-amx → ggml-cpu/amx}/mmq.cpp +93 -92
  140. package/src/llama.cpp/ggml/src/{ggml-amx → ggml-cpu/amx}/mmq.h +2 -9
  141. package/src/llama.cpp/ggml/src/ggml-cpu/cpu-feats-x86.cpp +323 -0
  142. package/src/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-aarch64.c → ggml-cpu-aarch64.cpp} +892 -190
  143. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +2 -24
  144. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-hbm.cpp +55 -0
  145. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-hbm.h +8 -0
  146. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +15 -0
  147. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +38 -25
  148. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-traits.cpp +36 -0
  149. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-traits.h +38 -0
  150. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +552 -399
  151. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +101 -136
  152. package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +2 -2
  153. package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +7 -10
  154. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +8 -0
  155. package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +4 -6
  156. package/src/llama.cpp/ggml/src/ggml-impl.h +32 -11
  157. package/src/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +13 -9
  158. package/src/llama.cpp/ggml/src/ggml-kompute/ggml-kompute.cpp +131 -64
  159. package/src/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +3 -6
  160. package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +39 -0
  161. package/src/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +14 -7
  162. package/src/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +147 -0
  163. package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +4004 -0
  164. package/src/llama.cpp/ggml/src/ggml-opt.cpp +67 -80
  165. package/src/llama.cpp/ggml/src/ggml-quants.c +0 -9
  166. package/src/llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt +3 -5
  167. package/src/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +5 -2
  168. package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +13 -10
  169. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +2 -11
  170. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +1 -0
  171. package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +2 -2
  172. package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +1 -1
  173. package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +5 -5
  174. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +32 -13
  175. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +80 -61
  176. package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +4 -4
  177. package/src/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +159 -114
  178. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +3 -2
  179. package/src/llama.cpp/ggml/src/ggml-sycl/mmq.cpp +6 -6
  180. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +6 -20
  181. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +4 -3
  182. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +8 -8
  183. package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +4 -3
  184. package/src/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +7 -7
  185. package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +1 -0
  186. package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.cpp +4 -1
  187. package/src/llama.cpp/ggml/src/ggml-threading.h +4 -2
  188. package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +21 -7
  189. package/src/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +1718 -399
  190. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +3 -1
  191. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +105 -31
  192. package/src/llama.cpp/ggml/src/ggml.c +367 -207
  193. package/src/llama.cpp/include/llama-cpp.h +25 -0
  194. package/src/llama.cpp/include/llama.h +26 -19
  195. package/src/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.inp +112 -0
  196. package/src/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.out +46 -0
  197. package/src/llama.cpp/pocs/CMakeLists.txt +3 -1
  198. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +2 -2
  199. package/src/llama.cpp/src/CMakeLists.txt +2 -7
  200. package/src/llama.cpp/src/llama-grammar.cpp +15 -15
  201. package/src/llama.cpp/src/llama-grammar.h +2 -5
  202. package/src/llama.cpp/src/llama-sampling.cpp +35 -90
  203. package/src/llama.cpp/src/llama-vocab.cpp +6 -1
  204. package/src/llama.cpp/src/llama.cpp +1748 -640
  205. package/src/llama.cpp/src/unicode.cpp +62 -51
  206. package/src/llama.cpp/src/unicode.h +9 -10
  207. package/src/llama.cpp/tests/CMakeLists.txt +48 -37
  208. package/src/llama.cpp/tests/test-arg-parser.cpp +2 -2
  209. package/src/llama.cpp/tests/test-backend-ops.cpp +140 -21
  210. package/src/llama.cpp/tests/test-chat-template.cpp +50 -4
  211. package/src/llama.cpp/tests/test-gguf.cpp +1303 -0
  212. package/src/llama.cpp/tests/test-grammar-integration.cpp +3 -6
  213. package/src/llama.cpp/tests/test-llama-grammar.cpp +2 -4
  214. package/src/llama.cpp/tests/test-quantize-fns.cpp +3 -3
  215. package/src/llama.cpp/tests/test-rope.cpp +61 -20
  216. package/src/llama.cpp/tests/test-sampling.cpp +2 -2
  217. package/src/llama.cpp/.github/workflows/nix-ci-aarch64.yml +0 -72
  218. package/src/llama.cpp/.github/workflows/nix-ci.yml +0 -79
  219. package/src/llama.cpp/.github/workflows/nix-flake-update.yml +0 -22
  220. package/src/llama.cpp/.github/workflows/nix-publish-flake.yml +0 -36
  221. package/src/llama.cpp/ggml/include/ggml-amx.h +0 -25
  222. package/src/llama.cpp/ggml/src/ggml-aarch64.c +0 -129
  223. package/src/llama.cpp/ggml/src/ggml-aarch64.h +0 -19
  224. package/src/llama.cpp/ggml/src/ggml-amx/CMakeLists.txt +0 -107
  225. package/src/llama.cpp/ggml/src/ggml-amx/ggml-amx.cpp +0 -446
@@ -0,0 +1,932 @@
1
+ #include "arg.h"
2
+ #include "common.h"
3
+ #include "sampling.h"
4
+ #include "log.h"
5
+ #include "llama.h"
6
+
7
+ #define _USE_MATH_DEFINES // For M_PI on MSVC
8
+
9
+ #include <algorithm>
10
+ #include <cmath>
11
+ #include <cstdio>
12
+ #include <fstream>
13
+ #include <map>
14
+ #include <regex>
15
+ #include <string>
16
+ #include <thread>
17
+ #include <vector>
18
+
19
+ //
20
+ // Terminal utils
21
+ //
22
+
23
+ #define SQR(X) ((X) * (X))
24
+ #define UNCUBE(x) x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40
25
+
26
+ /**
27
+ * Quantizes 24-bit RGB to xterm256 code range [16,256).
28
+ */
29
+ static int rgb2xterm256(int r, int g, int b) {
30
+ unsigned char cube[] = {0, 0137, 0207, 0257, 0327, 0377};
31
+ int av, ir, ig, ib, il, qr, qg, qb, ql;
32
+ av = r * .299 + g * .587 + b * .114 + .5;
33
+ ql = (il = av > 238 ? 23 : (av - 3) / 10) * 10 + 8;
34
+ qr = cube[(ir = UNCUBE(r))];
35
+ qg = cube[(ig = UNCUBE(g))];
36
+ qb = cube[(ib = UNCUBE(b))];
37
+ if (SQR(qr - r) + SQR(qg - g) + SQR(qb - b) <=
38
+ SQR(ql - r) + SQR(ql - g) + SQR(ql - b))
39
+ return ir * 36 + ig * 6 + ib + 020;
40
+ return il + 0350;
41
+ }
42
+
43
+ static std::string set_xterm256_foreground(int r, int g, int b) {
44
+ int x = rgb2xterm256(r, g, b);
45
+ std::ostringstream oss;
46
+ oss << "\033[38;5;" << x << "m";
47
+ return oss.str();
48
+ }
49
+
50
+ const std::vector<std::string> k_colors = {
51
+ set_xterm256_foreground(220, 5, 12),
52
+ set_xterm256_foreground(232, 96, 28),
53
+ set_xterm256_foreground(241, 147, 45),
54
+ set_xterm256_foreground(246, 193, 65),
55
+ set_xterm256_foreground(247, 240, 86),
56
+ set_xterm256_foreground(144, 201, 135),
57
+ set_xterm256_foreground( 78, 178, 101),
58
+ };
59
+
60
+ static void print_usage(int, char ** argv) {
61
+ LOG("\nexample usage:\n");
62
+ LOG("\n %s -m model.gguf -p \"Hello!\"\n", argv[0]);
63
+ LOG("\n");
64
+ }
65
+
66
+ struct wav_header {
67
+ char riff[4] = {'R', 'I', 'F', 'F'};
68
+ uint32_t chunk_size;
69
+ char wave[4] = {'W', 'A', 'V', 'E'};
70
+ char fmt[4] = {'f', 'm', 't', ' '};
71
+ uint32_t fmt_chunk_size = 16;
72
+ uint16_t audio_format = 1; // PCM
73
+ uint16_t num_channels = 1; // Mono
74
+ uint32_t sample_rate;
75
+ uint32_t byte_rate;
76
+ uint16_t block_align;
77
+ uint16_t bits_per_sample = 16;
78
+ char data[4] = {'d', 'a', 't', 'a'};
79
+ uint32_t data_size;
80
+ };
81
+
82
+ static void save_wav16(const std::string & fname, const std::vector<float> & data, int sample_rate) {
83
+ std::ofstream file(fname, std::ios::binary);
84
+ if (!file) {
85
+ LOG_ERR("%s: Failed to open file '%s' for writing", __func__, fname.c_str());
86
+ return;
87
+ }
88
+
89
+ wav_header header;
90
+ header.sample_rate = sample_rate;
91
+ header.byte_rate = header.sample_rate * header.num_channels * (header.bits_per_sample / 8);
92
+ header.block_align = header.num_channels * (header.bits_per_sample / 8);
93
+ header.data_size = data.size() * (header.bits_per_sample / 8);
94
+ header.chunk_size = 36 + header.data_size;
95
+
96
+ file.write(reinterpret_cast<const char*>(&header), sizeof(header));
97
+
98
+ for (const auto & sample : data) {
99
+ int16_t pcm_sample = static_cast<int16_t>(std::clamp(sample * 32767.0, -32768.0, 32767.0));
100
+ file.write(reinterpret_cast<const char*>(&pcm_sample), sizeof(pcm_sample));
101
+ }
102
+
103
+ file.close();
104
+ }
105
+
106
+ static void fill_hann_window(int length, bool periodic, float * output) {
107
+ int offset = -1;
108
+ if (periodic) {
109
+ offset = 0;
110
+ }
111
+ for (int i = 0; i < length; i++) {
112
+ output[i] = 0.5 * (1.0 - cosf((2.0 * M_PI * i) / (length + offset)));
113
+ }
114
+ }
115
+
116
+ // very poor-man fft
117
+ static void twiddle(float * real, float * imag, int k, int N) {
118
+ float angle = 2 * M_PI * k / N;
119
+ *real = cos(angle);
120
+ *imag = sin(angle);
121
+ }
122
+
123
+ static void irfft(int n, const float * inp_cplx, float * out_real) {
124
+ int N = n / 2 + 1;
125
+
126
+ std::vector<float> real_input(N);
127
+ std::vector<float> imag_input(N);
128
+ for (int i = 0; i < N; ++i) {
129
+ real_input[i] = inp_cplx[2 * i];
130
+ imag_input[i] = inp_cplx[2 * i + 1];
131
+ }
132
+
133
+ std::vector<float> real_output(n);
134
+ std::vector<float> imag_output(n);
135
+
136
+ for (int k = 0; k < n; ++k) {
137
+ real_output[k] = 0.0f;
138
+ imag_output[k] = 0.0f;
139
+ for (int m = 0; m < N; ++m) {
140
+ float twiddle_real;
141
+ float twiddle_imag;
142
+
143
+ twiddle(&twiddle_real, &twiddle_imag, k * m, n);
144
+
145
+ real_output[k] += real_input[m] * twiddle_real - imag_input[m] * twiddle_imag;
146
+ imag_output[k] += real_input[m] * twiddle_imag + imag_input[m] * twiddle_real;
147
+ }
148
+ }
149
+
150
+ for (int i = 0; i < n; ++i) {
151
+ out_real[i] = real_output[i] / N;
152
+ }
153
+ }
154
+
155
+ //
156
+ // y = torch.nn.functional.fold(
157
+ // data, output_size=(1, output_size), kernel_size=(1, self.win_length), stride=(1, self.hop_length),
158
+ // )[:, 0, 0, pad:-pad]
159
+ //
160
+ // data.shape = torch.Size([1, 1280, 261])
161
+ // output_size = 84480
162
+ // win_length = 1280
163
+ // hop_length = 320
164
+ // pad = 480
165
+ //
166
+ static void fold(const std::vector<float> & data, int64_t n_out, int64_t n_win, int64_t n_hop, int64_t n_pad, std::vector<float> & output) {
167
+ int64_t output_height = n_out;
168
+ int64_t kernel_w = n_win;
169
+ int64_t stride_w = n_hop;
170
+ int64_t width = n_out;
171
+
172
+ output.resize(width, 0.0f);
173
+
174
+ int64_t col_idx = 0;
175
+ for (int64_t w_col = 0; w_col < width; ++w_col) {
176
+ int64_t start = w_col * stride_w - n_pad;
177
+ int64_t end = start + kernel_w;
178
+
179
+ for (int64_t w_im = start; w_im < end; ++w_im) {
180
+ if (w_im >= 0 && w_im < output_height && col_idx < (int64_t) data.size()) {
181
+ output[w_im] += data[col_idx];
182
+ }
183
+ col_idx++;
184
+ }
185
+ }
186
+
187
+ output.resize(n_out - 2 * n_pad);
188
+ }
189
+
190
+ // TODO: not optimized at all
191
+ static std::vector<float> embd_to_audio(
192
+ const float * embd,
193
+ const int n_codes,
194
+ const int n_embd,
195
+ const int n_thread) {
196
+ const int n_fft = 1280;
197
+ const int n_hop = 320;
198
+ const int n_win = 1280;
199
+ const int n_pad = (n_win - n_hop)/2;
200
+ const int n_out = (n_codes - 1)*n_hop + n_win;
201
+
202
+ std::vector<float> hann(n_fft);
203
+
204
+ fill_hann_window(hann.size(), true, hann.data());
205
+
206
+ int n_spec = n_embd*n_codes;
207
+
208
+ std::vector<float> E (n_spec);
209
+ std::vector<float> S (n_spec);
210
+ std::vector<float> ST(n_spec);
211
+
212
+ for (int l = 0; l < n_codes; ++l) {
213
+ for (int k = 0; k < n_embd; ++k) {
214
+ E[k*n_codes + l] = embd[l*n_embd + k];
215
+ }
216
+ }
217
+
218
+ for (int k = 0; k < n_embd/2; ++k) {
219
+ for (int l = 0; l < n_codes; ++l) {
220
+ float mag = E[(k )*n_codes + l];
221
+ float phi = E[(k + n_embd/2)*n_codes + l];
222
+
223
+ mag = exp(mag);
224
+
225
+ if (mag > 1e2) {
226
+ mag = 1e2;
227
+ }
228
+ S[2*(k*n_codes + l) + 0] = mag*cosf(phi);
229
+ S[2*(k*n_codes + l) + 1] = mag*sinf(phi);
230
+ }
231
+ }
232
+
233
+ for (int l = 0; l < n_codes; ++l) {
234
+ for (int k = 0; k < n_embd/2; ++k) {
235
+ ST[l*n_embd + 2*k + 0] = S[2*(k*n_codes + l) + 0];
236
+ ST[l*n_embd + 2*k + 1] = S[2*(k*n_codes + l) + 1];
237
+ }
238
+ }
239
+
240
+ std::vector<float> res (n_codes*n_fft);
241
+ std::vector<float> hann2(n_codes*n_fft);
242
+
243
+ std::vector<std::thread> workers(n_thread);
244
+ for (int i = 0; i < n_thread; ++i) {
245
+ workers[i] = std::thread([&, i]() {
246
+ for (int l = i; l < n_codes; l += n_thread) {
247
+ irfft(n_fft, ST.data() + l*n_embd, res.data() + l*n_fft);
248
+ for (int j = 0; j < n_fft; ++j) {
249
+ res [l*n_fft + j] *= hann[j];
250
+ hann2[l*n_fft + j] = hann[j] * hann[j];
251
+ }
252
+ }
253
+ });
254
+ }
255
+ for (int i = 0; i < n_thread; ++i) {
256
+ workers[i].join();
257
+ }
258
+
259
+ std::vector<float> audio;
260
+ std::vector<float> env;
261
+
262
+ fold(res, n_out, n_win, n_hop, n_pad, audio);
263
+ fold(hann2, n_out, n_win, n_hop, n_pad, env); // TODO: can be done once
264
+
265
+ for (size_t i = 0; i < audio.size(); ++i) {
266
+ audio[i] /= env[i];
267
+ }
268
+
269
+ return audio;
270
+ }
271
+
272
+ static const std::map<int, std::string> ones = {
273
+ {0, "zero"}, {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"},
274
+ {5, "five"}, {6, "six"}, {7, "seven"}, {8, "eight"}, {9, "nine"},
275
+ {10, "ten"}, {11, "eleven"}, {12, "twelve"}, {13, "thirteen"}, {14, "fourteen"},
276
+ {15, "fifteen"}, {16, "sixteen"}, {17, "seventeen"}, {18, "eighteen"}, {19, "nineteen"}
277
+ };
278
+
279
+ static const std::map<int, std::string> tens = {
280
+ {2, "twenty"}, {3, "thirty"}, {4, "forty"}, {5, "fifty"},
281
+ {6, "sixty"}, {7, "seventy"}, {8, "eighty"}, {9, "ninety"}
282
+ };
283
+
284
+ // Convert a number less than 1000 to words
285
+ static std::string convert_less_than_thousand(int num) {
286
+ std::string result;
287
+
288
+ if (num >= 100) {
289
+ result += ones.at(num / 100) + " hundred ";
290
+ num %= 100;
291
+ }
292
+
293
+ if (num >= 20) {
294
+ result += tens.at(num / 10);
295
+ if (num % 10 > 0) {
296
+ result += "-" + ones.at(num % 10);
297
+ }
298
+ } else if (num > 0) {
299
+ result += ones.at(num);
300
+ }
301
+
302
+ return result;
303
+ }
304
+
305
+ static std::string number_to_words(const std::string & number_str) {
306
+ try {
307
+ size_t decimal_pos = number_str.find('.');
308
+ std::string integer_part = number_str.substr(0, decimal_pos);
309
+
310
+ int int_number = std::stoi(integer_part);
311
+ std::string result;
312
+
313
+ if (int_number == 0) {
314
+ result = "zero";
315
+ } else {
316
+ if (int_number >= 1000000000) {
317
+ int billions = int_number / 1000000000;
318
+ result += convert_less_than_thousand(billions) + " billion ";
319
+ int_number %= 1000000000;
320
+ }
321
+
322
+ if (int_number >= 1000000) {
323
+ int millions = int_number / 1000000;
324
+ result += convert_less_than_thousand(millions) + " million ";
325
+ int_number %= 1000000;
326
+ }
327
+
328
+ if (int_number >= 1000) {
329
+ int thousands = int_number / 1000;
330
+ result += convert_less_than_thousand(thousands) + " thousand ";
331
+ int_number %= 1000;
332
+ }
333
+
334
+ if (int_number > 0) {
335
+ result += convert_less_than_thousand(int_number);
336
+ }
337
+ }
338
+
339
+ // Handle decimal part
340
+ if (decimal_pos != std::string::npos) {
341
+ result += " point";
342
+ std::string decimal_part = number_str.substr(decimal_pos + 1);
343
+ for (char digit : decimal_part) {
344
+ result += " " + ones.at(digit - '0');
345
+ }
346
+ }
347
+
348
+ return result;
349
+ } catch (const std::exception& e) {
350
+ // Skip if fails
351
+ return " ";
352
+ }
353
+ }
354
+
355
+ static std::string replace_numbers_with_words(const std::string & input_text) {
356
+ std::regex number_pattern(R"(\d+(\.\d+)?)");
357
+ std::string result;
358
+ auto it = std::sregex_iterator(input_text.begin(), input_text.end(), number_pattern);
359
+ auto end = std::sregex_iterator();
360
+
361
+ size_t last_pos = 0;
362
+ for (std::sregex_iterator i = it; i != end; ++i) {
363
+ const std::smatch& match = *i;
364
+ result.append(input_text, last_pos, match.position() - last_pos);
365
+ result.append(number_to_words(match.str()));
366
+ last_pos = match.position() + match.length();
367
+ }
368
+ result.append(input_text, last_pos);
369
+
370
+ return result;
371
+ }
372
+
373
+ // Based on: https://github.com/edwko/OuteTTS/blob/a613e79c489d8256dd657ea9168d78de75895d82/outetts/version/v1/prompt_processor.py#L39
374
+ static std::string process_text(const std::string & text) {
375
+
376
+ // For now I skipped text romanization as I am unsure how to handle
377
+ // uroman and MeCab implementations in C++
378
+ // maybe something like https://github.com/anyascii/anyascii/ could work.
379
+ // currently only English would be supported in this function
380
+
381
+ std::string processed_text = replace_numbers_with_words(text);
382
+
383
+ std::transform(processed_text.begin(), processed_text.end(),
384
+ processed_text.begin(), ::tolower);
385
+
386
+ std::regex special_chars(R"([-_/,\.\\])");
387
+ processed_text = std::regex_replace(processed_text, special_chars, " ");
388
+
389
+ std::regex non_alpha(R"([^a-z\s])");
390
+ processed_text = std::regex_replace(processed_text, non_alpha, "");
391
+
392
+ std::regex multiple_spaces(R"(\s+)");
393
+ processed_text = std::regex_replace(processed_text, multiple_spaces, " ");
394
+
395
+ processed_text = std::regex_replace(processed_text, std::regex(R"(^\s+|\s+$)"), "");
396
+
397
+ /*
398
+ Replace spaces with the separator token same as in line 365
399
+
400
+ for (auto & c : prompt_user) {
401
+ if (c == ' ') {
402
+ prompt_clean += "<|text_sep|>";
403
+ */
404
+ processed_text = std::regex_replace(processed_text, std::regex(R"(\s)"), "<|text_sep|>");
405
+
406
+ return processed_text;
407
+ }
408
+
409
+ static void prompt_add(llama_tokens & prompt, llama_token token) {
410
+ prompt.push_back(token);
411
+ }
412
+
413
+ static void prompt_add(llama_tokens & prompt, const llama_tokens & tokens) {
414
+ prompt.insert(prompt.end(), tokens.begin(), tokens.end());
415
+ }
416
+
417
+ static void prompt_add(llama_tokens & prompt, const llama_model * model, const std::string & txt, bool add_special, bool parse_special) {
418
+ auto tmp = common_tokenize(model, txt, add_special, parse_special);
419
+ prompt_add(prompt, tmp);
420
+ }
421
+
422
+ static void prompt_init(llama_tokens & prompt, const llama_model * model) {
423
+ prompt.clear();
424
+
425
+ prompt_add(prompt, model, "<|im_start|>\n", true, true);
426
+ }
427
+
428
+ int main(int argc, char ** argv) {
429
+ common_params params;
430
+
431
+ params.prompt = "";
432
+
433
+ params.n_predict = 4096;
434
+ params.n_batch = 8192;
435
+ params.n_ctx = 8192;
436
+
437
+ params.sampling.top_k = 4;
438
+ params.sampling.samplers = { COMMON_SAMPLER_TYPE_TOP_K, };
439
+
440
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_TTS, print_usage)) {
441
+ return 1;
442
+ }
443
+
444
+ const int n_parallel = params.n_parallel;
445
+ const int n_predict = params.n_predict;
446
+
447
+ common_init();
448
+
449
+ // init LLM
450
+
451
+ llama_backend_init();
452
+ llama_numa_init(params.numa);
453
+
454
+ llama_model * model_ttc = NULL; // text-to-codes
455
+ llama_model * model_cts = NULL; // codes-to-speech
456
+
457
+ llama_context * ctx_ttc = NULL;
458
+ llama_context * ctx_cts = NULL;
459
+
460
+ common_init_result llama_init_ttc = common_init_from_params(params);
461
+ model_ttc = llama_init_ttc.model;
462
+ ctx_ttc = llama_init_ttc.context;
463
+
464
+ // TODO: refactor in a common struct
465
+ params.model = params.vocoder.model;
466
+ params.model_url = params.vocoder.model_url;
467
+ params.hf_repo = params.vocoder.hf_repo;
468
+ params.hf_file = params.vocoder.hf_file;
469
+
470
+ params.embedding = true;
471
+
472
+ common_init_result llama_init_cts = common_init_from_params(params);
473
+ model_cts = llama_init_cts.model;
474
+ ctx_cts = llama_init_cts.context;
475
+
476
+ std::vector<common_sampler *> smpl(n_parallel);
477
+ for (int i = 0; i < n_parallel; ++i) {
478
+ params.sampling.no_perf = (i != 0);
479
+ params.sampling.seed = params.sampling.seed + 1;
480
+
481
+ smpl[i] = common_sampler_init(model_ttc, params.sampling);
482
+ }
483
+
484
+ LOG_INF("sampler seed: %u\n", common_sampler_get_seed(smpl[0]));
485
+ LOG_INF("sampler params: \n%s\n", params.sampling.print().c_str());
486
+ LOG_INF("sampler chain: %s\n", common_sampler_print(smpl[0]).c_str());
487
+
488
+ LOG_INF("%s: loading done\n", __func__);
489
+
490
+ const auto t_main_start = ggml_time_us();
491
+
492
+ std::vector<llama_token> codes;
493
+
494
+ // process prompt and generate voice codes
495
+ {
496
+ LOG_INF("%s: constructing prompt ..\n", __func__);
497
+
498
+ std::vector<llama_token> prompt_inp;
499
+
500
+ prompt_init(prompt_inp, model_ttc);
501
+
502
+ prompt_add(prompt_inp, model_ttc, "<|text_start|>the<|text_sep|>overall<|text_sep|>package<|text_sep|>from<|text_sep|>just<|text_sep|>two<|text_sep|>people<|text_sep|>is<|text_sep|>pretty<|text_sep|>remarkable<|text_sep|>sure<|text_sep|>i<|text_sep|>have<|text_sep|>some<|text_sep|>critiques<|text_sep|>about<|text_sep|>some<|text_sep|>of<|text_sep|>the<|text_sep|>gameplay<|text_sep|>aspects<|text_sep|>but<|text_sep|>its<|text_sep|>still<|text_sep|>really<|text_sep|>enjoyable<|text_sep|>and<|text_sep|>it<|text_sep|>looks<|text_sep|>lovely<|text_sep|>", false, true);
503
+
504
+ // convert the input text into the necessary format expected by OuteTTS
505
+ {
506
+ std::string prompt_clean = process_text(params.prompt);
507
+
508
+ LOG_INF("%s: prompt: '%s'\n", __func__, prompt_clean.c_str());
509
+
510
+ prompt_add(prompt_inp, model_ttc, prompt_clean, false, true);
511
+ }
512
+
513
+ prompt_add(prompt_inp, model_ttc, "<|text_end|>\n", false, true);
514
+
515
+ // disabled to save time on tokenizing each time
516
+ // TODO: load voices from the json files
517
+ #if 0
518
+ const std::string voice_data = R"(<|audio_start|>
519
+ the<|t_0.08|><|code_start|><|257|><|740|><|636|><|913|><|788|><|1703|><|code_end|>
520
+ overall<|t_0.36|><|code_start|><|127|><|201|><|191|><|774|><|700|><|532|><|1056|><|557|><|798|><|298|><|1741|><|747|><|1662|><|1617|><|1702|><|1527|><|368|><|1588|><|1049|><|1008|><|1625|><|747|><|1576|><|728|><|1019|><|1696|><|1765|><|code_end|>
521
+ package<|t_0.56|><|code_start|><|935|><|584|><|1319|><|627|><|1016|><|1491|><|1344|><|1117|><|1526|><|1040|><|239|><|1435|><|951|><|498|><|723|><|1180|><|535|><|789|><|1649|><|1637|><|78|><|465|><|1668|><|901|><|595|><|1675|><|117|><|1009|><|1667|><|320|><|840|><|79|><|507|><|1762|><|1508|><|1228|><|1768|><|802|><|1450|><|1457|><|232|><|639|><|code_end|>
522
+ from<|t_0.19|><|code_start|><|604|><|782|><|1682|><|872|><|1532|><|1600|><|1036|><|1761|><|647|><|1554|><|1371|><|653|><|1595|><|950|><|code_end|>
523
+ just<|t_0.25|><|code_start|><|1782|><|1670|><|317|><|786|><|1748|><|631|><|599|><|1155|><|1364|><|1524|><|36|><|1591|><|889|><|1535|><|541|><|440|><|1532|><|50|><|870|><|code_end|>
524
+ two<|t_0.24|><|code_start|><|1681|><|1510|><|673|><|799|><|805|><|1342|><|330|><|519|><|62|><|640|><|1138|><|565|><|1552|><|1497|><|1552|><|572|><|1715|><|1732|><|code_end|>
525
+ people<|t_0.39|><|code_start|><|593|><|274|><|136|><|740|><|691|><|633|><|1484|><|1061|><|1138|><|1485|><|344|><|428|><|397|><|1562|><|645|><|917|><|1035|><|1449|><|1669|><|487|><|442|><|1484|><|1329|><|1832|><|1704|><|600|><|761|><|653|><|269|><|code_end|>
526
+ is<|t_0.16|><|code_start|><|566|><|583|><|1755|><|646|><|1337|><|709|><|802|><|1008|><|485|><|1583|><|652|><|10|><|code_end|>
527
+ pretty<|t_0.32|><|code_start|><|1818|><|1747|><|692|><|733|><|1010|><|534|><|406|><|1697|><|1053|><|1521|><|1355|><|1274|><|816|><|1398|><|211|><|1218|><|817|><|1472|><|1703|><|686|><|13|><|822|><|445|><|1068|><|code_end|>
528
+ remarkable<|t_0.68|><|code_start|><|230|><|1048|><|1705|><|355|><|706|><|1149|><|1535|><|1787|><|1356|><|1396|><|835|><|1583|><|486|><|1249|><|286|><|937|><|1076|><|1150|><|614|><|42|><|1058|><|705|><|681|><|798|><|934|><|490|><|514|><|1399|><|572|><|1446|><|1703|><|1346|><|1040|><|1426|><|1304|><|664|><|171|><|1530|><|625|><|64|><|1708|><|1830|><|1030|><|443|><|1509|><|1063|><|1605|><|1785|><|721|><|1440|><|923|><|code_end|>
529
+ sure<|t_0.36|><|code_start|><|792|><|1780|><|923|><|1640|><|265|><|261|><|1525|><|567|><|1491|><|1250|><|1730|><|362|><|919|><|1766|><|543|><|1|><|333|><|113|><|970|><|252|><|1606|><|133|><|302|><|1810|><|1046|><|1190|><|1675|><|code_end|>
530
+ i<|t_0.08|><|code_start|><|123|><|439|><|1074|><|705|><|1799|><|637|><|code_end|>
531
+ have<|t_0.16|><|code_start|><|1509|><|599|><|518|><|1170|><|552|><|1029|><|1267|><|864|><|419|><|143|><|1061|><|0|><|code_end|>
532
+ some<|t_0.16|><|code_start|><|619|><|400|><|1270|><|62|><|1370|><|1832|><|917|><|1661|><|167|><|269|><|1366|><|1508|><|code_end|>
533
+ critiques<|t_0.60|><|code_start|><|559|><|584|><|1163|><|1129|><|1313|><|1728|><|721|><|1146|><|1093|><|577|><|928|><|27|><|630|><|1080|><|1346|><|1337|><|320|><|1382|><|1175|><|1682|><|1556|><|990|><|1683|><|860|><|1721|><|110|><|786|><|376|><|1085|><|756|><|1523|><|234|><|1334|><|1506|><|1578|><|659|><|612|><|1108|><|1466|><|1647|><|308|><|1470|><|746|><|556|><|1061|><|code_end|>
534
+ about<|t_0.29|><|code_start|><|26|><|1649|><|545|><|1367|><|1263|><|1728|><|450|><|859|><|1434|><|497|><|1220|><|1285|><|179|><|755|><|1154|><|779|><|179|><|1229|><|1213|><|922|><|1774|><|1408|><|code_end|>
535
+ some<|t_0.23|><|code_start|><|986|><|28|><|1649|><|778|><|858|><|1519|><|1|><|18|><|26|><|1042|><|1174|><|1309|><|1499|><|1712|><|1692|><|1516|><|1574|><|code_end|>
536
+ of<|t_0.07|><|code_start|><|197|><|716|><|1039|><|1662|><|64|><|code_end|>
537
+ the<|t_0.08|><|code_start|><|1811|><|1568|><|569|><|886|><|1025|><|1374|><|code_end|>
538
+ gameplay<|t_0.48|><|code_start|><|1269|><|1092|><|933|><|1362|><|1762|><|1700|><|1675|><|215|><|781|><|1086|><|461|><|838|><|1022|><|759|><|649|><|1416|><|1004|><|551|><|909|><|787|><|343|><|830|><|1391|><|1040|><|1622|><|1779|><|1360|><|1231|><|1187|><|1317|><|76|><|997|><|989|><|978|><|737|><|189|><|code_end|>
539
+ aspects<|t_0.56|><|code_start|><|1423|><|797|><|1316|><|1222|><|147|><|719|><|1347|><|386|><|1390|><|1558|><|154|><|440|><|634|><|592|><|1097|><|1718|><|712|><|763|><|1118|><|1721|><|1311|><|868|><|580|><|362|><|1435|><|868|><|247|><|221|><|886|><|1145|><|1274|><|1284|><|457|><|1043|><|1459|><|1818|><|62|><|599|><|1035|><|62|><|1649|><|778|><|code_end|>
540
+ but<|t_0.20|><|code_start|><|780|><|1825|><|1681|><|1007|><|861|><|710|><|702|><|939|><|1669|><|1491|><|613|><|1739|><|823|><|1469|><|648|><|code_end|>
541
+ its<|t_0.09|><|code_start|><|92|><|688|><|1623|><|962|><|1670|><|527|><|599|><|code_end|>
542
+ still<|t_0.27|><|code_start|><|636|><|10|><|1217|><|344|><|713|><|957|><|823|><|154|><|1649|><|1286|><|508|><|214|><|1760|><|1250|><|456|><|1352|><|1368|><|921|><|615|><|5|><|code_end|>
543
+ really<|t_0.36|><|code_start|><|55|><|420|><|1008|><|1659|><|27|><|644|><|1266|><|617|><|761|><|1712|><|109|><|1465|><|1587|><|503|><|1541|><|619|><|197|><|1019|><|817|><|269|><|377|><|362|><|1381|><|507|><|1488|><|4|><|1695|><|code_end|>
544
+ enjoyable<|t_0.49|><|code_start|><|678|><|501|><|864|><|319|><|288|><|1472|><|1341|><|686|><|562|><|1463|><|619|><|1563|><|471|><|911|><|730|><|1811|><|1006|><|520|><|861|><|1274|><|125|><|1431|><|638|><|621|><|153|><|876|><|1770|><|437|><|987|><|1653|><|1109|><|898|><|1285|><|80|><|593|><|1709|><|843|><|code_end|>
545
+ and<|t_0.15|><|code_start|><|1285|><|987|><|303|><|1037|><|730|><|1164|><|502|><|120|><|1737|><|1655|><|1318|><|code_end|>
546
+ it<|t_0.09|><|code_start|><|848|><|1366|><|395|><|1601|><|1513|><|593|><|1302|><|code_end|>
547
+ looks<|t_0.27|><|code_start|><|1281|><|1266|><|1755|><|572|><|248|><|1751|><|1257|><|695|><|1380|><|457|><|659|><|585|><|1315|><|1105|><|1776|><|736|><|24|><|736|><|654|><|1027|><|code_end|>
548
+ lovely<|t_0.56|><|code_start|><|634|><|596|><|1766|><|1556|><|1306|><|1285|><|1481|><|1721|><|1123|><|438|><|1246|><|1251|><|795|><|659|><|1381|><|1658|><|217|><|1772|><|562|><|952|><|107|><|1129|><|1112|><|467|><|550|><|1079|><|840|><|1615|><|1469|><|1380|><|168|><|917|><|836|><|1827|><|437|><|583|><|67|><|595|><|1087|><|1646|><|1493|><|1677|><|code_end|>)";
549
+
550
+ auto tmp = common_tokenize(model_ttc, voice_data, false, true);
551
+ printf("\n\n");
552
+ for (int i = 0; i < tmp.size(); ++i) {
553
+ printf("%d, ", tmp[i]);
554
+ }
555
+ printf("\n\n");
556
+ #else
557
+ prompt_add(prompt_inp, llama_tokens {
558
+ 151667, 198, 1782, 155780, 151669, 151929, 152412, 152308, 152585,
559
+ 152460, 153375, 151670, 198, 74455, 155808, 151669, 151799,
560
+ 151873, 151863, 152446, 152372, 152204, 152728, 152229, 152470,
561
+ 151970, 153413, 152419, 153334, 153289, 153374, 153199, 152040,
562
+ 153260, 152721, 152680, 153297, 152419, 153248, 152400, 152691,
563
+ 153368, 153437, 151670, 198, 1722, 155828, 151669, 152607,
564
+ 152256, 152991, 152299, 152688, 153163, 153016, 152789, 153198,
565
+ 152712, 151911, 153107, 152623, 152170, 152395, 152852, 152207,
566
+ 152461, 153321, 153309, 151750, 152137, 153340, 152573, 152267,
567
+ 153347, 151789, 152681, 153339, 151992, 152512, 151751, 152179,
568
+ 153434, 153180, 152900, 153440, 152474, 153122, 153129, 151904,
569
+ 152311, 151670, 198, 1499, 155791, 151669, 152276, 152454,
570
+ 153354, 152544, 153204, 153272, 152708, 153433, 152319, 153226,
571
+ 153043, 152325, 153267, 152622, 151670, 198, 4250, 155797,
572
+ 151669, 153454, 153342, 151989, 152458, 153420, 152303, 152271,
573
+ 152827, 153036, 153196, 151708, 153263, 152561, 153207, 152213,
574
+ 152112, 153204, 151722, 152542, 151670, 198, 19789, 155796,
575
+ 151669, 153353, 153182, 152345, 152471, 152477, 153014, 152002,
576
+ 152191, 151734, 152312, 152810, 152237, 153224, 153169, 153224,
577
+ 152244, 153387, 153404, 151670, 198, 16069, 155811, 151669,
578
+ 152265, 151946, 151808, 152412, 152363, 152305, 153156, 152733,
579
+ 152810, 153157, 152016, 152100, 152069, 153234, 152317, 152589,
580
+ 152707, 153121, 153341, 152159, 152114, 153156, 153001, 153504,
581
+ 153376, 152272, 152433, 152325, 151941, 151670, 198, 285,
582
+ 155788, 151669, 152238, 152255, 153427, 152318, 153009, 152381,
583
+ 152474, 152680, 152157, 153255, 152324, 151682, 151670, 198,
584
+ 32955, 155804, 151669, 153490, 153419, 152364, 152405, 152682,
585
+ 152206, 152078, 153369, 152725, 153193, 153027, 152946, 152488,
586
+ 153070, 151883, 152890, 152489, 153144, 153375, 152358, 151685,
587
+ 152494, 152117, 152740, 151670, 198, 37448, 480, 155840, 151669,
588
+ 151902, 152720, 153377, 152027, 152378, 152821, 153207, 153459,
589
+ 153028, 153068, 152507, 153255, 152158, 152921, 151958, 152609,
590
+ 152748, 152822, 152286, 151714, 152730, 152377, 152353, 152470,
591
+ 152606, 152162, 152186, 153071, 152244, 153118, 153375, 153018,
592
+ 152712, 153098, 152976, 152336, 151843, 153202, 152297, 151736,
593
+ 153380, 153502, 152702, 152115, 153181, 152735, 153277, 153457,
594
+ 152393, 153112, 152595, 151670, 198, 19098, 155808, 151669,
595
+ 152464, 153452, 152595, 153312, 151937, 151933, 153197, 152239,
596
+ 153163, 152922, 153402, 152034, 152591, 153438, 152215, 151673,
597
+ 152005, 151785, 152642, 151924, 153278, 151805, 151974, 153482,
598
+ 152718, 152862, 153347, 151670, 198, 72, 155780, 151669, 151795,
599
+ 152111, 152746, 152377, 153471, 152309, 151670, 198, 19016,
600
+ 155788, 151669, 153181, 152271, 152190, 152842, 152224, 152701,
601
+ 152939, 152536, 152091, 151815, 152733, 151672, 151670, 198,
602
+ 14689, 155788, 151669, 152291, 152072, 152942, 151734, 153042,
603
+ 153504, 152589, 153333, 151839, 151941, 153038, 153180, 151670,
604
+ 198, 36996, 8303, 155832, 151669, 152231, 152256, 152835,
605
+ 152801, 152985, 153400, 152393, 152818, 152765, 152249, 152600,
606
+ 151699, 152302, 152752, 153018, 153009, 151992, 153054, 152847,
607
+ 153354, 153228, 152662, 153355, 152532, 153393, 151782, 152458,
608
+ 152048, 152757, 152428, 153195, 151906, 153006, 153178, 153250,
609
+ 152331, 152284, 152780, 153138, 153319, 151980, 153142, 152418,
610
+ 152228, 152733, 151670, 198, 9096, 155801, 151669, 151698,
611
+ 153321, 152217, 153039, 152935, 153400, 152122, 152531, 153106,
612
+ 152169, 152892, 152957, 151851, 152427, 152826, 152451, 151851,
613
+ 152901, 152885, 152594, 153446, 153080, 151670, 198, 14689,
614
+ 155795, 151669, 152658, 151700, 153321, 152450, 152530, 153191,
615
+ 151673, 151690, 151698, 152714, 152846, 152981, 153171, 153384,
616
+ 153364, 153188, 153246, 151670, 198, 1055, 155779, 151669,
617
+ 151869, 152388, 152711, 153334, 151736, 151670, 198, 1782,
618
+ 155780, 151669, 153483, 153240, 152241, 152558, 152697, 153046,
619
+ 151670, 198, 5804, 1363, 155820, 151669, 152941, 152764, 152605,
620
+ 153034, 153434, 153372, 153347, 151887, 152453, 152758, 152133,
621
+ 152510, 152694, 152431, 152321, 153088, 152676, 152223, 152581,
622
+ 152459, 152015, 152502, 153063, 152712, 153294, 153451, 153032,
623
+ 152903, 152859, 152989, 151748, 152669, 152661, 152650, 152409,
624
+ 151861, 151670, 198, 300, 7973, 155828, 151669, 153095, 152469,
625
+ 152988, 152894, 151819, 152391, 153019, 152058, 153062, 153230,
626
+ 151826, 152112, 152306, 152264, 152769, 153390, 152384, 152435,
627
+ 152790, 153393, 152983, 152540, 152252, 152034, 153107, 152540,
628
+ 151919, 151893, 152558, 152817, 152946, 152956, 152129, 152715,
629
+ 153131, 153490, 151734, 152271, 152707, 151734, 153321, 152450,
630
+ 151670, 198, 8088, 155792, 151669, 152452, 153497, 153353,
631
+ 152679, 152533, 152382, 152374, 152611, 153341, 153163, 152285,
632
+ 153411, 152495, 153141, 152320, 151670, 198, 1199, 155781,
633
+ 151669, 151764, 152360, 153295, 152634, 153342, 152199, 152271,
634
+ 151670, 198, 43366, 155799, 151669, 152308, 151682, 152889,
635
+ 152016, 152385, 152629, 152495, 151826, 153321, 152958, 152180,
636
+ 151886, 153432, 152922, 152128, 153024, 153040, 152593, 152287,
637
+ 151677, 151670, 198, 53660, 155808, 151669, 151727, 152092,
638
+ 152680, 153331, 151699, 152316, 152938, 152289, 152433, 153384,
639
+ 151781, 153137, 153259, 152175, 153213, 152291, 151869, 152691,
640
+ 152489, 151941, 152049, 152034, 153053, 152179, 153160, 151676,
641
+ 153367, 151670, 198, 268, 4123, 480, 155821, 151669, 152350,
642
+ 152173, 152536, 151991, 151960, 153144, 153013, 152358, 152234,
643
+ 153135, 152291, 153235, 152143, 152583, 152402, 153483, 152678,
644
+ 152192, 152533, 152946, 151797, 153103, 152310, 152293, 151825,
645
+ 152548, 153442, 152109, 152659, 153325, 152781, 152570, 152957,
646
+ 151752, 152265, 153381, 152515, 151670, 198, 437, 155787,
647
+ 151669, 152957, 152659, 151975, 152709, 152402, 152836, 152174,
648
+ 151792, 153409, 153327, 152990, 151670, 198, 275, 155781,
649
+ 151669, 152520, 153038, 152067, 153273, 153185, 152265, 152974,
650
+ 151670, 198, 94273, 155799, 151669, 152953, 152938, 153427,
651
+ 152244, 151920, 153423, 152929, 152367, 153052, 152129, 152331,
652
+ 152257, 152987, 152777, 153448, 152408, 151696, 152408, 152326,
653
+ 152699, 151670, 198, 385, 16239, 155828, 151669, 152306, 152268,
654
+ 153438, 153228, 152978, 152957, 153153, 153393, 152795, 152110,
655
+ 152918, 152923, 152467, 152331, 153053, 153330, 151889, 153444,
656
+ 152234, 152624, 151779, 152801, 152784, 152139, 152222, 152751,
657
+ 152512, 153287, 153141, 153052, 151840, 152589, 152508, 153499,
658
+ 152109, 152255, 151739, 152267, 152759, 153318, 153165, 153349,
659
+ 151670,});
660
+ #endif
661
+
662
+ // print the prompt token-by-token
663
+
664
+ LOG("\n");
665
+
666
+ for (auto id : prompt_inp) {
667
+ LOG("%s", common_token_to_piece(ctx_ttc, id).c_str());
668
+ }
669
+
670
+ LOG_INF("%s: prompt size: %d\n", __func__, (int) prompt_inp.size());
671
+
672
+ LOG("\n");
673
+
674
+ // create a llama_batch
675
+ // we use this object to submit token data for decoding
676
+ llama_batch batch = llama_batch_init(std::max(prompt_inp.size(), (size_t) n_parallel), 0, n_parallel);
677
+
678
+ std::vector<llama_seq_id> seq_ids(n_parallel, 0);
679
+ for (int32_t i = 0; i < n_parallel; ++i) {
680
+ seq_ids[i] = i;
681
+ }
682
+
683
+ // evaluate the initial prompt
684
+ for (size_t i = 0; i < prompt_inp.size(); ++i) {
685
+ common_batch_add(batch, prompt_inp[i], i, seq_ids, false);
686
+ }
687
+ GGML_ASSERT(batch.n_tokens == (int) prompt_inp.size());
688
+
689
+ // llama_decode will output logits only for the last token of the prompt
690
+ batch.logits[batch.n_tokens - 1] = true;
691
+
692
+ if (llama_decode(ctx_ttc, batch) != 0) {
693
+ LOG_ERR("%s: llama_decode() failed\n", __func__);
694
+ return 1;
695
+ }
696
+
697
+ if (n_parallel > 1) {
698
+ LOG_INF("\n\n%s: generating %d sequences ...\n", __func__, n_parallel);
699
+ }
700
+
701
+ llama_synchronize(ctx_ttc);
702
+
703
+ LOG_INF("%s: time for prompt: %.3f ms\n\n", __func__, (ggml_time_us() - t_main_start) / 1000.0f);
704
+
705
+ const auto t_dec_start = ggml_time_us();
706
+
707
+ // main loop
708
+
709
+ // remember the batch index of the last token for each parallel sequence
710
+ // we need this to determine which logits to sample from
711
+ std::vector<int32_t> i_batch(n_parallel, batch.n_tokens - 1);
712
+
713
+ int n_past = batch.n_tokens;
714
+ int n_decode = 0;
715
+
716
+ while (n_decode <= n_predict) {
717
+ // prepare the next batch
718
+ common_batch_clear(batch);
719
+
720
+ // sample the next token for each parallel sequence / stream
721
+ for (int32_t i = 0; i < n_parallel; ++i) {
722
+ if (i_batch[i] < 0) {
723
+ // the stream has already finished
724
+ continue;
725
+ }
726
+
727
+ const llama_token new_token_id = common_sampler_sample(smpl[i], ctx_ttc, i_batch[i]);
728
+
729
+ common_sampler_accept(smpl[i], new_token_id, true);
730
+
731
+ codes.push_back(new_token_id);
732
+
733
+ const auto * cands = common_sampler_get_candidates(smpl[i]);
734
+
735
+ // is it an end of generation? -> mark the stream as finished
736
+ if (llama_token_is_eog(model_ttc, new_token_id) || n_decode == n_predict) {
737
+ std::string reason;
738
+ if (llama_token_is_eog(model_ttc, new_token_id)) {
739
+ reason = "eos";
740
+ } else {
741
+ reason = "n_predict";
742
+ }
743
+
744
+ i_batch[i] = -1;
745
+
746
+ LOG("\n");
747
+ if (n_parallel > 1) {
748
+ LOG_CNT("\n");
749
+ LOG_INF("%s: stream %d finished at n_past = %d, reason = '%s'\n", __func__, i, n_past, reason.c_str());
750
+ }
751
+
752
+ continue;
753
+ }
754
+
755
+ {
756
+ const float p = cands->data[cands->selected].p;
757
+
758
+ const int col = std::max(0, std::min((int) k_colors.size() - 1, (int) ((3*p)*float(k_colors.size()))));
759
+
760
+ LOG_CNT("%s%d%s", k_colors[col].c_str(), i, "\033[0m");
761
+ //LOG_CNT("%d", i);
762
+ }
763
+
764
+ i_batch[i] = batch.n_tokens;
765
+
766
+ // push this new token for next evaluation
767
+ common_batch_add(batch, new_token_id, n_past, { i }, true);
768
+ }
769
+
770
+ // all streams are finished
771
+ if (batch.n_tokens == 0) {
772
+ break;
773
+ }
774
+
775
+ n_decode += 1;
776
+ n_past += 1;
777
+
778
+ // evaluate the current batch with the transformer model
779
+ if (llama_decode(ctx_ttc, batch)) {
780
+ LOG_ERR("%s : failed to eval, return code %d\n", __func__, 1);
781
+ return 1;
782
+ }
783
+ }
784
+
785
+ llama_batch_free(batch);
786
+
787
+ LOG("\n");
788
+ LOG_INF("%s: time for decoder: %.3f ms\n", __func__, (ggml_time_us() - t_dec_start) / 1000.0f);
789
+ }
790
+
791
+ common_perf_print(ctx_ttc, smpl[0]);
792
+
793
+ //std::vector<llama_token> codes = {198, 88225, 155856, 151669, 152205,
794
+ // 153064, 152537, 153421, 153209, 152524, 151689, 152993, 152438, 152695,
795
+ // 153091, 152945, 152829, 152534, 152934, 153020, 151997, 152263, 153010,
796
+ // 153146, 152399, 153208, 152496, 151793, 152848, 152263, 152571, 153286,
797
+ // 152227, 153300, 152934, 152263, 153208, 152263, 152965, 152430, 152296,
798
+ // 153146, 152920, 152376, 152556, 153363, 151775, 152044, 152972, 152690,
799
+ // 153379, 152368, 152233, 153422, 152490, 151996, 152022, 151694, 152061,
800
+ // 153238, 152539, 153356, 152640, 153021, 153123, 151962, 153094, 151670,
801
+ // 198, 20339, 13189, 155824, 151669, 152070, 152007, 152910, 151683,
802
+ // 152000, 152373, 152760, 152046, 151735, 152334, 152394, 153073, 152908,
803
+ // 151856, 151953, 153247, 153293, 151903, 153480, 153168, 152478, 153359,
804
+ // 153429, 151905, 151678, 152567, 152411, 152165, 152556, 153075, 153424,
805
+ // 151993, 152999, 153078, 152151, 152088, 153389, 152484, 151874, 151670,
806
+ // 198, 285, 155784, 151669, 152226, 152126, 152638, 153215, 151729,
807
+ // 152959, 153479, 153059, 151838, 151670, 198, 1782, 155783, 151669,
808
+ // 153288, 153055, 153314, 152497, 152962, 152741, 152076, 153253, 151670,
809
+ // 198, 471, 16488, 155825, 151669, 152060, 152916, 151893, 153469, 152501,
810
+ // 152080, 152743, 151932, 153161, 152096, 152761, 152698, 153401, 153242,
811
+ // 153336, 152441, 152838, 153467, 152706, 153496, 153310, 152422, 153360,
812
+ // 153115, 152763, 151998, 152373, 153450, 152554, 151968, 153323, 152055,
813
+ // 152468, 153111, 153358, 152813, 152010, 151770, 152823, 152960, 151670,
814
+ // 198, 22627, 155823, 151669, 152814, 152366, 153484, 152931, 153441,
815
+ // 152164, 152877, 152915, 153463, 151692, 152911, 152747, 152776, 151831,
816
+ // 153449, 151882, 152975, 152031, 152513, 153150, 152448, 152667, 153133,
817
+ // 153189, 152619, 153466, 152054, 152106, 153119, 152277, 152439, 153109,
818
+ // 152997, 152141, 153154, 153256, 153311, 151922, 151670, 198, 1055,
819
+ // 155781, 151669, 152633, 151850, 153060, 153270, 152560, 153348, 152729,
820
+ // 151670, 198, 25312, 155803, 151669, 152521, 153403, 152561, 153337,
821
+ // 153383, 152199, 153493, 153326, 151830, 152254, 152248, 152349, 152153,
822
+ // 153007, 151823, 153037, 152575, 152457, 152406, 152592, 153116, 153365,
823
+ // 153456, 151670, 198, 88225, 155817, 151669, 153271, 151925, 152218,
824
+ // 152418, 152253, 153140, 151903, 153151, 152626, 152338, 152647, 153464,
825
+ // 152785, 152768, 151711, 152037, 152033, 151804, 152216, 151701, 151855,
826
+ // 152348, 152995, 152955, 152905, 152342, 152340, 153391, 153453, 152418,
827
+ // 153415, 151990, 153083, 152884, 151670, 198, 151668, 198, 151645};
828
+
829
+ {
830
+ const std::string inp_txt = common_detokenize(ctx_ttc, codes, true);
831
+
832
+ LOG("\n");
833
+ LOG_INF("codes: '%s'\n", inp_txt.c_str());
834
+ LOG_INF("%s: codes size: %d\n", __func__, (int) codes.size());
835
+ }
836
+
837
+ // remove all non-audio tokens (i.e. < 151672 || > 155772)
838
+ codes.erase(std::remove_if(codes.begin(), codes.end(), [](llama_token t) { return t < 151672 || t > 155772; }), codes.end());
839
+
840
+ {
841
+ const std::string inp_txt = common_detokenize(ctx_ttc, codes, true);
842
+ LOG_INF("codes audio: '%s'\n", inp_txt.c_str());
843
+ LOG_INF("%s: codes audio size: %d\n", __func__, (int) codes.size());
844
+ }
845
+
846
+ for (auto & token : codes) {
847
+ token -= 151672;
848
+ }
849
+
850
+ const auto t_voc_start = ggml_time_us();
851
+
852
+ const int n_codes = codes.size();
853
+
854
+ llama_batch batch = llama_batch_init(n_codes, 0, 1);
855
+
856
+ for (size_t i = 0; i < codes.size(); ++i) {
857
+ common_batch_add(batch, codes[i], i, { 0 }, true); // TODO: all logits?
858
+ }
859
+ GGML_ASSERT(batch.n_tokens == n_codes);
860
+
861
+ if (llama_decode(ctx_cts, batch) != 0) {
862
+ LOG_ERR("%s: llama_decode() failed\n", __func__);
863
+ return 1;
864
+ }
865
+
866
+ llama_synchronize(ctx_cts);
867
+
868
+ LOG_INF("%s: time for vocoder: %.3f ms\n", __func__, (ggml_time_us() - t_voc_start) / 1000.0f);
869
+
870
+ const auto t_spec_start = ggml_time_us();
871
+
872
+ #if 1
873
+ // spectral operations
874
+ const int n_embd = llama_n_embd(model_cts);
875
+ const float * embd = llama_get_embeddings(ctx_cts);
876
+
877
+ auto audio = embd_to_audio(embd, n_codes, n_embd, params.cpuparams.n_threads);
878
+
879
+ #else
880
+ // read the spectrogram from a file for debugging purposes
881
+ std::vector<float> audio;
882
+ {
883
+ std::ifstream fin("out.bin", std::ios::binary);
884
+ if (!fin) {
885
+ LOG_ERR("%s: failed to open file '%s'\n", __func__, "out.bin");
886
+ return 1;
887
+ }
888
+
889
+ std::vector<float> embd;
890
+
891
+ int n_codes;
892
+ int n_embd;
893
+
894
+ fin.read(reinterpret_cast<char *>(&n_codes), sizeof(int));
895
+ fin.read(reinterpret_cast<char *>(&n_embd), sizeof(int));
896
+
897
+ embd.resize(n_codes * n_embd);
898
+ fin.read(reinterpret_cast<char *>(embd.data()), n_codes * n_embd * sizeof(float));
899
+ fin.close();
900
+
901
+ LOG_INF("%s: n_codes: %d, n_embd: %d\n", __func__, n_codes, n_embd);
902
+
903
+ audio = embd_to_audio(embd.data(), n_codes, n_embd, params.cpuparams.n_threads);
904
+ }
905
+ #endif
906
+
907
+ const std::string fname = "output.wav";
908
+
909
+ const int n_sr = 24000; // sampling rate
910
+
911
+ // zero out first 0.25 seconds
912
+ for (int i = 0; i < 24000/4; ++i) {
913
+ audio[i] = 0.0f;
914
+ }
915
+
916
+ LOG_INF("%s: time for spectral ops: %.3f ms\n", __func__, (ggml_time_us() - t_spec_start) / 1000.0f);
917
+ LOG_INF("%s: total time: %.3f ms\n", __func__, (ggml_time_us() - t_main_start) / 1000.0f);
918
+
919
+ save_wav16(fname, audio, n_sr);
920
+
921
+ LOG_INF("%s: audio written to file '%s'\n", __func__, fname.c_str());
922
+
923
+ llama_free(ctx_ttc);
924
+ llama_free_model(model_ttc);
925
+
926
+ llama_free(ctx_cts);
927
+ llama_free_model(model_cts);
928
+
929
+ llama_backend_free();
930
+
931
+ return 0;
932
+ }