forked from docker/model-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu.go
More file actions
97 lines (85 loc) · 2.82 KB
/
gpu.go
File metadata and controls
97 lines (85 loc) · 2.82 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
package gpu
import (
"context"
"os/exec"
"github.com/docker/docker/client"
)
// GPUSupport encodes the GPU support available on a Docker engine.
type GPUSupport uint8
const (
// GPUSupportNone indicates no detectable GPU support.
GPUSupportNone GPUSupport = iota
// GPUSupportCUDA indicates CUDA GPU support.
GPUSupportCUDA
// GPUSupportROCm indicates ROCm GPU support.
GPUSupportROCm
// GPUSupportMUSA indicates MUSA GPU support.
GPUSupportMUSA
// GPUSupportCANN indicates Ascend NPU support.
GPUSupportCANN
)
// ProbeGPUSupport determines whether or not the Docker engine has GPU support.
func ProbeGPUSupport(ctx context.Context, dockerClient client.SystemAPIClient) (GPUSupport, error) {
// Check for ROCm runtime first
if hasROCm, err := HasROCmRuntime(ctx, dockerClient); err == nil && hasROCm {
return GPUSupportROCm, nil
}
// Then check for MTHREADS runtime
if hasMTHREADS, err := HasMTHREADSRuntime(ctx, dockerClient); err == nil && hasMTHREADS {
return GPUSupportMUSA, nil
}
// Check for CANN runtime first
if hasCANN, err := HasCANNRuntime(ctx, dockerClient); err == nil && hasCANN {
return GPUSupportCANN, nil
}
// Then search for nvidia-container-runtime on PATH
if _, err := exec.LookPath("nvidia-container-runtime"); err == nil {
return GPUSupportCUDA, nil
}
// Next look for explicitly configured nvidia runtime. This is not required in Docker 19.03+ but
// may be configured on some systems
hasNvidia, err := HasNVIDIARuntime(ctx, dockerClient)
if err != nil {
return GPUSupportNone, err
}
if hasNvidia {
return GPUSupportCUDA, nil
}
return GPUSupportNone, nil
}
// HasNVIDIARuntime determines whether there is an nvidia runtime available
func HasNVIDIARuntime(ctx context.Context, dockerClient client.SystemAPIClient) (bool, error) {
info, err := dockerClient.Info(ctx)
if err != nil {
return false, err
}
_, hasNvidia := info.Runtimes["nvidia"]
return hasNvidia, nil
}
// HasROCmRuntime determines whether there is a ROCm runtime available
func HasROCmRuntime(ctx context.Context, dockerClient client.SystemAPIClient) (bool, error) {
info, err := dockerClient.Info(ctx)
if err != nil {
return false, err
}
_, hasROCm := info.Runtimes["rocm"]
return hasROCm, nil
}
// HasMTHREADSRuntime determines whether there is a mthreads runtime available
func HasMTHREADSRuntime(ctx context.Context, dockerClient client.SystemAPIClient) (bool, error) {
info, err := dockerClient.Info(ctx)
if err != nil {
return false, err
}
_, hasMTHREADS := info.Runtimes["mthreads"]
return hasMTHREADS, nil
}
// HasCANNRuntime determines whether there is a Ascend CANN runtime available
func HasCANNRuntime(ctx context.Context, dockerClient client.SystemAPIClient) (bool, error) {
info, err := dockerClient.Info(ctx)
if err != nil {
return false, err
}
_, hasCANN := info.Runtimes["cann"]
return hasCANN, nil
}