fix: amdsmi import when libamd_smi is missing (macOS / no ROCm)#1201
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1201 +/- ##
=======================================
Coverage 88.90% 88.90%
=======================================
Files 45 45
Lines 4301 4301
=======================================
Hits 3824 3824
Misses 477 477 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
benoit-cty
approved these changes
May 20, 2026
When the amdsmi Python package is installed without ROCm (e.g. on macOS), import can raise KeyError while resolving libamd_smi.so. Treat AttributeError, OSError, and KeyError like a broken install: disable AMD metrics and warn instead of crashing the CodeCarbon CLI on import.
Keep original warning message and comment; catch OSError and KeyError when libamd_smi fails to load (e.g. macOS with amdsmi wheel but no ROCm). Co-authored-by: Cursor <cursoragent@cursor.com>
6c649a9 to
ef18da7
Compare
Contributor
|
@copilot can you improve coverage by updating the tests ? |
…failures Agent-Logs-Url: https://github.com/mlco2/codecarbon/sessions/009db467-8429-4146-8907-bcbd0a16593b Co-authored-by: benoit-cty <6603048+benoit-cty@users.noreply.github.com>
Contributor
Added a new
Each test uses Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
benoit-cty
approved these changes
May 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On systems where the
amdsmiPython package is installed but ROCm /libamd_smi.sois not available (common on macOS), importing CodeCarbon could crash withKeyErrorduringimport amdsmiinsidegpu_amd.py, breaking the CLI and any code path that loads GPU support.This change broadens the existing
AttributeErrorhandler to also catchOSErrorandKeyError, setsamdsmi = NoneandAMDSMI_AVAILABLE = False, and logs a clear warning so AMD GPU metrics are skipped instead of failing import.Test plan
amdsmiinstalled:codecarbon --helpandpython -c "import codecarbon"succeed with a warning, no traceback.amdsmi: AMD GPU metrics still work as before.Made with Cursor