amd_common.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //go:build linux || windows
  2. package discover
  3. import (
  4. "errors"
  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 commonAMDValidateLibDir() (string, error) {
  34. // Favor our bundled version
  35. // Installer payload location if we're running the installed binary
  36. rocmTargetDir := filepath.Join(LibOllamaPath, "rocm")
  37. if rocmLibUsable(rocmTargetDir) {
  38. slog.Debug("detected ROCM next to ollama executable " + rocmTargetDir)
  39. return rocmTargetDir, nil
  40. }
  41. // Prefer explicit HIP env var
  42. hipPath := os.Getenv("HIP_PATH")
  43. if hipPath != "" {
  44. hipLibDir := filepath.Join(hipPath, "bin")
  45. if rocmLibUsable(hipLibDir) {
  46. slog.Debug("detected ROCM via HIP_PATH=" + hipPath)
  47. return hipLibDir, nil
  48. }
  49. }
  50. // Scan the LD_LIBRARY_PATH or PATH
  51. pathEnv := "LD_LIBRARY_PATH"
  52. if runtime.GOOS == "windows" {
  53. pathEnv = "PATH"
  54. }
  55. paths := os.Getenv(pathEnv)
  56. for _, path := range filepath.SplitList(paths) {
  57. d, err := filepath.Abs(path)
  58. if err != nil {
  59. continue
  60. }
  61. if rocmLibUsable(d) {
  62. return d, nil
  63. }
  64. }
  65. // Well known location(s)
  66. for _, path := range RocmStandardLocations {
  67. if rocmLibUsable(path) {
  68. return path, nil
  69. }
  70. }
  71. return "", errors.New("no suitable rocm found, falling back to CPU")
  72. }