Repository navigation
Expand file tree
/
Copy pathtest_runtime.py
More file actions
765 lines (628 loc) · 23.6 KB
/
Copy pathtest_runtime.py
File metadata and controls
765 lines (628 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
import os
import ctypes
import json
import multiprocessing
import threading
from types import SimpleNamespace
import pytest
import numpy as np
import llama_cpp
import llama_cpp._internals as internals
from llama_cpp.llama_embedding import LlamaEmbedding, LLAMA_POOLING_TYPE_NONE
MODEL = "./vendor/llama.cpp/models/ggml-vocab-llama-spm.gguf"
@pytest.mark.parametrize("entry", ["create_completion", "__call__", "create_chat_completion"])
@pytest.mark.parametrize("present,expected", [(0.0, 1.5), (0.7, 0.7)])
def test_public_generation_options_reach_execution(entry, present, expected):
llm = object.__new__(llama_cpp.Llama)
calls = []
def completion(**kwargs):
calls.append(kwargs)
yield {"choices": [{"text": "", "finish_reason": "length"}]}
def chat(**kwargs):
calls.append(kwargs)
return {"choices": [{"message": {"content": ""}}]}
llm._create_completion = completion
llm.chat_handler, llm._chat_handlers, llm.chat_format = chat, {}, None
options = dict(present_penalty=present, presence_penalty=1.5)
if entry == "create_chat_completion":
llm.create_chat_completion(messages=[{"role": "user", "content": "Hello"}], **options)
else:
getattr(llm, entry)([1], ignore_eos=True, **options)
assert calls[0]["ignore_eos"] is True
assert calls[0]["present_penalty"] == expected
assert "presence_penalty" not in calls[0]
@pytest.mark.parametrize(
("ignore_eos", "expected_text", "expected_finish_reason"),
[(False, "", "stop"), (True, "<eog>", "length")],
)
def test_private_completion_respects_ignore_eos_at_eog_boundary(
monkeypatch, ignore_eos, expected_text, expected_finish_reason
):
llm = object.__new__(llama_cpp.Llama)
forwarded = []
def fake_generate(tokens, **kwargs):
assert tokens == [1]
forwarded.append(kwargs["ignore_eos"])
yield 2
llm._model = SimpleNamespace(
vocab=object(),
token_bos=lambda: 1,
token_eos=lambda: 2,
token_sep=lambda: -1,
token_fim_pre=lambda: -1,
token_fim_mid=lambda: -1,
token_fim_suf=lambda: -1,
get_add_sep=lambda: False,
)
llm._abort_event = threading.Event()
llm.metadata = {}
llm.spm_infill = False
llm.verbose = False
llm._n_ctx = 8
llm._logits_all = False
llm._seed = 0
llm.cache = None
llm.model_path = "fake.gguf"
llm.input_ids = np.zeros(8, dtype=np.intc)
llm.n_tokens = 1
llm.scores = np.zeros((1, 4), dtype=np.float32)
llm.generate = fake_generate
llm.detokenize = (
lambda tokens, prev_tokens=None, **kwargs: b"".join(
b"<eog>" if token == 2 else b"x" for token in tokens
)
)
monkeypatch.setattr(
"llama_cpp.llama.llama_cpp_lib.llama_token_is_eog",
lambda vocab, token: token == 2,
)
result = next(
llm._create_completion(
prompt=[1], max_tokens=1, ignore_eos=ignore_eos
)
)
assert forwarded == [ignore_eos]
assert result["choices"][0]["text"] == expected_text
assert result["choices"][0]["finish_reason"] == expected_finish_reason
def test_model_init_frees_native_model_when_vocab_lookup_fails(monkeypatch):
native_model_handle = object()
freed_model_handles = []
def model_path_exists(_path):
return True
def load_native_model(_path, _params):
return native_model_handle
def fail_to_get_model_vocab(_model_handle):
return None
def record_model_free(model_handle):
freed_model_handles.append(model_handle)
monkeypatch.setattr(internals.os.path, "exists", model_path_exists)
monkeypatch.setattr(
internals.llama_cpp,
"llama_model_load_from_file",
load_native_model,
)
monkeypatch.setattr(
internals.llama_cpp,
"llama_model_get_vocab",
fail_to_get_model_vocab,
)
monkeypatch.setattr(
internals.llama_cpp,
"llama_model_free",
record_model_free,
)
with pytest.raises(ValueError, match="Failed to get vocab"):
internals.LlamaModel(
path_model="model.gguf",
params=object(),
verbose=False,
)
assert freed_model_handles == [native_model_handle]
def test_batch_init_frees_native_batch_when_validation_fails(monkeypatch):
class InvalidMixedNativeBatch:
token = object()
embd = object()
invalid_mixed_batch = InvalidMixedNativeBatch()
freed_batch_handles = []
def allocate_invalid_mixed_batch(_n_tokens, _embd, _n_seq_max):
return invalid_mixed_batch
def record_batch_free(batch_handle):
freed_batch_handles.append(batch_handle)
monkeypatch.setattr(
internals.llama_cpp,
"llama_batch_init",
allocate_invalid_mixed_batch,
)
monkeypatch.setattr(
internals.llama_cpp,
"llama_batch_free",
record_batch_free,
)
with pytest.raises(RuntimeError, match="expected batch.token to be NULL"):
internals.LlamaBatch(
n_tokens=1,
embd=1,
n_seq_max=1,
mixed=True,
verbose=False,
)
assert freed_batch_handles == [invalid_mixed_batch]
def test_context_manages_borrowed_threadpool_references(monkeypatch):
context = internals.LlamaContext.__new__(internals.LlamaContext)
context.ctx = object()
context._threadpool_refs = None
calls = []
monkeypatch.setattr(
internals.llama_cpp,
"llama_synchronize",
lambda ctx: calls.append(("synchronize", ctx)),
)
monkeypatch.setattr(
internals.llama_cpp,
"llama_attach_threadpool",
lambda ctx, pool, batch_pool: calls.append(
("attach", ctx, pool, batch_pool)
),
)
monkeypatch.setattr(
internals.llama_cpp,
"llama_detach_threadpool",
lambda ctx: calls.append(("detach", ctx)),
)
pool = ctypes.c_void_p(1)
context.attach_threadpool(pool)
assert context._threadpool_refs == (pool, pool)
assert calls[-2:] == [
("synchronize", context.ctx),
("attach", context.ctx, pool, None),
]
context.detach_threadpool()
assert context._threadpool_refs is None
assert calls[-2:] == [
("synchronize", context.ctx),
("detach", context.ctx),
]
def test_context_manages_native_abort_callback_lifetime(monkeypatch):
context = internals.LlamaContext.__new__(internals.LlamaContext)
context.ctx = object()
context._abort_callback_ref = None
context._abort_callback_data_ref = None
calls = []
monkeypatch.setattr(
internals.llama_cpp,
"llama_set_abort_callback",
lambda ctx, callback, data: calls.append((ctx, callback, data)),
)
data = ctypes.c_void_p(42)
context.set_abort_callback(lambda user_data: user_data == data.value, data)
callback = context._abort_callback_ref
assert callback(data) is True
assert context._abort_callback_data_ref is data
assert calls[-1] == (context.ctx, callback, data)
context.set_abort_callback(None)
assert context._abort_callback_ref is None
assert context._abort_callback_data_ref is None
assert bool(calls[-1][1]) is False
def test_context_decode_raises_distinct_native_abort(monkeypatch):
context = internals.LlamaContext.__new__(internals.LlamaContext)
context.ctx = object()
batch = SimpleNamespace(batch=object())
monkeypatch.setattr(internals.llama_cpp, "llama_decode", lambda _ctx, _batch: 2)
with pytest.raises(internals.LlamaDecodeAbort, match="aborted by user callback"):
context.decode(batch)
def test_decode_eval_batch_resets_and_preserves_native_abort(monkeypatch):
llm = object.__new__(llama_cpp.Llama)
llm._batch = SimpleNamespace(batch=SimpleNamespace(n_tokens=0))
llm._ctx = SimpleNamespace(
decode=lambda _batch: (_ for _ in ()).throw(
internals.LlamaDecodeAbort("native abort")
)
)
llm._active_speculative_phase_stats = None
reset_calls = []
monkeypatch.setattr(llm, "reset", lambda: reset_calls.append(True))
with pytest.raises(internals.LlamaDecodeAbort, match="native abort"):
llm._decode_eval_batch([1, 2], 2)
assert reset_calls == [True]
def test_high_level_abort_updates_python_and_native_flags():
llm = object.__new__(llama_cpp.Llama)
llm.verbose = False
llm._abort_event = threading.Event()
llm._native_abort_flag = ctypes.c_bool(False)
data = ctypes.cast(ctypes.pointer(llm._native_abort_flag), ctypes.c_void_p)
assert llama_cpp.llama._llama_native_abort_callback(data) is False
llm.abort()
assert llm._abort_event.is_set()
assert llama_cpp.llama._llama_native_abort_callback(data) is True
def test_sampling_context_partial_init_can_close_idempotently(monkeypatch):
closed_resources = []
class MinimalModelForSampling:
model = object()
verbose = False
def n_vocab(self):
return 8
class TrackedTokenDataArray:
def __init__(self, *, n_vocab):
assert n_vocab == 8
def close(self):
closed_resources.append("token-data")
class TrackedSamplerChain:
def close(self):
closed_resources.append("sampler-chain")
def get_sampling_vocab(_model_handle):
return object()
def fail_sampler_chain_build(_sampling_context):
raise RuntimeError("sampler chain build failed")
monkeypatch.setattr(internals, "LlamaTokenDataArray", TrackedTokenDataArray)
monkeypatch.setattr(internals, "LlamaSampler", TrackedSamplerChain)
monkeypatch.setattr(
internals.llama_cpp,
"llama_model_get_vocab",
get_sampling_vocab,
)
monkeypatch.setattr(
internals.LlamaSamplingContext,
"_build_sampler_chain",
fail_sampler_chain_build,
)
sampling_context = internals.LlamaSamplingContext.__new__(
internals.LlamaSamplingContext
)
with pytest.raises(RuntimeError, match="sampler chain build failed"):
sampling_context.__init__(
params=internals.LlamaSamplingParams(),
model=MinimalModelForSampling(),
)
sampling_context.close()
sampling_context.close() # Closing an already closed context must be a no-op.
# Full-vocabulary candidate storage is lazy and was never needed because
# sampler-chain construction failed first.
assert closed_resources == ["sampler-chain"]
assert sampling_context.model is None
assert sampling_context.params is None
assert sampling_context.vocab is None
def test_llama_cpp_tokenization():
"""
Test the tokenizer API (Llama.tokenize and Llama.detokenize).
Verifies handling of BOS (Begin of Sentence), EOS (End of Sentence), and special tokens.
"""
llama = llama_cpp.Llama(model_path=MODEL, vocab_only=True, verbose=False)
try:
text = b"Hello World"
tokens = llama.tokenize(text)
assert tokens == [llama.token_bos(), 15043, 2787]
assert llama.detokenize(tokens)[1:] == text
tokens = llama.tokenize(text, add_bos=False)
assert tokens == [15043, 2787]
assert llama.detokenize(tokens) == text
text = b"Hello World</s>"
assert llama.tokenize(text) == [1, 15043, 2787, 829, 29879, 29958]
assert llama.tokenize(text, special=True) == [1, 15043, 2787, llama.token_eos()]
tokens = llama.tokenize(b"", add_bos=True, special=True)
assert tokens == [llama.token_bos()]
assert llama.detokenize(tokens) == b""
finally:
llama.close()
def test_llama_batch_seq_id_error_guidance():
"""Sequence-capacity errors should explain how to fix parallel batching."""
batch = internals.LlamaBatch(
n_tokens=2,
embd=0,
n_seq_max=1,
verbose=False,
)
try:
with pytest.raises(ValueError) as exc_info:
batch.add_sequence(
token_array=[1],
pos_array=[0],
seq_ids=[1],
logits_array=[True],
)
message = str(exc_info.value)
assert "n_seq_max=1" in message
assert "valid IDs are 0 through 0" in message
assert "n_seq_max>=2" in message
assert "LlamaEmbedding" in message
finally:
batch.close()
def test_llama_batch_mixed_embeddings_are_copied_contiguously():
batch = internals.LlamaBatch(
n_tokens=2,
embd=4,
n_seq_max=1,
mixed=True,
verbose=False,
)
try:
first = np.asarray([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
second = np.asarray([5.0, 6.0, 7.0, 8.0], dtype=np.float32)
batch.add_token_embedding(10, first, 0, [0], False)
batch.add_token_embedding(11, second, 1, [0], True)
actual = np.ctypeslib.as_array(batch.batch.embd, shape=(8,)).copy()
np.testing.assert_array_equal(actual, np.concatenate((first, second)))
assert batch.batch.n_tokens == 2
assert [batch.batch.token[i] for i in range(2)] == [10, 11]
finally:
batch.close()
def test_llama_batch_mrope_embeddings_use_four_position_planes():
batch = internals.LlamaBatch(
n_tokens=3,
embd=2,
n_seq_max=1,
verbose=False,
)
try:
original_pos = ctypes.cast(batch.batch.pos, ctypes.c_void_p).value
batch.enable_mrope_positions()
expanded_pos = ctypes.cast(batch.batch.pos, ctypes.c_void_p).value
batch.add_embeddings_mrope(
[1.0, 2.0, 3.0, 4.0],
pos_array=[[4, 5], [4, 5], [4, 5], [0, 0]],
seq_ids=[0],
logits_array=[False, True],
)
assert expanded_pos != original_pos
assert [batch.batch.pos[i] for i in range(8)] == [
4, 5, 4, 5, 4, 5, 0, 0
]
assert [batch.batch.logits[i] for i in range(2)] == [0, 1]
assert batch.batch.n_tokens == 2
finally:
batch.close()
@pytest.fixture(scope="module")
def llama_cpp_model_path():
model_path = os.environ.get("LLAMA_TEST_TRANSFORMER_MODEL")
if not model_path:
if os.environ.get("GITHUB_ACTIONS") == "true":
pytest.fail("LLAMA_TEST_TRANSFORMER_MODEL is required in Actions")
pytest.skip("Set LLAMA_TEST_TRANSFORMER_MODEL to run real-model tests")
assert os.path.isfile(model_path), model_path
return model_path
@pytest.fixture(scope="module")
def shared_completion_model(llama_cpp_model_path):
"""Load the common completion model once for independent feature tests."""
model = llama_cpp.Llama(
llama_cpp_model_path,
n_ctx=64,
n_batch=32,
n_ubatch=32,
n_threads=multiprocessing.cpu_count(),
n_threads_batch=multiprocessing.cpu_count(),
logits_all=False,
swa_full=True,
kv_unified=True,
)
try:
yield model
finally:
model.close()
@pytest.fixture
def completion_model(shared_completion_model):
"""Give each feature test a clean context without reloading model weights."""
shared_completion_model.reset()
try:
yield shared_completion_model
finally:
shared_completion_model.reset()
def test_real_model(llama_cpp_model_path):
"""
Test the Low-Level API (internals.*).
This manually constructs the Model, Context, Batch, and Sampler Chain.
"""
# 1. Setup Model Parameters
params = llama_cpp.llama_model_default_params()
params.check_tensors = False
# 2. Load the Model
model = internals.LlamaModel(path_model=llama_cpp_model_path, params=params)
# 3. Setup Context Parameters
cparams = llama_cpp.llama_context_default_params()
cparams.n_ctx = 32
cparams.n_batch = 16
cparams.n_ubatch = 16
cparams.n_threads = multiprocessing.cpu_count()
cparams.n_threads_batch = multiprocessing.cpu_count()
cparams.swa_full = True
cparams.kv_unified = True
# 4. Create the Context
context = internals.LlamaContext(model=model, params=cparams)
tokens = model.tokenize(b"Hello, world!", add_bos=True, special=True)
assert tokens == [9707, 11, 1879, 0]
# New prompt for generation test
tokens = model.tokenize(b"The quick brown fox jumps", add_bos=True, special=True)
batch = internals.LlamaBatch(n_tokens=len(tokens), embd=0, n_seq_max=1)
seed = 1337
sampler = internals.LlamaSampler()
sampler.add_top_k(50)
sampler.add_top_p(0.9, 1)
sampler.add_temp(0.8)
sampler.add_dist(seed)
result = list(tokens)
n_eval = len(tokens)
batch.reset()
pos_array = list(range(n_eval))
logits_array = [False] * (n_eval - 1) + [True]
batch.add_sequence(
token_array=tokens,
pos_array=pos_array,
seq_ids=[0],
logits_array=logits_array
)
context.decode(batch)
for _ in range(4):
token_id = sampler.sample(context, -1)
sampler.accept(token_id)
result.append(token_id)
batch.reset()
batch.add_token(
token=token_id,
pos=n_eval,
seq_ids=[0],
logits=True
)
context.decode(batch)
n_eval += 1
output = result[len(tokens):]
output_text = model.detokenize(output, special=True)
assert b"over" in output_text or b"lazy dog" in output_text
@pytest.mark.parametrize("stream", [False, True])
def test_native_decode_abort_finishes_and_resets_context(
completion_model, monkeypatch, stream
):
"""A native decode abort must be a normal, reusable completion boundary."""
class RecordingCache:
def __init__(self):
self.writes = []
def __getitem__(self, _key):
raise KeyError
def __setitem__(self, key, value):
self.writes.append((key, value))
def abort_decode(_batch):
raise internals.LlamaDecodeAbort("native abort")
cache = RecordingCache()
monkeypatch.setattr(completion_model._ctx, "decode", abort_decode)
monkeypatch.setattr(completion_model, "cache", cache)
output = completion_model.create_completion(
"Abort this request during prompt evaluation",
max_tokens=4,
stream=stream,
)
result = list(output)[-1] if stream else output
assert result["choices"][0]["finish_reason"] == "abort"
assert completion_model.n_tokens == 0
assert completion_model._ctx.memory_seq_pos_min(0) == -1
assert completion_model._ctx.memory_seq_pos_max(0) == -1
assert cache.writes == []
@pytest.mark.parametrize("rollback", [False, True], ids=["append", "rollback"])
def test_real_transformer_prefix_and_state(completion_model, rollback):
model = completion_model
assert not model.is_hybrid
tokens = model.tokenize(b"The capital of France is")
def complete():
return model.create_completion(tokens, max_tokens=4, temperature=0,
seed=42)["choices"][0]["text"]
reference = complete()
model.reset()
model.eval(tokens[:-1])
if rollback:
model.eval(tokens[-1:])
assert complete() == reference
state = model.save_state()
model.eval(tokens[-1:])
model.load_state(state)
assert model.n_tokens == state.n_tokens
np.testing.assert_array_equal(model._restored_logits, state.last_logits)
def test_grammar_sampling_safety(completion_model):
"""A strict grammar must produce a complete, parseable JSON object."""
grammar_text = r'''
root ::= object
object ::= "{" space pair "}"
pair ::= string ":" space value
string ::= "\"" [a-z]+ "\""
value ::= number
number ::= [0-9]+
space ::= [ ]?
'''
grammar = llama_cpp.LlamaGrammar.from_string(grammar_text)
output = completion_model.create_completion(
"Generate a JSON with age:",
max_tokens=20,
grammar=grammar,
temperature=0.1
)
generated_text = output["choices"][0]["text"]
parsed = json.loads(generated_text)
assert len(parsed) == 1
assert isinstance(next(iter(parsed.values())), int)
def test_logit_bias(completion_model):
"""A strong positive bias must force the selected token."""
# Target token we want to force the model to generate
target_word = " banana" # Note the leading space — important for most tokenizers
# Get the token ID corresponding to " banana" (Qwen-style tokenizer expected)
target_token = completion_model.tokenize(target_word.encode("utf-8"), add_bos=False)[0]
# Apply very strong positive bias to make this token extremely likely
bias = {target_token: 100.0}
# Generate a very short continuation with temperature=0 (greedy) + strong bias
output = completion_model.create_completion(
"I like to eat",
max_tokens=3,
logit_bias=bias,
temperature=0.0
)
generated_text = output["choices"][0]["text"]
assert "banana" in generated_text, f"Expected 'banana' in output, got: '{generated_text}'"
def test_custom_logits_processor(completion_model):
"""
Test 4: Custom Logits Processor (Pure Python Implementation).
Verifies that we can manipulate logits in Python before sampling.
In this test, we suppress any token containing the letter 'e'.
"""
def no_e_processor(input_ids, scores):
"""
Filters out tokens containing 'e'.
"""
for token_id in range(len(scores)):
# Decode single token → get its string representation
token_str = completion_model.detokenize([token_id]).decode("utf-8", errors="ignore")
# Ban tokens that contain 'e' anywhere in their decoded form
if "e" in token_str:
scores[token_id] = -float("inf")
return scores
# Generate with greedy sampling (temperature=0) + our custom processor
output = completion_model.create_completion(
"The alphabet starts with",
max_tokens=10,
logits_processor=llama_cpp.LogitsProcessorList([no_e_processor]),
temperature=0.0
)
generated_text = output["choices"][0]["text"]
assert "e" not in generated_text, \
f"Expected no letter 'e' in output, but found one:\n Output was: '{generated_text}'"
@pytest.mark.parametrize("model_class,n_seq_max", [(llama_cpp.Llama, 2), (LlamaEmbedding, 1)])
def test_real_llama_base_embedding_api(llama_cpp_model_path, model_class, n_seq_max):
"""
Test the maintained embedding API on the standard Llama class.
Covers pre-tokenized batching, normalization, separator-based string
batching, token counts, and the OpenAI-compatible response wrapper.
"""
model = model_class(
model_path=llama_cpp_model_path,
embeddings=True,
n_ctx=32,
n_batch=32,
n_ubatch=32,
n_seq_max=n_seq_max,
kv_unified=True,
pooling_type=LLAMA_POOLING_TYPE_NONE,
verbose=False,
)
try:
token_inputs = [
model.tokenize(b"Hello"),
model.tokenize(b"world"),
]
embeddings, token_count = model.embed(
token_inputs,
normalize=True,
return_count=True,
)
assert len(embeddings) == len(token_inputs)
assert token_count == sum(map(len, token_inputs))
assert len(embeddings[0]) == len(token_inputs[0])
assert np.linalg.norm(embeddings[0][0]) == pytest.approx(1.0)
split_embeddings = model.embed(
"Hello\nworld",
separator="\n",
normalize=False,
)
assert len(split_embeddings) == 2
response = model.create_embedding(
["Hello", "world"],
normalize=2,
)
assert response["object"] == "list"
assert len(response["data"]) == 2
assert response["usage"]["prompt_tokens"] > 0
assert response["usage"]["total_tokens"] == response["usage"]["prompt_tokens"]
finally:
model.close()