forked from kherud/java-llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLlamaModelTest.java
More file actions
1257 lines (1094 loc) · 53.2 KB
/
Copy pathLlamaModelTest.java
File metadata and controls
1257 lines (1094 loc) · 53.2 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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2026 Bernard Ladenthin <bernard.ladenthin@gmail.com>
// SPDX-FileCopyrightText: 2023-2025 Konstantin Herud
//
// SPDX-License-Identifier: MIT
package net.ladenthin.llama;
import static org.junit.jupiter.api.Assertions.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Pattern;
import net.ladenthin.llama.args.LogFormat;
import net.ladenthin.llama.callback.CancellationToken;
import net.ladenthin.llama.callback.ToolHandler;
import net.ladenthin.llama.exception.LlamaException;
import net.ladenthin.llama.parameters.ChatRequest;
import net.ladenthin.llama.parameters.InferenceParameters;
import net.ladenthin.llama.parameters.ModelParameters;
import net.ladenthin.llama.value.ChatMessage;
import net.ladenthin.llama.value.ChatResponse;
import net.ladenthin.llama.value.CompletionResult;
import net.ladenthin.llama.value.LlamaOutput;
import net.ladenthin.llama.value.LogLevel;
import net.ladenthin.llama.value.ModelMeta;
import net.ladenthin.llama.value.Pair;
import net.ladenthin.llama.value.ToolDefinition;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
public class LlamaModelTest {
private static final String prefix = "def remove_non_ascii(s: str) -> str:\n \"\"\" ";
private static final String suffix = "\n return result\n";
private static final int nPredict = 10;
/**
* Minimum expected tokens when testing cancellation.
* The test cancels generation after reaching maxExpectedTokensOnCancel.
* Due to significant performance variations across different platforms and accelerators,
* the actual token count may vary greatly:
* - macOS with Metal (slower): ~2 tokens
* - Linux with CUDA (faster): ~4-5 tokens
* This range accounts for such variations across different hardware, OS, and versions.
*/
private static final int minExpectedTokensOnCancel = 2;
/**
* Maximum expected tokens when testing cancellation.
* The test will trigger cancellation when reaching this count to ensure
* the cancellation mechanism is properly exercised.
* @see #minExpectedTokensOnCancel
*/
private static final int maxExpectedTokensOnCancel = 5;
private static LlamaModel model;
@BeforeAll
public static void setup() {
Assumptions.assumeTrue(
new java.io.File("models/codellama-7b.Q2_K.gguf").exists(),
"Model file not found, skipping LlamaModelTest");
// LlamaModel.setLogger(LogFormat.TEXT, (level, msg) -> System.out.println(level + ": " + msg));
int gpuLayers = Integer.getInteger(TestConstants.PROP_TEST_NGL, TestConstants.DEFAULT_TEST_NGL);
model = new LlamaModel(new ModelParameters()
.setCtxSize(128)
.setModel(TestConstants.MODEL_PATH)
// .setModelUrl("https://huggingface.co/TheBloke/CodeLlama-7B-GGUF/resolve/main/codellama-7b.Q2_K.gguf")
.setGpuLayers(gpuLayers)
.setFit(false)
.enableEmbedding()
.enableLogTimestamps()
.enableLogPrefix());
}
@AfterAll
public static void tearDown() {
if (model != null) {
model.close();
}
}
@Test
public void testGenerateAnswer() {
Map<Integer, Float> logitBias = new HashMap<>();
logitBias.put(2, 2.0f);
InferenceParameters params = new InferenceParameters(prefix)
.withTemperature(0.95f)
.withStopStrings("\"\"\"")
.withNPredict(nPredict)
.withTokenIdBias(logitBias);
int generated = 0;
for (LlamaOutput ignored : model.generate(params)) {
generated++;
}
// todo: currently, after generating nPredict tokens, there is an additional empty output
assertTrue(generated > 0 && generated <= nPredict + 1);
}
@Test
public void testGenerateInfill() {
Map<Integer, Float> logitBias = new HashMap<>();
logitBias.put(2, 2.0f);
InferenceParameters params = new InferenceParameters("")
.withInputPrefix(prefix)
.withInputSuffix(suffix)
.withTemperature(0.95f)
.withStopStrings("\"\"\"")
.withNPredict(nPredict)
.withTokenIdBias(logitBias)
.withSeed(42);
int generated = 0;
for (LlamaOutput ignored : model.generate(params)) {
generated++;
}
assertTrue(generated > 0 && generated <= nPredict + 1);
}
@Test
public void testGenerateGrammar() {
InferenceParameters params = new InferenceParameters("")
.withGrammar("root ::= (\"a\" | \"b\")+")
.withNPredict(nPredict);
StringBuilder sb = new StringBuilder();
for (LlamaOutput output : model.generate(params)) {
sb.append(output);
}
String output = sb.toString();
assertTrue(output.matches("[ab]+"));
int generated = model.encode(output).length;
assertTrue(generated > 0 && generated <= nPredict + 1);
}
@Test
public void testCompleteAnswer() {
Map<Integer, Float> logitBias = new HashMap<>();
logitBias.put(2, 2.0f);
InferenceParameters params = new InferenceParameters(prefix)
.withTemperature(0.95f)
.withStopStrings("\"\"\"")
.withNPredict(nPredict)
.withTokenIdBias(logitBias)
.withSeed(42);
String output = model.complete(params);
assertFalse(output.isEmpty());
}
@Test
public void testCompleteInfillCustom() {
Map<Integer, Float> logitBias = new HashMap<>();
logitBias.put(2, 2.0f);
InferenceParameters params = new InferenceParameters("")
.withInputPrefix(prefix)
.withInputSuffix(suffix)
.withTemperature(0.95f)
.withStopStrings("\"\"\"")
.withNPredict(nPredict)
.withTokenIdBias(logitBias)
.withSeed(42);
String output = model.complete(params);
assertFalse(output.isEmpty());
}
@Test
public void testCompleteGrammar() {
InferenceParameters params = new InferenceParameters("")
.withGrammar("root ::= (\"a\" | \"b\")+")
.withNPredict(nPredict);
String output = model.complete(params);
assertTrue(output.matches("[ab]+"), output + " doesn't match [ab]+");
int generated = model.encode(output).length;
assertTrue(generated > 0 && generated <= nPredict + 1, "generated count is: " + generated);
}
@Test
public void testCancelGenerating() {
InferenceParameters params = new InferenceParameters(prefix).withNPredict(nPredict);
int generated = 0;
LlamaIterator iterator = model.generate(params).iterator();
while (iterator.hasNext()) {
iterator.next();
generated++;
if (generated == maxExpectedTokensOnCancel) {
iterator.cancel();
}
}
String errorMessage = String.format(
"Expected between %d and %d tokens, but got %d. "
+ "This can happen due to timing variations in the llama.cpp inference engine.",
minExpectedTokensOnCancel, maxExpectedTokensOnCancel, generated);
assertTrue(generated >= minExpectedTokensOnCancel && generated <= maxExpectedTokensOnCancel, errorMessage);
}
/**
* LlamaIterable implements AutoCloseable. Breaking out of a for-each loop early inside a
* try-with-resources block must not throw and must not leave the task slot hanging — the
* iterator's close() cancels the native task automatically.
*/
@Test
public void testGenerateAutoCloseOnEarlyBreak() throws Exception {
InferenceParameters params = new InferenceParameters(prefix).withNPredict(nPredict);
int collected = 0;
try (LlamaIterable iterable = model.generate(params)) {
for (LlamaOutput ignored : iterable) {
collected++;
if (collected >= 1) {
break; // exit before stop token
}
}
} // close() must cancel without throwing
assertTrue(collected >= 1, "Should have collected at least one token before break");
// The model must still be usable after an early-exit close
String result = model.complete(new InferenceParameters(prefix).withNPredict(5));
assertNotNull(result, "Model must be functional after autoclosed iterator");
}
/**
* Regression: {@link LlamaIterator#close()} must be idempotent. Calling it
* after natural completion (the iterator already drained to its stop token)
* and calling it twice on an already-cancelled iterator must not throw and
* must not affect subsequent inference.
*/
@Test
public void testIteratorCloseIdempotent() {
InferenceParameters params = new InferenceParameters(prefix).withNPredict(3);
// Case A: drain to natural stop, then close()
LlamaIterable a = model.generate(params);
for (LlamaOutput ignored : a) {
// drain
}
a.close();
a.close(); // second close still a no-op
// Case B: cancel mid-stream, then close()
LlamaIterator b = model.generate(params).iterator();
if (b.hasNext()) b.next();
b.cancel();
b.close();
b.close();
// Model must still be usable
assertNotNull(model.complete(new InferenceParameters(prefix).withNPredict(3)));
}
/**
* Regression: {@link LlamaModel#complete(InferenceParameters, CancellationToken)}
* must return when {@link CancellationToken#cancel()} is invoked from another
* thread, returning whatever text was generated up to that point without
* throwing. Cancellation is cooperative — the loop checks the flag at token
* boundaries — so the budget here is "much less than full n_predict completion
* would take", not instantaneous.
*/
@Test
public void testCompleteWithCancellationToken() throws Exception {
InferenceParameters params = new InferenceParameters(prefix).withNPredict(512);
CancellationToken token = new CancellationToken();
Thread canceller = new Thread(() -> {
try {
Thread.sleep(200);
} catch (InterruptedException ignored) {
}
token.cancel();
});
long start = System.currentTimeMillis();
canceller.start();
String partial = model.complete(params, token);
long elapsed = System.currentTimeMillis() - start;
canceller.join();
// 512 tokens on CPU would take many tens of seconds; cancellation should bring
// this well under that. Tolerate ~10s for the in-flight token to finish.
assertTrue(elapsed < 30000, "complete should return within 30s of cancel, took " + elapsed + "ms");
assertNotNull(partial);
// Token is reset on return so it can be reused.
assertFalse(token.isCancelled(), "token should be reset after call returns");
// Model is still usable
assertNotNull(model.complete(new InferenceParameters(prefix).withNPredict(3)));
}
/**
* Regression: {@link LlamaModel#completeAsync(InferenceParameters)} must
* complete with the same text {@link LlamaModel#complete(InferenceParameters)}
* would have produced, on a background thread.
*/
@Test
public void testCompleteAsync() throws Exception {
InferenceParameters params =
new InferenceParameters(prefix).withNPredict(8).withSeed(42);
String sync =
model.complete(new InferenceParameters(prefix).withNPredict(8).withSeed(42));
String async = model.completeAsync(params).get(30, java.util.concurrent.TimeUnit.SECONDS);
assertEquals(sync, async);
}
/**
* Regression: cancelling the future from {@link LlamaModel#completeAsync(InferenceParameters, CancellationToken)}
* must not leak the underlying inference loop or destabilise the model. The
* worker thread keeps running until the next token boundary, then returns;
* future.cancel(true) only flips the future's state, the whenComplete handler
* flips the token, and the cooperative loop unwinds shortly after.
*/
@Test
public void testCompleteAsyncCancelPropagates() throws Exception {
InferenceParameters params = new InferenceParameters(prefix).withNPredict(512);
CancellationToken token = new CancellationToken();
java.util.concurrent.CompletableFuture<String> future = model.completeAsync(params, token);
Thread.sleep(200);
future.cancel(true);
assertTrue(future.isCancelled(), "future should report cancelled");
// Give the cooperative cancel time to unwind the worker thread before the
// next call. Polling the model state directly is racy; sleeping a generous
// interval (one token + cancel propagation) is sufficient on CPU.
Thread.sleep(5000);
// Model is still usable
assertNotNull(model.complete(new InferenceParameters(prefix).withNPredict(3)));
}
/**
* Regression: {@link Session} must accumulate user/assistant turns across
* multiple {@link Session#send(String)} calls and expose them via
* {@link Session#getMessages()}. Save/restore round-trip is exercised
* separately in slot save/restore tests.
*/
@Test
public void testSessionMultiTurn() {
try (Session session = new Session(
model,
0,
"You are a terse assistant.",
params -> params.withNPredict(8).withSeed(1))) {
String r1 = session.send("Say hi.");
assertNotNull(r1);
String r2 = session.send("Say bye.");
assertNotNull(r2);
java.util.List<ChatMessage> msgs = session.getMessages();
// system + user + assistant + user + assistant
assertEquals(5, msgs.size());
assertEquals("system", msgs.get(0).getRole());
assertEquals("user", msgs.get(1).getRole());
assertEquals("Say hi.", msgs.get(1).getContent());
assertEquals("assistant", msgs.get(2).getRole());
assertEquals("user", msgs.get(3).getRole());
assertEquals("Say bye.", msgs.get(3).getContent());
assertEquals("assistant", msgs.get(4).getRole());
}
}
/**
* Regression: {@link LlamaModel#chat(ChatRequest)} returns a typed
* {@link ChatResponse} with usage / timings populated and at least one
* choice carrying assistant content.
*/
@Test
public void testTypedChat() {
ChatRequest req = ChatRequest.empty()
.appendMessage("user", "Say hi in one word.")
.withInferenceCustomizer(p -> p.withNPredict(8).withSeed(1));
ChatResponse r = model.chat(req);
assertNotNull(r);
assertFalse(r.getChoices().isEmpty());
assertTrue(r.getFirstMessage().isPresent());
assertTrue(r.getUsage().getTotalTokens() > 0);
}
/**
* Regression: {@link LlamaModel#chatWithTools(ChatRequest, java.util.Map)}
* runs at least one round and returns a final {@link ChatResponse} even when
* no tools are triggered. CodeLlama-7B is not a tool-trained model, so this
* primarily exercises the loop contract; tool wiring is unit-tested in
* ChatResponseTest.
*/
@Test
public void testChatWithToolsLoopShortCircuits() {
ToolDefinition echo = new ToolDefinition(
"echo",
"Echo a string",
"{\"type\":\"object\",\"properties\":{\"s\":{\"type\":\"string\"}},\"required\":[\"s\"]}");
ChatRequest req = ChatRequest.empty()
.appendMessage("user", "Hello.")
.appendTool(echo)
.withMaxToolRounds(2)
.withInferenceCustomizer(p -> p.withNPredict(8).withSeed(1));
java.util.Map<String, ToolHandler> handlers = new java.util.HashMap<>();
handlers.put("echo", args -> args);
ChatResponse r = model.chatWithTools(req, handlers);
assertNotNull(r);
assertFalse(r.getChoices().isEmpty());
}
/**
* Regression: {@link LlamaModel#completeBatch(java.util.List)} returns results in
* the same order as the input list, with one non-null text per request. The shared
* test model is single-slot, so this primarily exercises the parallel dispatch and
* order-preservation contract, not actual parallel throughput.
*/
@Test
public void testCompleteBatch() {
java.util.List<InferenceParameters> requests = java.util.Arrays.asList(
new InferenceParameters(prefix).withNPredict(3).withSeed(1),
new InferenceParameters(prefix).withNPredict(3).withSeed(2),
new InferenceParameters(prefix).withNPredict(3).withSeed(3));
java.util.List<String> results = model.completeBatch(requests);
assertEquals(3, results.size());
for (String r : results) {
assertNotNull(r);
}
}
@Test
public void testCompleteBatchWithStats() {
java.util.List<InferenceParameters> requests = java.util.Arrays.asList(
new InferenceParameters(prefix).withNPredict(3).withSeed(1),
new InferenceParameters(prefix).withNPredict(3).withSeed(2));
java.util.List<CompletionResult> results = model.completeBatchWithStats(requests);
assertEquals(2, results.size());
for (CompletionResult r : results) {
assertNotNull(r);
assertTrue(
r.getUsage().getTotalTokens() > 0,
"expected non-zero total tokens, got " + r.getUsage().getTotalTokens());
}
}
@Test
public void testChatBatch() {
java.util.List<ChatRequest> requests = java.util.Arrays.asList(
ChatRequest.empty()
.appendMessage("user", "Say hi.")
.withInferenceCustomizer(p -> p.withNPredict(4).withSeed(1)),
ChatRequest.empty()
.appendMessage("user", "Say bye.")
.withInferenceCustomizer(p -> p.withNPredict(4).withSeed(2)));
java.util.List<ChatResponse> results = model.chatBatch(requests);
assertEquals(2, results.size());
for (ChatResponse r : results) {
assertFalse(r.getChoices().isEmpty());
}
}
@Test
public void testEmbedding() {
float[] embedding = model.embed(prefix);
assertEquals(4096, embedding.length);
}
@Disabled
/**
* To run this test download the model from here https://huggingface.co/mradermacher/jina-reranker-v1-tiny-en-GGUF/tree/main
* remove .enableEmbedding() from model setup and add .enableReRanking() and then enable the test.
*/
public void testReRanking() {
String query = "Machine learning is";
String[] TEST_DOCUMENTS = new String[] {
"A machine is a physical system that uses power to apply forces and control movement to perform an action. The term is commonly applied to artificial devices, such as those employing engines or motors, but also to natural biological macromolecules, such as molecular machines.",
"Learning is the process of acquiring new understanding, knowledge, behaviors, skills, values, attitudes, and preferences. The ability to learn is possessed by humans, non-human animals, and some machines; there is also evidence for some kind of learning in certain plants.",
"Machine learning is a field of study in artificial intelligence concerned with the development and study of statistical algorithms that can learn from data and generalize to unseen data, and thus perform tasks without explicit instructions.",
"Paris, capitale de la France, est une grande ville européenne et un centre mondial de l'art, de la mode, de la gastronomie et de la culture. Son paysage urbain du XIXe siècle est traversé par de larges boulevards et la Seine."
};
LlamaOutput llamaOutput =
model.rerank(query, TEST_DOCUMENTS[0], TEST_DOCUMENTS[1], TEST_DOCUMENTS[2], TEST_DOCUMENTS[3]);
System.out.println(llamaOutput);
}
@Test
public void testTokenization() {
String prompt = "Hello, world!";
int[] encoded = model.encode(prompt);
String decoded = model.decode(encoded);
// the llama tokenizer adds a space before the prompt
assertEquals(" " + prompt, decoded);
}
@Test
public void testVocabOnly() {
try (LlamaModel vocabModel = new LlamaModel(
new ModelParameters().setModel(TestConstants.MODEL_PATH).setVocabOnly())) {
String prompt = "Hello, world!";
int[] encoded = vocabModel.encode(prompt);
assertTrue(encoded.length > 0, "Should produce at least one token");
String decoded = vocabModel.decode(encoded);
assertEquals(" " + prompt, decoded);
}
}
@Test
public void testVocabOnlyMatchesFullModel() {
try (LlamaModel vocabModel = new LlamaModel(
new ModelParameters().setModel(TestConstants.MODEL_PATH).setVocabOnly())) {
String prompt = "def remove_non_ascii(s: str) -> str:";
int[] vocabTokens = vocabModel.encode(prompt);
int[] fullTokens = model.encode(prompt);
assertArrayEquals(fullTokens, vocabTokens, "Vocab-only tokenization should match full model");
}
}
@Test
public void testVocabOnlyDecodeEmptyArray() {
try (LlamaModel vocabModel = new LlamaModel(
new ModelParameters().setModel(TestConstants.MODEL_PATH).setVocabOnly())) {
String decoded = vocabModel.decode(new int[0]);
assertEquals("", decoded, "Decoding empty token array should give empty string");
}
}
@Test
public void testVocabOnlyUnicodeRoundTrip() {
try (LlamaModel vocabModel = new LlamaModel(
new ModelParameters().setModel(TestConstants.MODEL_PATH).setVocabOnly())) {
// Multi-byte characters: accents, CJK ideograph, emoji
String prompt = "naïve café résumé";
int[] tokens = vocabModel.encode(prompt);
assertTrue(tokens.length > 0, "Unicode string should tokenise to at least one token");
String decoded = vocabModel.decode(tokens);
// Leading space is normal (llama tokenizer behaviour); compare content
assertTrue(
decoded.contains("na") && decoded.contains("caf") && decoded.contains("sum"),
"Decoded text should contain original characters");
}
}
@Test
public void testVocabOnlyTwoInstancesCoexist() {
// Two independent vocab-only models open simultaneously must not interfere.
try (LlamaModel a = new LlamaModel(
new ModelParameters().setModel(TestConstants.MODEL_PATH).setVocabOnly());
LlamaModel b = new LlamaModel(
new ModelParameters().setModel(TestConstants.MODEL_PATH).setVocabOnly())) {
String prompt = "hello";
int[] tokensA = a.encode(prompt);
int[] tokensB = b.encode(prompt);
assertArrayEquals(tokensA, tokensB, "Two concurrent vocab-only instances must produce equal tokens");
}
}
@Test
public void testVocabOnlyCoexistsWithFullModel() {
// A vocab-only instance must work correctly while the class-level full model is loaded.
try (LlamaModel vocabModel = new LlamaModel(
new ModelParameters().setModel(TestConstants.MODEL_PATH).setVocabOnly())) {
String prompt = "int main()";
int[] vocabTokens = vocabModel.encode(prompt);
int[] fullTokens = model.encode(prompt);
assertArrayEquals(fullTokens, vocabTokens, "Vocab-only instance must match full-model tokenization");
}
}
@Disabled
public void testLogText() {
List<LogMessage> messages = new ArrayList<>();
LlamaModel.setLogger(LogFormat.TEXT, (level, msg) -> messages.add(new LogMessage(level, msg)));
InferenceParameters params =
new InferenceParameters(prefix).withNPredict(nPredict).withSeed(42);
model.complete(params);
assertFalse(messages.isEmpty());
Pattern jsonPattern = Pattern.compile("^\\s*[\\[{].*[}\\]]\\s*$");
for (LogMessage message : messages) {
assertNotNull(message.level);
assertFalse(jsonPattern.matcher(message.text).matches());
}
}
@Disabled
public void testLogJSON() {
List<LogMessage> messages = new ArrayList<>();
LlamaModel.setLogger(LogFormat.JSON, (level, msg) -> messages.add(new LogMessage(level, msg)));
InferenceParameters params =
new InferenceParameters(prefix).withNPredict(nPredict).withSeed(42);
model.complete(params);
assertFalse(messages.isEmpty());
Pattern jsonPattern = Pattern.compile("^\\s*[\\[{].*[}\\]]\\s*$");
for (LogMessage message : messages) {
assertNotNull(message.level);
assertTrue(jsonPattern.matcher(message.text).matches());
}
}
@Disabled
@Test
public void testLogStdout() {
// Unfortunately, `printf` can't be easily re-directed to Java. This test only works manually, thus.
InferenceParameters params =
new InferenceParameters(prefix).withNPredict(nPredict).withSeed(42);
System.out.println("########## Log Text ##########");
LlamaModel.setLogger(LogFormat.TEXT, null);
model.complete(params);
System.out.println("########## Log JSON ##########");
LlamaModel.setLogger(LogFormat.JSON, null);
model.complete(params);
System.out.println("########## Log None ##########");
LlamaModel.setLogger(LogFormat.TEXT, (level, msg) -> {});
model.complete(params);
System.out.println("##############################");
}
private String completeAndReadStdOut() {
PrintStream stdOut = System.out;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream, false, StandardCharsets.UTF_8);
System.setOut(printStream);
try {
InferenceParameters params =
new InferenceParameters(prefix).withNPredict(nPredict).withSeed(42);
model.complete(params);
} finally {
System.out.flush();
System.setOut(stdOut);
printStream.close();
}
return outputStream.toString(StandardCharsets.UTF_8);
}
private List<String> splitLines(String text) {
List<String> lines = new ArrayList<>();
Scanner scanner = new Scanner(text);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lines.add(line);
}
scanner.close();
return lines;
}
private static final class LogMessage {
private final LogLevel level;
private final String text;
private LogMessage(LogLevel level, String text) {
this.level = level;
this.text = text;
}
}
@Test
public void testJsonSchemaToGrammar() {
String schema = "{\n" + " \"properties\": {\n"
+ " \"a\": {\"type\": \"string\"},\n"
+ " \"b\": {\"type\": \"string\"},\n"
+ " \"c\": {\"type\": \"string\"}\n"
+ " },\n"
+ " \"additionalProperties\": false\n"
+ "}";
String expectedGrammar =
"a-kv ::= \"\\\"a\\\"\" space \":\" space string\n" + "a-rest ::= ( \",\" space b-kv )? b-rest\n"
+ "b-kv ::= \"\\\"b\\\"\" space \":\" space string\n"
+ "b-rest ::= ( \",\" space c-kv )?\n"
+ "c-kv ::= \"\\\"c\\\"\" space \":\" space string\n"
+ "char ::= [^\"\\\\\\x7F\\x00-\\x1F] | [\\\\] ([\"\\\\bfnrt] | \"u\" [0-9a-fA-F]{4})\n"
+ "root ::= \"{\" space (a-kv a-rest | b-kv b-rest | c-kv )? \"}\" space\n"
+ "space ::= | \" \" | \"\\n\"{1,2} [ \\t]{0,20}\n"
+ "string ::= \"\\\"\" char* \"\\\"\" space\n";
String actualGrammar = LlamaModel.jsonSchemaToGrammar(schema);
assertEquals(expectedGrammar, actualGrammar);
}
@Test
public void testJsonSchemaToGrammarRejectsMalformedSchema() {
// Regression for the JNI exception-boundary fix (audit Tier 1, PR #258): a malformed schema
// string used to let a C++ json::parse exception escape across the JNI boundary and abort the
// JVM. It must now surface as a catchable LlamaException instead of crashing the process.
assertThrows(LlamaException.class, () -> LlamaModel.jsonSchemaToGrammar("{ this is not valid json"));
}
@Test
public void testTemplate() {
List<Pair<String, String>> userMessages = new ArrayList<>();
userMessages.add(new Pair<>("user", "What is the best book?"));
userMessages.add(new Pair<>("assistant", "It depends on your interests. Do you like fiction or non-fiction?"));
InferenceParameters params = new InferenceParameters("A book recommendation system.")
.withMessages("Book", userMessages)
.withTemperature(0.95f)
.withStopStrings("\"\"\"")
.withNPredict(nPredict)
.withSeed(42);
assertEquals(
model.applyTemplate(params),
"<|im_start|>system\nBook<|im_end|>\n<|im_start|>user\nWhat is the best book?<|im_end|>\n<|im_start|>assistant\nIt depends on your interests. Do you like fiction or non-fiction?");
}
// ------------------------------------------------------------------
// chatComplete / handleChatCompletions
// ------------------------------------------------------------------
@Test
public void testChatComplete() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "Write a single word."));
InferenceParameters params = new InferenceParameters("")
.withMessages(null, messages)
.withNPredict(nPredict)
.withSeed(42)
.withTemperature(0.0f);
String response = model.chatComplete(params);
assertNotNull(response, "Chat completion should return a non-null response");
assertFalse(response.isEmpty(), "Chat completion should return a non-empty response");
}
@Test
public void testChatCompleteWithSystemMessage() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "Say hello."));
InferenceParameters params = new InferenceParameters("")
.withMessages("You are a helpful assistant.", messages)
.withNPredict(nPredict)
.withSeed(42)
.withTemperature(0.0f);
String response = model.chatComplete(params);
assertNotNull(response);
assertFalse(response.isEmpty());
}
@Test
public void testGenerateChat() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "Write a single word."));
InferenceParameters params = new InferenceParameters("")
.withMessages(null, messages)
.withNPredict(nPredict)
.withSeed(42)
.withTemperature(0.0f);
int generated = 0;
StringBuilder sb = new StringBuilder();
for (LlamaOutput output : model.generateChat(params)) {
sb.append(output.text);
generated++;
}
assertTrue(generated > 0, "Expected at least one token from streaming chat");
assertTrue(generated <= nPredict + 1, "Expected at most nPredict+1 tokens");
assertFalse(sb.toString().isEmpty(), "Streamed content should not be empty");
}
@Test
public void testGenerateChatCancel() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "Count from 1 to 100."));
InferenceParameters params =
new InferenceParameters("").withMessages(null, messages).withNPredict(nPredict);
int generated = 0;
LlamaIterator iterator = model.generateChat(params).iterator();
while (iterator.hasNext()) {
iterator.next();
generated++;
if (generated == maxExpectedTokensOnCancel) {
iterator.cancel();
}
}
assertTrue(
generated >= minExpectedTokensOnCancel,
"Expected at least " + minExpectedTokensOnCancel + " tokens, got " + generated);
assertTrue(
generated <= maxExpectedTokensOnCancel,
"Expected at most " + maxExpectedTokensOnCancel + " tokens, got " + generated);
}
@Test
public void testChatCompleteMultiTurn() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "What is 2+2?"));
messages.add(new Pair<>("assistant", "4"));
messages.add(new Pair<>("user", "And 3+3?"));
InferenceParameters params = new InferenceParameters("")
.withMessages(null, messages)
.withNPredict(nPredict)
.withSeed(42)
.withTemperature(0.0f);
String response = model.chatComplete(params);
assertNotNull(response);
assertFalse(response.isEmpty());
}
@Test
public void testChatCompleteWithTemplateKwargs() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "Hello"));
Map<String, String> kwargs = new HashMap<>();
kwargs.put("custom_var", "\"test_value\"");
InferenceParameters params = new InferenceParameters("")
.withMessages(null, messages)
.withChatTemplateKwargs(kwargs)
.withNPredict(nPredict)
.withSeed(42)
.withTemperature(0.0f);
// Template kwargs should pass through without error even if
// the template doesn't use them — they're simply ignored.
String response = model.chatComplete(params);
assertNotNull(response);
assertFalse(response.isEmpty());
}
@Test
public void testApplyTemplateWithKwargs() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "Hello"));
Map<String, String> kwargs = new HashMap<>();
kwargs.put("custom_var", "\"test_value\"");
InferenceParameters params =
new InferenceParameters("").withMessages(null, messages).withChatTemplateKwargs(kwargs);
// Should not throw — kwargs are passed through to the template
String result = model.applyTemplate(params);
assertNotNull(result);
assertTrue(result.contains("Hello"));
}
// ------------------------------------------------------------------
// applyTemplate / oaicompat_chat_params_parse (changed in b8576)
// ------------------------------------------------------------------
/**
* oaicompat_chat_params_parse with a single user message and no system message.
* The existing testTemplate() only tests system + user + assistant.
* This exercises the minimal messages path and verifies that the
* generation prompt (assistant prefix) is appended when the last
* message is from the user.
*/
@Test
public void testApplyTemplateUserOnly() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "Tell me a joke"));
InferenceParameters params = new InferenceParameters("").withMessages(null, messages);
String result = model.applyTemplate(params);
assertNotNull(result);
assertTrue(result.contains("<|im_start|>user"), "Expected user role marker");
assertTrue(result.contains("Tell me a joke"), "Expected message content");
assertFalse(result.contains("<|im_start|>system"), "Should not have system block when none given");
// add_generation_prompt defaults to true → assistant continuation is appended
assertTrue(result.contains("<|im_start|>assistant"), "Expected assistant continuation prompt");
}
/**
* oaicompat_chat_params_parse with multiple turns: system + user → assistant → user.
* Verifies that all messages appear in correct order and the assistant turn
* in the middle is correctly delimited.
*/
@Test
public void testApplyTemplateMultipleTurns() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "What is 2+2?"));
messages.add(new Pair<>("assistant", "4"));
messages.add(new Pair<>("user", "And 3+3?"));
InferenceParameters params = new InferenceParameters("").withMessages("Math tutor", messages);
String result = model.applyTemplate(params);
assertTrue(result.contains("What is 2+2?"));
assertTrue(result.contains("And 3+3?"));
// The intermediate assistant reply must also be present
assertTrue(result.contains("4"), "Intermediate assistant turn missing");
// Last message is user → generation prompt adds assistant prefix
assertTrue(result.contains("<|im_start|>assistant"));
}
/**
* Empty system message must be treated the same as no system message
* (setMessages skips the system block when the string is empty).
*/
@Test
public void testApplyTemplateEmptySystemSkipped() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "Hello"));
// empty string → setMessages skips the system block
InferenceParameters params = new InferenceParameters("").withMessages("", messages);
String result = model.applyTemplate(params);
assertFalse(result.contains("<|im_start|>system"), "Empty system message must not produce a system block");
assertTrue(result.contains("Hello"));
}
/**
* When the conversation ends with an assistant turn, oaicompat_chat_params_parse
* must NOT append another generation prompt — it should instead allow the
* caller to continue the partially generated assistant response.
*/
@Test
public void testApplyTemplateLastMessageAssistantNoContinuationPrompt() {
List<Pair<String, String>> messages = new ArrayList<>();
messages.add(new Pair<>("user", "Capital of France?"));
messages.add(new Pair<>("assistant", "The capital of France is"));
InferenceParameters params = new InferenceParameters("").withMessages(null, messages);
String result = model.applyTemplate(params);
assertTrue(result.contains("The capital of France is"));
// There must not be a second <|im_start|>assistant after the partial reply
int firstAssistant = result.indexOf("<|im_start|>assistant");
int secondAssistant = result.indexOf("<|im_start|>assistant", firstAssistant + 1);
assertEquals(-1, secondAssistant, "Should have exactly one assistant block");
}
// ------------------------------------------------------------------
// server_tokens::detokenize / validate — exercised via generate/complete
// ------------------------------------------------------------------
/**
* Multi-byte UTF-8 in the prompt exercises server_tokens construction
* from tokenized_prompts and subsequently server_tokens::validate(ctx)
* and detokenize() for the generated output.
*/
@Test
public void testCompleteNonAsciiPrompt() {
// café, naïve, résumé contain multi-byte UTF-8 sequences
InferenceParameters params = new InferenceParameters("Translate to English: café")
.withNPredict(nPredict)
.withSeed(42);
String output = model.complete(params);
// If server_tokens / detokenize is broken, this throws or returns garbage
assertNotNull(output);
}
/**
* Verifies that the JNI string conversion ({@code parse_jstring}) correctly
* handles multi-byte UTF-8 input through the full model's encode/decode path.
*
* <p>The test covers three UTF-8 byte widths:
* <ul>
* <li>2-byte sequences: Latin characters with diacritics (ü, ö, é)</li>
* <li>3-byte sequences: CJK ideographs (日, 本, 語)</li>
* <li>Mixing both in a single prompt</li>
* </ul>
*
* <p>A successful encode→decode round-trip proves that the native layer
* receives the bytes intact and that no truncation or mojibake occurs.
*/
@Test
public void testTokenizationUnicode() {
// 2-byte UTF-8: Latin extended (U+00FC, U+00F6, U+00E9)
String latin = "über, größe, résumé";
int[] latinTokens = model.encode(latin);
assertTrue(latinTokens.length > 0, "Latin extended string should produce at least one token");
String latinDecoded = model.decode(latinTokens);
assertTrue(
latinDecoded.contains("ber") && latinDecoded.contains("r") && latinDecoded.contains("sum"),
"Decoded Latin-extended text should preserve multi-byte chars");
// 3-byte UTF-8: CJK (U+65E5, U+672C, U+8A9E)
String cjk = "日本語";