|
1 | 1 | package config_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
4 | 9 | . "github.com/mudler/LocalAI/core/config" |
5 | 10 | "github.com/mudler/LocalAI/core/schema" |
6 | 11 |
|
| 12 | + gguf "github.com/gpustack/gguf-parser-go" |
7 | 13 | . "github.com/onsi/ginkgo/v2" |
8 | 14 | . "github.com/onsi/gomega" |
9 | 15 | ) |
10 | 16 |
|
| 17 | +// GGUF metadata value type tags (see github.com/gpustack/gguf-parser-go). |
| 18 | +const ( |
| 19 | + ggufTypeUint32 uint32 = 4 |
| 20 | + ggufTypeString uint32 = 8 |
| 21 | + ggufTypeArray uint32 = 9 |
| 22 | +) |
| 23 | + |
| 24 | +// writeTestGGUF emits a minimal but valid little-endian GGUF v3 header carrying |
| 25 | +// the scalar metadata the llama-cpp hook guesses from plus a large string vocab |
| 26 | +// array (tokenizer.ggml.tokens). The big array is exactly what SkipLargeMetadata |
| 27 | +// + UseMMap are expected to avoid reading element-by-element, so it must survive a |
| 28 | +// round-trip through the real hook without corrupting the guessed defaults. |
| 29 | +func writeTestGGUF(path, chatTemplate string, vocab int) error { |
| 30 | + wStr := func(b *bytes.Buffer, s string) { |
| 31 | + binary.Write(b, binary.LittleEndian, uint64(len(s))) |
| 32 | + b.WriteString(s) |
| 33 | + } |
| 34 | + kvStr := func(b *bytes.Buffer, k, v string) { |
| 35 | + wStr(b, k) |
| 36 | + binary.Write(b, binary.LittleEndian, ggufTypeString) |
| 37 | + wStr(b, v) |
| 38 | + } |
| 39 | + kvU32 := func(b *bytes.Buffer, k string, v uint32) { |
| 40 | + wStr(b, k) |
| 41 | + binary.Write(b, binary.LittleEndian, ggufTypeUint32) |
| 42 | + binary.Write(b, binary.LittleEndian, v) |
| 43 | + } |
| 44 | + |
| 45 | + var meta bytes.Buffer |
| 46 | + kvStr(&meta, "general.architecture", "llama") |
| 47 | + kvStr(&meta, "general.name", "ReproModel") |
| 48 | + kvU32(&meta, "llama.context_length", 4096) |
| 49 | + kvU32(&meta, "llama.attention.head_count", 32) |
| 50 | + kvU32(&meta, "llama.feed_forward_length", 11008) |
| 51 | + kvU32(&meta, "llama.block_count", 32) |
| 52 | + kvU32(&meta, "tokenizer.ggml.bos_token_id", 1) |
| 53 | + kvStr(&meta, "tokenizer.chat_template", chatTemplate) |
| 54 | + |
| 55 | + // large array value — the one the optimization skips reading |
| 56 | + wStr(&meta, "tokenizer.ggml.tokens") |
| 57 | + binary.Write(&meta, binary.LittleEndian, ggufTypeArray) |
| 58 | + binary.Write(&meta, binary.LittleEndian, ggufTypeString) |
| 59 | + binary.Write(&meta, binary.LittleEndian, uint64(vocab)) |
| 60 | + for i := 0; i < vocab; i++ { |
| 61 | + wStr(&meta, "token") |
| 62 | + } |
| 63 | + |
| 64 | + var out bytes.Buffer |
| 65 | + binary.Write(&out, binary.LittleEndian, gguf.GGUFMagicGGUFLe) |
| 66 | + binary.Write(&out, binary.LittleEndian, uint32(3)) // version |
| 67 | + binary.Write(&out, binary.LittleEndian, uint64(0)) // tensor count |
| 68 | + binary.Write(&out, binary.LittleEndian, uint64(9)) // metadata kv count |
| 69 | + out.Write(meta.Bytes()) |
| 70 | + |
| 71 | + return os.WriteFile(path, out.Bytes(), 0o644) |
| 72 | +} |
| 73 | + |
11 | 74 | var _ = Describe("Backend hooks and parser defaults", func() { |
12 | 75 | Context("MatchParserDefaults", func() { |
13 | 76 | It("matches Qwen3 family", func() { |
@@ -137,6 +200,58 @@ var _ = Describe("Backend hooks and parser defaults", func() { |
137 | 200 | }) |
138 | 201 | }) |
139 | 202 |
|
| 203 | + Context("llamaCppDefaults GGUF guessing", func() { |
| 204 | + // Regression coverage for https://github.com/mudler/LocalAI/issues/9790: |
| 205 | + // the hook reads GGUF headers with SkipLargeMetadata + UseMMap to avoid |
| 206 | + // pulling the whole tokenizer vocab off (slow) disk on every startup. This |
| 207 | + // verifies that skipping the vocab array still yields the correct guessed |
| 208 | + // defaults from the remaining scalar metadata. |
| 209 | + const chatTemplate = "{{ bos_token }}{% for m in messages %}{{ m.content }}{% endfor %}" |
| 210 | + |
| 211 | + It("guesses defaults from a GGUF whose large vocab is skipped", func() { |
| 212 | + dir := GinkgoT().TempDir() |
| 213 | + modelFile := "repro.gguf" |
| 214 | + Expect(writeTestGGUF(filepath.Join(dir, modelFile), chatTemplate, 50000)).To(Succeed()) |
| 215 | + |
| 216 | + // A pre-set context size short-circuits the GGUF run-estimate, which |
| 217 | + // needs full tensor info this header-only fixture deliberately omits; |
| 218 | + // the metadata-reading path the optimization touches is unaffected. |
| 219 | + ctxSize := 4096 |
| 220 | + cfg := &ModelConfig{ |
| 221 | + Backend: "llama-cpp", |
| 222 | + LLMConfig: LLMConfig{ContextSize: &ctxSize}, |
| 223 | + PredictionOptions: schema.PredictionOptions{ |
| 224 | + BasicModelRequest: schema.BasicModelRequest{Model: modelFile}, |
| 225 | + }, |
| 226 | + } |
| 227 | + cfg.SetDefaults(ModelPath(dir)) |
| 228 | + |
| 229 | + // chat_template is a scalar string, not part of the skipped array, |
| 230 | + // so it must be captured verbatim. |
| 231 | + Expect(cfg.GetModelTemplate()).To(Equal(chatTemplate)) |
| 232 | + // scalar-derived defaults are still applied |
| 233 | + Expect(cfg.ContextSize).NotTo(BeNil()) |
| 234 | + Expect(cfg.NGPULayers).NotTo(BeNil()) |
| 235 | + Expect(cfg.TemplateConfig.UseTokenizerTemplate).To(BeTrue()) |
| 236 | + Expect(cfg.KnownUsecaseStrings).To(ContainElement("FLAG_CHAT")) |
| 237 | + }) |
| 238 | + |
| 239 | + It("falls back to the default context size when the GGUF is unreadable", func() { |
| 240 | + dir := GinkgoT().TempDir() |
| 241 | + Expect(os.WriteFile(filepath.Join(dir, "bad.gguf"), []byte("not a gguf"), 0o644)).To(Succeed()) |
| 242 | + |
| 243 | + cfg := &ModelConfig{ |
| 244 | + Backend: "llama-cpp", |
| 245 | + PredictionOptions: schema.PredictionOptions{ |
| 246 | + BasicModelRequest: schema.BasicModelRequest{Model: "bad.gguf"}, |
| 247 | + }, |
| 248 | + } |
| 249 | + cfg.SetDefaults(ModelPath(dir)) |
| 250 | + |
| 251 | + Expect(cfg.ContextSize).NotTo(BeNil()) |
| 252 | + }) |
| 253 | + }) |
| 254 | + |
140 | 255 | Context("PromptCacheAll default", func() { |
141 | 256 | It("defaults to true when omitted from YAML", func() { |
142 | 257 | cfg := &ModelConfig{} |
|
0 commit comments