-
Notifications
You must be signed in to change notification settings - Fork 688
Expand file tree
/
Copy pathserve.go
More file actions
112 lines (90 loc) · 2.69 KB
/
serve.go
File metadata and controls
112 lines (90 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package cli
import (
"runtime"
"strings"
"github.com/spf13/cobra"
"github.com/replicate/cog/pkg/config"
"github.com/replicate/cog/pkg/docker"
"github.com/replicate/cog/pkg/image"
"github.com/replicate/cog/pkg/util"
"github.com/replicate/cog/pkg/util/console"
)
var (
port = 8393
)
func newServeCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "serve",
Short: "Run a prediction HTTP server",
Long: `Run a prediction HTTP server.
Generate and run an HTTP server based on the declared model inputs and outputs.`,
RunE: cmdServe,
Args: cobra.MaximumNArgs(0),
SuggestFor: []string{"http"},
}
addBuildProgressOutputFlag(cmd)
addUseCudaBaseImageFlag(cmd)
addUseCogBaseImageFlag(cmd)
addGpusFlag(cmd)
addFastFlag(cmd)
addConfigFlag(cmd)
cmd.Flags().IntVarP(&port, "port", "p", port, "Port on which to listen")
return cmd
}
func cmdServe(cmd *cobra.Command, arg []string) error {
ctx := cmd.Context()
cfg, projectDir, err := config.GetConfig(configFilename)
if err != nil {
return err
}
imageName, err := image.BuildBase(ctx, cfg, projectDir, buildUseCudaBaseImage, DetermineUseCogBaseImage(cmd), buildProgressOutput)
if err != nil {
return err
}
if buildFast {
console.Info("Fast serve enabled.")
}
gpus := ""
if gpusFlag != "" {
gpus = gpusFlag
} else if cfg.Build.GPU {
gpus = "all"
}
args := []string{
"python",
"--check-hash-based-pycs", "never",
"-m", "cog.server.http",
"--await-explicit-shutdown", "true",
}
dockerCommand := docker.NewDockerCommand()
runOptions := docker.RunOptions{
Args: args,
Env: envFlags,
GPUs: gpus,
Image: imageName,
Volumes: []docker.Volume{{Source: projectDir, Destination: "/src"}},
Workdir: "/src",
}
runOptions, err = docker.FillInWeightsManifestVolumes(ctx, dockerCommand, runOptions)
if err != nil {
return err
}
if util.IsAppleSiliconMac(runtime.GOOS, runtime.GOARCH) {
runOptions.Platform = "linux/amd64"
}
runOptions.Ports = append(runOptions.Ports, docker.Port{HostPort: port, ContainerPort: 5000})
console.Info("")
console.Infof("Running '%[1]s' in Docker with the current directory mounted as a volume...", strings.Join(args, " "))
console.Info("")
console.Infof("Serving at http://127.0.0.1:%[1]v", port)
console.Info("")
err = docker.Run(ctx, runOptions)
// Only retry if we're using a GPU but but the user didn't explicitly select a GPU with --gpus
// If the user specified the wrong GPU, they are explicitly selecting a GPU and they'll want to hear about it
if runOptions.GPUs == "all" && err == docker.ErrMissingDeviceDriver {
console.Info("Missing device driver, re-trying without GPU")
runOptions.GPUs = ""
err = docker.Run(ctx, runOptions)
}
return err
}