amd_common.go 2.5 KB

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