Skip to content
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

Add ramalama gpu_detector #670

Merged
merged 1 commit into from
Feb 1, 2025
Merged

Conversation

dougsland
Copy link
Collaborator

@dougsland dougsland commented Jan 30, 2025

GPUDetector can be used in any part of the code.
However, useful for users to run in the cli.

ramalama --show-gpus-available

Summary by Sourcery

New Features:

  • Users can now list available GPUs and their VRAM using the --show-gpus-available CLI flag.

Copy link
Contributor

sourcery-ai bot commented Jan 30, 2025

Reviewer's Guide by Sourcery

This pull request introduces a new feature to detect and display available GPUs on the system. It adds a new command-line argument --show-gpus-available that, when used, will list the detected GPUs along with their VRAM. The implementation includes a new GPUDetector class that handles the detection of NVIDIA, AMD, and Intel GPUs.

Sequence diagram for GPU detection flow

sequenceDiagram
    participant User
    participant CLI
    participant GPUDetector
    participant System

    User->>CLI: ramalama --show-gpus-available
    CLI->>GPUDetector: create GPUDetector()
    CLI->>GPUDetector: detect_best_gpu(gpu_list)
    GPUDetector->>System: Check NVIDIA GPUs (nvidia-smi)
    GPUDetector->>System: Check AMD GPUs (sysfs)
    GPUDetector->>System: Check Intel GPUs (sysfs/lspci)
    GPUDetector-->>CLI: Return detection result
    CLI-->>User: Display available GPUs
Loading

Class diagram for the new GPUDetector

classDiagram
    class GPUDetector {
        -best_gpu: str
        -best_vram: int
        -best_env: str
        +__init__()
        -_update_best_gpu(memory_path: str, gpu_index: int, env_var: str)
        +get_nvidia_gpu()
        +get_amd_gpu()
        +get_intel_gpu()
        +detect_best_gpu(gpu_template: list) bool
    }
    note for GPUDetector "Detects and compares GPU capabilities
Supports NVIDIA, AMD, and Intel GPUs"
Loading

File-Level Changes

Change Details Files
Added a new command-line argument to display available GPUs.
  • Added --show-gpus-available argument to the argument parser.
  • Added show_gpus_available_cli function to handle the new argument.
  • Modified parse_arguments to call show_gpus_available_cli when the argument is present.
ramalama/cli.py
Implemented GPU detection logic.
  • Created GPUDetector class to encapsulate GPU detection logic.
  • Added methods to detect NVIDIA, AMD, and Intel GPUs.
  • Added logic to determine the best GPU based on VRAM.
  • Added logic to return the best GPU index, VRAM, and environment variable.
ramalama/gpu_detector.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @dougsland - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider extracting the bytes to MiB conversion logic into a utility function to avoid duplication and ensure consistent handling across different GPU detection methods
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

ramalama/gpu_detector.py Outdated Show resolved Hide resolved
@dougsland
Copy link
Collaborator Author

cc Brian [email protected]

@dougsland
Copy link
Collaborator Author

in a different patch will take care of macOS env, need to test it but it's very later now 2AM.

ramalama/cli.py Outdated Show resolved Hide resolved
ramalama/gpu_detector.py Outdated Show resolved Hide resolved
ramalama/gpu_detector.py Outdated Show resolved Hide resolved
ramalama/gpu_detector.py Outdated Show resolved Hide resolved
@dougsland
Copy link
Collaborator Author

CI/CD error seems not related to the PR.

@dougsland
Copy link
Collaborator Author

I don't have permission to re-run CI/CD. Someone can re-run please? @ericcurtin

ramalama/cli.py Outdated Show resolved Hide resolved
ramalama/gpu_detector.py Outdated Show resolved Hide resolved
@dougsland dougsland force-pushed the gpudetector branch 6 times, most recently from c449866 to 7617f70 Compare January 30, 2025 22:11
@dougsland
Copy link
Collaborator Author

dougsland commented Jan 31, 2025

Thinking here I believe maybe instead of ERROR we should print INFO to avoid confusion. When found one GPU and try to get the others but don't find. @rhatdan @ericcurtin. At the same time, it's getting more and more code, I would merge this one and guarantee we have something tested fine and a next patch I change to INFO if all agree with the idea.

GPUDetector can be used in any part of the code.
However, useful for users to run in the cli.

ramalama info

Signed-off-by: Douglas Schilling Landgraf <[email protected]>
Signed-off-by: Brian <[email protected]>
@dougsland
Copy link
Collaborator Author

Thinking here I believe maybe instead of ERROR we should print INFO to avoid confusion. When found one GPU and try to > get the others but don't find. @rhatdan @ericcurtin. At the same time, it's getting more and more code, I would merge this
one and guarantee we have something tested fine and a next patch I change to INFO if all agree with the idea.

Already changed to INFO, should be all set.

@dougsland
Copy link
Collaborator Author

Intel that I have:

  "GPUs": {
        "Detected GPUs": [
            {
                "Details": "00:02.0 VGA compatible controller: Intel Corporation TigerLake-H GT1 [UHD Graphics] (rev 01)",
                "Env": "ONEAPI_DEVICE_SELECTOR",
                "GPU": "Intel GPU",
                "VRAM": "Unknown"
            }
        ],
        "INFO": [
            {
                "INFO": "Unable to detect NVIDIA GPU(s). Error: Unknown error (check if NVIDIA drivers are loaded).",
                "Vendor": "NVIDIA"
            },
            {
                "INFO": "No AMD GPU detected or drivers missing.",
                "Vendor": "AMD"
            }
        ]
    },
    "Image": "quay.io/ramalama/ramalama",
    "Runtime": "llama.cpp",
    "Store": "/home/douglas/.local/share/ramalama",
    "UseContainer": true,
    "Version": "0.5.4"
}

macOS:

 "GPUs": {
        "Detected GPUs": [
            {
                "Cores": "32",
                "GPU": "Apple M1 Max",
                "Metal": "Metal 3",
                "Vendor": "Apple (0x106b)"
            },
            {
                "GPU": "Color LCD"
            }
        ],
        "INFO": "No errors"
    },
    "Image": "quay.io/ramalama/ramalama",
    "Runtime": "llama.cpp",
    "Store": "/Users/douglaslandgraf/.local/share/ramalama",
    "UseContainer": false,
    "Version": "0.5.4"

@maxamillion
Copy link
Collaborator

This looks great. Thank you for the contribution and for addressing all the feedback. 😄

@maxamillion maxamillion self-requested a review January 31, 2025 23:10
@dougsland
Copy link
Collaborator Author

dougsland commented Feb 1, 2025

This looks great. Thank you for the contribution and for addressing all the feedback. 😄

My pleasure @maxamillion , looking forward to contribute more. Please share issues that you guys looking for help.

@rhatdan
Copy link
Member

rhatdan commented Feb 1, 2025

Great work @dougsland

LGTM

@rhatdan rhatdan merged commit 4b34290 into containers:main Feb 1, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants