amd_common.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //go:build linux || windows
  2. package gpu
  3. import (
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. )
  11. // Determine if the given ROCm lib directory is usable by checking for existence of some glob patterns
  12. func rocmLibUsable(libDir string) bool {
  13. slog.Debug("evaluating potential rocm lib dir " + libDir)
  14. for _, g := range ROCmLibGlobs {
  15. res, _ := filepath.Glob(filepath.Join(libDir, g))
  16. if len(res) == 0 {
  17. return false
  18. }
  19. }
  20. return true
  21. }
  22. func GetSupportedGFX(libDir string) ([]string, error) {
  23. var ret []string
  24. files, err := filepath.Glob(filepath.Join(libDir, "rocblas", "library", "TensileLibrary_lazy_gfx*.dat"))
  25. if err != nil {
  26. return nil, err
  27. }
  28. for _, file := range files {
  29. ret = append(ret, strings.TrimSuffix(strings.TrimPrefix(filepath.Base(file), "TensileLibrary_lazy_"), ".dat"))
  30. }
  31. return ret, nil
  32. }
  33. func rocmGetVisibleDevicesEnv(gpuInfo []GpuInfo) (string, string) {
  34. ids := []string{}
  35. for _, info := range gpuInfo {
  36. if info.Library != "rocm" {
  37. // TODO shouldn't happen if things are wired correctly...
  38. slog.Debug("rocmGetVisibleDevicesEnv skipping over non-rocm device", "library", info.Library)
  39. continue
  40. }
  41. ids = append(ids, info.ID)
  42. }
  43. return "HIP_VISIBLE_DEVICES", strings.Join(ids, ",")
  44. }
  45. func commonAMDValidateLibDir() (string, error) {
  46. // Favor our bundled version
  47. // Installer payload location if we're running the installed binary
  48. exe, err := os.Executable()
  49. if err == nil {
  50. rocmTargetDir := filepath.Join(filepath.Dir(exe), "rocm")
  51. if rocmLibUsable(rocmTargetDir) {
  52. slog.Debug("detected ROCM next to ollama executable " + rocmTargetDir)
  53. return rocmTargetDir, nil
  54. }
  55. }
  56. // Prefer explicit HIP env var
  57. hipPath := os.Getenv("HIP_PATH")
  58. if hipPath != "" {
  59. hipLibDir := filepath.Join(hipPath, "bin")
  60. if rocmLibUsable(hipLibDir) {
  61. slog.Debug("detected ROCM via HIP_PATH=" + hipPath)
  62. return hipLibDir, nil
  63. }
  64. }
  65. // Scan the LD_LIBRARY_PATH or PATH
  66. pathEnv := "LD_LIBRARY_PATH"
  67. if runtime.GOOS == "windows" {
  68. pathEnv = "PATH"
  69. }
  70. paths := os.Getenv(pathEnv)
  71. for _, path := range filepath.SplitList(paths) {
  72. d, err := filepath.Abs(path)
  73. if err != nil {
  74. continue
  75. }
  76. if rocmLibUsable(d) {
  77. return d, nil
  78. }
  79. }
  80. // Well known location(s)
  81. for _, path := range RocmStandardLocations {
  82. if rocmLibUsable(path) {
  83. return path, nil
  84. }
  85. }
  86. return "", fmt.Errorf("no suitable rocm found, falling back to CPU")
  87. }