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

import (
	"encoding/binary"
	"errors"
Michael Yang's avatar
Michael Yang committed
6
	"fmt"
7
	"io"
Michael Yang's avatar
Michael Yang committed
8
	"strings"
9
10
)

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

Michael Yang's avatar
Michael Yang committed
16
17
18
19
20
21
22
23
24
25
func (ggml *GGML) LayerSize(prefix string) (n int64) {
	for _, t := range ggml.Tensors() {
		if strings.HasPrefix(t.Name, prefix) {
			n += int64(t.size())
		}
	}

	return
}

26
const (
Michael Yang's avatar
Michael Yang committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
	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
Michael Yang's avatar
Michael Yang committed
44
45
46
47
48
	fileTypeIQ2_XXS
	fileTypeIQ2_XS
	fileTypeQ2_K_S
	fileTypeQ3_K_XS
	fileTypeIQ3_XXS
49
50
)

Michael Yang's avatar
Michael Yang committed
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
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
87
88
89
90
91
92
93
94
95
96
	case fileTypeIQ2_XXS:
		return "IQ2_XXS"
	case fileTypeIQ2_XS:
		return "IQ2_XS"
	case fileTypeQ2_K_S:
		return "Q2_K_S"
	case fileTypeQ3_K_XS:
		return "Q3_K_XS"
	case fileTypeIQ3_XXS:
		return "IQ3_XXS"
Michael Yang's avatar
Michael Yang committed
97
	default:
Michael Yang's avatar
Michael Yang committed
98
		return "unknown"
Michael Yang's avatar
Michael Yang committed
99
100
	}
}
101

Michael Yang's avatar
Michael Yang committed
102
type model interface {
Michael Yang's avatar
Michael Yang committed
103
104
	KV() KV
	Tensors() []*Tensor
105
106
}

107
108
type KV map[string]any

Michael Yang's avatar
Michael Yang committed
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
func (kv KV) u64(key string) uint64 {
	switch v := kv[key].(type) {
	case uint64:
		return v
	case uint32:
		return uint64(v)
	case float64:
		return uint64(v)
	default:
		return 0
	}
}

func (kv KV) Architecture() string {
	if s, ok := kv["general.architecture"].(string); ok {
		return s
	}

	return "unknown"
}

func (kv KV) ParameterCount() uint64 {
	return kv.u64("general.parameter_count")
}

func (kv KV) FileType() string {
	if u64 := kv.u64("general.file_type"); u64 > 0 {
		return fileType(uint32(u64))
	}

	return "unknown"
}

func (kv KV) BlockCount() uint64 {
	return kv.u64(fmt.Sprintf("%s.block_count", kv.Architecture()))
}

func (kv KV) HeadCount() uint64 {
	return kv.u64(fmt.Sprintf("%s.attention.head_count", kv.Architecture()))
}

func (kv KV) HeadCountKV() uint64 {
Michael Yang's avatar
Michael Yang committed
151
152
153
154
155
	if headCountKV := kv.u64(fmt.Sprintf("%s.attention.head_count_kv", kv.Architecture())); headCountKV > 0 {
		return headCountKV
	}

	return 1
Michael Yang's avatar
Michael Yang committed
156
157
158
}

func (kv KV) GQA() uint64 {
Michael Yang's avatar
Michael Yang committed
159
	return kv.HeadCount() / kv.HeadCountKV()
Michael Yang's avatar
Michael Yang committed
160
161
162
163
164
165
166
167
168
169
}

func (kv KV) EmbeddingLength() uint64 {
	return kv.u64(fmt.Sprintf("%s.embedding_length", kv.Architecture()))
}

func (kv KV) ContextLength() uint64 {
	return kv.u64(fmt.Sprintf("%s.context_length", kv.Architecture()))
}

170
type Tensor struct {
Michael Yang's avatar
Michael Yang committed
171
172
173
	Name   string `json:"name"`
	Kind   uint32 `json:"kind"`
	Offset uint64 `json:"-"`
174
175

	// Shape is the number of elements in each dimension
Michael Yang's avatar
Michael Yang committed
176
	Shape []uint64 `json:"shape"`
177

Michael Yang's avatar
Michael Yang committed
178
	io.WriterTo `json:"-"`
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
}

func (t Tensor) blockSize() uint64 {
	switch {
	case t.Kind < 2:
		return 1
	case t.Kind < 10:
		return 32
	default:
		return 256
	}
}

func (t Tensor) typeSize() uint64 {
	blockSize := t.blockSize()

	switch t.Kind {
	case 0: // FP32
		return 4
	case 1: // FP16
		return 2
	case 2: // Q4_0
		return 2 + blockSize/2
	case 3: // Q4_1
		return 2 + 2 + blockSize/2
	case 6: // Q5_0
		return 2 + 4 + blockSize/2
	case 7: // Q5_1
		return 2 + 2 + 4 + blockSize/2
	case 8: // Q8_0
		return 2 + blockSize
	case 9: // Q8_1
		return 4 + 4 + blockSize
	case 10: // Q2_K
		return blockSize/16 + blockSize/4 + 2 + 2
	case 11: // Q3_K
		return blockSize/8 + blockSize/4 + 12 + 2
	case 12: // Q4_K
		return 2 + 2 + 12 + blockSize/2
	case 13: // Q5_K
		return 2 + 2 + 12 + blockSize/8 + blockSize/2
	case 14: // Q6_K
		return blockSize/2 + blockSize/4 + blockSize/16 + 2
	case 15: // Q8_K
		return 2 + blockSize + 2*blockSize/16
	case 16: // IQ2_XXS
		return 2 + 2*blockSize/8
	case 17: // IQ2_XS
		return 2 + 2*blockSize/8 + blockSize/32
	case 18: // IQ3_XXS
		return 2 + 3*blockSize/8
	default:
		return 0
	}
}

func (t Tensor) parameters() uint64 {
	var count uint64 = 1
	for _, n := range t.Shape {
		count *= n
	}
	return count
}

func (t Tensor) size() uint64 {
	return t.parameters() * t.typeSize() / t.blockSize()
}

247
248
type container interface {
	Name() string
Michael Yang's avatar
Michael Yang committed
249
	Decode(io.ReadSeeker) (model, error)
250
251
252
}

const (
Bruce MacDonald's avatar
Bruce MacDonald committed
253
	// Magic constant for `ggml` files (unversioned).
254
	FILE_MAGIC_GGML = 0x67676d6c
Bruce MacDonald's avatar
Bruce MacDonald committed
255
	// Magic constant for `ggml` files (versioned, ggmf).
256
	FILE_MAGIC_GGMF = 0x67676d66
Bruce MacDonald's avatar
Bruce MacDonald committed
257
	// Magic constant for `ggml` files (versioned, ggjt).
258
	FILE_MAGIC_GGJT = 0x67676a74
Bruce MacDonald's avatar
Bruce MacDonald committed
259
	// Magic constant for `ggla` files (LoRA adapter).
260
	FILE_MAGIC_GGLA = 0x67676C61
Bruce MacDonald's avatar
Bruce MacDonald committed
261
	// Magic constant for `gguf` files (versioned, gguf)
Michael Yang's avatar
ggufv3  
Michael Yang committed
262
263
	FILE_MAGIC_GGUF_LE = 0x46554747
	FILE_MAGIC_GGUF_BE = 0x47475546
264
265
)

Bruce MacDonald's avatar
Bruce MacDonald committed
266
267
var ErrUnsupportedFormat = errors.New("unsupported model format")

Michael Yang's avatar
Michael Yang committed
268
func DecodeGGML(rs io.ReadSeeker) (*GGML, int64, error) {
269
	var magic uint32
Michael Yang's avatar
Michael Yang committed
270
	if err := binary.Read(rs, binary.LittleEndian, &magic); err != nil {
Michael Yang's avatar
Michael Yang committed
271
		return nil, 0, err
272
273
274
275
	}

	var c container
	switch magic {
Bruce MacDonald's avatar
Bruce MacDonald committed
276
	case FILE_MAGIC_GGML, FILE_MAGIC_GGMF, FILE_MAGIC_GGJT:
Michael Yang's avatar
Michael Yang committed
277
		return nil, 0, ErrUnsupportedFormat
278
	case FILE_MAGIC_GGLA:
279
		c = &containerGGLA{}
Michael Yang's avatar
ggufv3  
Michael Yang committed
280
	case FILE_MAGIC_GGUF_LE:
281
		c = &containerGGUF{ByteOrder: binary.LittleEndian}
Michael Yang's avatar
ggufv3  
Michael Yang committed
282
	case FILE_MAGIC_GGUF_BE:
283
		c = &containerGGUF{ByteOrder: binary.BigEndian}
284
	default:
Michael Yang's avatar
Michael Yang committed
285
		return nil, 0, errors.New("invalid file magic")
286
287
	}

Michael Yang's avatar
Michael Yang committed
288
	model, err := c.Decode(rs)
Michael Yang's avatar
Michael Yang committed
289
290
291
	if errors.Is(err, io.EOF) {
		// noop
	} else if err != nil {
Michael Yang's avatar
Michael Yang committed
292
		return nil, 0, err
293
294
	}

Michael Yang's avatar
Michael Yang committed
295
296
	offset, err := rs.Seek(0, io.SeekCurrent)
	if err != nil {
Michael Yang's avatar
Michael Yang committed
297
		return nil, 0, err
Michael Yang's avatar
Michael Yang committed
298
299
	}

300
	// final model type
301
302
303
	return &GGML{
		container: c,
		model:     model,
Michael Yang's avatar
Michael Yang committed
304
	}, offset, nil
305
}
Michael Yang's avatar
Michael Yang committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329

func (llm GGML) GraphSize(context, batch int) (int64, bool) {
	embeddingLength := llm.KV().EmbeddingLength()
	headCount := llm.KV().HeadCount()
	headCountKV := llm.KV().HeadCountKV()
	vocabLength := len(llm.KV()["tokenizer.ggml.tokens"].([]any))

	var attnQKVWeight1 uint64 = 0
	for _, t := range llm.Tensors() {
		if strings.HasSuffix(t.Name, ".attn_qkv.weight") && len(t.Shape) >= 2 {
			attnQKVWeight1 = t.Shape[1]
			break
		}
	}

	var ffnGate1 uint64 = 0
	for _, t := range llm.Tensors() {
		if strings.Index(t.Name, ".ffn_gate") > 0 && len(t.Shape) >= 2 {
			ffnGate1 = t.Shape[1]
			break
		}
	}

	switch llm.KV().Architecture() {
Michael Yang's avatar
Michael Yang committed
330
	case "gemma", "command-r":
Michael Yang's avatar
Michael Yang committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
		return 4 * int64(batch) * int64(embeddingLength+uint64(vocabLength)), true
	case "phi2":
		return max(
			4*int64(batch)*int64(embeddingLength+uint64(vocabLength)),
			4*int64(batch)*int64(1+4*embeddingLength+uint64(context)+attnQKVWeight1+uint64(context)*headCount),
		), true
	case "qwen2":
		return max(
			4*int64(batch)*int64(embeddingLength+uint64(vocabLength)),
			4*int64(batch)*int64(1+2*embeddingLength+uint64(context)+uint64(context)*headCount),
		), true
	case "llama":
		if ffnGate1 > 0 {
			// moe
			return 4 * int64(batch) * int64(2+3*embeddingLength+uint64(context)+uint64(context)*headCount+2*headCountKV+ffnGate1), true
		}
	
		return 4 * int64(batch) * int64(1+4*embeddingLength+uint64(context)+uint64(context)*headCount), true
	}

	return 0, false
}