amd_common.go 2.45 KB
Newer Older
Daniel Hiltgen's avatar
Daniel Hiltgen committed
1
2
3
4
5
//go:build linux || windows

package gpu

import (
Michael Yang's avatar
lint  
Michael Yang committed
6
	"errors"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
7
8
9
	"log/slog"
	"os"
	"path/filepath"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
10
	"runtime"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
11
	"strings"
12
13

	"github.com/ollama/ollama/envconfig"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
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
)

// Determine if the given ROCm lib directory is usable by checking for existence of some glob patterns
func rocmLibUsable(libDir string) bool {
	slog.Debug("evaluating potential rocm lib dir " + libDir)
	for _, g := range ROCmLibGlobs {
		res, _ := filepath.Glob(filepath.Join(libDir, g))
		if len(res) == 0 {
			return false
		}
	}
	return true
}

func GetSupportedGFX(libDir string) ([]string, error) {
	var ret []string
	files, err := filepath.Glob(filepath.Join(libDir, "rocblas", "library", "TensileLibrary_lazy_gfx*.dat"))
	if err != nil {
		return nil, err
	}
	for _, file := range files {
		ret = append(ret, strings.TrimSuffix(strings.TrimPrefix(filepath.Base(file), "TensileLibrary_lazy_"), ".dat"))
	}
	return ret, nil
}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
40
41
42
43
44
45
func rocmGetVisibleDevicesEnv(gpuInfo []GpuInfo) (string, string) {
	ids := []string{}
	for _, info := range gpuInfo {
		if info.Library != "rocm" {
			// TODO shouldn't happen if things are wired correctly...
			slog.Debug("rocmGetVisibleDevicesEnv skipping over non-rocm device", "library", info.Library)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
46
47
			continue
		}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
48
		ids = append(ids, info.ID)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
49
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
50
51
	return "HIP_VISIBLE_DEVICES", strings.Join(ids, ",")
}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
52

Daniel Hiltgen's avatar
Daniel Hiltgen committed
53
func commonAMDValidateLibDir() (string, error) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
54
55
56
57
58
	// Favor our bundled version

	// Installer payload location if we're running the installed binary
	exe, err := os.Executable()
	if err == nil {
59
		rocmTargetDir := filepath.Join(filepath.Dir(exe), envconfig.LibRelativeToExe(), "lib", "ollama")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
60
61
62
63
64
		if rocmLibUsable(rocmTargetDir) {
			slog.Debug("detected ROCM next to ollama executable " + rocmTargetDir)
			return rocmTargetDir, nil
		}
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
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

	// Prefer explicit HIP env var
	hipPath := os.Getenv("HIP_PATH")
	if hipPath != "" {
		hipLibDir := filepath.Join(hipPath, "bin")
		if rocmLibUsable(hipLibDir) {
			slog.Debug("detected ROCM via HIP_PATH=" + hipPath)
			return hipLibDir, nil
		}
	}

	// Scan the LD_LIBRARY_PATH or PATH
	pathEnv := "LD_LIBRARY_PATH"
	if runtime.GOOS == "windows" {
		pathEnv = "PATH"
	}

	paths := os.Getenv(pathEnv)
	for _, path := range filepath.SplitList(paths) {
		d, err := filepath.Abs(path)
		if err != nil {
			continue
		}
		if rocmLibUsable(d) {
			return d, nil
		}
	}

	// Well known location(s)
94
95
96
97
	for _, path := range RocmStandardLocations {
		if rocmLibUsable(path) {
			return path, nil
		}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
98
99
	}

Michael Yang's avatar
lint  
Michael Yang committed
100
	return "", errors.New("no suitable rocm found, falling back to CPU")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
101
}