Skip to content

Commit 2220cdf

Browse files
committed
Detect operating environment and adjust certain behaviors.
This commit introduces a new environment detection mechanism based on process name and environment variables. It uses this information (initially) for the purposes of tuning concurrent model behaviors on cloud. Signed-off-by: Jacob Howard <jacob.howard@docker.com>
1 parent 6d3a7ac commit 2220cdf

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

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.Split(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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"runtime"
88
"time"
99

10+
"github.com/docker/model-runner/pkg/environment"
1011
"github.com/docker/model-runner/pkg/inference"
1112
"github.com/docker/model-runner/pkg/inference/models"
1213
"github.com/docker/model-runner/pkg/logging"
@@ -110,7 +111,12 @@ func newLoader(
110111
// VRAM size here (and potentially even reserving a portion of it) and
111112
// computing model size through estimation (using parameter count and
112113
// quantization data type size).
114+
//
115+
// HACK: On GPU-enabled cloud engines, we'll temporarily bump this to 2.
113116
totalMemory := uint64(1)
117+
if environment.Get() == EnvironmentCloud && os.Getenv("NVIDIA_VISIBLE_DEVICES") != "" {
118+
totalMemory = 2
119+
}
114120

115121
// Create the loader.
116122
l := &loader{

0 commit comments

Comments
 (0)