Skip to content

Commit

Permalink
Request GPU without deprecated --runtime=nvidia (#913)
Browse files Browse the repository at this point in the history
(cherry picked from commit 730e8b0)
  • Loading branch information
Egor-S authored and peterschmidt85 committed Feb 19, 2024
1 parent 4d60c80 commit 5e74a49
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions runner/internal/shim/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ func pullImage(ctx context.Context, client docker.APIClient, imageName string, r
}

func createContainer(ctx context.Context, client docker.APIClient, params DockerParameters) (string, error) {
runtime, err := getRuntime(ctx, client)
gpuRequest, err := requestGpuIfAvailable(ctx, client)
if err != nil {
return "", gerrors.Wrap(err)
}

mounts, err := params.DockerMounts()
if err != nil {
return "", gerrors.Wrap(err)
Expand All @@ -103,8 +102,10 @@ func createContainer(ctx context.Context, client docker.APIClient, params Docker
PortBindings: bindPorts(params.DockerPorts()...),
PublishAllPorts: true,
Sysctls: map[string]string{},
Runtime: runtime,
Mounts: mounts,
Resources: container.Resources{
DeviceRequests: gpuRequest,
},
Mounts: mounts,
}
resp, err := client.ContainerCreate(ctx, containerConfig, hostConfig, nil, nil, "")
if err != nil {
Expand Down Expand Up @@ -178,17 +179,21 @@ func getNetworkMode() container.NetworkMode {
return "default"
}

func getRuntime(ctx context.Context, client docker.APIClient) (string, error) {
func requestGpuIfAvailable(ctx context.Context, client docker.APIClient) ([]container.DeviceRequest, error) {
info, err := client.Info(ctx)
if err != nil {
return "", gerrors.Wrap(err)
return nil, gerrors.Wrap(err)
}
for name := range info.Runtimes {
if name == consts.NVIDIA_RUNTIME {
return name, nil

for runtime := range info.Runtimes {
if runtime == consts.NVIDIA_RUNTIME {
return []container.DeviceRequest{
{Capabilities: [][]string{{"gpu"}}, Count: -1}, // --gpus=all
}, nil
}
}
return info.DefaultRuntime, nil

return nil, nil
}

/* DockerParameters interface implementation for CLIArgs */
Expand Down

0 comments on commit 5e74a49

Please sign in to comment.