Skip to content

Commit 61c1a43

Browse files
committed
Identify llama3
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 884ee8b commit 61c1a43

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

core/config/application_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ func (o *ApplicationConfig) ToConfigLoaderOptions() []ConfigLoaderOption {
306306
LoadOptionDebug(o.Debug),
307307
LoadOptionF16(o.F16),
308308
LoadOptionThreads(o.Threads),
309+
ModelPath(o.ModelPath),
309310
}
310311
}
311312

core/config/guesser.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ import (
88
gguf "github.com/thxcode/gguf-parser-go"
99
)
1010

11-
type FamilyType uint8
11+
type familyType uint8
1212

1313
const (
14-
Unknown FamilyType = iota
14+
Unknown familyType = iota
1515
LLaMa3 = iota
1616
LLama2 = iota
1717
)
1818

19-
var defaultsTemplate map[FamilyType]TemplateConfig = map[FamilyType]TemplateConfig{
20-
LLaMa3: {},
19+
var defaultsTemplate map[familyType]TemplateConfig = map[familyType]TemplateConfig{
20+
LLaMa3: {
21+
Chat: "<|begin_of_text|>{{.Input }}\n<|start_header_id|>assistant<|end_header_id|>",
22+
ChatMessage: "<|start_header_id|>{{ .RoleName }}<|end_header_id|>\n\n{{.Content }}<|eot_id|>",
23+
},
2124
}
2225

2326
func guessDefaultsFromFile(cfg *BackendConfig, modelPath string) {
@@ -26,8 +29,13 @@ func guessDefaultsFromFile(cfg *BackendConfig, modelPath string) {
2629
log.Debug().Msgf("guessDefaultsFromFile: %s", "modelPath is empty")
2730
return
2831
}
32+
if cfg.HasTemplate() || cfg.Name != "" {
33+
// nothing to guess here
34+
log.Debug().Any("name", cfg.Name).Msgf("guessDefaultsFromFile: %s", "template or name already set")
35+
return
36+
}
2937

30-
// We try to guess only if we don't have a template defined already+
38+
// We try to guess only if we don't have a template defined already
3139
f, err := gguf.ParseGGUFFile(filepath.Join(modelPath, cfg.ModelFileName()))
3240
if err != nil {
3341
// Only valid for gguf files
@@ -37,17 +45,15 @@ func guessDefaultsFromFile(cfg *BackendConfig, modelPath string) {
3745

3846
log.Debug().
3947
Any("eosTokenID", f.Tokenizer().EOSTokenID).
48+
Any("bosTokenID", f.Tokenizer().BOSTokenID).
4049
Any("modelName", f.Model().Name).
4150
Any("architecture", f.Architecture().Architecture).Msgf("Model file loaded: %s", cfg.ModelFileName())
4251

52+
// guess the name
4353
if cfg.Name == "" {
4454
cfg.Name = f.Model().Name
4555
}
4656

47-
if cfg.HasTemplate() {
48-
return
49-
}
50-
5157
family := identifyFamily(f)
5258

5359
if family == Unknown {
@@ -58,14 +64,15 @@ func guessDefaultsFromFile(cfg *BackendConfig, modelPath string) {
5864
templ, ok := defaultsTemplate[family]
5965
if ok {
6066
cfg.TemplateConfig = templ
67+
log.Debug().Any("family", family).Msgf("guessDefaultsFromFile: guessed template %+v", cfg.TemplateConfig)
68+
} else {
69+
log.Debug().Any("family", family).Msgf("guessDefaultsFromFile: no template found for family")
6170
}
62-
6371
}
6472

65-
func identifyFamily(f *gguf.GGUFFile) FamilyType {
66-
73+
func identifyFamily(f *gguf.GGUFFile) familyType {
6774
switch {
68-
case f.Model().Name == "llama":
75+
case f.Architecture().Architecture == "llama" && f.Tokenizer().EOSTokenID == 128009:
6976
return LLaMa3
7077
}
7178

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,11 @@ require (
256256
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
257257
go.opentelemetry.io/otel/trace v1.19.0 // indirect
258258
golang.org/x/crypto v0.23.0 // indirect
259-
golang.org/x/mod v0.16.0 // indirect
259+
golang.org/x/mod v0.17.0 // indirect
260260
golang.org/x/net v0.25.0 // indirect
261261
golang.org/x/term v0.20.0 // indirect
262262
golang.org/x/text v0.15.0 // indirect
263-
golang.org/x/tools v0.19.0 // indirect
263+
golang.org/x/tools v0.21.0 // indirect
264264
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
265265
gopkg.in/fsnotify.v1 v1.4.7 // indirect
266266
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect

0 commit comments

Comments
 (0)