Skip to content

Commit 183b92b

Browse files
committed
[AIE-151] native: support dynamic detection of OpenCL
Signed-off-by: Jacob Howard <jacob.howard@docker.com>
1 parent 4239791 commit 183b92b

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

pkg/inference/backends/llamacpp/download_windows.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func (l *llamaCpp) ensureLatestLlamaCpp(ctx context.Context, log logging.Logger,
1414
llamaCppPath, vendoredServerStoragePath string,
1515
) error {
1616
nvGPUInfoBin := filepath.Join(vendoredServerStoragePath, "com.docker.nv-gpu-info.exe")
17-
var canUseCUDA11 bool
17+
var canUseCUDA11, canUseOpenCL bool
1818
var err error
1919
ShouldUseGPUVariantLock.Lock()
2020
defer ShouldUseGPUVariantLock.Unlock()
@@ -25,15 +25,19 @@ func (l *llamaCpp) ensureLatestLlamaCpp(ctx context.Context, log logging.Logger,
2525
l.status = fmt.Sprintf("failed to check CUDA 11 capability: %v", err)
2626
return fmt.Errorf("failed to check CUDA 11 capability: %w", err)
2727
}
28+
} else if runtime.GOARCH == "arm64" {
29+
canUseOpenCL, err = hasOpenCL()
30+
if err != nil {
31+
l.status = fmt.Sprintf("failed to check OpenCL capability: %v", err)
32+
return fmt.Errorf("failed to check OpenCL capability: %w", err)
33+
}
2834
}
2935
}
3036
desiredVersion := "latest"
3137
desiredVariant := "cpu"
3238
if canUseCUDA11 {
3339
desiredVariant = "cuda"
34-
}
35-
// TODO(p1-0tr): we should auto-detect if we can use opencl, but for now assume that we can
36-
if runtime.GOARCH == "arm64" {
40+
} else if canUseOpenCL {
3741
desiredVariant = "opencl"
3842
}
3943
l.status = fmt.Sprintf("looking for updates for %s variant", desiredVariant)

pkg/inference/backends/llamacpp/gpuinfo_windows.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package llamacpp
33
import (
44
"bufio"
55
"context"
6+
"errors"
67
"fmt"
78
"os/exec"
89
"strconv"
910
"strings"
11+
"syscall"
1012

1113
"github.com/jaypipes/ghw"
1214
)
@@ -56,6 +58,27 @@ func hasCUDA11CapableGPU(ctx context.Context, nvGPUInfoBin string) (bool, error)
5658
return false, nil
5759
}
5860

61+
func hasOpenCL() (bool, error) {
62+
opencl, err := syscall.LoadLibrary("OpenCL.dll")
63+
if err != nil {
64+
if errors.Is(err, syscall.ERROR_MOD_NOT_FOUND) {
65+
return false, nil
66+
}
67+
return false, fmt.Errorf("unable to load OpenCL DLL: %w", err)
68+
}
69+
// We could perform additional platform and device version checks here (if
70+
// we scaffold out the relevant OpenCL API datatypes in Go), but since users
71+
// can opt-out of GPU support, we can probably skip that and just let users
72+
// disable it if things don't work. Alternatively, we could inspect the GPUs
73+
// found by the ghw package, if it supports (e.g.) Adreno GPUs.
74+
syscall.FreeLibrary(opencl)
75+
return true, nil
76+
}
77+
5978
func CanUseGPU(ctx context.Context, nvGPUInfoBin string) (bool, error) {
60-
return hasCUDA11CapableGPU(ctx, nvGPUInfoBin)
79+
haveCUDA11GPU, err := hasCUDA11CapableGPU(ctx, nvGPUInfoBin)
80+
if haveCUDA11GPU || err != nil {
81+
return haveCUDA11GPU, err
82+
}
83+
return hasOpenCL()
6184
}

0 commit comments

Comments
 (0)