convert.go 2.99 KB
Newer Older
1
2
3
4
package convert

import (
	"encoding/json"
Michael Yang's avatar
Michael Yang committed
5
	"errors"
6
	"fmt"
Michael Yang's avatar
Michael Yang committed
7
	"io"
8
9
10
11
	"log/slog"
	"os"
	"path/filepath"

12
	"github.com/ollama/ollama/llm"
13
14
)

Michael Yang's avatar
Michael Yang committed
15
16
17
type Parameters struct {
	Architectures []string `json:"architectures"`
	VocabSize     uint32   `json:"vocab_size"`
18
19
}

Michael Yang's avatar
Michael Yang committed
20
21
22
23
24
25
26
27
28
29
func (Parameters) KV(t *Tokenizer) llm.KV {
	kv := llm.KV{
		"general.file_type":            uint32(1),
		"general.quantization_version": uint32(2),
		"tokenizer.ggml.pre":           t.Pre,
		"tokenizer.ggml.model":         t.Vocabulary.Model,
		"tokenizer.ggml.tokens":        t.Vocabulary.Tokens,
		"tokenizer.ggml.scores":        t.Vocabulary.Scores,
		"tokenizer.ggml.token_type":    t.Vocabulary.Types,
	}
30

Michael Yang's avatar
Michael Yang committed
31
32
33
	if t.Template != "" {
		kv["tokenizer.chat_template"] = t.Template
	}
34

Michael Yang's avatar
Michael Yang committed
35
36
37
38
	for _, sv := range t.SpecialVocabulary {
		kv[fmt.Sprintf("tokenizer.ggml.%s_token_id", sv.Key())] = uint32(sv.ID)
		kv[fmt.Sprintf("tokenizer.ggml.add_%s_token", sv.Key())] = sv.AddToken
	}
39

Michael Yang's avatar
Michael Yang committed
40
	return kv
41
42
}

Michael Yang's avatar
Michael Yang committed
43
44
45
func (Parameters) specialTypes() []string {
	return []string{
		"bos", "eos", "unk", "sep", "pad", "cls", "mask",
46
	}
Michael Yang's avatar
Michael Yang committed
47
}
48

Michael Yang's avatar
Michael Yang committed
49
50
func (Parameters) writeFile(ws io.WriteSeeker, kv llm.KV, ts []*llm.Tensor) error {
	return llm.WriteGGUF(ws, kv, ts)
51
52
}

Michael Yang's avatar
Michael Yang committed
53
54
55
56
57
58
59
60
61
62
63
type Converter interface {
	// KV maps parameters to LLM key-values
	KV(*Tokenizer) llm.KV
	// Tensors maps input tensors to LLM tensors. Model specific modifications can be done here.
	Tensors([]Tensor) []*llm.Tensor

	// tensorName returns the LLM tensor name for a specific input name
	tensorName(string) string
	// specialTypes returns any special token types the model uses
	specialTypes() []string
	writeFile(io.WriteSeeker, llm.KV, []*llm.Tensor) error
64
65
}

Michael Yang's avatar
Michael Yang committed
66
67
func Convert(d string, ws io.WriteSeeker) error {
	f, err := os.Open(filepath.Join(d, "config.json"))
68
	if err != nil {
Michael Yang's avatar
Michael Yang committed
69
		return err
70
	}
Michael Yang's avatar
Michael Yang committed
71
	defer f.Close()
72

Michael Yang's avatar
Michael Yang committed
73
74
75
	var p Parameters
	if err := json.NewDecoder(f).Decode(&p); err != nil {
		return err
76
77
	}

Michael Yang's avatar
Michael Yang committed
78
79
	if len(p.Architectures) < 1 {
		return errors.New("unknown architecture")
80
81
	}

Michael Yang's avatar
Michael Yang committed
82
83
84
85
86
87
88
89
90
91
	var c Converter
	switch p.Architectures[0] {
	case "LlamaForCausalLM", "MistralForCausalLM":
		c = &llama{}
	case "MixtralForCausalLM":
		c = &mixtral{}
	case "GemmaForCausalLM":
		c = &gemma{}
	default:
		return errors.New("unsupported architecture")
92
93
	}

Michael Yang's avatar
Michael Yang committed
94
95
96
	bts, err := os.ReadFile(filepath.Join(d, "config.json"))
	if err != nil {
		return err
97
98
	}

Michael Yang's avatar
Michael Yang committed
99
100
	if err := json.Unmarshal(bts, c); err != nil {
		return err
101
102
	}

Michael Yang's avatar
Michael Yang committed
103
104
105
	t, err := parseTokenizer(d, c.specialTypes())
	if err != nil {
		return err
106
107
	}

Michael Yang's avatar
Michael Yang committed
108
109
110
111
112
113
	if vocabSize := int(p.VocabSize); vocabSize > len(t.Vocabulary.Tokens) {
		slog.Warn("vocabulary is smaller than expected, padding with dummy tokens", "expect", p.VocabSize, "actual", len(t.Vocabulary.Tokens))
		for i := range vocabSize - len(t.Vocabulary.Tokens) {
			t.Vocabulary.Tokens = append(t.Vocabulary.Tokens, fmt.Sprintf("[PAD%d]", i))
			t.Vocabulary.Scores = append(t.Vocabulary.Scores, -1)
			t.Vocabulary.Types = append(t.Vocabulary.Types, tokenTypeUserDefined)
114
115
		}
	}
Michael Yang's avatar
Michael Yang committed
116
117
118
119

	ts, err := parseTensors(d)
	if err != nil {
		return err
120
121
	}

Michael Yang's avatar
Michael Yang committed
122
	return c.writeFile(ws, c.KV(t), c.Tensors(ts))
123
}