ggml.go 3.92 KB
Newer Older
1
2
3
4
5
6
package llm

import (
	"encoding/binary"
	"errors"
	"io"
Bruce MacDonald's avatar
Bruce MacDonald committed
7
	"sync"
8
9
)

Michael Yang's avatar
Michael Yang committed
10
11
12
13
14
type GGML struct {
	magic uint32
	container
	model
}
15
16

const (
Michael Yang's avatar
Michael Yang committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
	fileTypeF32 uint32 = iota
	fileTypeF16
	fileTypeQ4_0
	fileTypeQ4_1
	fileTypeQ4_1_F16
	fileTypeQ8_0 uint32 = iota + 2
	fileTypeQ5_0
	fileTypeQ5_1
	fileTypeQ2_K
	fileTypeQ3_K_S
	fileTypeQ3_K_M
	fileTypeQ3_K_L
	fileTypeQ4_K_S
	fileTypeQ4_K_M
	fileTypeQ5_K_S
	fileTypeQ5_K_M
	fileTypeQ6_K
34
35
)

Michael Yang's avatar
Michael Yang committed
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
func fileType(fileType uint32) string {
	switch fileType {
	case fileTypeF32:
		return "F32"
	case fileTypeF16:
		return "F16"
	case fileTypeQ4_0:
		return "Q4_0"
	case fileTypeQ4_1:
		return "Q4_1"
	case fileTypeQ4_1_F16:
		return "Q4_1_F16"
	case fileTypeQ8_0:
		return "Q8_0"
	case fileTypeQ5_0:
		return "Q5_0"
	case fileTypeQ5_1:
		return "Q5_1"
	case fileTypeQ2_K:
		return "Q2_K"
	case fileTypeQ3_K_S:
		return "Q3_K_S"
	case fileTypeQ3_K_M:
		return "Q3_K_M"
	case fileTypeQ3_K_L:
		return "Q3_K_L"
	case fileTypeQ4_K_S:
		return "Q4_K_S"
	case fileTypeQ4_K_M:
		return "Q4_K_M"
	case fileTypeQ5_K_S:
		return "Q5_K_S"
	case fileTypeQ5_K_M:
		return "Q5_K_M"
	case fileTypeQ6_K:
		return "Q6_K"
Michael Yang's avatar
Michael Yang committed
72
73
74
75
	default:
		return "Unknown"
	}
}
76

Michael Yang's avatar
Michael Yang committed
77
type model interface {
Michael Yang's avatar
Michael Yang committed
78
79
80
	ModelFamily() string
	ModelType() string
	FileType() string
81
82
83
84
}

type container interface {
	Name() string
Bruce MacDonald's avatar
Bruce MacDonald committed
85
	Decode(io.Reader) (model, error)
86
87
}

Bruce MacDonald's avatar
Bruce MacDonald committed
88
type containerGGML struct{}
89
90
91
92
93

func (c *containerGGML) Name() string {
	return "ggml"
}

Bruce MacDonald's avatar
Bruce MacDonald committed
94
95
func (c *containerGGML) Decode(r io.Reader) (model, error) {
	return nil, nil
96
97
98
99
100
101
102
103
104
105
}

type containerGGMF struct {
	version uint32
}

func (c *containerGGMF) Name() string {
	return "ggmf"
}

Bruce MacDonald's avatar
Bruce MacDonald committed
106
func (c *containerGGMF) Decode(r io.Reader) (model, error) {
107
108
109
110
111
112
	var version uint32
	binary.Read(r, binary.LittleEndian, &version)

	switch version {
	case 1:
	default:
Bruce MacDonald's avatar
Bruce MacDonald committed
113
		return nil, errors.New("invalid version")
114
115
116
	}

	c.version = version
Bruce MacDonald's avatar
Bruce MacDonald committed
117
	return nil, nil
118
119
120
121
122
123
124
125
126
127
}

type containerGGJT struct {
	version uint32
}

func (c *containerGGJT) Name() string {
	return "ggjt"
}

Bruce MacDonald's avatar
Bruce MacDonald committed
128
func (c *containerGGJT) Decode(r io.Reader) (model, error) {
129
130
131
132
133
134
	var version uint32
	binary.Read(r, binary.LittleEndian, &version)

	switch version {
	case 1, 2, 3:
	default:
Bruce MacDonald's avatar
Bruce MacDonald committed
135
		return nil, errors.New("invalid version")
136
137
138
	}

	c.version = version
Bruce MacDonald's avatar
Bruce MacDonald committed
139
140
141
142
143

	// different model types may have different layouts for hyperparameters
	var llama llamaModel
	binary.Read(r, binary.LittleEndian, &llama.hyperparameters)
	return &llama, nil
144
145
146
147
148
149
150
151
152
153
}

type containerLORA struct {
	version uint32
}

func (c *containerLORA) Name() string {
	return "ggla"
}

Bruce MacDonald's avatar
Bruce MacDonald committed
154
func (c *containerLORA) Decode(r io.Reader) (model, error) {
155
156
157
158
159
160
	var version uint32
	binary.Read(r, binary.LittleEndian, &version)

	switch version {
	case 1:
	default:
Bruce MacDonald's avatar
Bruce MacDonald committed
161
		return nil, errors.New("invalid version")
162
163
164
	}

	c.version = version
Bruce MacDonald's avatar
Bruce MacDonald committed
165
166
167
168
	return nil, nil
}

var (
169
170
	ggmlInit    sync.Once
	ggmlRunners []ModelRunner // a slice of ModelRunners ordered by priority
Bruce MacDonald's avatar
Bruce MacDonald committed
171
172
)

173
func ggmlRunner() []ModelRunner {
Bruce MacDonald's avatar
Bruce MacDonald committed
174
	ggmlInit.Do(func() {
175
		ggmlRunners = chooseRunners("ggml")
Bruce MacDonald's avatar
Bruce MacDonald committed
176
	})
177
	return ggmlRunners
178
179
180
}

const (
Bruce MacDonald's avatar
Bruce MacDonald committed
181
	// Magic constant for `ggml` files (unversioned).
182
	FILE_MAGIC_GGML = 0x67676d6c
Bruce MacDonald's avatar
Bruce MacDonald committed
183
	// Magic constant for `ggml` files (versioned, ggmf).
184
	FILE_MAGIC_GGMF = 0x67676d66
Bruce MacDonald's avatar
Bruce MacDonald committed
185
	// Magic constant for `ggml` files (versioned, ggjt).
186
	FILE_MAGIC_GGJT = 0x67676a74
Bruce MacDonald's avatar
Bruce MacDonald committed
187
	// Magic constant for `ggla` files (LoRA adapter).
188
	FILE_MAGIC_GGLA = 0x67676C61
Bruce MacDonald's avatar
Bruce MacDonald committed
189
190
	// Magic constant for `gguf` files (versioned, gguf)
	FILE_MAGIC_GGUF = 0x46554747
191
192
)

Bruce MacDonald's avatar
Bruce MacDonald committed
193
func DecodeGGML(r io.ReadSeeker) (*GGML, error) {
194
195
196
197
198
199
200
201
202
203
204
205
	var ggml GGML
	binary.Read(r, binary.LittleEndian, &ggml.magic)

	switch ggml.magic {
	case FILE_MAGIC_GGML:
		ggml.container = &containerGGML{}
	case FILE_MAGIC_GGMF:
		ggml.container = &containerGGMF{}
	case FILE_MAGIC_GGJT:
		ggml.container = &containerGGJT{}
	case FILE_MAGIC_GGLA:
		ggml.container = &containerLORA{}
Bruce MacDonald's avatar
Bruce MacDonald committed
206
207
	case FILE_MAGIC_GGUF:
		ggml.container = &containerGGUF{}
208
209
210
211
	default:
		return nil, errors.New("invalid file magic")
	}

Bruce MacDonald's avatar
Bruce MacDonald committed
212
213
	model, err := ggml.Decode(r)
	if err != nil {
214
215
216
		return nil, err
	}

Bruce MacDonald's avatar
Bruce MacDonald committed
217
	ggml.model = model
218
219
220
221

	// final model type
	return &ggml, nil
}