- 
                Notifications
    You must be signed in to change notification settings 
- Fork 46
Automatically use ROCm when appropriate #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,116 @@ | ||||||||
| //go:build linux | ||||||||
|         
                  ericcurtin marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||
|  | ||||||||
| package gpuinfo | ||||||||
|  | ||||||||
| import ( | ||||||||
| "bufio" | ||||||||
| "os" | ||||||||
| "path/filepath" | ||||||||
| "regexp" | ||||||||
| "sort" | ||||||||
| "strconv" | ||||||||
| "strings" | ||||||||
| ) | ||||||||
|  | ||||||||
| // supportedAMDGPUs are the AMD GPU targets that should use ROCm | ||||||||
| var supportedAMDGPUs = map[string]bool{ | ||||||||
| "gfx908": true, | ||||||||
| "gfx90a": true, | ||||||||
| "gfx942": true, | ||||||||
| "gfx1010": true, | ||||||||
| "gfx1030": true, | ||||||||
| "gfx1100": true, | ||||||||
| "gfx1200": true, | ||||||||
| "gfx1201": true, | ||||||||
| "gfx1151": true, | ||||||||
| } | ||||||||
|  | ||||||||
| func hasSupportedAMDGPU() (bool, error) { | ||||||||
| // Check if KFD topology directory exists | ||||||||
| topologyDir := "/sys/class/kfd/kfd/topology/nodes/" | ||||||||
| info, err := os.Stat(topologyDir) | ||||||||
| if err != nil || !info.IsDir() { | ||||||||
| return false, nil // KFD not available | ||||||||
| } | ||||||||
|         
                  ericcurtin marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||
|  | ||||||||
| entries, err := os.ReadDir(topologyDir) | ||||||||
| if err != nil { | ||||||||
| return false, err | ||||||||
| } | ||||||||
|  | ||||||||
| // Sort entries by name to maintain consistent order | ||||||||
| sort.Slice(entries, func(i, j int) bool { | ||||||||
| return entries[i].Name() < entries[j].Name() | ||||||||
| }) | ||||||||
|  | ||||||||
| // Compile regex to match gfx_target_version lines | ||||||||
| reTarget := regexp.MustCompile(`gfx_target_version[ \t]+([0-9]+)`) | ||||||||
|         
                  ericcurtin marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||
|  | ||||||||
| for _, e := range entries { | ||||||||
| if !e.IsDir() { | ||||||||
| continue | ||||||||
| } | ||||||||
| nodePath := filepath.Join(topologyDir, e.Name()) | ||||||||
| propPath := filepath.Join(nodePath, "properties") | ||||||||
|  | ||||||||
| // Attempt to open the properties file directly; skip on error (e.g., permissions) | ||||||||
| f, err := os.Open(propPath) | ||||||||
| if err != nil { | ||||||||
| // Could be permission denied or file doesn't exist; just skip like the Python code | ||||||||
| continue | ||||||||
| } | ||||||||
|  | ||||||||
| sc := bufio.NewScanner(f) | ||||||||
| for sc.Scan() { | ||||||||
| line := sc.Text() | ||||||||
| matches := reTarget.FindStringSubmatch(line) | ||||||||
| if len(matches) < 2 { | ||||||||
| continue | ||||||||
| } | ||||||||
|  | ||||||||
| deviceIDStr := matches[1] | ||||||||
| deviceID, err := strconv.Atoi(deviceIDStr) | ||||||||
| if err != nil || deviceID == 0 { | ||||||||
| continue | ||||||||
| } | ||||||||
|  | ||||||||
| var majorVer, minorVer, steppingVer int | ||||||||
| if gfxOverride := os.Getenv("HSA_OVERRIDE_GFX_VERSION"); gfxOverride != "" { | ||||||||
| parts := strings.Split(strings.TrimSpace(gfxOverride), ".") | ||||||||
| if len(parts) != 3 { | ||||||||
| // Invalid format, skip | ||||||||
| continue | ||||||||
| } | ||||||||
| mv, err1 := strconv.Atoi(parts[0]) | ||||||||
| nv, err2 := strconv.Atoi(parts[1]) | ||||||||
| sv, err3 := strconv.Atoi(parts[2]) | ||||||||
| if err1 != nil || err2 != nil || err3 != nil { | ||||||||
| // Invalid format, skip | ||||||||
| continue | ||||||||
| } | ||||||||
| if mv > 63 || nv > 255 || sv > 255 { | ||||||||
| // Invalid values, skip | ||||||||
| continue | ||||||||
| } | ||||||||
| majorVer, minorVer, steppingVer = mv, nv, sv | ||||||||
|         
                  ericcurtin marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||
| } else { | ||||||||
| majorVer = (deviceID / 10000) % 100 | ||||||||
| minorVer = (deviceID / 100) % 100 | ||||||||
| steppingVer = deviceID % 100 | ||||||||
| } | ||||||||
|  | ||||||||
| gfx := "gfx" + | ||||||||
| strconv.FormatInt(int64(majorVer), 10) + | ||||||||
| strconv.FormatInt(int64(minorVer), 16) + | ||||||||
| strconv.FormatInt(int64(steppingVer), 16) | ||||||||
|         
                  ericcurtin marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||
|  | ||||||||
| if supportedAMDGPUs[gfx] { | ||||||||
| f.Close() | ||||||||
| return true, nil // Found a supported AMD GPU | ||||||||
| } | ||||||||
| } | ||||||||
| f.Close() | ||||||||
| } | ||||||||
|  | ||||||||
| return false, nil // No supported AMD GPU found | ||||||||
| } | ||||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
 | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,3 @@ | ||||||||
| //go:build linux | ||||||||
|  | ||||||||
| package gpuinfo | ||||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
 | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,8 @@ | ||||||||
| //go:build !linux | ||||||||
|  | ||||||||
| package gpuinfo | ||||||||
|  | ||||||||
| func (g *GPUInfo) HasSupportedAMDGPU() (bool, error) { | ||||||||
| // AMD GPU detection is only supported on Linux | ||||||||
| return false, nil | ||||||||
| } | ||||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
 | ||||||||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,18 +1,50 @@ | ||
| //go:build linux | ||
|  | ||
| package llamacpp | ||
|  | ||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "path/filepath" | ||
|  | ||
| "github.com/docker/model-runner/pkg/gpuinfo" | ||
| "github.com/docker/model-runner/pkg/logging" | ||
| ) | ||
|  | ||
| func (l *llamaCpp) ensureLatestLlamaCpp(_ context.Context, log logging.Logger, _ *http.Client, | ||
| _, vendoredServerStoragePath string, | ||
| func init() { | ||
| // Enable GPU variant detection by default on Linux | ||
| ShouldUseGPUVariantLock.Lock() | ||
| defer ShouldUseGPUVariantLock.Unlock() | ||
| ShouldUseGPUVariant = true | ||
| } | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have  | ||
|  | ||
| func (l *llamaCpp) ensureLatestLlamaCpp(ctx context.Context, log logging.Logger, httpClient *http.Client, | ||
| llamaCppPath, vendoredServerStoragePath string, | ||
| ) error { | ||
| l.status = fmt.Sprintf("running llama.cpp version: %s", | ||
| getLlamaCppVersion(log, filepath.Join(vendoredServerStoragePath, "com.docker.llama-server"))) | ||
| return errLlamaCppUpdateDisabled | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be better to stick to a consistent logic for picking whether we use acceleration or not. Currently on LInux we expect people to run the model-runner container (appropriate to their usecase and system), so the accelerate/not-accelerate question should be decided in the CLI rather than here. | ||
| var hasAMD bool | ||
| var err error | ||
|  | ||
| ShouldUseGPUVariantLock.Lock() | ||
| defer ShouldUseGPUVariantLock.Unlock() | ||
| if ShouldUseGPUVariant { | ||
| // Create GPU info to check for supported AMD GPUs | ||
| gpuInfo := gpuinfo.New(vendoredServerStoragePath) | ||
| hasAMD, err = gpuInfo.HasSupportedAMDGPU() | ||
| if err != nil { | ||
| log.Debugf("AMD GPU detection failed: %v", err) | ||
| } | ||
| } | ||
|  | ||
| desiredVersion := GetDesiredServerVersion() | ||
| desiredVariant := "cpu" | ||
|  | ||
| // Use ROCm if supported AMD GPU is detected | ||
| if hasAMD { | ||
| log.Info("Supported AMD GPU detected, using ROCm variant") | ||
| desiredVariant = "rocm" | ||
| } | ||
|  | ||
| l.status = fmt.Sprintf("looking for updates for %s variant", desiredVariant) | ||
| return l.downloadLatestLlamaCpp(ctx, log, httpClient, llamaCppPath, vendoredServerStoragePath, desiredVersion, | ||
| desiredVariant) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.