amd_linux.go 17.3 KB
Newer Older
1
package discover
Daniel Hiltgen's avatar
Daniel Hiltgen committed
2
3
4
5
6
7

import (
	"bufio"
	"errors"
	"fmt"
	"io"
8
	"io/fs"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
9
10
11
	"log/slog"
	"os"
	"path/filepath"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
12
	"regexp"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
13
	"slices"
14
	"sort"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
15
16
	"strconv"
	"strings"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
17

18
	"github.com/ollama/ollama/envconfig"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
19
	"github.com/ollama/ollama/format"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
20
21
22
23
24
25
26
27
28
29
30
)

// Discovery logic for AMD/ROCm GPUs

const (
	DriverVersionFile     = "/sys/module/amdgpu/version"
	AMDNodesSysfsDir      = "/sys/class/kfd/kfd/topology/nodes/"
	GPUPropertiesFileGlob = AMDNodesSysfsDir + "*/properties"

	// Prefix with the node dir
	GPUTotalMemoryFileGlob = "mem_banks/*/properties" // size_in_bytes line
31
32

	// Direct Rendering Manager sysfs location
33
	DRMDeviceDirGlob   = "/sys/class/drm/card*/device"
34
35
36
37
38
39
40
	DRMTotalMemoryFile = "mem_info_vram_total"
	DRMUsedMemoryFile  = "mem_info_vram_used"

	// In hex; properties file is in decimal
	DRMUniqueIDFile = "unique_id"
	DRMVendorFile   = "vendor"
	DRMDeviceFile   = "device"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
41
42
43
44
)

var (
	// Used to validate if the given ROCm lib is usable
45
46
	ROCmLibGlobs          = []string{"libhipblas.so.2*", "rocblas"} // TODO - probably include more coverage of files here...
	RocmStandardLocations = []string{"/opt/rocm/lib", "/usr/lib64"}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
47
48
49
)

// Gather GPU information from the amdgpu driver if any supported GPUs are detected
50
51
// Only called once during bootstrap
func AMDGetGPUInfo() ([]RocmGPUInfo, error) {
52
	resp := []RocmGPUInfo{}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
53
	if !AMDDetected() {
54
		return resp, fmt.Errorf("AMD GPUs not detected")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
55
56
57
	}

	// Opportunistic logging of driver version to aid in troubleshooting
Daniel Hiltgen's avatar
Daniel Hiltgen committed
58
59
	driverMajor, driverMinor, err := AMDDriverVersion()
	if err != nil {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
60
		// TODO - if we see users crash and burn with the upstreamed kernel this can be adjusted to hard-fail rocm support and fallback to CPU
61
		slog.Warn("ollama recommends running the https://www.amd.com/en/support/download/linux-drivers.html", "error", err)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
62
63
	}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
64
65
	// Determine if the user has already pre-selected which GPUs to look at, then ignore the others
	var visibleDevices []string
Michael Yang's avatar
string  
Michael Yang committed
66
	hipVD := envconfig.HipVisibleDevices()   // zero based index only
67
	rocrVD := envconfig.RocrVisibleDevices() // zero based index or UUID
Michael Yang's avatar
string  
Michael Yang committed
68
	gpuDO := envconfig.GpuDeviceOrdinal()    // zero based index
Daniel Hiltgen's avatar
Daniel Hiltgen committed
69
70
71
	switch {
	case rocrVD != "":
		visibleDevices = strings.Split(rocrVD, ",")
72
73
	case hipVD != "":
		visibleDevices = strings.Split(hipVD, ",")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
74
75
	case gpuDO != "":
		visibleDevices = strings.Split(gpuDO, ",")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
76
77
	}

Michael Yang's avatar
string  
Michael Yang committed
78
	gfxOverride := envconfig.HsaOverrideGfxVersion()
Daniel Hiltgen's avatar
Daniel Hiltgen committed
79
	var supported []string
Michael Yang's avatar
Michael Yang committed
80
	var libDir string
Daniel Hiltgen's avatar
Daniel Hiltgen committed
81
82
83
84

	// The amdgpu driver always exposes the host CPU(s) first, but we have to skip them and subtract
	// from the other IDs to get alignment with the HIP libraries expectations (zero is the first GPU, not the CPU)
	matches, _ := filepath.Glob(GPUPropertiesFileGlob)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
	sort.Slice(matches, func(i, j int) bool {
		// /sys/class/kfd/kfd/topology/nodes/<number>/properties
		a, err := strconv.ParseInt(filepath.Base(filepath.Dir(matches[i])), 10, 64)
		if err != nil {
			slog.Debug("parse err", "error", err, "match", matches[i])
			return false
		}
		b, err := strconv.ParseInt(filepath.Base(filepath.Dir(matches[j])), 10, 64)
		if err != nil {
			slog.Debug("parse err", "error", err, "match", matches[i])
			return false
		}
		return a < b
	})
99
	gpuCount := 0
Jesse Gross's avatar
Jesse Gross committed
100
	gpuOrdinalID := 0
Daniel Hiltgen's avatar
Daniel Hiltgen committed
101
102
103
104
105
	for _, match := range matches {
		slog.Debug("evaluating amdgpu node " + match)
		fp, err := os.Open(match)
		if err != nil {
			slog.Debug("failed to open sysfs node", "file", match, "error", err)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
106
107
			continue
		}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
108
		defer fp.Close()
Daniel Hiltgen's avatar
Daniel Hiltgen committed
109

Daniel Hiltgen's avatar
Daniel Hiltgen committed
110
111
112
		scanner := bufio.NewScanner(fp)
		isCPU := false
		var major, minor, patch uint64
113
		var vendor, device, uniqueID uint64
Daniel Hiltgen's avatar
Daniel Hiltgen committed
114
115
116
117
118
		for scanner.Scan() {
			line := strings.TrimSpace(scanner.Text())
			// Note: we could also use "cpu_cores_count X" where X is greater than zero to detect CPUs
			if strings.HasPrefix(line, "gfx_target_version") {
				ver := strings.Fields(line)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
119

Daniel Hiltgen's avatar
Daniel Hiltgen committed
120
121
122
123
124
125
				// Detect CPUs
				if len(ver) == 2 && ver[1] == "0" {
					slog.Debug("detected CPU " + match)
					isCPU = true
					break
				}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
126

Daniel Hiltgen's avatar
Daniel Hiltgen committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
				if len(ver) != 2 || len(ver[1]) < 5 {
					slog.Warn("malformed "+match, "gfx_target_version", line)
					// If this winds up being a CPU, our offsets may be wrong
					continue
				}
				l := len(ver[1])
				var err1, err2, err3 error
				patch, err1 = strconv.ParseUint(ver[1][l-2:l], 10, 32)
				minor, err2 = strconv.ParseUint(ver[1][l-4:l-2], 10, 32)
				major, err3 = strconv.ParseUint(ver[1][:l-4], 10, 32)
				if err1 != nil || err2 != nil || err3 != nil {
					slog.Debug("malformed int " + line)
					continue
				}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
141
142
143
			} else if strings.HasPrefix(line, "vendor_id") {
				ver := strings.Fields(line)
				if len(ver) != 2 {
144
					slog.Debug("malformed", "vendor_id", line)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
145
146
					continue
				}
147
				vendor, err = strconv.ParseUint(ver[1], 10, 64)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
148
				if err != nil {
149
					slog.Debug("malformed", "vendor_id", line, "error", err)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
150
151
152
153
				}
			} else if strings.HasPrefix(line, "device_id") {
				ver := strings.Fields(line)
				if len(ver) != 2 {
154
					slog.Debug("malformed", "device_id", line)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
155
156
					continue
				}
157
				device, err = strconv.ParseUint(ver[1], 10, 64)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
158
				if err != nil {
159
160
161
162
163
164
165
166
167
168
169
					slog.Debug("malformed", "device_id", line, "error", err)
				}
			} else if strings.HasPrefix(line, "unique_id") {
				ver := strings.Fields(line)
				if len(ver) != 2 {
					slog.Debug("malformed", "unique_id", line)
					continue
				}
				uniqueID, err = strconv.ParseUint(ver[1], 10, 64)
				if err != nil {
					slog.Debug("malformed", "unique_id", line, "error", err)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
170
				}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
171
172
173
174
			}
			// TODO - any other properties we want to extract and record?
			// vendor_id + device_id -> pci lookup for "Name"
			// Other metrics that may help us understand relative performance between multiple GPUs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
175
176
		}

177
178
179
180
		// Note: while ./mem_banks/*/used_memory exists, it doesn't appear to take other VRAM consumers
		// into consideration, so we instead map the device over to the DRM driver sysfs nodes which
		// do reliably report VRAM usage.

Daniel Hiltgen's avatar
Daniel Hiltgen committed
181
182
		if isCPU {
			continue
Daniel Hiltgen's avatar
Daniel Hiltgen committed
183
184
		}

185
186
187
188
		// Skip over any GPUs that are masked
		if major == 0 && minor == 0 && patch == 0 {
			slog.Debug("skipping gpu with gfx000")
			continue
Daniel Hiltgen's avatar
Daniel Hiltgen committed
189
		}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
190
191

		// Look up the memory for the current node
Daniel Hiltgen's avatar
Daniel Hiltgen committed
192
193
		totalMemory := uint64(0)
		usedMemory := uint64(0)
194
		var usedFile string
195
196
197
198
199
200
201
		mapping := []struct {
			id       uint64
			filename string
		}{
			{vendor, DRMVendorFile},
			{device, DRMDeviceFile},
			{uniqueID, DRMUniqueIDFile}, // Not all devices will report this
Daniel Hiltgen's avatar
Daniel Hiltgen committed
202
		}
203
204
205
206
207
208
209
		slog.Debug("mapping amdgpu to drm sysfs nodes", "amdgpu", match, "vendor", vendor, "device", device, "unique_id", uniqueID)
		// Map over to DRM location to find the total/free memory
		drmMatches, _ := filepath.Glob(DRMDeviceDirGlob)
		for _, devDir := range drmMatches {
			matched := true
			for _, m := range mapping {
				if m.id == 0 {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
210
					// Null ID means it didn't populate, so we can't use it to match
211
212
213
					continue
				}
				filename := filepath.Join(devDir, m.filename)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
214
				buf, err := os.ReadFile(filename)
215
216
217
218
219
				if err != nil {
					slog.Debug("failed to read sysfs node", "file", filename, "error", err)
					matched = false
					break
				}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
220
				// values here are in hex, strip off the lead 0x and parse so we can compare the numeric (decimal) values in amdgpu
221
222
223
224
225
226
227
228
229
230
231
232
				cmp, err := strconv.ParseUint(strings.TrimPrefix(strings.TrimSpace(string(buf)), "0x"), 16, 64)
				if err != nil {
					slog.Debug("failed to parse sysfs node", "file", filename, "error", err)
					matched = false
					break
				}
				if cmp != m.id {
					matched = false
					break
				}
			}
			if !matched {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
233
234
				continue
			}
235
236
237
238

			// Found the matching DRM directory
			slog.Debug("matched", "amdgpu", match, "drm", devDir)
			totalFile := filepath.Join(devDir, DRMTotalMemoryFile)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
239
			buf, err := os.ReadFile(totalFile)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
240
			if err != nil {
241
242
				slog.Debug("failed to read sysfs node", "file", totalFile, "error", err)
				break
Daniel Hiltgen's avatar
Daniel Hiltgen committed
243
			}
244
			totalMemory, err = strconv.ParseUint(strings.TrimSpace(string(buf)), 10, 64)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
245
			if err != nil {
246
247
248
249
				slog.Debug("failed to parse sysfs node", "file", totalFile, "error", err)
				break
			}

250
251
			usedFile = filepath.Join(devDir, DRMUsedMemoryFile)
			usedMemory, err = getFreeMemory(usedFile)
252
			if err != nil {
253
				slog.Debug("failed to update used memory", "error", err)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
254
			}
255
			break
Daniel Hiltgen's avatar
Daniel Hiltgen committed
256
		}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
257

Daniel Hiltgen's avatar
Daniel Hiltgen committed
258
259
260
261
262
		var name string
		// TODO - PCI ID lookup
		if vendor > 0 && device > 0 {
			name = fmt.Sprintf("%04x:%04x", vendor, device)
		}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
263

264
265
266
267
268
		// Favor UUIDs if available to reduce possibility of getting the numeric IDs wrong
		var ID string
		if uniqueID != 0 {
			ID = fmt.Sprintf("GPU-%016x", uniqueID)
		} else {
Jesse Gross's avatar
Jesse Gross committed
269
			ID = strconv.Itoa(gpuOrdinalID)
270
271
		}

272
273
274
275
276
277
278
		gpuInfo := RocmGPUInfo{
			GpuInfo: GpuInfo{
				Library: "rocm",
				memInfo: memInfo{
					TotalMemory: totalMemory,
					FreeMemory:  (totalMemory - usedMemory),
				},
279
				ID:            ID,
280
				filterID:      gpuOrdinalID,
281
282
283
284
285
				Name:          name,
				Compute:       fmt.Sprintf("gfx%d%x%x", major, minor, patch),
				MinimumMemory: rocmMinimumMemory,
				DriverMajor:   driverMajor,
				DriverMinor:   driverMinor,
Daniel Hiltgen's avatar
Daniel Hiltgen committed
286
			},
287
			usedFilepath: usedFile,
Jesse Gross's avatar
Jesse Gross committed
288
			index:        gpuCount,
Daniel Hiltgen's avatar
Daniel Hiltgen committed
289
290
		}

Jesse Gross's avatar
Jesse Gross committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
		// Keep track of numeric IDs based on valid GPUs
		gpuCount += 1

		// If the user wants to filter to a subset of devices, filter out if we aren't a match
		if len(visibleDevices) > 0 {
			include := false
			for _, visible := range visibleDevices {
				if (uniqueID != 0 && visible == gpuInfo.ID) || visible == strconv.Itoa(gpuInfo.index) {
					include = true
					break
				}
			}
			if !include {
				reason := "filtering out device per user request"
				slog.Info(reason, "id", gpuInfo.ID, "index", gpuInfo.index, "visible_devices", visibleDevices)
				unsupportedGPUs = append(unsupportedGPUs, UnsupportedGPUInfo{
					GpuInfo: gpuInfo.GpuInfo,
					Reason:  reason,
				})

				continue
			}
		}

		// Ordinal IDs are based on the visible GPUs
		gpuOrdinalID += 1

318
319
320
		// iGPU detection, remove this check once we can support an iGPU variant of the rocm library
		if totalMemory < IGPUMemLimit {
			reason := "unsupported Radeon iGPU detected skipping"
Jesse Gross's avatar
Jesse Gross committed
321
			slog.Info(reason, "id", gpuInfo.ID, "total", format.HumanBytes2(totalMemory))
322
323
324
325
326
327
			unsupportedGPUs = append(unsupportedGPUs, UnsupportedGPUInfo{
				GpuInfo: gpuInfo.GpuInfo,
				Reason:  reason,
			})
			continue
		}
328
329
330
331
332
		minVer, err := strconv.Atoi(RocmComputeMajorMin)
		if err != nil {
			slog.Error("invalid RocmComputeMajorMin setting", "value", RocmComputeMajorMin, "error", err)
		}
		if int(major) < minVer {
333
			reason := fmt.Sprintf("amdgpu too old gfx%d%x%x", major, minor, patch)
Jesse Gross's avatar
Jesse Gross committed
334
			slog.Warn(reason, "gpu", gpuInfo.ID)
335
336
337
338
339
340
341
342
			unsupportedGPUs = append(unsupportedGPUs, UnsupportedGPUInfo{
				GpuInfo: gpuInfo.GpuInfo,
				Reason:  reason,
			})

			continue
		}

Jesse Gross's avatar
Jesse Gross committed
343
344
		slog.Debug("amdgpu memory", "gpu", gpuInfo.ID, "total", format.HumanBytes2(totalMemory))
		slog.Debug("amdgpu memory", "gpu", gpuInfo.ID, "available", format.HumanBytes2(totalMemory-usedMemory))
Daniel Hiltgen's avatar
Daniel Hiltgen committed
345
346
347
348
349
350

		// Final validation is gfx compatibility - load the library if we haven't already loaded it
		// even if the user overrides, we still need to validate the library
		if libDir == "" {
			libDir, err = AMDValidateLibDir()
			if err != nil {
351
352
353
354
355
356
357
				err = fmt.Errorf("unable to verify rocm library: %w", err)
				slog.Warn(err.Error())
				unsupportedGPUs = append(unsupportedGPUs, UnsupportedGPUInfo{
					GpuInfo: gpuInfo.GpuInfo,
					Reason:  err.Error(),
				})
				return nil, err
Daniel Hiltgen's avatar
Daniel Hiltgen committed
358
359
			}
		}
Michael Yang's avatar
Michael Yang committed
360
		gpuInfo.DependencyPath = []string{libDir}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
361
362
363
364
365
366

		if gfxOverride == "" {
			// Only load supported list once
			if len(supported) == 0 {
				supported, err = GetSupportedGFX(libDir)
				if err != nil {
367
368
369
370
371
372
373
					err = fmt.Errorf("failed to lookup supported GFX types: %w", err)
					slog.Warn(err.Error())
					unsupportedGPUs = append(unsupportedGPUs, UnsupportedGPUInfo{
						GpuInfo: gpuInfo.GpuInfo,
						Reason:  err.Error(),
					})
					return nil, err
Daniel Hiltgen's avatar
Daniel Hiltgen committed
374
375
376
				}
				slog.Debug("rocm supported GPUs", "types", supported)
			}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
377
			gfx := gpuInfo.Compute
Daniel Hiltgen's avatar
Daniel Hiltgen committed
378
			if !slices.Contains[[]string, string](supported, gfx) {
379
380
381
382
383
384
385
				reason := fmt.Sprintf("amdgpu is not supported (supported types:%s)", supported)
				slog.Warn(reason, "gpu_type", gfx, "gpu", gpuInfo.ID, "library", libDir)
				unsupportedGPUs = append(unsupportedGPUs, UnsupportedGPUInfo{
					GpuInfo: gpuInfo.GpuInfo,
					Reason:  reason,
				})

Daniel Hiltgen's avatar
Daniel Hiltgen committed
386
387
388
389
390
391
392
				// TODO - consider discrete markdown just for ROCM troubleshooting?
				slog.Warn("See https://github.com/ollama/ollama/blob/main/docs/gpu.md#overrides for HSA_OVERRIDE_GFX_VERSION usage")
				continue
			} else {
				slog.Info("amdgpu is supported", "gpu", gpuInfo.ID, "gpu_type", gfx)
			}
		} else {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
393
			slog.Info("skipping rocm gfx compatibility check", "HSA_OVERRIDE_GFX_VERSION", gfxOverride)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
394
395
		}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
396
397
		// Check for env var workarounds
		if name == "1002:687f" { // Vega RX 56
398
			gpuInfo.EnvWorkarounds = append(gpuInfo.EnvWorkarounds, "HSA_ENABLE_SDMA=0")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
399
400
		}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
401
402
		// The GPU has passed all the verification steps and is supported
		resp = append(resp, gpuInfo)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
403
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
404
	if len(resp) == 0 {
405
406
407
		err := fmt.Errorf("no compatible amdgpu devices detected")
		slog.Info(err.Error())
		return nil, err
Daniel Hiltgen's avatar
Daniel Hiltgen committed
408
	}
409
	if err := verifyKFDDriverAccess(); err != nil {
410
411
412
		err = fmt.Errorf("amdgpu devices detected but permission problems block access: %w", err)
		slog.Error(err.Error())
		return nil, err
413
	}
414
	return resp, nil
Daniel Hiltgen's avatar
Daniel Hiltgen committed
415
416
417
418
419
420
421
422
423
424
425
}

// Quick check for AMD driver so we can skip amdgpu discovery if not present
func AMDDetected() bool {
	// Some driver versions (older?) don't have a version file, so just lookup the parent dir
	sysfsDir := filepath.Dir(DriverVersionFile)
	_, err := os.Stat(sysfsDir)
	if errors.Is(err, os.ErrNotExist) {
		slog.Debug("amdgpu driver not detected " + sysfsDir)
		return false
	} else if err != nil {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
426
		slog.Debug("error looking up amd driver", "path", sysfsDir, "error", err)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
427
428
429
430
431
432
433
434
		return false
	}
	return true
}

// Prefer to use host installed ROCm, as long as it meets our minimum requirements
// failing that, tell the user how to download it on their own
func AMDValidateLibDir() (string, error) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
435
	libDir, err := commonAMDValidateLibDir()
436
	if err == nil {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
437
		return libDir, nil
438
439
	}

440
441
442
	// Well known ollama installer path
	installedRocmDir := "/usr/share/ollama/lib/rocm"
	if rocmLibUsable(installedRocmDir) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
443
		return installedRocmDir, nil
Daniel Hiltgen's avatar
Daniel Hiltgen committed
444
445
	}

446
447
	// If we still haven't found a usable rocm, the user will have to install it on their own
	slog.Warn("amdgpu detected, but no compatible rocm library found.  Either install rocm v6, or follow manual install instructions at https://github.com/ollama/ollama/blob/main/docs/linux.md#manual-install")
Michael Yang's avatar
lint  
Michael Yang committed
448
	return "", errors.New("no suitable rocm found, falling back to CPU")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
449
450
}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
451
452
func AMDDriverVersion() (driverMajor, driverMinor int, err error) {
	_, err = os.Stat(DriverVersionFile)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
453
	if err != nil {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
454
		return 0, 0, fmt.Errorf("amdgpu version file missing: %s %w", DriverVersionFile, err)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
455
456
457
	}
	fp, err := os.Open(DriverVersionFile)
	if err != nil {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
458
		return 0, 0, err
Daniel Hiltgen's avatar
Daniel Hiltgen committed
459
460
461
462
	}
	defer fp.Close()
	verString, err := io.ReadAll(fp)
	if err != nil {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
		return 0, 0, err
	}

	pattern := `\A(\d+)\.(\d+).*`
	regex := regexp.MustCompile(pattern)
	match := regex.FindStringSubmatch(string(verString))
	if len(match) < 2 {
		return 0, 0, fmt.Errorf("malformed version string %s", string(verString))
	}
	driverMajor, err = strconv.Atoi(match[1])
	if err != nil {
		return 0, 0, err
	}
	driverMinor, err = strconv.Atoi(match[2])
	if err != nil {
		return 0, 0, err
Daniel Hiltgen's avatar
Daniel Hiltgen committed
479
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
480
	return driverMajor, driverMinor, nil
Daniel Hiltgen's avatar
Daniel Hiltgen committed
481
}
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498

func (gpus RocmGPUInfoList) RefreshFreeMemory() error {
	if len(gpus) == 0 {
		return nil
	}
	for i := range gpus {
		usedMemory, err := getFreeMemory(gpus[i].usedFilepath)
		if err != nil {
			return err
		}
		slog.Debug("updating rocm free memory", "gpu", gpus[i].ID, "name", gpus[i].Name, "before", format.HumanBytes2(gpus[i].FreeMemory), "now", format.HumanBytes2(gpus[i].TotalMemory-usedMemory))
		gpus[i].FreeMemory = gpus[i].TotalMemory - usedMemory
	}
	return nil
}

func getFreeMemory(usedFile string) (uint64, error) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
499
	buf, err := os.ReadFile(usedFile)
500
501
502
503
504
505
506
507
508
509
	if err != nil {
		return 0, fmt.Errorf("failed to read sysfs node %s %w", usedFile, err)
	}
	usedMemory, err := strconv.ParseUint(strings.TrimSpace(string(buf)), 10, 64)
	if err != nil {
		slog.Debug("failed to parse sysfs node", "file", usedFile, "error", err)
		return 0, fmt.Errorf("failed to parse sysfs node %s %w", usedFile, err)
	}
	return usedMemory, nil
}
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525

func verifyKFDDriverAccess() error {
	// Verify we have permissions - either running as root, or we have group access to the driver
	fd, err := os.OpenFile("/dev/kfd", os.O_RDWR, 0o666)
	if err != nil {
		if errors.Is(err, fs.ErrPermission) {
			return fmt.Errorf("permissions not set up properly.  Either run ollama as root, or add you user account to the render group. %w", err)
		} else if errors.Is(err, fs.ErrNotExist) {
			// Container runtime failure?
			return fmt.Errorf("kfd driver not loaded.  If running in a container, remember to include '--device /dev/kfd --device /dev/dri'")
		}
		return fmt.Errorf("failed to check permission on /dev/kfd: %w", err)
	}
	fd.Close()
	return nil
}
526

527
func rocmGetVisibleDevicesEnv(gpuInfo []GpuInfo) string {
528
529
530
531
532
	ids := []string{}
	for _, info := range gpuInfo {
		if info.Library != "rocm" {
			continue
		}
533
534
535
536
537
538
		// If the devices requires a numeric ID, for filtering purposes, we use the unfiltered ID number
		if _, err := strconv.Atoi(info.ID); err == nil {
			ids = append(ids, fmt.Sprintf("%d", info.filterID))
		} else {
			ids = append(ids, info.ID)
		}
539
	}
540
541
542
543
	if len(ids) == 0 {
		return ""
	}

544
545
546
547
	// There are 3 potential env vars to use to select GPUs.
	// ROCR_VISIBLE_DEVICES supports UUID or numeric so is our preferred on linux
	// GPU_DEVICE_ORDINAL supports numeric IDs only
	// HIP_VISIBLE_DEVICES supports numeric IDs only
548
	return "ROCR_VISIBLE_DEVICES=" + strings.Join(ids, ",")
549
}