| 
									
										
										
										
											2024-02-16 09:15:09 +08:00
										 |  |  | //go:build linux || windows
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 08:45:00 +08:00
										 |  |  | package discover | 
					
						
							| 
									
										
										
										
											2024-02-16 09:15:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-08-02 05:52:15 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2024-02-16 09:15:09 +08:00
										 |  |  | 	"log/slog" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2024-03-31 00:50:05 +08:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2024-02-16 09:15:09 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Determine if the given ROCm lib directory is usable by checking for existence of some glob patterns
 | 
					
						
							|  |  |  | func rocmLibUsable(libDir string) bool { | 
					
						
							|  |  |  | 	slog.Debug("evaluating potential rocm lib dir " + libDir) | 
					
						
							|  |  |  | 	for _, g := range ROCmLibGlobs { | 
					
						
							|  |  |  | 		res, _ := filepath.Glob(filepath.Join(libDir, g)) | 
					
						
							|  |  |  | 		if len(res) == 0 { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func GetSupportedGFX(libDir string) ([]string, error) { | 
					
						
							|  |  |  | 	var ret []string | 
					
						
							|  |  |  | 	files, err := filepath.Glob(filepath.Join(libDir, "rocblas", "library", "TensileLibrary_lazy_gfx*.dat")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, file := range files { | 
					
						
							|  |  |  | 		ret = append(ret, strings.TrimSuffix(strings.TrimPrefix(filepath.Base(file), "TensileLibrary_lazy_"), ".dat")) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ret, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-31 00:50:05 +08:00
										 |  |  | func commonAMDValidateLibDir() (string, error) { | 
					
						
							| 
									
										
										
										
											2024-07-11 02:01:22 +08:00
										 |  |  | 	// Favor our bundled version
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Installer payload location if we're running the installed binary
 | 
					
						
							| 
									
										
										
										
											2025-01-30 07:03:38 +08:00
										 |  |  | 	rocmTargetDir := filepath.Join(LibOllamaPath, "rocm") | 
					
						
							|  |  |  | 	if rocmLibUsable(rocmTargetDir) { | 
					
						
							|  |  |  | 		slog.Debug("detected ROCM next to ollama executable " + rocmTargetDir) | 
					
						
							|  |  |  | 		return rocmTargetDir, nil | 
					
						
							| 
									
										
										
										
											2024-07-11 02:01:22 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-31 00:50:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Prefer explicit HIP env var
 | 
					
						
							|  |  |  | 	hipPath := os.Getenv("HIP_PATH") | 
					
						
							|  |  |  | 	if hipPath != "" { | 
					
						
							|  |  |  | 		hipLibDir := filepath.Join(hipPath, "bin") | 
					
						
							|  |  |  | 		if rocmLibUsable(hipLibDir) { | 
					
						
							|  |  |  | 			slog.Debug("detected ROCM via HIP_PATH=" + hipPath) | 
					
						
							|  |  |  | 			return hipLibDir, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Scan the LD_LIBRARY_PATH or PATH
 | 
					
						
							|  |  |  | 	pathEnv := "LD_LIBRARY_PATH" | 
					
						
							|  |  |  | 	if runtime.GOOS == "windows" { | 
					
						
							|  |  |  | 		pathEnv = "PATH" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	paths := os.Getenv(pathEnv) | 
					
						
							|  |  |  | 	for _, path := range filepath.SplitList(paths) { | 
					
						
							|  |  |  | 		d, err := filepath.Abs(path) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if rocmLibUsable(d) { | 
					
						
							|  |  |  | 			return d, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Well known location(s)
 | 
					
						
							| 
									
										
										
										
											2024-05-02 06:47:12 +08:00
										 |  |  | 	for _, path := range RocmStandardLocations { | 
					
						
							|  |  |  | 		if rocmLibUsable(path) { | 
					
						
							|  |  |  | 			return path, nil | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-03-31 00:50:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-02 05:52:15 +08:00
										 |  |  | 	return "", errors.New("no suitable rocm found, falling back to CPU") | 
					
						
							| 
									
										
										
										
											2024-02-16 09:15:09 +08:00
										 |  |  | } |