no iterator
This commit is contained in:
parent
7ffe45734d
commit
4d0d0fa383
3 changed files with 34 additions and 85 deletions
|
@ -30,7 +30,6 @@ import (
|
||||||
"github.com/ollama/ollama/server/envconfig"
|
"github.com/ollama/ollama/server/envconfig"
|
||||||
"github.com/ollama/ollama/types/errtypes"
|
"github.com/ollama/ollama/types/errtypes"
|
||||||
"github.com/ollama/ollama/types/model"
|
"github.com/ollama/ollama/types/model"
|
||||||
"github.com/ollama/ollama/types/ordered"
|
|
||||||
"github.com/ollama/ollama/version"
|
"github.com/ollama/ollama/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -344,7 +343,7 @@ func CreateModel(ctx context.Context, name, modelFileDir, quantization string, m
|
||||||
|
|
||||||
switch c.Name {
|
switch c.Name {
|
||||||
case "model", "adapter":
|
case "model", "adapter":
|
||||||
var baseLayers *ordered.Map[*Layer, *llm.GGML]
|
var baseLayers []*layerWithGGML
|
||||||
if name := model.ParseName(c.Args); name.IsValid() {
|
if name := model.ParseName(c.Args); name.IsValid() {
|
||||||
baseLayers, err = parseFromModel(ctx, name, fn)
|
baseLayers, err = parseFromModel(ctx, name, fn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -377,70 +376,51 @@ func CreateModel(ctx context.Context, name, modelFileDir, quantization string, m
|
||||||
return fmt.Errorf("invalid model reference: %s", c.Args)
|
return fmt.Errorf("invalid model reference: %s", c.Args)
|
||||||
}
|
}
|
||||||
|
|
||||||
var err2 error
|
for _, baseLayer := range baseLayers {
|
||||||
var tempfiles []*os.File
|
if quantization != "" && baseLayer.GGML != nil && baseLayer.GGML.Name() == "gguf" {
|
||||||
|
|
||||||
// TODO(mxyng): replace with rangefunc
|
|
||||||
baseLayers.Items()(func(layer *Layer, ggml *llm.GGML) bool {
|
|
||||||
if quantization != "" && ggml != nil && ggml.Name() == "gguf" {
|
|
||||||
ftype, err := llm.ParseFileType(quantization)
|
ftype, err := llm.ParseFileType(quantization)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err2 = err
|
return err
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filetype := ggml.KV().FileType()
|
filetype := baseLayer.GGML.KV().FileType()
|
||||||
if !slices.Contains([]string{"F16", "F32"}, filetype) {
|
if !slices.Contains([]string{"F16", "F32"}, filetype) {
|
||||||
err2 = errors.New("quantization is only supported for F16 and F32 models")
|
return errors.New("quantization is only supported for F16 and F32 models")
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn(api.ProgressResponse{Status: fmt.Sprintf("quantizing %s model to %s", filetype, quantization)})
|
fn(api.ProgressResponse{Status: fmt.Sprintf("quantizing %s model to %s", filetype, quantization)})
|
||||||
|
|
||||||
blob, err := GetBlobsPath(layer.Digest)
|
blob, err := GetBlobsPath(baseLayer.Digest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err2 = err
|
return err
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
temp, err := os.CreateTemp(filepath.Dir(blob), quantization)
|
temp, err := os.CreateTemp(filepath.Dir(blob), quantization)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err2 = err
|
return err
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
tempfiles = append(tempfiles, temp)
|
defer temp.Close()
|
||||||
|
defer os.Remove(temp.Name())
|
||||||
|
|
||||||
if err := llm.Quantize(blob, temp.Name(), ftype); err != nil {
|
if err := llm.Quantize(blob, temp.Name(), ftype); err != nil {
|
||||||
err2 = err
|
return err
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
layer, err = NewLayer(temp, layer.MediaType)
|
baseLayer.Layer, err = NewLayer(temp, baseLayer.Layer.MediaType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err2 = err
|
return err
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ggml != nil {
|
if baseLayer.GGML != nil {
|
||||||
config.ModelFormat = cmp.Or(config.ModelFormat, ggml.Name())
|
config.ModelFormat = cmp.Or(config.ModelFormat, baseLayer.GGML.Name())
|
||||||
config.ModelFamily = cmp.Or(config.ModelFamily, ggml.KV().Architecture())
|
config.ModelFamily = cmp.Or(config.ModelFamily, baseLayer.GGML.KV().Architecture())
|
||||||
config.ModelType = cmp.Or(config.ModelType, format.HumanNumber(ggml.KV().ParameterCount()))
|
config.ModelType = cmp.Or(config.ModelType, format.HumanNumber(baseLayer.GGML.KV().ParameterCount()))
|
||||||
config.FileType = cmp.Or(config.FileType, ggml.KV().FileType())
|
config.FileType = cmp.Or(config.FileType, baseLayer.GGML.KV().FileType())
|
||||||
config.ModelFamilies = append(config.ModelFamilies, ggml.KV().Architecture())
|
config.ModelFamilies = append(config.ModelFamilies, baseLayer.GGML.KV().Architecture())
|
||||||
}
|
}
|
||||||
|
|
||||||
layers = append(layers, layer)
|
layers = append(layers, baseLayer.Layer)
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, tempfile := range tempfiles {
|
|
||||||
defer tempfile.Close()
|
|
||||||
defer os.Remove(tempfile.Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err2 != nil {
|
|
||||||
return err2
|
|
||||||
}
|
}
|
||||||
case "license", "template", "system":
|
case "license", "template", "system":
|
||||||
blob := strings.NewReader(c.Args)
|
blob := strings.NewReader(c.Args)
|
||||||
|
|
|
@ -15,10 +15,14 @@ import (
|
||||||
"github.com/ollama/ollama/convert"
|
"github.com/ollama/ollama/convert"
|
||||||
"github.com/ollama/ollama/llm"
|
"github.com/ollama/ollama/llm"
|
||||||
"github.com/ollama/ollama/types/model"
|
"github.com/ollama/ollama/types/model"
|
||||||
"github.com/ollama/ollama/types/ordered"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseFromModel(ctx context.Context, name model.Name, fn func(api.ProgressResponse)) (*ordered.Map[*Layer, *llm.GGML], error) {
|
type layerWithGGML struct {
|
||||||
|
*Layer
|
||||||
|
*llm.GGML
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseFromModel(ctx context.Context, name model.Name, fn func(api.ProgressResponse)) (layers []*layerWithGGML, err error) {
|
||||||
modelpath := ParseModelPath(name.DisplayLongest())
|
modelpath := ParseModelPath(name.DisplayLongest())
|
||||||
manifest, _, err := GetManifest(modelpath)
|
manifest, _, err := GetManifest(modelpath)
|
||||||
switch {
|
switch {
|
||||||
|
@ -36,7 +40,6 @@ func parseFromModel(ctx context.Context, name model.Name, fn func(api.ProgressRe
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
layers := ordered.NewMap[*Layer, *llm.GGML]()
|
|
||||||
for _, layer := range manifest.Layers {
|
for _, layer := range manifest.Layers {
|
||||||
layer, err := NewLayerFromLayer(layer.Digest, layer.MediaType, modelpath.GetShortTagname())
|
layer, err := NewLayerFromLayer(layer.Digest, layer.MediaType, modelpath.GetShortTagname())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -62,9 +65,10 @@ func parseFromModel(ctx context.Context, name model.Name, fn func(api.ProgressRe
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
layers.Add(layer, ggml)
|
|
||||||
|
layers = append(layers, &layerWithGGML{layer, ggml})
|
||||||
default:
|
default:
|
||||||
layers.Add(layer, nil)
|
layers = append(layers, &layerWithGGML{layer, nil})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -72,7 +76,7 @@ func parseFromModel(ctx context.Context, name model.Name, fn func(api.ProgressRe
|
||||||
return layers, nil
|
return layers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFromZipFile(_ context.Context, file *os.File, fn func(api.ProgressResponse)) (*ordered.Map[*Layer, *llm.GGML], error) {
|
func parseFromZipFile(_ context.Context, file *os.File, fn func(api.ProgressResponse)) (layers []*layerWithGGML, err error) {
|
||||||
stat, err := file.Stat()
|
stat, err := file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -184,12 +188,11 @@ func parseFromZipFile(_ context.Context, file *os.File, fn func(api.ProgressResp
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
layers := ordered.NewMap[*Layer, *llm.GGML]()
|
layers = append(layers, &layerWithGGML{layer, ggml})
|
||||||
layers.Add(layer, ggml)
|
|
||||||
return layers, nil
|
return layers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFromFile(ctx context.Context, file *os.File, fn func(api.ProgressResponse)) (*ordered.Map[*Layer, *llm.GGML], error) {
|
func parseFromFile(ctx context.Context, file *os.File, fn func(api.ProgressResponse)) (layers []*layerWithGGML, err error) {
|
||||||
sr := io.NewSectionReader(file, 0, 512)
|
sr := io.NewSectionReader(file, 0, 512)
|
||||||
contentType, err := detectContentType(sr)
|
contentType, err := detectContentType(sr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -205,8 +208,6 @@ func parseFromFile(ctx context.Context, file *os.File, fn func(api.ProgressRespo
|
||||||
return nil, fmt.Errorf("unsupported content type: %s", contentType)
|
return nil, fmt.Errorf("unsupported content type: %s", contentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
layers := ordered.NewMap[*Layer, *llm.GGML]()
|
|
||||||
|
|
||||||
stat, err := file.Stat()
|
stat, err := file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -233,7 +234,7 @@ func parseFromFile(ctx context.Context, file *os.File, fn func(api.ProgressRespo
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
layers.Add(layer, ggml)
|
layers = append(layers, &layerWithGGML{layer, ggml})
|
||||||
offset = n
|
offset = n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
package ordered
|
|
||||||
|
|
||||||
type Map[K comparable, V any] struct {
|
|
||||||
s []K
|
|
||||||
m map[K]V
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMap[K comparable, V any]() *Map[K, V] {
|
|
||||||
return &Map[K, V]{
|
|
||||||
s: make([]K, 0),
|
|
||||||
m: make(map[K]V),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type iter_Seq2[K, V any] func(func(K, V) bool)
|
|
||||||
|
|
||||||
func (m *Map[K, V]) Items() iter_Seq2[K, V] {
|
|
||||||
return func(yield func(K, V) bool) {
|
|
||||||
for _, k := range m.s {
|
|
||||||
if !yield(k, m.m[k]) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Map[K, V]) Add(k K, v V) {
|
|
||||||
if _, ok := m.m[k]; !ok {
|
|
||||||
m.s = append(m.s, k)
|
|
||||||
m.m[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue