From f36f1d6be988848d24d269a6bc5e3697fa0fb18a Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Mon, 20 May 2024 14:58:27 -0700 Subject: [PATCH 1/2] tidy intermediate blobs --- server/images.go | 12 ++++-------- server/model.go | 7 +++---- server/routes.go | 8 ++++---- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/server/images.go b/server/images.go index 8e8fd921..aaf66ae0 100644 --- a/server/images.go +++ b/server/images.go @@ -341,8 +341,8 @@ func CreateModel(ctx context.Context, name, modelFileDir, quantization string, m } } else if strings.HasPrefix(c.Args, "@") { digest := strings.TrimPrefix(c.Args, "@") - if ib, ok := intermediateBlobs.Load(digest); ok { - p, err := GetBlobsPath(ib.(string)) + if ib, ok := intermediateBlobs[digest]; ok { + p, err := GetBlobsPath(ib) if err != nil { return err } @@ -352,8 +352,8 @@ func CreateModel(ctx context.Context, name, modelFileDir, quantization string, m } else if err != nil { return err } else { - fn(api.ProgressResponse{Status: fmt.Sprintf("using cached layer %s", ib.(string))}) - digest = ib.(string) + fn(api.ProgressResponse{Status: fmt.Sprintf("using cached layer %s", ib)}) + digest = ib } } @@ -415,14 +415,10 @@ func CreateModel(ctx context.Context, name, modelFileDir, quantization string, m return err } - f16digest := baseLayer.Layer.Digest - baseLayer.Layer, err = NewLayer(temp, baseLayer.Layer.MediaType) if err != nil { return err } - - intermediateBlobs.Store(f16digest, baseLayer.Layer.Digest) } } diff --git a/server/model.go b/server/model.go index eabb8f3d..fcf406f6 100644 --- a/server/model.go +++ b/server/model.go @@ -10,7 +10,6 @@ import ( "net/http" "os" "path/filepath" - "sync" "github.com/ollama/ollama/api" "github.com/ollama/ollama/convert" @@ -18,7 +17,7 @@ import ( "github.com/ollama/ollama/types/model" ) -var intermediateBlobs sync.Map +var intermediateBlobs map[string]string = make(map[string]string) type layerWithGGML struct { *Layer @@ -169,7 +168,7 @@ func parseFromZipFile(_ context.Context, file *os.File, digest string, fn func(a layer, err := NewLayer(temp, "application/vnd.ollama.image.model") if err != nil { - return nil, fmt.Errorf("aaa: %w", err) + return nil, err } bin, err := layer.Open() @@ -185,7 +184,7 @@ func parseFromZipFile(_ context.Context, file *os.File, digest string, fn func(a layers = append(layers, &layerWithGGML{layer, ggml}) - intermediateBlobs.Store(digest, layer.Digest) + intermediateBlobs[digest] = layer.Digest return layers, nil } diff --git a/server/routes.go b/server/routes.go index 12b11b5c..4b3239e1 100644 --- a/server/routes.go +++ b/server/routes.go @@ -841,16 +841,16 @@ func (s *Server) HeadBlobHandler(c *gin.Context) { } func (s *Server) CreateBlobHandler(c *gin.Context) { - ib, ok := intermediateBlobs.Load(c.Param("digest")) - if ok { - p, err := GetBlobsPath(ib.(string)) + if ib, ok := intermediateBlobs[c.Param("digest")]; ok { + p, err := GetBlobsPath(ib) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } if _, err := os.Stat(p); errors.Is(err, os.ErrNotExist) { - intermediateBlobs.Delete(c.Param("digest")) + slog.Info("evicting intermediate blob which no longer exists", "digest", ib) + delete(intermediateBlobs, c.Param("digest")) } else if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return From 807d092761a1100704d260fccb6f0bc679f9b98e Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Fri, 17 May 2024 11:29:04 -0700 Subject: [PATCH 2/2] fix quantize file types --- server/images.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/server/images.go b/server/images.go index aaf66ae0..520c899b 100644 --- a/server/images.go +++ b/server/images.go @@ -415,10 +415,17 @@ func CreateModel(ctx context.Context, name, modelFileDir, quantization string, m return err } - baseLayer.Layer, err = NewLayer(temp, baseLayer.Layer.MediaType) + layers, err := parseFromFile(ctx, temp, "", fn) if err != nil { return err } + + if len(layers) != 1 { + return errors.New("quantization failed") + } + + baseLayer.Layer = layers[0].Layer + baseLayer.GGML = layers[0].GGML } }