Skip to content

Commit 1a5e637

Browse files
authored
Merge pull request #83 from docker/model-runner-environment
Detect operating environment and adjust certain behaviors.
2 parents 9933b7d + 0cd306e commit 1a5e637

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

pkg/environment/environment.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package environment
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"runtime"
7+
"sync"
8+
)
9+
10+
// Environment encodes the operating environment for the model runner.
11+
type Environment uint8
12+
13+
const (
14+
// EnvironmentUnknown represents an unknown environment in which basic,
15+
// non-specialized defaults should be used.
16+
EnvironmentUnknown Environment = iota
17+
// EnvironmentDesktop represents a Docker Desktop environment.
18+
EnvironmentDesktop
19+
// EnvironmentMoby represents a Moby engine environment, if installed via
20+
// the model CLI.
21+
EnvironmentMoby
22+
// EnvironmentCloud represents a Docker Cloud environment, if installed via
23+
// the model CLI.
24+
EnvironmentCloud
25+
)
26+
27+
// environment is the cached environment.
28+
var environment Environment
29+
30+
// environmentOnce guards initialization of environment.
31+
var environmentOnce sync.Once
32+
33+
// isDockerBackend checks if an executable path is com.docker.backend.
34+
func isDockerBackend(path string) bool {
35+
leaf := filepath.Base(path)
36+
if runtime.GOOS == "windows" {
37+
return leaf == "com.docker.backend.exe"
38+
}
39+
return leaf == "com.docker.backend"
40+
}
41+
42+
// Get returns the current environment type.
43+
func Get() Environment {
44+
environmentOnce.Do(func() {
45+
// Check if we're running in a Docker Desktop backend process.
46+
if executable, err := os.Executable(); err == nil && isDockerBackend(executable) {
47+
environment = EnvironmentDesktop
48+
return
49+
}
50+
51+
// Look for a MODEL_RUNNER_ENVIRONMENT variable. If none is set or it's
52+
// invalid, then leave the environment unknown.
53+
switch os.Getenv("MODEL_RUNNER_ENVIRONMENT") {
54+
case "moby":
55+
environment = EnvironmentMoby
56+
case "cloud":
57+
environment = EnvironmentCloud
58+
}
59+
})
60+
return environment
61+
}

pkg/inference/scheduling/loader.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
78
"runtime"
89
"time"
910

11+
"github.com/docker/model-runner/pkg/environment"
1012
"github.com/docker/model-runner/pkg/inference"
1113
"github.com/docker/model-runner/pkg/inference/models"
1214
"github.com/docker/model-runner/pkg/logging"
@@ -110,7 +112,12 @@ func newLoader(
110112
// VRAM size here (and potentially even reserving a portion of it) and
111113
// computing model size through estimation (using parameter count and
112114
// quantization data type size).
115+
//
116+
// HACK: On GPU-enabled cloud engines, we'll temporarily bump this to 2.
113117
totalMemory := uint64(1)
118+
if environment.Get() == environment.EnvironmentCloud && os.Getenv("NVIDIA_VISIBLE_DEVICES") != "" {
119+
totalMemory = 2
120+
}
114121

115122
// Create the loader.
116123
l := &loader{

0 commit comments

Comments
 (0)