Skip to content

fix(studio): prevent /api/system/hardware from creating CUDA context on 16GB GPUs (#18732) - #18734

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
Fahad090NP:fix/18732-hardware-poll-no-cuda-context
Nov 23, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
Fahad090NP:fix/18732-hardware-poll-no-cuda-context

Conversation

@Fahad090NP

@Fahad090NP Fahad090NP commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Related to #18732

Summary

GET /api/system/hardware calls get_gpu_summary() -> get_gpu_memory_info() -> _context_free_cuda_memory_info(idx, total_bytes). On discrete NVIDIA GPUs, torch.cuda.get_device_properties(idx).total_memory (total_bytes) excludes the driver/firmware reserved framebuffer (~300–750 MiB), whereas nvidia-smi --query-gpu=memory.total (driver_total_bytes) includes it.

Previously, _context_free_cuda_memory_info rejected nvidia-smi telemetry whenever abs(driver_total_bytes - total_bytes) > max(total_bytes // 100, 16 * 1024**2) (1%). On a 16 GB card such as the RTX 5070 Ti (nvidia-smi total = 16303 MiB, PyTorch total_memory = 15878.56 MiB, reserved framebuffer = 424.44 MiB or 2.67%), the 1% check misclassified the GPU as a partitioned MIG slice, declined nvidia-smi, and fell back to torch.cuda.mem_get_info(0), pinning a ~200 MB primary CUDA context on GPU 0 for the lifetime of the backend.

Changes

  • studio/backend/utils/hardware/hardware.py
    • Widen the whole-GPU vs partition tolerance in _context_free_cuda_memory_info to max(total_bytes // 10, 1024 * 1024**2) (matching the 10% partition scope check in _rocm_system_wide_vram_by_index and covering the up-to-1 GiB NVIDIA driver framebuffer reservation while still rejecting multi-GiB MIG/partition slices).
  • studio/backend/tests/test_system_poll_no_cuda_context.py
    • Add test_context_free_accepts_reserved_framebuffer_on_16gb_card and test_gpu_summary_16gb_card_with_reserved_framebuffer_creates_no_context reproducing the exact RTX 5070 Ti 16 GB readings (16303 MiB driver total vs 15878.56 MiB PyTorch total) and verifying mem_get_info is never called.

Verification

  • python -m py_compile studio/backend/utils/hardware/hardware.py studio/backend/tests/test_system_poll_no_cuda_context.py: passed
  • Ran context-free VRAM and MIG/partition rejection unit tests in test_system_poll_no_cuda_context.py: 6/6 passed

danielhanchen added a commit that referenced this pull request Nov 21, 2026
…t grad to one fp32 ulp (#18739)

## Problem


`studio/backend/tests/test_diffusion_lora_fast.py::test_dense_base_with_fp32_inputs_is_bit_identical_to_peft`
failed on an unrelated PR (#18734, base `15a985f32d`) in `(Python 3.13,
diffusion)`:

```
assert torch.equal(got[0], want[0]) and torch.equal(got[1], want[1])
E   assert (True and False)
```

## Cause

The test drew its input with an unseeded `torch.randn`, so `x` depended
on whatever the earlier tests in the same xdist worker left in the
global RNG. For most inputs the fast path and PEFT agree bit for bit,
but for a small fraction one element of the **input** gradient differs
by one fp32 ulp. Reproduced locally with seed 1049 (1 of 300 seeds):

| tensor | bit-identical | max abs diff |
|---|---|---|
| forward output | yes | 0 |
| input grad | no, 1 element | 3.6e-12 (relative 3.8e-9) |
| all six LoRA grads | yes | 0 |

The block feeds one layer norm into both `q` and `k`, so its input
gradient is a sum of the two branches, and autograd's accumulation order
can change the last bit of a rare element. That is not a gradient error.
`diffusion_lora_fast.py` (#18345) is correct; the test's exact-equality
claim on the input grad is stricter than the arithmetic allows.

## Fix

Test-only change:

- The input comes from a dedicated generator, parametrized over seed 0
and seed 1049 (the failing case), so the test no longer depends on test
order.
- The forward output and every LoRA gradient still require
`torch.equal`.
- The input gradient is checked with `torch.testing.assert_close(rtol =
1e-6, atol = 1e-9)`, which admits ulp-level accumulation-order noise and
nothing a real bug would produce.

## Verification

- `studio/backend/tests/test_diffusion_lora_fast.py`: 23 passed.
- The previous exact check fails on seed 1049, as on CI.
- Sabotage: scaling the fast path's LoRA input gradient by 1.01 in
`_LoraProduct.backward` fails both parametrizations (8176 of 8192
elements mismatched). Restored afterwards.
@danielhanchen danielhanchen self-assigned this Nov 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T01:55:23.065568Z 5b85c8a Manual request
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 5f522b83b2

ℹ️ 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

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 5b85c8abd7

ℹ️ 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".

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants