ggml.go 4.34 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
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
	default:
Michael Yang's avatar
Michael Yang committed
73
		return "unknown"
Michael Yang's avatar
Michael Yang committed
74
75
	}
}
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
	NumLayers() int64
82
83
84
85
}

type container interface {
	Name() string
Michael Yang's avatar
Michael Yang committed
86
	Decode(*readSeekOffset) (model, error)
87
88
}

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

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

Michael Yang's avatar
Michael Yang committed
95
func (c *containerGGML) Decode(ro *readSeekOffset) (model, error) {
Bruce MacDonald's avatar
Bruce MacDonald committed
96
	return nil, nil
97
98
99
100
101
102
103
104
105
106
}

type containerGGMF struct {
	version uint32
}

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

Michael Yang's avatar
Michael Yang committed
107
func (c *containerGGMF) Decode(ro *readSeekOffset) (model, error) {
108
	var version uint32
109
	binary.Read(ro, binary.LittleEndian, &version)
110
111
112
113

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

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

type containerGGJT struct {
	version uint32
}

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

Michael Yang's avatar
Michael Yang committed
129
func (c *containerGGJT) Decode(ro *readSeekOffset) (model, error) {
130
	var version uint32
131
	binary.Read(ro, binary.LittleEndian, &version)
132
133
134
135

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

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

	// different model types may have different layouts for hyperparameters
	var llama llamaModel
143
	binary.Read(ro, binary.LittleEndian, &llama.hyperparameters)
Bruce MacDonald's avatar
Bruce MacDonald committed
144
	return &llama, nil
145
146
147
148
149
150
151
152
153
154
}

type containerLORA struct {
	version uint32
}

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

Michael Yang's avatar
Michael Yang committed
155
func (c *containerLORA) Decode(ro *readSeekOffset) (model, error) {
156
	var version uint32
157
	binary.Read(ro, binary.LittleEndian, &version)
158
159
160
161

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

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

169
const (
Bruce MacDonald's avatar
Bruce MacDonald committed
170
	// Magic constant for `ggml` files (unversioned).
171
	FILE_MAGIC_GGML = 0x67676d6c
Bruce MacDonald's avatar
Bruce MacDonald committed
172
	// Magic constant for `ggml` files (versioned, ggmf).
173
	FILE_MAGIC_GGMF = 0x67676d66
Bruce MacDonald's avatar
Bruce MacDonald committed
174
	// Magic constant for `ggml` files (versioned, ggjt).
175
	FILE_MAGIC_GGJT = 0x67676a74
Bruce MacDonald's avatar
Bruce MacDonald committed
176
	// Magic constant for `ggla` files (LoRA adapter).
177
	FILE_MAGIC_GGLA = 0x67676C61
Bruce MacDonald's avatar
Bruce MacDonald committed
178
	// Magic constant for `gguf` files (versioned, gguf)
Michael Yang's avatar
ggufv3  
Michael Yang committed
179
180
	FILE_MAGIC_GGUF_LE = 0x46554747
	FILE_MAGIC_GGUF_BE = 0x47475546
181
182
)

Michael Yang's avatar
Michael Yang committed
183
184
func DecodeGGML(r io.ReadSeeker) (*GGML, error) {
	ro := readSeekOffset{ReadSeeker: r}
185

186
187
188
189
190
191
192
	var magic uint32
	if err := binary.Read(&ro, binary.LittleEndian, &magic); err != nil {
		return nil, err
	}

	var c container
	switch magic {
193
	case FILE_MAGIC_GGML:
194
		c = &containerGGML{}
195
	case FILE_MAGIC_GGMF:
196
		c = &containerGGMF{}
197
	case FILE_MAGIC_GGJT:
198
		c = &containerGGJT{}
199
	case FILE_MAGIC_GGLA:
200
		c = &containerLORA{}
Michael Yang's avatar
ggufv3  
Michael Yang committed
201
	case FILE_MAGIC_GGUF_LE:
202
		c = &containerGGUF{bo: binary.LittleEndian}
Michael Yang's avatar
ggufv3  
Michael Yang committed
203
	case FILE_MAGIC_GGUF_BE:
204
		c = &containerGGUF{bo: binary.BigEndian}
205
206
207
208
	default:
		return nil, errors.New("invalid file magic")
	}

209
	model, err := c.Decode(&ro)
Bruce MacDonald's avatar
Bruce MacDonald committed
210
	if err != nil {
211
212
213
214
		return nil, err
	}

	// final model type
215
216
217
218
219
220
221
	return &GGML{
		container: c,
		model:     model,
		Size:      ro.offset,
	}, nil
}

Michael Yang's avatar
Michael Yang committed
222
223
type readSeekOffset struct {
	io.ReadSeeker
224
225
226
	offset int64
}

Michael Yang's avatar
Michael Yang committed
227
228
229
230
231
232
233
234
235
236
237
238
239
func (rso *readSeekOffset) Seek(offset int64, whence int) (int64, error) {
	offset, err := rso.ReadSeeker.Seek(offset, whence)
	if err != nil {
		return 0, err
	}

	rso.offset = offset
	return offset, nil
}

func (rso *readSeekOffset) Read(p []byte) (int, error) {
	n, err := rso.ReadSeeker.Read(p)
	rso.offset += int64(n)
240
	return n, err
241
}