"vscode:/vscode.git/clone" did not exist on "3234189b60092a2408fdf4f412eae3346404ae27"
cuda_common.go 2.02 KB
Newer Older
Daniel Hiltgen's avatar
Daniel Hiltgen committed
1
2
//go:build linux || windows

3
package discover
Daniel Hiltgen's avatar
Daniel Hiltgen committed
4
5

import (
Daniel Hiltgen's avatar
Daniel Hiltgen committed
6
	"fmt"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
7
	"log/slog"
8
9
10
11
	"os"
	"regexp"
	"runtime"
	"strconv"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
12
13
14
	"strings"
)

15
16
17
18
// Jetson devices have JETSON_JETPACK="x.y.z" factory set to the Jetpack version installed.
// Included to drive logic for reducing Ollama-allocated overhead on L4T/Jetson devices.
var CudaTegra string = os.Getenv("JETSON_JETPACK")

19
func cudaVariant(gpuInfos []CudaGPUInfo) string {
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
	if runtime.GOARCH == "arm64" && runtime.GOOS == "linux" {
		if CudaTegra != "" {
			ver := strings.Split(CudaTegra, ".")
			if len(ver) > 0 {
				return "jetpack" + ver[0]
			}
		} else if data, err := os.ReadFile("/etc/nv_tegra_release"); err == nil {
			r := regexp.MustCompile(` R(\d+) `)
			m := r.FindSubmatch(data)
			if len(m) != 2 {
				slog.Info("Unexpected format for /etc/nv_tegra_release.  Set JETSON_JETPACK to select version")
			} else {
				if l4t, err := strconv.Atoi(string(m[1])); err == nil {
					// Note: mapping from L4t -> JP is inconsistent (can't just subtract 30)
					// https://developer.nvidia.com/embedded/jetpack-archive
					switch l4t {
					case 35:
						return "jetpack5"
					case 36:
						return "jetpack6"
					default:
						slog.Info("unsupported L4T version", "nv_tegra_release", string(data))
					}
				}
			}
		}
	}

48
49
50
51
52
53
	// Check GPU compute capability FIRST, lowest common denominator if multi-gpu
	for _, gpuInfo := range gpuInfos {
		if gpuInfo.computeMajor < 7 || (gpuInfo.computeMajor == 7 && gpuInfo.computeMinor < 5) {
			// GPU is Pascal or older (CC <= 7.4) - use CUDA v12 (supports CC 6.1)
			return "v12"
		}
54
55
56
	}

	// GPU is Turing or newer (CC >= 7.5) - can use newer CUDA
57
	if len(gpuInfos) > 0 && gpuInfos[0].DriverMajor < 13 {
58
59
		// The detected driver is older than 580 (Aug 2025)
		// Warn if their CC is compatible with v13 and they should upgrade their driver to get better performance
60
		slog.Warn("old CUDA driver detected - please upgrade to a newer driver for best performance", "version", fmt.Sprintf("%d.%d", gpuInfos[0].DriverMajor, gpuInfos[0].DriverMinor))
61
		return "v12"
62
	}
63
	return "v13"
64
}