forked from bernardladenthin/java-llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelMetaTest.java
More file actions
162 lines (136 loc) · 7.28 KB
/
Copy pathModelMetaTest.java
File metadata and controls
162 lines (136 loc) · 7.28 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
// SPDX-FileCopyrightText: 2026 Bernard Ladenthin <bernard.ladenthin@gmail.com>
// SPDX-FileCopyrightText: 2023-2025 Konstantin Herud
//
// SPDX-License-Identifier: MIT
package net.ladenthin.llama.value;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.ladenthin.llama.ClaudeGenerated;
import org.junit.jupiter.api.Test;
/**
* Unit tests for {@link ModelMeta} typed getters.
* Constructs {@code ModelMeta} directly from JSON strings — no native library or model file required.
*/
@ClaudeGenerated(
purpose = "Verify that ModelMeta typed getters map correctly from the underlying JsonNode, "
+ "including the new architecture and name fields from GGUF general.* metadata.")
public class ModelMetaTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private ModelMeta parse(String json) throws Exception {
return new ModelMeta(MAPPER.readTree(json));
}
@Test
public void testNumericGetters() throws Exception {
ModelMeta meta = parse("{\"vocab_type\":1,\"n_vocab\":32016,\"n_ctx_train\":16384,"
+ "\"n_embd\":4096,\"n_params\":6738546688,\"size\":2825274880,"
+ "\"modalities\":{\"vision\":false,\"audio\":false},"
+ "\"architecture\":\"llama\",\"name\":\"CodeLlama-7B\"}");
assertThat(meta.getVocabType(), is(1));
assertThat(meta.getNVocab(), is(32016));
assertThat(meta.getNCtxTrain(), is(16384));
assertThat(meta.getNEmbd(), is(4096));
assertThat(meta.getNParams(), is(6738546688L));
assertThat(meta.getSize(), is(2825274880L));
}
@Test
public void testModalityGetters() throws Exception {
ModelMeta textOnly = parse("{\"vocab_type\":1,\"n_vocab\":100,\"n_ctx_train\":4096,"
+ "\"n_embd\":512,\"n_params\":1000000,\"size\":500000,"
+ "\"modalities\":{\"vision\":false,\"audio\":false},"
+ "\"architecture\":\"llama\",\"name\":\"\"}");
assertThat(textOnly.supportsVision(), is(false));
assertThat(textOnly.supportsAudio(), is(false));
ModelMeta multimodal = parse("{\"vocab_type\":1,\"n_vocab\":100,\"n_ctx_train\":4096,"
+ "\"n_embd\":512,\"n_params\":1000000,\"size\":500000,"
+ "\"modalities\":{\"vision\":true,\"audio\":true},"
+ "\"architecture\":\"gemma3\",\"name\":\"Gemma-3\"}");
assertThat(multimodal.supportsVision(), is(true));
assertThat(multimodal.supportsAudio(), is(true));
}
@Test
public void testGetArchitecture() throws Exception {
ModelMeta meta = parse("{\"vocab_type\":1,\"n_vocab\":32016,\"n_ctx_train\":16384,"
+ "\"n_embd\":4096,\"n_params\":6738546688,\"size\":2825274880,"
+ "\"modalities\":{\"vision\":false,\"audio\":false},"
+ "\"architecture\":\"llama\",\"name\":\"CodeLlama-7B\"}");
assertThat(meta.getArchitecture(), is("llama"));
}
@Test
public void testGetModelName() throws Exception {
ModelMeta meta = parse("{\"vocab_type\":1,\"n_vocab\":32016,\"n_ctx_train\":16384,"
+ "\"n_embd\":4096,\"n_params\":6738546688,\"size\":2825274880,"
+ "\"modalities\":{\"vision\":false,\"audio\":false},"
+ "\"architecture\":\"mistral\",\"name\":\"Mistral-7B-v0.1\"}");
assertThat(meta.getModelName(), is("Mistral-7B-v0.1"));
}
@Test
public void testGetArchitectureEmptyWhenAbsent() throws Exception {
ModelMeta meta = parse("{\"vocab_type\":1,\"n_vocab\":100,\"n_ctx_train\":4096,"
+ "\"n_embd\":512,\"n_params\":1000000,\"size\":500000,"
+ "\"modalities\":{\"vision\":false,\"audio\":false}}");
assertThat(meta.getArchitecture(), is(""));
}
@Test
public void testGetModelNameEmptyWhenAbsent() throws Exception {
ModelMeta meta = parse("{\"vocab_type\":1,\"n_vocab\":100,\"n_ctx_train\":4096,"
+ "\"n_embd\":512,\"n_params\":1000000,\"size\":500000,"
+ "\"modalities\":{\"vision\":false,\"audio\":false}}");
assertThat(meta.getModelName(), is(""));
}
@Test
public void testGetArchitectureVariousModels() throws Exception {
for (String arch : new String[] {"llama", "gemma3", "mistral", "falcon", "phi3"}) {
ModelMeta meta = parse("{\"vocab_type\":1,\"n_vocab\":100,\"n_ctx_train\":4096,"
+ "\"n_embd\":512,\"n_params\":1000000,\"size\":500000,"
+ "\"modalities\":{\"vision\":false,\"audio\":false},"
+ "\"architecture\":\""
+ arch + "\",\"name\":\"\"}");
assertThat(meta.getArchitecture(), is(arch));
}
}
@Test
public void testAsJsonReturnsBackingNode() throws Exception {
ModelMeta meta = parse("{\"vocab_type\":1,\"n_vocab\":32016,\"n_ctx_train\":16384,"
+ "\"n_embd\":4096,\"n_params\":6738546688,\"size\":2825274880,"
+ "\"modalities\":{\"vision\":false,\"audio\":false},"
+ "\"architecture\":\"llama\",\"name\":\"CodeLlama-7B\"}");
// Dereferencing the returned node kills the "return null" mutant on asJson().
assertThat(meta.asJson().get("architecture").asText(), is("llama"));
}
@Test
public void testToStringContainsNewFields() throws Exception {
ModelMeta meta = parse("{\"vocab_type\":1,\"n_vocab\":32016,\"n_ctx_train\":16384,"
+ "\"n_embd\":4096,\"n_params\":6738546688,\"size\":2825274880,"
+ "\"modalities\":{\"vision\":false,\"audio\":false},"
+ "\"architecture\":\"llama\",\"name\":\"CodeLlama-7B\"}");
String json = meta.toString();
assertThat(json, containsString("\"architecture\""));
assertThat(json, containsString("\"name\""));
assertThat(json, containsString("\"llama\""));
assertThat(json, containsString("\"CodeLlama-7B\""));
}
@Test
public void testChatTemplateSpecialTokensAndMetadata() throws Exception {
ModelMeta meta = parse("{\"n_vocab\":32000,"
+ "\"chat_template\":\"{% for m in messages %}{{ m.content }}{% endfor %}\","
+ "\"special_tokens\":{\"bos\":1,\"eos\":2,\"eot\":32000,\"sep\":-1,\"nl\":13,\"pad\":-1},"
+ "\"metadata\":{\"general.architecture\":\"llama\",\"general.quantization_version\":\"2\"}}");
assertThat(meta.getChatTemplate(), containsString("for m in messages"));
assertThat(meta.getBosTokenId(), is(1));
assertThat(meta.getEosTokenId(), is(2));
assertThat(meta.getEotTokenId(), is(32000));
assertThat(meta.getMetadata("general.architecture"), is("llama"));
assertThat(meta.getMetadata("general.quantization_version"), is("2"));
}
@Test
public void testNewGettersDefaultWhenAbsent() throws Exception {
ModelMeta meta = parse("{\"n_vocab\":100}");
assertThat(meta.getChatTemplate(), is(""));
assertThat(meta.getBosTokenId(), is(-1));
assertThat(meta.getEosTokenId(), is(-1));
assertThat(meta.getEotTokenId(), is(-1));
assertThat(meta.getMetadata("general.architecture"), is(""));
}
}