Skip to content

fix: amdsmi import when libamd_smi is missing (macOS / no ROCm)#1201

Merged
benoit-cty merged 4 commits into
masterfrom
fix/amdsmi-import-keyerror-macos
May 20, 2026
Merged

fix: amdsmi import when libamd_smi is missing (macOS / no ROCm)#1201
benoit-cty merged 4 commits into
masterfrom
fix/amdsmi-import-keyerror-macos

Conversation

@davidberenstein1957
Copy link
Copy Markdown
Collaborator

Summary

On systems where the amdsmi Python package is installed but ROCm / libamd_smi.so is not available (common on macOS), importing CodeCarbon could crash with KeyError during import amdsmi inside gpu_amd.py, breaking the CLI and any code path that loads GPU support.

This change broadens the existing AttributeError handler to also catch OSError and KeyError, sets amdsmi = None and AMDSMI_AVAILABLE = False, and logs a clear warning so AMD GPU metrics are skipped instead of failing import.

Test plan

  • On macOS (or any host without ROCm) with amdsmi installed: codecarbon --help and python -c "import codecarbon" succeed with a warning, no traceback.
  • On a ROCm system with working amdsmi: AMD GPU metrics still work as before.

Made with Cursor

@davidberenstein1957 davidberenstein1957 requested a review from a team as a code owner May 19, 2026 18:49
@codecov
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.90%. Comparing base (afa3ac8) to head (ba1565f).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

davidberenstein1957 and others added 2 commits May 20, 2026 11:37
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>
@benoit-cty benoit-cty force-pushed the fix/amdsmi-import-keyerror-macos branch from 6c649a9 to ef18da7 Compare May 20, 2026 09:37
@benoit-cty
Copy link
Copy Markdown
Contributor

@copilot can you improve coverage by updating the tests ?

Copy link
Copy Markdown
Contributor

Copilot AI commented May 20, 2026

@copilot can you improve coverage by updating the tests ?

Added a new TestAmdsmiImportFailures class (commit 241e498) with 4 tests covering the import-time exception handling in gpu_amd.py:

  • test_oserror_sets_amdsmi_unavailable — verifies OSError (e.g. missing libamd_smi.so) sets amdsmi=None and AMDSMI_AVAILABLE=False
  • test_keyerror_sets_amdsmi_unavailable — same for KeyError (macOS / no ROCm)
  • test_attributeerror_sets_amdsmi_unavailable — same for AttributeError (pre-existing case)
  • test_import_failure_logs_warning — verifies all three failure types emit a logger warning

Each test uses builtins.__import__ patching + sys.modules manipulation to simulate a fresh module import that raises the target exception, then restores the original state.

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:

  • get.geojs.io
    • Triggering command: /usr/bin/python python -m pytest tests/test_gpu_amd.py tests/test_gpu.py tests/test_gpu_nvidia.py tests/test_unsupported_gpu.py -v ader (dns block)
  • ipinfo.io
    • Triggering command: /usr/bin/python python -m pytest tests/test_gpu_amd.py tests/test_gpu.py tests/test_gpu_nvidia.py tests/test_unsupported_gpu.py -v ader (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from benoit-cty May 20, 2026 10:20
@benoit-cty benoit-cty merged commit 5c3331a into master May 20, 2026
13 checks passed
@benoit-cty benoit-cty deleted the fix/amdsmi-import-keyerror-macos branch May 20, 2026 12:48
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