Skip to content

Commit b58d4d0

Browse files
committed
feat(pkg): expose complete llamacpp module API
1 parent 048e752 commit b58d4d0

10 files changed

Lines changed: 112 additions & 63 deletions

File tree

pkgs/g/ggml-org.llamacpp.lua

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ package = {
4949
"*/src",
5050
"mcpp_generated",
5151
},
52-
modules = { "llama" },
52+
modules = { "llamacpp" },
5353
generated_files = {
5454
["mcpp_generated/ggml_cpp.cpp"] = "#include \"ggml.cpp\"\n",
5555
["mcpp_generated/ggml-cpu_cpp.cpp"] = "#include \"ggml-cpu.cpp\"\n",
@@ -59,7 +59,7 @@ package = {
5959
#define GGML_VERSION "b10069"
6060
#define GGML_COMMIT "178a6c44937154dc4c4eff0d166f4a044c4fceba"
6161
]=],
62-
["mcpp_generated/llama.cppm"] = [==[
62+
["mcpp_generated/llamacpp.cppm"] = [==[
6363
module;
6464
6565
#include <llama.h>
@@ -80,7 +80,7 @@ module;
8080
#undef LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY
8181
#undef LLAMA_STATE_SEQ_FLAGS_ON_DEVICE
8282
83-
export module llama;
83+
export module llamacpp;
8484
8585
#include "gen_exports/required_ggml.inc"
8686
#include "gen_exports/llama.inc"
@@ -320,6 +320,8 @@ export using ::llama_adapter_meta_count;
320320
export using ::llama_adapter_meta_key_by_index;
321321
export using ::llama_adapter_meta_val_str;
322322
export using ::llama_adapter_meta_val_str_by_index;
323+
export using ::llama_add_bos_token;
324+
export using ::llama_add_eos_token;
323325
export using ::llama_attach_threadpool;
324326
export using ::llama_attention_type;
325327
export using ::llama_backend_free;
@@ -335,13 +337,15 @@ export using ::llama_context;
335337
export using ::llama_context_default_params;
336338
export using ::llama_context_params;
337339
export using ::llama_context_type;
340+
export using ::llama_copy_state_data;
338341
export using ::llama_decode;
339342
export using ::llama_detach_threadpool;
340343
export using ::llama_detokenize;
341344
export using ::llama_encode;
342345
export using ::llama_flash_attn_type;
343346
export using ::llama_flash_attn_type_name;
344347
export using ::llama_free;
348+
export using ::llama_free_model;
345349
export using ::llama_ftype;
346350
export using ::llama_ftype_name;
347351
export using ::llama_get_embeddings;
@@ -358,7 +362,10 @@ export using ::llama_get_sampled_logits_ith;
358362
export using ::llama_get_sampled_probs_count_ith;
359363
export using ::llama_get_sampled_probs_ith;
360364
export using ::llama_get_sampled_token_ith;
365+
export using ::llama_get_state_size;
361366
export using ::llama_init_from_model;
367+
export using ::llama_load_model_from_file;
368+
export using ::llama_load_session_file;
362369
export using ::llama_log_get;
363370
export using ::llama_log_set;
364371
export using ::llama_logit_bias;
@@ -428,11 +435,17 @@ export using ::llama_model_tensor_override;
428435
export using ::llama_n_batch;
429436
export using ::llama_n_ctx;
430437
export using ::llama_n_ctx_seq;
438+
export using ::llama_n_ctx_train;
439+
export using ::llama_n_embd;
440+
export using ::llama_n_head;
441+
export using ::llama_n_layer;
431442
export using ::llama_n_rs_seq;
432443
export using ::llama_n_seq_max;
433444
export using ::llama_n_threads;
434445
export using ::llama_n_threads_batch;
435446
export using ::llama_n_ubatch;
447+
export using ::llama_n_vocab;
448+
export using ::llama_new_context_with_model;
436449
export using ::llama_numa_init;
437450
export using ::llama_opt_epoch;
438451
export using ::llama_opt_init;
@@ -474,6 +487,7 @@ export using ::llama_sampler_init_adaptive_p;
474487
export using ::llama_sampler_init_dist;
475488
export using ::llama_sampler_init_dry;
476489
export using ::llama_sampler_init_grammar;
490+
export using ::llama_sampler_init_grammar_lazy;
477491
export using ::llama_sampler_init_grammar_lazy_patterns;
478492
export using ::llama_sampler_init_greedy;
479493
export using ::llama_sampler_init_infill;
@@ -493,6 +507,7 @@ export using ::llama_sampler_name;
493507
export using ::llama_sampler_reset;
494508
export using ::llama_sampler_sample;
495509
export using ::llama_sampler_seq_config;
510+
export using ::llama_save_session_file;
496511
export using ::llama_seq_id;
497512
export using ::llama_set_abort_callback;
498513
export using ::llama_set_adapter_cvec;
@@ -501,6 +516,8 @@ export using ::llama_set_causal_attn;
501516
export using ::llama_set_embeddings;
502517
export using ::llama_set_n_threads;
503518
export using ::llama_set_sampler;
519+
export using ::llama_set_state_data;
520+
export using ::llama_set_warmup;
504521
export using ::llama_split_mode;
505522
export using ::llama_split_path;
506523
export using ::llama_split_prefix;
@@ -526,13 +543,32 @@ export using ::llama_synchronize;
526543
export using ::llama_time_us;
527544
export using ::llama_token;
528545
export using ::llama_token_attr;
546+
export using ::llama_token_bos;
547+
export using ::llama_token_cls;
529548
export using ::llama_token_data;
530549
export using ::llama_token_data_array;
550+
export using ::llama_token_eos;
551+
export using ::llama_token_eot;
552+
export using ::llama_token_fim_mid;
553+
export using ::llama_token_fim_pad;
554+
export using ::llama_token_fim_pre;
555+
export using ::llama_token_fim_rep;
556+
export using ::llama_token_fim_sep;
557+
export using ::llama_token_fim_suf;
558+
export using ::llama_token_get_attr;
559+
export using ::llama_token_get_score;
560+
export using ::llama_token_get_text;
561+
export using ::llama_token_is_control;
562+
export using ::llama_token_is_eog;
563+
export using ::llama_token_nl;
564+
export using ::llama_token_pad;
565+
export using ::llama_token_sep;
531566
export using ::llama_token_to_piece;
532567
export using ::llama_token_type;
533568
export using ::llama_tokenize;
534569
export using ::llama_vocab;
535570
export using ::llama_vocab_bos;
571+
export using ::llama_vocab_cls;
536572
export using ::llama_vocab_eos;
537573
export using ::llama_vocab_eot;
538574
export using ::llama_vocab_fim_mid;
@@ -697,7 +733,7 @@ int main() try {
697733
"*/ggml/src/ggml-cpu/amx/amx.cpp",
698734
"*/ggml/src/ggml-cpu/amx/mmq.cpp",
699735
"*/ggml/src/ggml-cpu/llamafile/sgemm.cpp",
700-
"mcpp_generated/llama.cppm",
736+
"mcpp_generated/llamacpp.cppm",
701737
"*/src/llama.cpp",
702738
"*/src/llama-adapter.cpp",
703739
"*/src/llama-arch.cpp",

tests/check_llamacpp_snapshot.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -657,22 +657,22 @@ def check_module_contract(descriptors: dict[str, dict]) -> None:
657657
mcpp = get_path(descriptors["ggml-org.llamacpp"], "mcpp")
658658
require(mcpp.get("targets") == {"llama": {"kind": "lib"}},
659659
"ggml-org.llamacpp must expose exactly one llama target")
660-
require(array(mcpp.get("modules"), "ggml-org.llamacpp.modules") == ["llama"],
661-
"ggml-org.llamacpp must expose exactly the llama module")
660+
require(array(mcpp.get("modules"), "ggml-org.llamacpp.modules") == ["llamacpp"],
661+
"ggml-org.llamacpp must expose exactly the llamacpp module")
662662
generated = get_path(mcpp, "generated_files")
663663
for generated_name, source_name in (
664-
("mcpp_generated/llama.cppm", "llama.cppm"),
664+
("mcpp_generated/llamacpp.cppm", "llamacpp.cppm"),
665665
("mcpp_generated/gen_exports/required_ggml.inc", "gen_exports/required_ggml.inc"),
666666
("mcpp_generated/gen_exports/llama.inc", "gen_exports/llama.inc"),
667667
):
668668
expected = (ROOT / "tools/llamacpp/module" / source_name).read_text()
669669
require(generated.get(generated_name) == expected,
670670
f"generated module input drift: {generated_name}")
671-
module = generated["mcpp_generated/llama.cppm"]
672-
require("export module llama;" in module
671+
module = generated["mcpp_generated/llamacpp.cppm"]
672+
require("export module llamacpp;" in module
673673
and "#include <llama.h>" in module
674674
and "#include \"gen_exports/llama.inc\"" in module,
675-
"llama module wrapper contract drift")
675+
"llamacpp module wrapper contract drift")
676676

677677

678678
def main() -> int:

tests/examples/llamacpp-internal-cpu/tests/decode.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
import std;
55

6-
import llama;
6+
import llamacpp;
77

88
#ifdef LLAMA_H
9-
#error "import llama leaked LLAMA_H"
9+
#error "import llamacpp leaked LLAMA_H"
1010
#endif
1111

1212
#ifdef LLAMA_API
13-
#error "import llama leaked LLAMA_API"
13+
#error "import llamacpp leaked LLAMA_API"
1414
#endif
1515

1616

tests/examples/llamacpp-internal-metal/tests/decode.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
import std;
1010

11-
import llama;
11+
import llamacpp;
1212

1313
#ifdef LLAMA_H
14-
#error "import llama leaked LLAMA_H"
14+
#error "import llamacpp leaked LLAMA_H"
1515
#endif
1616

1717
#ifdef LLAMA_API
18-
#error "import llama leaked LLAMA_API"
18+
#error "import llamacpp leaked LLAMA_API"
1919
#endif
2020

2121

tests/test_check_llamacpp_snapshot.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,18 @@ def test_rejects_llamafile_macro_removal(self):
177177

178178
def test_rejects_generated_module_drift(self):
179179
generated = self.descriptors["ggml-org.llamacpp"]["mcpp"]["generated_files"]
180-
generated["mcpp_generated/llama.cppm"] += "\n// drift\n"
180+
generated["mcpp_generated/llamacpp.cppm"] += "\n// drift\n"
181181
self.assert_rejected(checker.check_module_contract, "generated module input drift")
182182

183+
def test_module_name_and_file_are_llamacpp(self):
184+
mcpp = self.descriptors["ggml-org.llamacpp"]["mcpp"]
185+
self.assertEqual(mcpp["modules"], {1: "llamacpp"})
186+
module = mcpp["generated_files"]["mcpp_generated/llamacpp.cppm"]
187+
self.assertIn("export module llamacpp;", module)
188+
183189
def test_rejects_module_name_drift(self):
184190
self.descriptors["ggml-org.llamacpp"]["mcpp"]["modules"] = {1: "wrong"}
185-
self.assert_rejected(checker.check_module_contract, "exactly the llama module")
191+
self.assert_rejected(checker.check_module_contract, "exactly the llamacpp module")
186192

187193
def test_rejects_extra_target(self):
188194
self.descriptors["ggml-org.llamacpp"]["mcpp"]["targets"]["extra"] = {
@@ -268,7 +274,7 @@ def test_metal_member_consumes_merged_package(self):
268274
self.assertIn('"tests/examples/llamacpp-internal-metal"', root_manifest)
269275
self.assertIn('features = ["backend-metal"]', metal_manifest.read_text())
270276
self.assertNotIn("compat.ggml-metal", metal_manifest.read_text())
271-
self.assertIn("import llama;", (checker.ROOT / "tests/examples/llamacpp-internal-metal/tests/decode.cpp").read_text())
277+
self.assertIn("import llamacpp;", (checker.ROOT / "tests/examples/llamacpp-internal-metal/tests/decode.cpp").read_text())
272278

273279

274280
if __name__ == "__main__":

tools/llamacpp/gen_exports.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,7 @@ def _walk(node):
140140
src_line = llama_lines[line_no - 1]
141141
else:
142142
src_line = ""
143-
# Check for DeprecatedAttr
144-
has_deprecated = any(
145-
c.get("kind") == "DeprecatedAttr"
146-
for c in node.get("inner", []))
147-
if has_deprecated or "DEPRECATED" in src_line:
148-
skipped.append(f"deprecated function '{name}'")
149-
elif "LLAMA_API" in src_line:
143+
if "LLAMA_API" in src_line:
150144
llama_exports.append(f"export using ::{name};")
151145
# else: static/inline helpers, skip
152146

tools/llamacpp/module/gen_exports/llama.inc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ export using ::llama_adapter_meta_count;
115115
export using ::llama_adapter_meta_key_by_index;
116116
export using ::llama_adapter_meta_val_str;
117117
export using ::llama_adapter_meta_val_str_by_index;
118+
export using ::llama_add_bos_token;
119+
export using ::llama_add_eos_token;
118120
export using ::llama_attach_threadpool;
119121
export using ::llama_attention_type;
120122
export using ::llama_backend_free;
@@ -130,13 +132,15 @@ export using ::llama_context;
130132
export using ::llama_context_default_params;
131133
export using ::llama_context_params;
132134
export using ::llama_context_type;
135+
export using ::llama_copy_state_data;
133136
export using ::llama_decode;
134137
export using ::llama_detach_threadpool;
135138
export using ::llama_detokenize;
136139
export using ::llama_encode;
137140
export using ::llama_flash_attn_type;
138141
export using ::llama_flash_attn_type_name;
139142
export using ::llama_free;
143+
export using ::llama_free_model;
140144
export using ::llama_ftype;
141145
export using ::llama_ftype_name;
142146
export using ::llama_get_embeddings;
@@ -153,7 +157,10 @@ export using ::llama_get_sampled_logits_ith;
153157
export using ::llama_get_sampled_probs_count_ith;
154158
export using ::llama_get_sampled_probs_ith;
155159
export using ::llama_get_sampled_token_ith;
160+
export using ::llama_get_state_size;
156161
export using ::llama_init_from_model;
162+
export using ::llama_load_model_from_file;
163+
export using ::llama_load_session_file;
157164
export using ::llama_log_get;
158165
export using ::llama_log_set;
159166
export using ::llama_logit_bias;
@@ -223,11 +230,17 @@ export using ::llama_model_tensor_override;
223230
export using ::llama_n_batch;
224231
export using ::llama_n_ctx;
225232
export using ::llama_n_ctx_seq;
233+
export using ::llama_n_ctx_train;
234+
export using ::llama_n_embd;
235+
export using ::llama_n_head;
236+
export using ::llama_n_layer;
226237
export using ::llama_n_rs_seq;
227238
export using ::llama_n_seq_max;
228239
export using ::llama_n_threads;
229240
export using ::llama_n_threads_batch;
230241
export using ::llama_n_ubatch;
242+
export using ::llama_n_vocab;
243+
export using ::llama_new_context_with_model;
231244
export using ::llama_numa_init;
232245
export using ::llama_opt_epoch;
233246
export using ::llama_opt_init;
@@ -269,6 +282,7 @@ export using ::llama_sampler_init_adaptive_p;
269282
export using ::llama_sampler_init_dist;
270283
export using ::llama_sampler_init_dry;
271284
export using ::llama_sampler_init_grammar;
285+
export using ::llama_sampler_init_grammar_lazy;
272286
export using ::llama_sampler_init_grammar_lazy_patterns;
273287
export using ::llama_sampler_init_greedy;
274288
export using ::llama_sampler_init_infill;
@@ -288,6 +302,7 @@ export using ::llama_sampler_name;
288302
export using ::llama_sampler_reset;
289303
export using ::llama_sampler_sample;
290304
export using ::llama_sampler_seq_config;
305+
export using ::llama_save_session_file;
291306
export using ::llama_seq_id;
292307
export using ::llama_set_abort_callback;
293308
export using ::llama_set_adapter_cvec;
@@ -296,6 +311,8 @@ export using ::llama_set_causal_attn;
296311
export using ::llama_set_embeddings;
297312
export using ::llama_set_n_threads;
298313
export using ::llama_set_sampler;
314+
export using ::llama_set_state_data;
315+
export using ::llama_set_warmup;
299316
export using ::llama_split_mode;
300317
export using ::llama_split_path;
301318
export using ::llama_split_prefix;
@@ -321,13 +338,32 @@ export using ::llama_synchronize;
321338
export using ::llama_time_us;
322339
export using ::llama_token;
323340
export using ::llama_token_attr;
341+
export using ::llama_token_bos;
342+
export using ::llama_token_cls;
324343
export using ::llama_token_data;
325344
export using ::llama_token_data_array;
345+
export using ::llama_token_eos;
346+
export using ::llama_token_eot;
347+
export using ::llama_token_fim_mid;
348+
export using ::llama_token_fim_pad;
349+
export using ::llama_token_fim_pre;
350+
export using ::llama_token_fim_rep;
351+
export using ::llama_token_fim_sep;
352+
export using ::llama_token_fim_suf;
353+
export using ::llama_token_get_attr;
354+
export using ::llama_token_get_score;
355+
export using ::llama_token_get_text;
356+
export using ::llama_token_is_control;
357+
export using ::llama_token_is_eog;
358+
export using ::llama_token_nl;
359+
export using ::llama_token_pad;
360+
export using ::llama_token_sep;
326361
export using ::llama_token_to_piece;
327362
export using ::llama_token_type;
328363
export using ::llama_tokenize;
329364
export using ::llama_vocab;
330365
export using ::llama_vocab_bos;
366+
export using ::llama_vocab_cls;
331367
export using ::llama_vocab_eos;
332368
export using ::llama_vocab_eot;
333369
export using ::llama_vocab_fim_mid;

0 commit comments

Comments
 (0)