Repository navigation
fix(models): don't pull again a model written in short form #14532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,12 +23,12 @@ import ( | |||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||
| "os/exec" | ||||||||||||||||||||||||||||||
| "slices" | ||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| "github.com/compose-spec/compose-go/v2/types" | ||||||||||||||||||||||||||||||
| "github.com/containerd/errdefs" | ||||||||||||||||||||||||||||||
| "github.com/distribution/reference" | ||||||||||||||||||||||||||||||
| "github.com/docker/cli/cli-plugins/manager" | ||||||||||||||||||||||||||||||
| "github.com/moby/moby/client/pkg/versions" | ||||||||||||||||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||||||||||||||||
|
|
@@ -49,6 +49,10 @@ func (s *composeService) ensureModels(ctx context.Context, project *types.Projec | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| defer mdlAPI.Close() | ||||||||||||||||||||||||||||||
| availableModels, err := mdlAPI.ListModels(ctx) | ||||||||||||||||||||||||||||||
| available := make(map[string]struct{}, len(availableModels)) | ||||||||||||||||||||||||||||||
| for _, m := range availableModels { | ||||||||||||||||||||||||||||||
| available[normalizeModelReference(m)] = struct{}{} | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| eg, ctx := concurrency.NewErrgroup(ctx, s.maxConcurrency) | ||||||||||||||||||||||||||||||
| eg.Go(func() error { | ||||||||||||||||||||||||||||||
|
|
@@ -60,7 +64,7 @@ func (s *composeService) ensureModels(ctx context.Context, project *types.Projec | |||||||||||||||||||||||||||||
| config.Name = name | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| eg.Go(func() error { | ||||||||||||||||||||||||||||||
| if !slices.Contains(availableModels, config.Model) { | ||||||||||||||||||||||||||||||
| if _, ok := available[normalizeModelReference(config.Model)]; !ok { | ||||||||||||||||||||||||||||||
| err = mdlAPI.PullModel(ctx, config, quietPull, s.events) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
|
|
@@ -72,6 +76,30 @@ func (s *composeService) ensureModels(ctx context.Context, project *types.Projec | |||||||||||||||||||||||||||||
| return eg.Wait() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // normalizeModelReference gives a model reference the form under which the model | ||||||||||||||||||||||||||||||
| // runner lists the model it has, whatever way it was asked for: docker.io/ai/smollm2:latest | ||||||||||||||||||||||||||||||
| // for ai/smollm2, and for a Hugging Face model huggingface.co/org/model-gguf:latest for | ||||||||||||||||||||||||||||||
| // hf.co/Org/Model-GGUF. The runner resolves Hugging Face references case-insensitively | ||||||||||||||||||||||||||||||
| // (the tag, such as Q4_K_M, keeps its case) and lists them under the huggingface.co | ||||||||||||||||||||||||||||||
| // host. A reference that cannot be parsed otherwise is left as it is. | ||||||||||||||||||||||||||||||
| func normalizeModelReference(model string) string { | ||||||||||||||||||||||||||||||
| if host, path, ok := strings.Cut(model, "/"); ok && (strings.EqualFold(host, "hf.co") || strings.EqualFold(host, "huggingface.co")) { | ||||||||||||||||||||||||||||||
| tag := "latest" | ||||||||||||||||||||||||||||||
| if i := strings.LastIndex(path, ":"); i > strings.LastIndex(path, "/") { | ||||||||||||||||||||||||||||||
| path, tag = path[:i], path[i+1:] | ||||||||||||||||||||||||||||||
| if tag == "" { | ||||||||||||||||||||||||||||||
| tag = "latest" | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+87
to
+93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
| return "huggingface.co/" + strings.ToLower(path) + ":" + tag | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| ref, err := reference.ParseNormalizedNamed(model) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return model | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return reference.TagNameOnly(ref).String() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| type modelAPI struct { | ||||||||||||||||||||||||||||||
| path string | ||||||||||||||||||||||||||||||
| env []string | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,8 @@ import ( | |||||
| "os" | ||||||
| "path/filepath" | ||||||
| "runtime" | ||||||
| "slices" | ||||||
| "strings" | ||||||
| "testing" | ||||||
|
|
||||||
| "github.com/compose-spec/compose-go/v2/types" | ||||||
|
|
@@ -73,3 +75,90 @@ esac | |||||
| url := project.Services["app"].Environment["LLM_URL"] | ||||||
| assert.Assert(t, url != nil && *url == "http://model-runner.test", "model variables are still computed from the read-only status") | ||||||
| } | ||||||
|
|
||||||
| // The model runner lists the models it has by their full tags, whatever way they | ||||||
| // were asked for: a model written in short form is the same model, and is not | ||||||
| // pulled again. | ||||||
| func TestEnsureModels_PullsOnlyWhatIsNotAvailable(t *testing.T) { | ||||||
| if runtime.GOOS == "windows" { | ||||||
| t.Skip("the fake plugin is a shell script") | ||||||
| } | ||||||
| tests := []struct { | ||||||
| name string | ||||||
| available string | ||||||
| model string | ||||||
| pulled bool | ||||||
| }{ | ||||||
| {name: "short form", available: "docker.io/ai/smollm2:latest", model: "ai/smollm2"}, | ||||||
| {name: "short form with the tag", available: "docker.io/ai/smollm2:latest", model: "ai/smollm2:latest"}, | ||||||
| {name: "full form without the tag", available: "docker.io/ai/smollm2:latest", model: "docker.io/ai/smollm2"}, | ||||||
| {name: "full form", available: "docker.io/ai/smollm2:latest", model: "docker.io/ai/smollm2:latest"}, | ||||||
|
ndeloof marked this conversation as resolved.
Resolved
|
||||||
| // the runner may list a model in a form that is not the canonical one | ||||||
| {name: "listed in short form", available: "ai/smollm2", model: "docker.io/ai/smollm2:latest"}, | ||||||
| {name: "another tag", available: "docker.io/ai/smollm2:latest", model: "ai/smollm2:360M-Q4_K_M", pulled: true}, | ||||||
| {name: "another model", available: "docker.io/ai/smollm3:latest", model: "ai/smollm2", pulled: true}, | ||||||
| {name: "nothing available", available: "", model: "ai/smollm2", pulled: true}, | ||||||
| // Hugging Face: the runner lists these under the huggingface.co host, in lower case | ||||||
| // (observed with Docker Model Runner v1.2.9), the tag keeping its case | ||||||
| {name: "hugging face", available: "huggingface.co/bartowski/smollm2-135m-instruct-gguf:latest", model: "hf.co/bartowski/SmolLM2-135M-Instruct-GGUF"}, | ||||||
| {name: "hugging face with the tag", available: "huggingface.co/bartowski/smollm2-135m-instruct-gguf:Q4_K_M", model: "hf.co/bartowski/SmolLM2-135M-Instruct-GGUF:Q4_K_M"}, | ||||||
| {name: "hugging face, another tag", available: "huggingface.co/bartowski/smollm2-135m-instruct-gguf:Q4_K_M", model: "hf.co/bartowski/SmolLM2-135M-Instruct-GGUF:Q8_0", pulled: true}, | ||||||
| {name: "hugging face, only another tag available", available: "huggingface.co/bartowski/smollm2-135m-instruct-gguf:Q4_K_M", model: "hf.co/bartowski/SmolLM2-135M-Instruct-GGUF", pulled: true}, | ||||||
| } | ||||||
| for _, tt := range tests { | ||||||
| t.Run(tt.name, func(t *testing.T) { | ||||||
| dir := t.TempDir() | ||||||
| calls := filepath.Join(dir, "calls.log") | ||||||
| tags := "[]" | ||||||
| if tt.available != "" { | ||||||
| tags = `[{"id":"sha256:1","tags":["` + tt.available + `"],"created":1}]` | ||||||
| } | ||||||
| script := `#!/bin/sh | ||||||
| case "$1" in | ||||||
| docker-cli-plugin-metadata) echo '{"SchemaVersion":"0.1.0","Vendor":"test","Version":"v1.2.6"}' ;; | ||||||
| ls) echo '` + tags + `' ;; | ||||||
| status) echo '{"endpoint":"http://model-runner.test"}' ;; | ||||||
| *) echo "$*" >> "` + calls + `" ;; | ||||||
| esac | ||||||
| ` | ||||||
| assert.NilError(t, os.WriteFile(filepath.Join(dir, "docker-model"), []byte(script), 0o700)) | ||||||
|
|
||||||
| cli := mocks.NewMockCli(gomock.NewController(t)) | ||||||
| cli.EXPECT().ConfigFile().Return(&configfile.ConfigFile{CLIPluginsExtraDirs: []string{dir}}).AnyTimes() | ||||||
| cli.EXPECT().CurrentContext().Return("default").AnyTimes() | ||||||
| cli.EXPECT().DockerEndpoint().Return(docker.Endpoint{}).AnyTimes() | ||||||
| s := &composeService{dockerCli: cli, events: progress.Noop{}} | ||||||
|
|
||||||
| var app types.ServiceConfig | ||||||
| app.Environment = types.MappingWithEquals{} | ||||||
| app.Models = map[string]*types.ServiceModelConfig{"llm": nil} | ||||||
| project := &types.Project{ | ||||||
| Models: types.Models{"llm": {Model: tt.model}}, | ||||||
| Services: types.Services{"app": app}, | ||||||
| } | ||||||
| assert.NilError(t, s.ensureModels(t.Context(), project, false)) | ||||||
|
|
||||||
| out, err := os.ReadFile(calls) | ||||||
| assert.NilError(t, err) | ||||||
| log := string(out) | ||||||
| lines := strings.Split(strings.TrimSpace(log), "\n") | ||||||
| assert.Equal(t, slices.Contains(lines, "pull "+tt.model), tt.pulled, "calls:\n%s", log) | ||||||
| assert.Assert(t, slices.Contains(lines, "configure "+tt.model), "the model is configured whether it was pulled or not, calls:\n%s", log) | ||||||
| }) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func TestNormalizeModelReference(t *testing.T) { | ||||||
| for model, want := range map[string]string{ | ||||||
| "ai/smollm2": "docker.io/ai/smollm2:latest", | ||||||
| "ai/smollm2:360M": "docker.io/ai/smollm2:360M", | ||||||
| "docker.io/ai/smollm2:latest": "docker.io/ai/smollm2:latest", | ||||||
| "index.docker.io/ai/smollm2": "docker.io/ai/smollm2:latest", | ||||||
| "hf.co/Org/Model-GGUF": "huggingface.co/org/model-gguf:latest", | ||||||
| "HF.co/Org/Model-GGUF:Q4_K_M": "huggingface.co/org/model-gguf:Q4_K_M", | ||||||
| "huggingface.co/org/model": "huggingface.co/org/model:latest", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This row passes through the generic path too (it is already lower case), so it doesn't pin the
Suggested change
|
||||||
| "hf.co/Org/Model:": "huggingface.co/org/model:latest", | ||||||
| } { | ||||||
| assert.Equal(t, normalizeModelReference(model), want, model) | ||||||
| } | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.