ollama/llm/ggml.go

205 lines
3.7 KiB
Go
Raw Normal View History

2023-07-21 20:33:56 +00:00
package llm
import (
"encoding/binary"
"errors"
"io"
2023-09-07 17:55:37 +00:00
"path"
"sync"
2023-07-21 20:33:56 +00:00
)
type ModelFamily string
2023-09-07 17:55:37 +00:00
const ModelFamilyUnknown ModelFamily = "unknown"
2023-07-21 20:33:56 +00:00
type ModelType uint32
const (
ModelType3B ModelType = 26
ModelType7B ModelType = 32
ModelType13B ModelType = 40
2023-08-24 17:35:44 +00:00
ModelType34B ModelType = 48
2023-07-21 20:33:56 +00:00
ModelType30B ModelType = 60
ModelType65B ModelType = 80
)
2023-08-17 18:37:27 +00:00
func (mt ModelType) String() string {
switch mt {
case ModelType3B:
return "3B"
case ModelType7B:
return "7B"
case ModelType13B:
return "13B"
2023-08-24 17:35:44 +00:00
case ModelType34B:
return "34B"
2023-08-17 18:37:27 +00:00
case ModelType30B:
return "30B"
case ModelType65B:
return "65B"
default:
return "Unknown"
}
}
2023-07-21 20:33:56 +00:00
2023-08-17 18:37:27 +00:00
type FileType interface {
String() string
}
2023-07-21 20:33:56 +00:00
type GGML struct {
magic uint32
container
2023-08-17 18:37:27 +00:00
model
}
2023-07-21 20:33:56 +00:00
2023-08-17 18:37:27 +00:00
type model interface {
ModelFamily() ModelFamily
ModelType() ModelType
FileType() FileType
2023-07-21 20:33:56 +00:00
}
type container interface {
Name() string
2023-09-07 17:55:37 +00:00
Decode(io.Reader) (model, error)
2023-07-21 20:33:56 +00:00
}
2023-09-07 17:55:37 +00:00
type containerGGML struct{}
2023-07-21 20:33:56 +00:00
func (c *containerGGML) Name() string {
return "ggml"
}
2023-09-07 17:55:37 +00:00
func (c *containerGGML) Decode(r io.Reader) (model, error) {
return nil, nil
2023-07-21 20:33:56 +00:00
}
type containerGGMF struct {
version uint32
}
func (c *containerGGMF) Name() string {
return "ggmf"
}
2023-09-07 17:55:37 +00:00
func (c *containerGGMF) Decode(r io.Reader) (model, error) {
2023-07-21 20:33:56 +00:00
var version uint32
binary.Read(r, binary.LittleEndian, &version)
switch version {
case 1:
default:
2023-09-07 17:55:37 +00:00
return nil, errors.New("invalid version")
2023-07-21 20:33:56 +00:00
}
c.version = version
2023-09-07 17:55:37 +00:00
return nil, nil
2023-07-21 20:33:56 +00:00
}
type containerGGJT struct {
version uint32
}
func (c *containerGGJT) Name() string {
return "ggjt"
}
2023-09-07 17:55:37 +00:00
func (c *containerGGJT) Decode(r io.Reader) (model, error) {
2023-07-21 20:33:56 +00:00
var version uint32
binary.Read(r, binary.LittleEndian, &version)
switch version {
case 1, 2, 3:
default:
2023-09-07 17:55:37 +00:00
return nil, errors.New("invalid version")
2023-07-21 20:33:56 +00:00
}
c.version = version
2023-09-07 17:55:37 +00:00
// different model types may have different layouts for hyperparameters
var llama llamaModel
binary.Read(r, binary.LittleEndian, &llama.hyperparameters)
return &llama, nil
2023-07-21 20:33:56 +00:00
}
type containerLORA struct {
version uint32
}
func (c *containerLORA) Name() string {
return "ggla"
}
2023-09-07 17:55:37 +00:00
func (c *containerLORA) Decode(r io.Reader) (model, error) {
2023-07-21 20:33:56 +00:00
var version uint32
binary.Read(r, binary.LittleEndian, &version)
switch version {
case 1:
default:
2023-09-07 17:55:37 +00:00
return nil, errors.New("invalid version")
2023-07-21 20:33:56 +00:00
}
c.version = version
2023-09-07 17:55:37 +00:00
return nil, nil
}
var (
ggmlGPU = path.Join("llama.cpp", "ggml", "build", "gpu", "bin")
ggmlCPU = path.Join("llama.cpp", "ggml", "build", "cpu", "bin")
)
var (
ggmlInit sync.Once
ggmlRunnerPath string
)
func ggmlRunner() ModelRunner {
ggmlInit.Do(func() {
ggmlRunnerPath = chooseRunner(ggmlGPU, ggmlCPU)
})
return ModelRunner{Path: ggmlRunnerPath}
2023-07-21 20:33:56 +00:00
}
const (
2023-09-07 17:55:37 +00:00
// Magic constant for `ggml` files (unversioned).
2023-07-21 20:33:56 +00:00
FILE_MAGIC_GGML = 0x67676d6c
2023-09-07 17:55:37 +00:00
// Magic constant for `ggml` files (versioned, ggmf).
2023-07-21 20:33:56 +00:00
FILE_MAGIC_GGMF = 0x67676d66
2023-09-07 17:55:37 +00:00
// Magic constant for `ggml` files (versioned, ggjt).
2023-07-21 20:33:56 +00:00
FILE_MAGIC_GGJT = 0x67676a74
2023-09-07 17:55:37 +00:00
// Magic constant for `ggla` files (LoRA adapter).
2023-07-21 20:33:56 +00:00
FILE_MAGIC_GGLA = 0x67676C61
2023-09-07 17:55:37 +00:00
// Magic constant for `gguf` files (versioned, gguf)
FILE_MAGIC_GGUF = 0x46554747
2023-07-21 20:33:56 +00:00
)
2023-09-07 17:55:37 +00:00
func DecodeGGML(r io.ReadSeeker) (*GGML, error) {
2023-07-21 20:33:56 +00:00
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{}
2023-09-07 17:55:37 +00:00
case FILE_MAGIC_GGUF:
ggml.container = &containerGGUF{}
2023-07-21 20:33:56 +00:00
default:
return nil, errors.New("invalid file magic")
}
2023-09-07 17:55:37 +00:00
model, err := ggml.Decode(r)
if err != nil {
2023-07-21 20:33:56 +00:00
return nil, err
}
2023-09-07 17:55:37 +00:00
ggml.model = model
2023-07-21 20:33:56 +00:00
// final model type
return &ggml, nil
}