Skip to content

Gate CUDA fallback patch on non-CUDA PyTorch build, not active device type - #15

Merged
lmangani merged 3 commits into
cuda-fallback-handlerfrom
copilot/sub-pr-14
Mar 16, 2026
Merged

Gate CUDA fallback patch on non-CUDA PyTorch build, not active device type#15
lmangani merged 3 commits into
cuda-fallback-handlerfrom
copilot/sub-pr-14

Conversation

Copilot AI commented Mar 16, 2026

Copy link
Copy Markdown

_setup_cuda_fallback() was patching torch.cuda.* whenever DEVICE.type != "cuda", which also fires on CUDA-compiled PyTorch running on CPU (e.g., driver unavailable) — silently swallowing errors that should surface as real misconfiguration signals.

Changes

  • backend/ltx2_server.py — Swap the outer guard from a device-type check to a build-time check:
# Before: patches any time the active device isn't CUDA
if device_type == "cuda":
    return

# After: patches only when PyTorch was compiled without CUDA
if torch.version.cuda is not None:
    return
Scenario Before After
Non-CUDA PyTorch wheel (MPS/CPU build) ✅ patched ✅ patched
CUDA PyTorch, driver missing → CPU fallback ⚠️ errors masked ✅ real errors surface
CUDA PyTorch, running on CUDA ✅ no patch ✅ no patch
  • Docstring updated to explain the torch.version.cuda is None intent and why CUDA-capable builds are explicitly excluded.

📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

- Add optional `device` param to safe_cuda_synchronize, safe_cuda_memory_reserved,
  and safe_cuda_memory_allocated to match the real torch.cuda API signatures
- Replace bare `except Exception: pass` with specific (RuntimeError, AttributeError)
  catches and DEBUG-level logging in MPS fallback wrappers
- Update _setup_cuda_fallback docstring to reflect runtime-device-based condition

Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Copilot AI changed the title [WIP] Add CUDA fallback handling for non-CUDA builds Fix CUDA fallback wrapper signatures and exception handling Mar 16, 2026
Copilot AI requested a review from lmangani March 16, 2026 01:03
Only apply torch.cuda.* monkey-patches when PyTorch was compiled without
CUDA support (torch.version.cuda is None). CUDA-capable builds running on
CPU due to driver issues now surface real errors instead of silently no-oping.
Update docstring to explain the intent precisely.

Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Copilot AI changed the title Fix CUDA fallback wrapper signatures and exception handling Gate CUDA fallback patch on non-CUDA PyTorch build, not active device type Mar 16, 2026
@lmangani
lmangani marked this pull request as ready for review March 16, 2026 01:09
@lmangani
lmangani merged commit 33b2ef9 into cuda-fallback-handler Mar 16, 2026
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