ollama/convert/reader.go

83 lines
1.4 KiB
Go
Raw Normal View History

2024-06-01 03:00:49 +00:00
package convert
import (
"errors"
"io"
2024-06-29 23:53:59 +00:00
"io/fs"
2024-06-01 03:00:49 +00:00
"strings"
)
type Tensor interface {
Name() string
Shape() []uint64
Kind() uint32
SetRepacker(repacker)
WriteTo(io.Writer) (int64, error)
}
type tensorBase struct {
name string
shape []uint64
repacker
}
func (t tensorBase) Name() string {
return t.name
}
func (t tensorBase) Shape() []uint64 {
return t.shape
}
2024-07-08 23:59:48 +00:00
const (
tensorKindF32 uint32 = iota
tensorKindF16
)
2024-06-01 03:00:49 +00:00
func (t tensorBase) Kind() uint32 {
if strings.HasSuffix(t.name, ".block_sparse_moe.gate.weight") {
return 0
}
switch len(t.shape) {
case 0:
panic("invalid tensor shape")
case 1:
2024-07-08 23:59:48 +00:00
return tensorKindF32
2024-06-01 03:00:49 +00:00
default:
2024-07-08 23:59:48 +00:00
return tensorKindF16
2024-06-01 03:00:49 +00:00
}
}
func (t *tensorBase) SetRepacker(fn repacker) {
t.repacker = fn
}
type repacker func(string, []float32, []uint64) ([]float32, error)
2024-06-29 23:53:59 +00:00
func parseTensors(fsys fs.FS) ([]Tensor, error) {
2024-07-31 22:39:11 +00:00
patterns := []struct {
Pattern string
Func func(fs.FS, ...string) ([]Tensor, error)
}{
{"model-*-of-*.safetensors", parseSafetensors},
{"model.safetensors", parseSafetensors},
{"pytorch_model-*-of-*.bin", parseTorch},
{"pytorch_model.bin", parseTorch},
{"consolidated.*.pth", parseTorch},
2024-06-01 03:00:49 +00:00
}
2024-07-31 22:39:11 +00:00
for _, pattern := range patterns {
matches, err := fs.Glob(fsys, pattern.Pattern)
2024-06-01 03:00:49 +00:00
if err != nil {
return nil, err
}
if len(matches) > 0 {
2024-07-31 22:39:11 +00:00
return pattern.Func(fsys, matches...)
2024-06-01 03:00:49 +00:00
}
}
return nil, errors.New("unknown tensor format")
}