Skip to content

Commit e7b086c

Browse files
authored
Add support for fast build images in cog run (#2408)
* Cog run fast builds the image then runs the command on that image if in fast mode
1 parent 0a49320 commit e7b086c

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

pkg/cli/run.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,44 @@ func run(cmd *cobra.Command, args []string) error {
5959
if err != nil {
6060
return err
6161
}
62+
client := registry.NewRegistryClient()
6263

6364
cfg, projectDir, err := config.GetConfig(configFilename)
6465
if err != nil {
6566
return err
6667
}
67-
client := registry.NewRegistryClient()
68-
imageName, err := image.BuildBase(ctx, dockerClient, cfg, projectDir, buildUseCudaBaseImage, DetermineUseCogBaseImage(cmd), buildProgressOutput, client)
69-
if err != nil {
70-
return err
68+
69+
var imageName string
70+
if cfg.Build.Fast || buildFast {
71+
imageName = config.DockerImageName(projectDir)
72+
err = image.Build(
73+
ctx,
74+
cfg,
75+
projectDir,
76+
imageName,
77+
buildSecrets,
78+
buildNoCache,
79+
buildSeparateWeights,
80+
buildUseCudaBaseImage,
81+
buildProgressOutput,
82+
buildSchemaFile,
83+
buildDockerfileFile,
84+
DetermineUseCogBaseImage(cmd),
85+
buildStrip,
86+
buildPrecompile,
87+
cfg.Build.Fast || buildFast,
88+
nil,
89+
buildLocalImage,
90+
dockerClient,
91+
client)
92+
if err != nil {
93+
return err
94+
}
95+
} else {
96+
imageName, err = image.BuildBase(ctx, dockerClient, cfg, projectDir, buildUseCudaBaseImage, DetermineUseCogBaseImage(cmd), buildProgressOutput, client)
97+
if err != nil {
98+
return err
99+
}
71100
}
72101

73102
gpus := ""
@@ -102,10 +131,6 @@ func run(cmd *cobra.Command, args []string) error {
102131
console.Info("")
103132
console.Infof("Running '%s' in Docker with the current directory mounted as a volume...", strings.Join(args, " "))
104133

105-
if buildFast {
106-
console.Info("Fast run enabled.")
107-
}
108-
109134
err = docker.Run(ctx, dockerClient, runOptions)
110135
// Only retry if we're using a GPU but but the user didn't explicitly select a GPU with --gpus
111136
// If the user specified the wrong GPU, they are explicitly selecting a GPU and they'll want to hear about it

test-integration/test_integration/test_run.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import subprocess
2+
from pathlib import Path
23

34

45
def test_run(tmpdir_factory, cog_binary):
@@ -46,3 +47,16 @@ def test_run_with_secret(tmpdir_factory, cog_binary):
4647
)
4748
assert b"RUN echo hello world" in result.stdout
4849
assert b"RUN --mount=type=secret,id=foo,target=secret.txt echo shh" in result.stdout
50+
51+
52+
def test_run_fast_build(cog_binary):
53+
project_dir = Path(__file__).parent / "fixtures/fast-build"
54+
result = subprocess.run(
55+
[cog_binary, "run", "echo", "hello world"],
56+
cwd=project_dir,
57+
check=True,
58+
capture_output=True,
59+
text=True,
60+
)
61+
assert result.returncode == 0
62+
assert result.stdout == "hello world\n"

0 commit comments

Comments
 (0)