ollama/convert/reader.go

87 lines
1.6 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 {
2024-06-28 20:27:05 +00:00
if strings.HasSuffix(t.name, ".ffn_gate_inp.weight") ||
t.name == "token_types.weight" {
// these tensors are always F32
2024-06-06 15:59:04 +00:00
return 0
2024-06-01 03:00:49 +00:00
}
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-28 20:27:05 +00:00
func parseTensors(fsys fs.FS, replacer *strings.Replacer) ([]Tensor, error) {
2024-07-31 22:39:11 +00:00
patterns := []struct {
Pattern string
2024-06-28 20:27:05 +00:00
Func func(fs.FS, *strings.Replacer, ...string) ([]Tensor, error)
2024-07-31 22:39:11 +00:00
}{
{"model-*-of-*.safetensors", parseSafetensors},
{"model.safetensors", parseSafetensors},
{"adapters.safetensors", parseSafetensors},
{"adapter_model.safetensors", parseSafetensors},
2024-07-31 22:39:11 +00:00
{"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-06-28 20:27:05 +00:00
return pattern.Func(fsys, replacer, matches...)
2024-06-01 03:00:49 +00:00
}
}
return nil, errors.New("unknown tensor format")
}