Skip to content

test(cuda.bindings): set a valid power limit in test_set_power_management_limit - #3596

Open
mdboom wants to merge 3 commits into
NVIDIA:mainfrom
mdboom:issue3564
Open

mdboom wants to merge 3 commits into
NVIDIA:mainfrom
mdboom:issue3564

Conversation

@mdboom

@mdboom mdboom commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3564. Part of #2077.

Summary

test_set_power_management_limit set every GPU's power limit to a hardcoded 10000 mW (10 W), which is below the minimum of every GPU in CI. Runners without permission skipped on NoPermissionError. Runners with permission skipped on the resulting InvalidArgumentError. So the test never ran.

 for each device:
-  set_power_management_limit_v2(GPU, 10000)        # always invalid
-  NoPermissionError    -> skip
-  InvalidArgumentError -> skip ("probably unsupported")
+  limit = get_power_management_limit()
+  min, max = get_power_management_limit_constraints()
+  assert min <= limit <= max
+  set_power_management_limit_v2(GPU, limit)       # valid everywhere; GPU unchanged
+  NoPermissionError    -> skip
+  try:
+    assert get_power_management_limit() == limit
+  finally:
+    set_power_management_limit_v2(GPU, limit)     # restore, even if the readback fails

The "not currently run in any CI job" markers are removed. The test is identical in cuda_bindings_12/tests/nvml/test_device.py and cuda_bindings_13/tests/nvml/test_device.py, and both get the same change.

Evidence

  • Before (main CI): 11 configurations had permission but skipped with InvalidArgumentError: every Windows row, and the two Linux 610.43.02 custom-driver rows. The other 19 skipped with NoPermissionError.
  • After (locally, CUDA 13 copy, RTX A400 and RTX 5070, not root): the read, range check, and assertion pass on both GPUs. The set call then skips with "No permission", as expected without root.

Confirming it runs in CI: in this PR's CI run, the Windows jobs and the Linux 610.43.02 jobs (CUDA 12 and CUDA 13 bindings) should report test_set_power_management_limit PASSED with SUBPASSED subtests, where main reports SUBSKIPPED. For example:

gh run view <CI run id> --repo NVIDIA/cuda-python --log | grep -F "test_set_power_management_limit"

The Linux rows without root still skip. Running it there is #3582.

Merge Danger

Door: two-way. Test-only change.

Blast Radius: CI runners that have NVML permission. The test now actually calls the power-limit setter there, but with the GPU's current limit, so the setting doesn't change. It can now fail on those runners if NVML rejects a valid limit, which is the point.

Reviewers: please feel free to push commits directly to this PR to improve it.

🤖 Generated with Claude Code

…ment_limit

The test set every GPU's power limit to a hardcoded 10 W, which is below
the minimum of every GPU in CI. Runners without permission skipped on
NoPermissionError, and runners with permission (Windows, and the Linux
custom-driver rows) skipped on the resulting InvalidArgumentError, so the
test never ran.

Read the current limit and its constraints, re-apply the current limit
(valid on every device, and it leaves a shared GPU unchanged), and assert
that it reads back. InvalidArgumentError is no longer treated as
"unsupported". Remove the "not currently run in any CI job" markers.

Same change in cuda_bindings_12 and cuda_bindings_13.

Closes NVIDIA#3564

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mdboom mdboom added this to the cuda.bindings 13.6.0 & 12.9.12 milestone Nov 25, 2026
@mdboom mdboom added test Improvements or additions to tests cuda.bindings Everything related to the cuda.bindings module labels Nov 25, 2026
@mdboom mdboom self-assigned this Nov 25, 2026
@mdboom
mdboom requested a review from rwgk November 25, 2026 18:12
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

Summary

Summary by CodeRabbit

  • Tests
    • Updated CUDA 12 and 13 power-limit checks to verify the initial setting is within the device’s supported range, then apply and confirm both the minimum and maximum limits.
    • Tests restore and verify the original limit afterward. They skip devices whose minimum and maximum limits are equal, and skip when permission is denied while setting the minimum limit. Other errors during that initial setting attempt are reported as failures.

Both NVML power-limit tests now use device-reported constraints, verify minimum and maximum settings, and restore and verify the initial limit. They skip single-value ranges and permission failures.

Changes: Power-limit test

Layer / File(s) Summary
Validate, set, and restore power limits
cuda_bindings_12/tests/nvml/test_device.py, cuda_bindings_13/tests/nvml/test_device.py
Both tests check the supported range, verify minimum and maximum settings, and restore and verify the initial limit in a finally block. They skip on NoPermissionError and no longer skip on InvalidArgumentError.

Assessment against linked issues

Objective Addressed Explanation
Read the valid range and current limit, set an in-range value, verify the result, and stop skipping on InvalidArgumentError [#3564] ✅
Restore the saved limit in a finally block [#3564] ✅
Remove the “not currently run in any CI job” markers from both bindings roots [#3564] ❓ The supplied change summaries do not state whether those markers were removed.

Possibly related issues

Suggested reviewers: jpascucci-nv

Change: Other · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 69f95

This updates the NVML power-limit tests to use device-reported bounds, verify the readbacks and restore the original limit. It does not change the bindings' public behavior, and no concrete merge-blocking risk remains. The tests will only be confirmed on hardware once CI runs on the target rows.

Security architecture risk: Low · up to 69f95

The tests use existing GPU permissions and try to restore the original limit. If restoration fails or the test process stops before cleanup, a GPU can retain the test limit and affect later workloads on that device.

Retained concerns

  • Trivial · reliability · inferred: The new device-wide writes rely on the test process to restore an in-memory snapshot. If the restore write fails or the process terminates without running cleanup after a successful write, a GPU can retain a test limit. A repeat run can then snapshot and restore that stale value instead of recovering the original. If the GPU is reused without a reset, later workloads inherit the changed limit.

View findings in the submitted review

Security review details

Security Blast Radius

  • inferred — A permitted invocation can change the device-wide limit on every GPU it enumerates for which the setter succeeds. Another workload on the same GPU can be affected while a limit is changed; if another writer updates the limit during the test, snapshot-based cleanup can overwrite that update. The thread-unsafe marker does not itself establish exclusive GPU ownership across jobs or processes.

Trust Boundaries and Controls

  • observed — The pytest code passes driver-reported bounds, not request-supplied values, to the existing NVML setter. The first write still runs under the caller's NVML authority and skips if NVML denies permission. The change adds no permission grant.

Hardening Proposals

  • proposed — Run the mutating tests only on exclusively assigned GPUs, and have the runner verify or reset the power limit before reuse. If the runner cannot confirm recovery after interruption or cleanup failure, keep the GPU out of service until recovery is verified.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Finishing Touches
Fix failing CI checks

  • Create a new PR

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c29b214 and 5bbfb4c.

Files selected for processing: 2

  • cuda_bindings_12/tests/nvml/test_device.py
  • cuda_bindings_13/tests/nvml/test_device.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 9 remain after this review.

…ment_limit

Wrap the readback assertion in try/finally and set the original limit
again in the finally block, so a failed or mismatched readback still
attempts to restore the GPU's power limit. The restore runs only after the
setter succeeded, so runners without permission still skip cleanly.

Addresses review feedback on NVIDIA#3596.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

# the (possibly shared) GPU unchanged.
try:
nvml.device_set_power_management_limit_v2(device, nvml.PowerScope.GPU, 10000)
nvml.device_set_power_management_limit_v2(device, nvml.PowerScope.GPU, limit)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

— codex GPT-6 on behalf of Ralf —

We already have min_limit and max_limit, so we can use them to verify that the setter actually changes the GPU’s power limit. I’d set and read back both endpoints, then restore and verify the original limit in finally.

The power limit is shared device state. I’d mark the test thread_unsafe to serialize its pytest invocations and run it on GPUs whose power settings the runner controls exclusively. A concurrent writer can make the readback flaky, and cleanup can overwrite that writer’s update.

I’d scope unsupported_before to the initial queries and the first setter call. Once that write succeeds, a failed readback, second write, or restore should fail the subtest.

I’d use this in both bindings roots.

@pytest.mark.thread_unsafe(reason="Changes the device-wide power limit")
def test_set_power_management_limit(all_devices, subtests):
    for device in all_devices:
        with subtests.test(device_index=nvml.device_get_index(device)):
            with unsupported_before(device, None):
                initial_limit = nvml.device_get_power_management_limit(device)
                min_limit, max_limit = nvml.device_get_power_management_limit_constraints(device)
                assert min_limit <= initial_limit <= max_limit
                if min_limit == max_limit:
                    pytest.skip("Device supports only one power limit")

                try:
                    nvml.device_set_power_management_limit_v2(device, nvml.PowerScope.GPU, min_limit)
                except nvml.NoPermissionError:
                    pytest.skip("No permission to set power management limit")

            try:
                assert nvml.device_get_power_management_limit(device) == min_limit
                nvml.device_set_power_management_limit_v2(device, nvml.PowerScope.GPU, max_limit)
                assert nvml.device_get_power_management_limit(device) == max_limit
            finally:
                nvml.device_set_power_management_limit_v2(device, nvml.PowerScope.GPU, initial_limit)
                assert nvml.device_get_power_management_limit(device) == initial_limit, "Power limit was not restored"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

…ment_limit

Set and read back both constraint endpoints so the test verifies the setter
changes the limit, restore and verify the initial limit in finally, scope
unsupported_before to the initial queries and first write, and mark the test
thread_unsafe since the power limit is device-wide state.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Architecture findings (1)

Advisory architecture findings (1)

  • Trivial — The new device-wide writes rely on the test process to restore an in-memory snapshot. If the restore write fails or the process terminates without running cleanup after a successful write, a GPU can retain a test limit. A repeat run can then snapshot and restore that stale value instead of recovering the original. If the GPU is reused without a reset, later workloads inherit the changed limit.
Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 54fbf3d and 69f9558.

Files selected for processing: 2

  • cuda_bindings_12/tests/nvml/test_device.py
  • cuda_bindings_13/tests/nvml/test_device.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 7 remain after this review.

@mdboom
mdboom requested a review from rwgk November 30, 2026 16:51

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.bindings Everything related to the cuda.bindings module test Improvements or additions to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(cuda.bindings): test_set_power_management_limit requests an invalid 10 W limit, so it skips even where it has permission

2 participants