Repository navigation
fix(studio): prevent /api/system/hardware from creating CUDA context on 16GB GPUs (#18732) - #18734
Conversation
…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.
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Related to #18732
Summary
GET /api/system/hardwarecallsget_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), whereasnvidia-smi --query-gpu=memory.total(driver_total_bytes) includes it.Previously,
_context_free_cuda_memory_inforejectednvidia-smitelemetry wheneverabs(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-smitotal =16303 MiB, PyTorchtotal_memory=15878.56 MiB, reserved framebuffer =424.44 MiBor2.67%), the 1% check misclassified the GPU as a partitioned MIG slice, declinednvidia-smi, and fell back totorch.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_context_free_cuda_memory_infotomax(total_bytes // 10, 1024 * 1024**2)(matching the 10% partition scope check in_rocm_system_wide_vram_by_indexand 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.pytest_context_free_accepts_reserved_framebuffer_on_16gb_cardandtest_gpu_summary_16gb_card_with_reserved_framebuffer_creates_no_contextreproducing the exact RTX 5070 Ti 16 GB readings (16303 MiBdriver total vs15878.56 MiBPyTorch total) and verifyingmem_get_infois never called.Verification
python -m py_compile studio/backend/utils/hardware/hardware.py studio/backend/tests/test_system_poll_no_cuda_context.py: passedtest_system_poll_no_cuda_context.py: 6/6 passed