[GH-ISSUE #1514] The code below appears to ignore CUDA_VISIBLE_DEVICES in its calculation, i.e. any GPU you won't use, will still be counted as VRAM. #823

Closed
opened 2026-04-12 10:29:48 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @phalexo on GitHub (Dec 14, 2023).
Original GitHub issue: https://github.com/ollama/ollama/issues/1514

Originally assigned to: @dhiltgen on GitHub.

func CheckVRAM() (int64, error) {
        cmd := exec.Command("nvidia-smi", "--query-gpu=memory.free", "--format=csv,noheader,nounits")
        var stdout bytes.Buffer
        cmd.Stdout = &stdout
        err := cmd.Run()
        if err != nil {
                return 0, errNvidiaSMI
        }

        var freeMiB int64
        scanner := bufio.NewScanner(&stdout)
        for scanner.Scan() {
                line := scanner.Text()
                if strings.Contains(line, "[Insufficient Permissions]") {
                        return 0, fmt.Errorf("GPU support may not enabled, check you have installed GPU drivers and have the necessary permissions to run nvidia-smi")
                }

                vram, err := strconv.ParseInt(strings.TrimSpace(line), 10, 64)
                if err != nil {
                        return 0, fmt.Errorf("failed to parse available VRAM: %v", err)
                }

                freeMiB += vram
        }

        freeBytes := freeMiB * 1024 * 1024
        if freeBytes < 2*format.GigaByte {
                log.Printf("less than 2 GB VRAM available")
                return 0, errAvailableVRAM
        }

        return freeBytes, nil
}
Originally created by @phalexo on GitHub (Dec 14, 2023). Original GitHub issue: https://github.com/ollama/ollama/issues/1514 Originally assigned to: @dhiltgen on GitHub. ```go func CheckVRAM() (int64, error) { cmd := exec.Command("nvidia-smi", "--query-gpu=memory.free", "--format=csv,noheader,nounits") var stdout bytes.Buffer cmd.Stdout = &stdout err := cmd.Run() if err != nil { return 0, errNvidiaSMI } var freeMiB int64 scanner := bufio.NewScanner(&stdout) for scanner.Scan() { line := scanner.Text() if strings.Contains(line, "[Insufficient Permissions]") { return 0, fmt.Errorf("GPU support may not enabled, check you have installed GPU drivers and have the necessary permissions to run nvidia-smi") } vram, err := strconv.ParseInt(strings.TrimSpace(line), 10, 64) if err != nil { return 0, fmt.Errorf("failed to parse available VRAM: %v", err) } freeMiB += vram } freeBytes := freeMiB * 1024 * 1024 if freeBytes < 2*format.GigaByte { log.Printf("less than 2 GB VRAM available") return 0, errAvailableVRAM } return freeBytes, nil } ```
GiteaMirror added the nvidia label 2026-04-12 10:29:48 -05:00
Author
Owner

@pdevine commented on GitHub (Jan 25, 2024):

cc @jmorganca

<!-- gh-comment-id:1911112970 --> @pdevine commented on GitHub (Jan 25, 2024): cc @jmorganca
Author
Owner

@dhiltgen commented on GitHub (Mar 12, 2024):

Good catch! Yes, we need to adjust the algorithm to respect the visible devices for CUDA.

<!-- gh-comment-id:1992588174 --> @dhiltgen commented on GitHub (Mar 12, 2024): Good catch! Yes, we need to adjust the algorithm to respect the visible devices for CUDA.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#823