ggml.go 4.48 KB
Newer Older
1
2
3
4
5
6
7
8
package llm

import (
	"encoding/binary"
	"errors"
	"io"
)

Michael Yang's avatar
Michael Yang committed
9
10
11
type GGML struct {
	container
	model
12
13

	Size int64
Michael Yang's avatar
Michael Yang committed
14
}
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
Michael Yang's avatar
Michael Yang committed
34
35
36
37
38
	fileTypeIQ2_XXS
	fileTypeIQ2_XS
	fileTypeQ2_K_S
	fileTypeQ3_K_XS
	fileTypeIQ3_XXS
39
40
)

Michael Yang's avatar
Michael Yang committed
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
72
73
74
75
76
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
77
78
79
80
81
82
83
84
85
86
	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
87
	default:
Michael Yang's avatar
Michael Yang committed
88
		return "unknown"
Michael Yang's avatar
Michael Yang committed
89
90
	}
}
91

Michael Yang's avatar
Michael Yang committed
92
type model interface {
Michael Yang's avatar
Michael Yang committed
93
94
95
	ModelFamily() string
	ModelType() string
	FileType() string
96
97
98
99
100
	NumLayers() uint32
	NumGQA() uint32
	NumEmbed() uint32
	NumHead() uint32
	NumHeadKv() uint32
Michael Yang's avatar
Michael Yang committed
101
	NumCtx() uint32
102
103
}

104
105
106
107
108
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
type KV map[string]any

type Tensor struct {
	Name   string
	Kind   uint32
	Offset uint64

	// Shape is the number of elements in each dimension
	Shape []uint64

	io.WriterTo
}

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()
}

183
184
type container interface {
	Name() string
Michael Yang's avatar
Michael Yang committed
185
	Decode(io.ReadSeeker) (model, error)
186
187
188
}

const (
Bruce MacDonald's avatar
Bruce MacDonald committed
189
	// Magic constant for `ggml` files (unversioned).
190
	FILE_MAGIC_GGML = 0x67676d6c
Bruce MacDonald's avatar
Bruce MacDonald committed
191
	// Magic constant for `ggml` files (versioned, ggmf).
192
	FILE_MAGIC_GGMF = 0x67676d66
Bruce MacDonald's avatar
Bruce MacDonald committed
193
	// Magic constant for `ggml` files (versioned, ggjt).
194
	FILE_MAGIC_GGJT = 0x67676a74
Bruce MacDonald's avatar
Bruce MacDonald committed
195
	// Magic constant for `ggla` files (LoRA adapter).
196
	FILE_MAGIC_GGLA = 0x67676C61
Bruce MacDonald's avatar
Bruce MacDonald committed
197
	// Magic constant for `gguf` files (versioned, gguf)
Michael Yang's avatar
ggufv3  
Michael Yang committed
198
199
	FILE_MAGIC_GGUF_LE = 0x46554747
	FILE_MAGIC_GGUF_BE = 0x47475546
200
201
)

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

Michael Yang's avatar
Michael Yang committed
204
func DecodeGGML(rs io.ReadSeeker) (*GGML, error) {
205
	var magic uint32
Michael Yang's avatar
Michael Yang committed
206
	if err := binary.Read(rs, binary.LittleEndian, &magic); err != nil {
207
208
209
210
211
		return nil, err
	}

	var c container
	switch magic {
Bruce MacDonald's avatar
Bruce MacDonald committed
212
213
	case FILE_MAGIC_GGML, FILE_MAGIC_GGMF, FILE_MAGIC_GGJT:
		return nil, ErrUnsupportedFormat
214
	case FILE_MAGIC_GGLA:
215
		c = &containerGGLA{}
Michael Yang's avatar
ggufv3  
Michael Yang committed
216
	case FILE_MAGIC_GGUF_LE:
217
		c = &containerGGUF{ByteOrder: binary.LittleEndian}
Michael Yang's avatar
ggufv3  
Michael Yang committed
218
	case FILE_MAGIC_GGUF_BE:
219
		c = &containerGGUF{ByteOrder: binary.BigEndian}
220
221
222
223
	default:
		return nil, errors.New("invalid file magic")
	}

Michael Yang's avatar
Michael Yang committed
224
	model, err := c.Decode(rs)
Michael Yang's avatar
Michael Yang committed
225
226
227
	if errors.Is(err, io.EOF) {
		// noop
	} else if err != nil {
228
229
230
		return nil, err
	}

Michael Yang's avatar
Michael Yang committed
231
232
233
234
235
	offset, err := rs.Seek(0, io.SeekCurrent)
	if err != nil {
		return nil, err
	}

236
	// final model type
237
238
239
	return &GGML{
		container: c,
		model:     model,
Michael Yang's avatar
Michael Yang committed
240
		Size:      offset,
241
242
	}, nil
}