Skip to content

Fix warning KeyError: 'gpu_index'#1122

Merged
inimaz merged 1 commit intomasterfrom
fix/key_error_gpu_index
Mar 19, 2026
Merged

Fix warning KeyError: 'gpu_index'#1122
inimaz merged 1 commit intomasterfrom
fix/key_error_gpu_index

Conversation

@benoit-cty
Copy link
Contributor

@benoit-cty benoit-cty commented Mar 19, 2026

Description

Fix warning in tests/test_emissions_tracker.py::TestCarbonTracker::test_carbon_tracker_TWO_GPU_PRIVATE_INFRA_CANADA:

=============================== warnings summary ===============================
tests/test_emissions_tracker.py::TestCarbonTracker::test_carbon_tracker_TWO_GPU_PRIVATE_INFRA_CANADA
  /home/runner/work/codecarbon/codecarbon/.venv/lib/python3.12/site-packages/_pytest/threadexception.py:58: PytestUnhandledThreadExceptionWarning: Exception in thread Thread-17
  
  Traceback (most recent call last):
    File "/home/runner/.local/share/uv/python/cpython-3.12.13-linux-x86_64-gnu/lib/python3.12/threading.py", line 1075, in _bootstrap_inner
      self.run()
    File "/home/runner/.local/share/uv/python/cpython-3.12.13-linux-x86_64-gnu/lib/python3.12/threading.py", line 1433, in run
      self.function(*self.args, **self.kwargs)
    File "/home/runner/work/codecarbon/codecarbon/.venv/lib/python3.12/site-packages/codecarbon/external/scheduler.py", line 42, in _run
      self.function(*self.args, **self.kwargs)
    File "/home/runner/work/codecarbon/codecarbon/.venv/lib/python3.12/site-packages/codecarbon/emissions_tracker.py", line 983, in _monitor_power
      gpu_detail["gpu_index"] in gpu_ids_to_monitor
      ~~~~~~~~~~^^^^^^^^^^^^^
  KeyError: 'gpu_index'

Motivation and Context

It's only a warning but it looks like an error.

The thread warning was caused by the background GPU utilization sampler assuming every GPU detail dict contains gpu_index. I fixed that in emissions_tracker.py:982 by falling back to the position in the returned GPU details list when the key is missing, which keeps subset filtering working and avoids the KeyError.

How Has This Been Tested?

❯ uv run python -m pytest tests/test_emissions_tracker.py
=========================================================== test session starts ===========================================================
platform linux -- Python 3.13.7, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/benoit/CODECARBON/codecarbon
configfile: pytest.ini (WARNING: ignoring pytest config in pyproject.toml!)
plugins: logfire-4.29.0, cov-7.0.0, requests-mock-1.12.1
collected 20 items                                                                                                                        

tests/test_emissions_tracker.py ....................                                                                                [100%]

=========================================================== 20 passed in 25.02s =================================

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codecov
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.74%. Comparing base (7ea4965) to head (9aa4910).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1122      +/-   ##
==========================================
+ Coverage   80.71%   80.74%   +0.03%     
==========================================
  Files          41       41              
  Lines        3920     3921       +1     
==========================================
+ Hits         3164     3166       +2     
+ Misses        756      755       -1     

☔ 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.

@benoit-cty benoit-cty marked this pull request as ready for review March 19, 2026 13:26
@benoit-cty benoit-cty requested a review from a team as a code owner March 19, 2026 13:26
Copy link
Collaborator

@inimaz inimaz left a comment

Choose a reason for hiding this comment

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

Thanks @benoit-cty !

@inimaz inimaz merged commit 46ac6dd into master Mar 19, 2026
12 checks passed
@inimaz inimaz deleted the fix/key_error_gpu_index branch March 19, 2026 14:00
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.

2 participants