@fugood/llama.node 0.0.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/CMakeLists.txt +85 -0
  2. package/README.md +56 -0
  3. package/bin/darwin/arm64/llama-node.node +0 -0
  4. package/bin/darwin/x64/llama-node.node +0 -0
  5. package/bin/linux/arm64/llama-node.node +0 -0
  6. package/bin/linux/x64/llama-node.node +0 -0
  7. package/bin/win32/arm64/llama-node.node +0 -0
  8. package/bin/win32/arm64/node.lib +0 -0
  9. package/bin/win32/x64/llama-node.node +0 -0
  10. package/bin/win32/x64/node.lib +0 -0
  11. package/lib/binding.js +13 -0
  12. package/lib/binding.ts +57 -0
  13. package/lib/index.js +24 -0
  14. package/lib/index.ts +13 -0
  15. package/package.json +65 -0
  16. package/src/addons.cpp +506 -0
  17. package/src/llama.cpp/CMakeLists.txt +1320 -0
  18. package/src/llama.cpp/build.zig +172 -0
  19. package/src/llama.cpp/cmake/FindSIMD.cmake +100 -0
  20. package/src/llama.cpp/common/CMakeLists.txt +87 -0
  21. package/src/llama.cpp/common/base64.hpp +392 -0
  22. package/src/llama.cpp/common/common.cpp +2949 -0
  23. package/src/llama.cpp/common/common.h +324 -0
  24. package/src/llama.cpp/common/console.cpp +501 -0
  25. package/src/llama.cpp/common/console.h +19 -0
  26. package/src/llama.cpp/common/grammar-parser.cpp +440 -0
  27. package/src/llama.cpp/common/grammar-parser.h +29 -0
  28. package/src/llama.cpp/common/json-schema-to-grammar.cpp +764 -0
  29. package/src/llama.cpp/common/json-schema-to-grammar.h +4 -0
  30. package/src/llama.cpp/common/json.hpp +24766 -0
  31. package/src/llama.cpp/common/log.h +724 -0
  32. package/src/llama.cpp/common/ngram-cache.cpp +282 -0
  33. package/src/llama.cpp/common/ngram-cache.h +94 -0
  34. package/src/llama.cpp/common/sampling.cpp +353 -0
  35. package/src/llama.cpp/common/sampling.h +147 -0
  36. package/src/llama.cpp/common/stb_image.h +8396 -0
  37. package/src/llama.cpp/common/train.cpp +1513 -0
  38. package/src/llama.cpp/common/train.h +233 -0
  39. package/src/llama.cpp/examples/CMakeLists.txt +52 -0
  40. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +5 -0
  41. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +1640 -0
  42. package/src/llama.cpp/examples/batched/CMakeLists.txt +5 -0
  43. package/src/llama.cpp/examples/batched/batched.cpp +262 -0
  44. package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +5 -0
  45. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +261 -0
  46. package/src/llama.cpp/examples/beam-search/CMakeLists.txt +5 -0
  47. package/src/llama.cpp/examples/beam-search/beam-search.cpp +188 -0
  48. package/src/llama.cpp/examples/benchmark/CMakeLists.txt +6 -0
  49. package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +275 -0
  50. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +5 -0
  51. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +936 -0
  52. package/src/llama.cpp/examples/embedding/CMakeLists.txt +5 -0
  53. package/src/llama.cpp/examples/embedding/embedding.cpp +211 -0
  54. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +9 -0
  55. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +195 -0
  56. package/src/llama.cpp/examples/export-lora/CMakeLists.txt +5 -0
  57. package/src/llama.cpp/examples/export-lora/export-lora.cpp +462 -0
  58. package/src/llama.cpp/examples/finetune/CMakeLists.txt +5 -0
  59. package/src/llama.cpp/examples/finetune/finetune.cpp +1861 -0
  60. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +5 -0
  61. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +132 -0
  62. package/src/llama.cpp/examples/gguf/CMakeLists.txt +5 -0
  63. package/src/llama.cpp/examples/gguf/gguf.cpp +256 -0
  64. package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +5 -0
  65. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +553 -0
  66. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +5 -0
  67. package/src/llama.cpp/examples/gritlm/gritlm.cpp +215 -0
  68. package/src/llama.cpp/examples/imatrix/CMakeLists.txt +5 -0
  69. package/src/llama.cpp/examples/imatrix/imatrix.cpp +655 -0
  70. package/src/llama.cpp/examples/infill/CMakeLists.txt +5 -0
  71. package/src/llama.cpp/examples/infill/infill.cpp +767 -0
  72. package/src/llama.cpp/examples/jeopardy/questions.txt +100 -0
  73. package/src/llama.cpp/examples/llama-bench/CMakeLists.txt +5 -0
  74. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +1286 -0
  75. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/CMakeLists.txt +50 -0
  76. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/llama-android.cpp +443 -0
  77. package/src/llama.cpp/examples/llava/CMakeLists.txt +37 -0
  78. package/src/llama.cpp/examples/llava/clip.cpp +2027 -0
  79. package/src/llama.cpp/examples/llava/clip.h +85 -0
  80. package/src/llama.cpp/examples/llava/llava-cli.cpp +309 -0
  81. package/src/llama.cpp/examples/llava/llava.cpp +426 -0
  82. package/src/llama.cpp/examples/llava/llava.h +50 -0
  83. package/src/llama.cpp/examples/llava/requirements.txt +3 -0
  84. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +5 -0
  85. package/src/llama.cpp/examples/lookahead/lookahead.cpp +485 -0
  86. package/src/llama.cpp/examples/lookup/CMakeLists.txt +23 -0
  87. package/src/llama.cpp/examples/lookup/lookup-create.cpp +41 -0
  88. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +47 -0
  89. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +160 -0
  90. package/src/llama.cpp/examples/lookup/lookup.cpp +258 -0
  91. package/src/llama.cpp/examples/main/CMakeLists.txt +5 -0
  92. package/src/llama.cpp/examples/main/main.cpp +957 -0
  93. package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +33 -0
  94. package/src/llama.cpp/examples/parallel/CMakeLists.txt +5 -0
  95. package/src/llama.cpp/examples/parallel/parallel.cpp +427 -0
  96. package/src/llama.cpp/examples/passkey/CMakeLists.txt +5 -0
  97. package/src/llama.cpp/examples/passkey/passkey.cpp +302 -0
  98. package/src/llama.cpp/examples/perplexity/CMakeLists.txt +5 -0
  99. package/src/llama.cpp/examples/perplexity/perplexity.cpp +1943 -0
  100. package/src/llama.cpp/examples/quantize/CMakeLists.txt +6 -0
  101. package/src/llama.cpp/examples/quantize/quantize.cpp +423 -0
  102. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +6 -0
  103. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +424 -0
  104. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +5 -0
  105. package/src/llama.cpp/examples/retrieval/retrieval.cpp +350 -0
  106. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +5 -0
  107. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +246 -0
  108. package/src/llama.cpp/examples/server/CMakeLists.txt +40 -0
  109. package/src/llama.cpp/examples/server/bench/requirements.txt +2 -0
  110. package/src/llama.cpp/examples/server/httplib.h +9465 -0
  111. package/src/llama.cpp/examples/server/server.cpp +3826 -0
  112. package/src/llama.cpp/examples/server/tests/requirements.txt +6 -0
  113. package/src/llama.cpp/examples/server/utils.hpp +653 -0
  114. package/src/llama.cpp/examples/simple/CMakeLists.txt +5 -0
  115. package/src/llama.cpp/examples/simple/simple.cpp +183 -0
  116. package/src/llama.cpp/examples/speculative/CMakeLists.txt +5 -0
  117. package/src/llama.cpp/examples/speculative/speculative.cpp +614 -0
  118. package/src/llama.cpp/examples/sycl/CMakeLists.txt +9 -0
  119. package/src/llama.cpp/examples/sycl/ls-sycl-device.cpp +13 -0
  120. package/src/llama.cpp/examples/tokenize/CMakeLists.txt +5 -0
  121. package/src/llama.cpp/examples/tokenize/tokenize.cpp +42 -0
  122. package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +5 -0
  123. package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +1252 -0
  124. package/src/llama.cpp/ggml-alloc.c +985 -0
  125. package/src/llama.cpp/ggml-alloc.h +76 -0
  126. package/src/llama.cpp/ggml-backend-impl.h +141 -0
  127. package/src/llama.cpp/ggml-backend.c +2099 -0
  128. package/src/llama.cpp/ggml-backend.h +233 -0
  129. package/src/llama.cpp/ggml-common.h +1853 -0
  130. package/src/llama.cpp/ggml-cuda.h +43 -0
  131. package/src/llama.cpp/ggml-impl.h +265 -0
  132. package/src/llama.cpp/ggml-kompute.cpp +2006 -0
  133. package/src/llama.cpp/ggml-kompute.h +46 -0
  134. package/src/llama.cpp/ggml-metal.h +66 -0
  135. package/src/llama.cpp/ggml-mpi.c +216 -0
  136. package/src/llama.cpp/ggml-mpi.h +39 -0
  137. package/src/llama.cpp/ggml-opencl.cpp +2301 -0
  138. package/src/llama.cpp/ggml-opencl.h +36 -0
  139. package/src/llama.cpp/ggml-quants.c +12678 -0
  140. package/src/llama.cpp/ggml-quants.h +133 -0
  141. package/src/llama.cpp/ggml-sycl.cpp +17882 -0
  142. package/src/llama.cpp/ggml-sycl.h +49 -0
  143. package/src/llama.cpp/ggml-vulkan-shaders.hpp +69849 -0
  144. package/src/llama.cpp/ggml-vulkan.cpp +6442 -0
  145. package/src/llama.cpp/ggml-vulkan.h +29 -0
  146. package/src/llama.cpp/ggml.c +21819 -0
  147. package/src/llama.cpp/ggml.h +2403 -0
  148. package/src/llama.cpp/llama.cpp +17468 -0
  149. package/src/llama.cpp/llama.h +1117 -0
  150. package/src/llama.cpp/pocs/CMakeLists.txt +12 -0
  151. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +9 -0
  152. package/src/llama.cpp/pocs/vdot/q8dot.cpp +172 -0
  153. package/src/llama.cpp/pocs/vdot/vdot.cpp +310 -0
  154. package/src/llama.cpp/prompts/LLM-questions.txt +49 -0
  155. package/src/llama.cpp/prompts/alpaca.txt +1 -0
  156. package/src/llama.cpp/prompts/assistant.txt +31 -0
  157. package/src/llama.cpp/prompts/chat-with-baichuan.txt +4 -0
  158. package/src/llama.cpp/prompts/chat-with-bob.txt +7 -0
  159. package/src/llama.cpp/prompts/chat-with-qwen.txt +1 -0
  160. package/src/llama.cpp/prompts/chat-with-vicuna-v0.txt +7 -0
  161. package/src/llama.cpp/prompts/chat-with-vicuna-v1.txt +7 -0
  162. package/src/llama.cpp/prompts/chat.txt +28 -0
  163. package/src/llama.cpp/prompts/dan-modified.txt +1 -0
  164. package/src/llama.cpp/prompts/dan.txt +1 -0
  165. package/src/llama.cpp/prompts/mnemonics.txt +93 -0
  166. package/src/llama.cpp/prompts/parallel-questions.txt +43 -0
  167. package/src/llama.cpp/prompts/reason-act.txt +18 -0
  168. package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +3 -0
  169. package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +1 -0
  170. package/src/llama.cpp/requirements/requirements-convert-lora-to-ggml.txt +2 -0
  171. package/src/llama.cpp/requirements/requirements-convert-persimmon-to-gguf.txt +2 -0
  172. package/src/llama.cpp/requirements/requirements-convert.txt +5 -0
  173. package/src/llama.cpp/requirements.txt +12 -0
  174. package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +24 -0
  175. package/src/llama.cpp/scripts/xxd.cmake +16 -0
  176. package/src/llama.cpp/sgemm.cpp +999 -0
  177. package/src/llama.cpp/sgemm.h +12 -0
  178. package/src/llama.cpp/tests/CMakeLists.txt +78 -0
  179. package/src/llama.cpp/tests/get-model.cpp +21 -0
  180. package/src/llama.cpp/tests/get-model.h +2 -0
  181. package/src/llama.cpp/tests/test-autorelease.cpp +24 -0
  182. package/src/llama.cpp/tests/test-backend-ops.cpp +2266 -0
  183. package/src/llama.cpp/tests/test-c.c +7 -0
  184. package/src/llama.cpp/tests/test-chat-template.cpp +107 -0
  185. package/src/llama.cpp/tests/test-double-float.cpp +57 -0
  186. package/src/llama.cpp/tests/test-grad0.cpp +1606 -0
  187. package/src/llama.cpp/tests/test-grammar-integration.cpp +243 -0
  188. package/src/llama.cpp/tests/test-grammar-parser.cpp +250 -0
  189. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +899 -0
  190. package/src/llama.cpp/tests/test-llama-grammar.cpp +402 -0
  191. package/src/llama.cpp/tests/test-model-load-cancel.cpp +27 -0
  192. package/src/llama.cpp/tests/test-opt.cpp +181 -0
  193. package/src/llama.cpp/tests/test-quantize-fns.cpp +185 -0
  194. package/src/llama.cpp/tests/test-quantize-perf.cpp +363 -0
  195. package/src/llama.cpp/tests/test-rope.cpp +221 -0
  196. package/src/llama.cpp/tests/test-sampling.cpp +301 -0
  197. package/src/llama.cpp/tests/test-tokenizer-0-falcon.cpp +187 -0
  198. package/src/llama.cpp/tests/test-tokenizer-0-llama.cpp +190 -0
  199. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +123 -0
  200. package/src/llama.cpp/tests/test-tokenizer-1-llama.cpp +111 -0
  201. package/src/llama.cpp/unicode-data.cpp +1651 -0
  202. package/src/llama.cpp/unicode-data.h +16 -0
  203. package/src/llama.cpp/unicode.cpp +277 -0
  204. package/src/llama.cpp/unicode.h +28 -0
@@ -0,0 +1,172 @@
1
+ // Compatible with Zig Version 0.11.0
2
+ const std = @import("std");
3
+ const ArrayList = std.ArrayList;
4
+ const Compile = std.Build.Step.Compile;
5
+ const ConfigHeader = std.Build.Step.ConfigHeader;
6
+ const Mode = std.builtin.Mode;
7
+ const CrossTarget = std.zig.CrossTarget;
8
+
9
+ const Maker = struct {
10
+ builder: *std.build.Builder,
11
+ target: CrossTarget,
12
+ optimize: Mode,
13
+ enable_lto: bool,
14
+
15
+ include_dirs: ArrayList([]const u8),
16
+ cflags: ArrayList([]const u8),
17
+ cxxflags: ArrayList([]const u8),
18
+ objs: ArrayList(*Compile),
19
+
20
+ fn addInclude(m: *Maker, dir: []const u8) !void {
21
+ try m.include_dirs.append(dir);
22
+ }
23
+ fn addProjectInclude(m: *Maker, path: []const []const u8) !void {
24
+ try m.addInclude(try m.builder.build_root.join(m.builder.allocator, path));
25
+ }
26
+ fn addCFlag(m: *Maker, flag: []const u8) !void {
27
+ try m.cflags.append(flag);
28
+ }
29
+ fn addCxxFlag(m: *Maker, flag: []const u8) !void {
30
+ try m.cxxflags.append(flag);
31
+ }
32
+ fn addFlag(m: *Maker, flag: []const u8) !void {
33
+ try m.addCFlag(flag);
34
+ try m.addCxxFlag(flag);
35
+ }
36
+
37
+ fn init(builder: *std.build.Builder) !Maker {
38
+ const target = builder.standardTargetOptions(.{});
39
+ const zig_version = @import("builtin").zig_version_string;
40
+ const commit_hash = try std.ChildProcess.exec(
41
+ .{ .allocator = builder.allocator, .argv = &.{ "git", "rev-parse", "HEAD" } },
42
+ );
43
+ try std.fs.cwd().writeFile("common/build-info.cpp", builder.fmt(
44
+ \\int LLAMA_BUILD_NUMBER = {};
45
+ \\char const *LLAMA_COMMIT = "{s}";
46
+ \\char const *LLAMA_COMPILER = "Zig {s}";
47
+ \\char const *LLAMA_BUILD_TARGET = "{s}";
48
+ \\
49
+ , .{ 0, commit_hash.stdout[0 .. commit_hash.stdout.len - 1], zig_version, try target.allocDescription(builder.allocator) }));
50
+ var m = Maker{
51
+ .builder = builder,
52
+ .target = target,
53
+ .optimize = builder.standardOptimizeOption(.{}),
54
+ .enable_lto = false,
55
+ .include_dirs = ArrayList([]const u8).init(builder.allocator),
56
+ .cflags = ArrayList([]const u8).init(builder.allocator),
57
+ .cxxflags = ArrayList([]const u8).init(builder.allocator),
58
+ .objs = ArrayList(*Compile).init(builder.allocator),
59
+ };
60
+
61
+ try m.addCFlag("-std=c11");
62
+ try m.addCxxFlag("-std=c++11");
63
+ try m.addProjectInclude(&.{});
64
+ try m.addProjectInclude(&.{"common"});
65
+ return m;
66
+ }
67
+
68
+ fn obj(m: *const Maker, name: []const u8, src: []const u8) *Compile {
69
+ const o = m.builder.addObject(.{ .name = name, .target = m.target, .optimize = m.optimize });
70
+ if (o.target.getAbi() != .msvc)
71
+ o.defineCMacro("_GNU_SOURCE", null);
72
+
73
+ if (std.mem.endsWith(u8, src, ".c")) {
74
+ o.addCSourceFiles(&.{src}, m.cflags.items);
75
+ o.linkLibC();
76
+ } else {
77
+ o.addCSourceFiles(&.{src}, m.cxxflags.items);
78
+ if (o.target.getAbi() == .msvc) {
79
+ o.linkLibC(); // need winsdk + crt
80
+ } else {
81
+ // linkLibCpp already add (libc++ + libunwind + libc)
82
+ o.linkLibCpp();
83
+ }
84
+ }
85
+ for (m.include_dirs.items) |i| o.addIncludePath(.{ .path = i });
86
+ o.want_lto = m.enable_lto;
87
+ return o;
88
+ }
89
+
90
+ fn exe(m: *const Maker, name: []const u8, src: []const u8, deps: []const *Compile) *Compile {
91
+ const e = m.builder.addExecutable(.{ .name = name, .target = m.target, .optimize = m.optimize });
92
+ e.addCSourceFiles(&.{src}, m.cxxflags.items);
93
+ for (deps) |d| e.addObject(d);
94
+ for (m.objs.items) |o| e.addObject(o);
95
+ for (m.include_dirs.items) |i| e.addIncludePath(.{ .path = i });
96
+
97
+ // https://github.com/ziglang/zig/issues/15448
98
+ if (e.target.getAbi() == .msvc) {
99
+ e.linkLibC(); // need winsdk + crt
100
+ } else {
101
+ // linkLibCpp already add (libc++ + libunwind + libc)
102
+ e.linkLibCpp();
103
+ }
104
+ m.builder.installArtifact(e);
105
+ e.want_lto = m.enable_lto;
106
+ return e;
107
+ }
108
+ };
109
+
110
+ pub fn build(b: *std.build.Builder) !void {
111
+ var make = try Maker.init(b);
112
+ make.enable_lto = b.option(bool, "lto", "Enable LTO optimization, (default: false)") orelse false;
113
+
114
+ const ggml = make.obj("ggml", "ggml.c");
115
+ const sgemm = make.obj("sgemm", "sgemm.cpp");
116
+ const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c");
117
+ const ggml_backend = make.obj("ggml-backend", "ggml-backend.c");
118
+ const ggml_quants = make.obj("ggml-quants", "ggml-quants.c");
119
+ const unicode = make.obj("unicode", "unicode.cpp");
120
+ const unicode_data = make.obj("unicode-data", "unicode-data.cpp");
121
+ const llama = make.obj("llama", "llama.cpp");
122
+ const buildinfo = make.obj("common", "common/build-info.cpp");
123
+ const common = make.obj("common", "common/common.cpp");
124
+ const console = make.obj("console", "common/console.cpp");
125
+ const sampling = make.obj("sampling", "common/sampling.cpp");
126
+ const grammar_parser = make.obj("grammar-parser", "common/grammar-parser.cpp");
127
+ const json_schema_to_grammar = make.obj("json-schema-to-grammar", "common/json-schema-to-grammar.cpp");
128
+ const train = make.obj("train", "common/train.cpp");
129
+ const clip = make.obj("clip", "examples/llava/clip.cpp");
130
+ const llava = make.obj("llava", "examples/llava/llava.cpp");
131
+
132
+ _ = make.exe("main", "examples/main/main.cpp", &.{ ggml, sgemm, ggml_alloc, ggml_backend, ggml_quants, llama, unicode, unicode_data, common, json_schema_to_grammar, buildinfo, sampling, console, grammar_parser });
133
+ _ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, sgemm, ggml_alloc, ggml_backend, ggml_quants, llama, unicode, unicode_data, common, json_schema_to_grammar, buildinfo });
134
+ _ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, sgemm, ggml_alloc, ggml_backend, ggml_quants, llama, unicode, unicode_data, common, json_schema_to_grammar, buildinfo });
135
+ _ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, sgemm, ggml_alloc, ggml_backend, ggml_quants, llama, unicode, unicode_data, common, json_schema_to_grammar, buildinfo });
136
+ _ = make.exe("finetune", "examples/finetune/finetune.cpp", &.{ ggml, sgemm, ggml_alloc, ggml_backend, ggml_quants, llama, unicode, unicode_data, common, json_schema_to_grammar, buildinfo, train });
137
+ _ = make.exe("train-text-from-scratch", "examples/train-text-from-scratch/train-text-from-scratch.cpp", &.{ ggml, sgemm, ggml_alloc, ggml_backend, ggml_quants, llama, unicode, unicode_data, common, json_schema_to_grammar, buildinfo, train });
138
+
139
+ const server = make.exe("server", "examples/server/server.cpp", &.{ ggml, sgemm, ggml_alloc, ggml_backend, ggml_quants, llama, unicode, unicode_data, common, json_schema_to_grammar, buildinfo, sampling, grammar_parser, clip, llava });
140
+ if (server.target.isWindows()) {
141
+ server.linkSystemLibrary("ws2_32");
142
+ }
143
+
144
+ const server_assets = [_][]const u8{ "index.html", "index.js", "completion.js", "json-schema-to-grammar.mjs" };
145
+ for (server_assets) |asset| {
146
+ const input_path = b.fmt("examples/server/public/{s}", .{asset});
147
+ const output_path = b.fmt("examples/server/{s}.hpp", .{asset});
148
+
149
+ // Portable equivalent of `b.addSystemCommand(&.{ "xxd", "-n", asset, "-i", input_path, output_path }) })`:
150
+
151
+ const input = try std.fs.cwd().readFileAlloc(b.allocator, input_path, std.math.maxInt(usize));
152
+ defer b.allocator.free(input);
153
+
154
+ var buf = std.ArrayList(u8).init(b.allocator);
155
+ defer buf.deinit();
156
+
157
+ for (input) |byte| {
158
+ try std.fmt.format(buf.writer(), "0x{X:0>2}, ", .{byte});
159
+ }
160
+
161
+ var name = try std.mem.replaceOwned(u8, b.allocator, asset, "-", "_");
162
+ defer b.allocator.free(name);
163
+ std.mem.replaceScalar(u8, name, '.', '_');
164
+
165
+ try std.fs.cwd().writeFile(output_path, b.fmt(
166
+ "unsigned char {s}[] = {{{s}}};\nunsigned int {s}_len = {d};\n",
167
+ .{ name, buf.items, name, input.len },
168
+ ));
169
+
170
+ std.debug.print("Dumped hex of \"{s}\" ({s}) to {s}\n", .{ input_path, name, output_path });
171
+ }
172
+ }
@@ -0,0 +1,100 @@
1
+ include(CheckCSourceRuns)
2
+
3
+ set(AVX_CODE "
4
+ #include <immintrin.h>
5
+ int main()
6
+ {
7
+ __m256 a;
8
+ a = _mm256_set1_ps(0);
9
+ return 0;
10
+ }
11
+ ")
12
+
13
+ set(AVX512_CODE "
14
+ #include <immintrin.h>
15
+ int main()
16
+ {
17
+ __m512i a = _mm512_set_epi8(0, 0, 0, 0, 0, 0, 0, 0,
18
+ 0, 0, 0, 0, 0, 0, 0, 0,
19
+ 0, 0, 0, 0, 0, 0, 0, 0,
20
+ 0, 0, 0, 0, 0, 0, 0, 0,
21
+ 0, 0, 0, 0, 0, 0, 0, 0,
22
+ 0, 0, 0, 0, 0, 0, 0, 0,
23
+ 0, 0, 0, 0, 0, 0, 0, 0,
24
+ 0, 0, 0, 0, 0, 0, 0, 0);
25
+ __m512i b = a;
26
+ __mmask64 equality_mask = _mm512_cmp_epi8_mask(a, b, _MM_CMPINT_EQ);
27
+ return 0;
28
+ }
29
+ ")
30
+
31
+ set(AVX2_CODE "
32
+ #include <immintrin.h>
33
+ int main()
34
+ {
35
+ __m256i a = {0};
36
+ a = _mm256_abs_epi16(a);
37
+ __m256i x;
38
+ _mm256_extract_epi64(x, 0); // we rely on this in our AVX2 code
39
+ return 0;
40
+ }
41
+ ")
42
+
43
+ set(FMA_CODE "
44
+ #include <immintrin.h>
45
+ int main()
46
+ {
47
+ __m256 acc = _mm256_setzero_ps();
48
+ const __m256 d = _mm256_setzero_ps();
49
+ const __m256 p = _mm256_setzero_ps();
50
+ acc = _mm256_fmadd_ps( d, p, acc );
51
+ return 0;
52
+ }
53
+ ")
54
+
55
+ macro(check_sse type flags)
56
+ set(__FLAG_I 1)
57
+ set(CMAKE_REQUIRED_FLAGS_SAVE ${CMAKE_REQUIRED_FLAGS})
58
+ foreach (__FLAG ${flags})
59
+ if (NOT ${type}_FOUND)
60
+ set(CMAKE_REQUIRED_FLAGS ${__FLAG})
61
+ check_c_source_runs("${${type}_CODE}" HAS_${type}_${__FLAG_I})
62
+ if (HAS_${type}_${__FLAG_I})
63
+ set(${type}_FOUND TRUE CACHE BOOL "${type} support")
64
+ set(${type}_FLAGS "${__FLAG}" CACHE STRING "${type} flags")
65
+ endif()
66
+ math(EXPR __FLAG_I "${__FLAG_I}+1")
67
+ endif()
68
+ endforeach()
69
+ set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE})
70
+
71
+ if (NOT ${type}_FOUND)
72
+ set(${type}_FOUND FALSE CACHE BOOL "${type} support")
73
+ set(${type}_FLAGS "" CACHE STRING "${type} flags")
74
+ endif()
75
+
76
+ mark_as_advanced(${type}_FOUND ${type}_FLAGS)
77
+ endmacro()
78
+
79
+ # flags are for MSVC only!
80
+ check_sse("AVX" " ;/arch:AVX")
81
+ if (NOT ${AVX_FOUND})
82
+ set(LLAMA_AVX OFF)
83
+ else()
84
+ set(LLAMA_AVX ON)
85
+ endif()
86
+
87
+ check_sse("AVX2" " ;/arch:AVX2")
88
+ check_sse("FMA" " ;/arch:AVX2")
89
+ if ((NOT ${AVX2_FOUND}) OR (NOT ${FMA_FOUND}))
90
+ set(LLAMA_AVX2 OFF)
91
+ else()
92
+ set(LLAMA_AVX2 ON)
93
+ endif()
94
+
95
+ check_sse("AVX512" " ;/arch:AVX512")
96
+ if (NOT ${AVX512_FOUND})
97
+ set(LLAMA_AVX512 OFF)
98
+ else()
99
+ set(LLAMA_AVX512 ON)
100
+ endif()
@@ -0,0 +1,87 @@
1
+ # common
2
+
3
+
4
+ # Build info header
5
+ #
6
+
7
+ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../.git")
8
+ set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../.git")
9
+
10
+ # Is git submodule
11
+ if(NOT IS_DIRECTORY "${GIT_DIR}")
12
+ file(READ ${GIT_DIR} REAL_GIT_DIR_LINK)
13
+ string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" REAL_GIT_DIR ${REAL_GIT_DIR_LINK})
14
+ string(FIND "${REAL_GIT_DIR}" "/" SLASH_POS)
15
+ if (SLASH_POS EQUAL 0)
16
+ set(GIT_DIR "${REAL_GIT_DIR}")
17
+ else()
18
+ set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${REAL_GIT_DIR}")
19
+ endif()
20
+ endif()
21
+
22
+ if(EXISTS "${GIT_DIR}/index")
23
+ set(GIT_INDEX "${GIT_DIR}/index")
24
+ else()
25
+ message(WARNING "Git index not found in git repository.")
26
+ set(GIT_INDEX "")
27
+ endif()
28
+ else()
29
+ message(WARNING "Git repository not found; to enable automatic generation of build info, make sure Git is installed and the project is a Git repository.")
30
+ set(GIT_INDEX "")
31
+ endif()
32
+
33
+ # Add a custom command to rebuild build-info.cpp when .git/index changes
34
+ add_custom_command(
35
+ OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/build-info.cpp"
36
+ COMMENT "Generating build details from Git"
37
+ COMMAND ${CMAKE_COMMAND} -DMSVC=${MSVC} -DCMAKE_C_COMPILER_VERSION=${CMAKE_C_COMPILER_VERSION}
38
+ -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID} -DCMAKE_VS_PLATFORM_NAME=${CMAKE_VS_PLATFORM_NAME}
39
+ -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -P "${CMAKE_CURRENT_SOURCE_DIR}/../scripts/gen-build-info-cpp.cmake"
40
+ WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.."
41
+ DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/build-info.cpp.in" ${GIT_INDEX}
42
+ VERBATIM
43
+ )
44
+ set(TARGET build_info)
45
+ add_library(${TARGET} OBJECT build-info.cpp)
46
+ if (BUILD_SHARED_LIBS)
47
+ set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
48
+ endif()
49
+
50
+ set(TARGET common)
51
+
52
+ add_library(${TARGET} STATIC
53
+ base64.hpp
54
+ common.h
55
+ common.cpp
56
+ sampling.h
57
+ sampling.cpp
58
+ console.h
59
+ console.cpp
60
+ grammar-parser.h
61
+ grammar-parser.cpp
62
+ json.hpp
63
+ json-schema-to-grammar.cpp
64
+ train.h
65
+ train.cpp
66
+ ngram-cache.h
67
+ ngram-cache.cpp
68
+ )
69
+
70
+ if (BUILD_SHARED_LIBS)
71
+ set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
72
+ endif()
73
+
74
+ set(LLAMA_COMMON_EXTRA_LIBS build_info)
75
+
76
+ # Use curl to download model url
77
+ if (LLAMA_CURL)
78
+ find_package(CURL REQUIRED)
79
+ add_definitions(-DLLAMA_USE_CURL)
80
+ include_directories(${CURL_INCLUDE_DIRS})
81
+ find_library(CURL_LIBRARY curl REQUIRED)
82
+ set(LLAMA_COMMON_EXTRA_LIBS ${LLAMA_COMMON_EXTRA_LIBS} ${CURL_LIBRARY})
83
+ endif ()
84
+
85
+ target_include_directories(${TARGET} PUBLIC .)
86
+ target_compile_features(${TARGET} PUBLIC cxx_std_11)
87
+ target_link_libraries(${TARGET} PRIVATE ${LLAMA_COMMON_EXTRA_LIBS} PUBLIC llama)