Skip to content

Keep the plain capability cache on single-GPU hosts - #13962

Merged
danielhanchen merged 1 commit into
mainfrom
r7-single-gpu-capability-cache
Oct 16, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
r7-single-gpu-capability-cache

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

#13923 replaced functools.cache(torch.cuda.get_device_capability) with a per-device wrapper, so a no-argument call follows torch.cuda.set_device. On a host with one visible GPU that correctness gain cannot apply, but every cache hit now runs Python code (resolve the device, then a dict lookup) instead of the C-level functools.cache hit. The attention helpers call get_device_capability(Q.device) on every eager forward, so single-GPU runs pay a small cost for nothing.

Measured on the cache itself (CPU, 300k calls, best of 7):

call main before #13923 after #13923 this PR
get_device_capability(Q.device) 0.073 us 0.502 us 0.096 us

The last column is the same functools.cache object as before #13923; the residual difference is timer noise.

Fix

_cache_device_capability returns the plain functools.cache wrapper when torch.cuda.device_count() <= 1, exactly what main had before #13923. With one visible device every spelling (no argument, 0, "cuda:0", torch.device("cuda"), x.device) names the same card, so caching the call is exact. The per-device wrapper is kept for hosts with two or more visible devices, where it is needed.

Tests

  • New test_capability_cache_single_device_keeps_the_plain_cache: a one-device host gets a functools.cache object, probed once per distinct argument, and re-wrapping does not stack.
  • tests/test_mixed_nvidia_capabilities.py: 36 passed (the multi-device cases still use the per-device wrapper).
  • On one B200, test_capability_cache_traces_in_fullgraph (no argument, 0, "cuda:0", torch.device("cuda"), x.device under fullgraph=True): 5 passed.
  • Related suites (bnb NF4 override, compile, context parallel, CE eviction hints, pre-Volta fast inference, int4, fp8): 209 passed, unchanged.

With one visible device every device spelling names it, so caching the call
is exact. The per-device wrapper added Python work to every eager
get_device_capability(Q.device) hit in attention dispatch; it now only applies
when more than one device is visible.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T06:00:09.356419Z 1fd739a Manual request
🔒 Security Review ⚠️ Failed 2026-10-16T05:39:30.935753Z 1fd739a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 1fd739a56a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 6baec3b into main Oct 16, 2026
@danielhanchen
danielhanchen deleted the r7-single-gpu-capability-cache branch October 16, 2026 07:22
danielhanchen added a commit that referenced this pull request Oct 16, 2026
…ice (#13982)

## Summary

`Repo tests (CPU, studio)` fails on every PR based on main since #13882
(for example #13960 and #13962):

```
tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe
E   AssertionError: these skip guards read a probe tests/_zoo_aggressive_cuda_spoof.py patches to True process-wide, ...
E   assert not ['test_optimizer_state_offload.py:139: torch.cuda.is_available()', ...]
```

#13882 added `tests/test_optimizer_state_offload.py` with seven
`@pytest.mark.skipif(not torch.cuda.is_available(), ...)` guards. The
CUDA spoof the zoo tests install patches `torch.cuda.is_available` to
True for the whole process. When this file shares a session with them on
a CPU runner, its CUDA cases un-skip and fail. The repo-wide guard
caught this before it could flake. The product change is fine; the
guards are what is wrong.

## Change

Gate those seven cases on `has_real_cuda()` from
`tests/_shared/real_accelerator.py`, the same probe the other CUDA-only
tests use (#13936, #13937). It checks for a real device rather than the
patchable `is_available`. The CPU cases in the file are unchanged.

## Testing

- `tests/python/test_accelerator_skip_guards.py` +
`tests/test_optimizer_state_offload.py` on CPU: 40 passed, 15 skipped.
- With the original file restored, the guard test fails again (1 failed,
23 passed), naming the same seven lines.
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.

1 participant