Skip to content

Add CUDA fallback handling for non-CUDA builds#14

Merged
lmangani merged 5 commits intomainfrom
cuda-fallback-handler
Mar 16, 2026
Merged

Add CUDA fallback handling for non-CUDA builds#14
lmangani merged 5 commits intomainfrom
cuda-fallback-handler

Conversation

@lmangani
Copy link
Copy Markdown

Implement CUDA fallback handling for non-CUDA PyTorch builds, including safe no-op implementations for CUDA functions and logging. Based on #12

Implement CUDA fallback handling for non-CUDA PyTorch builds, including safe no-op implementations for CUDA functions and logging.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds runtime safeguards so the backend can run on non-CUDA environments (MPS/CPU) without crashing when upstream libraries call CUDA APIs unconditionally.

Changes:

  • Introduces a CUDA fallback monkey-patch for selected torch.cuda.* functions on non-CUDA devices.
  • Disables SageAttention when MPS is detected.
  • Adds a CPU fallback log when no CUDA/MPS device is available.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread backend/ltx2_server.py Outdated
Comment on lines +67 to +71
def safe_cuda_synchronize() -> None:
"""No-op synchronize for non-CUDA devices."""
if device_type == "mps":
try:
torch.mps.synchronize()
Comment thread backend/ltx2_server.py Outdated
except Exception:
pass

def safe_cuda_memory_reserved() -> int:
Comment thread backend/ltx2_server.py Outdated
"""Return 0 for memory reserved on non-CUDA devices."""
return 0

def safe_cuda_memory_allocated() -> int:
Comment thread backend/ltx2_server.py Outdated
Comment on lines +72 to +81
except Exception:
pass

def safe_cuda_empty_cache() -> None:
"""No-op empty_cache for non-CUDA devices."""
if device_type == "mps":
try:
torch.mps.empty_cache()
except Exception:
pass
Comment thread backend/ltx2_server.py Outdated
Comment on lines +49 to +64
def _setup_cuda_fallback() -> None:
"""
Monkey-patch torch.cuda functions to handle cases where PyTorch is not
compiled with CUDA support (e.g., running on MPS or CPU).

The ltx-pipelines library calls torch.cuda.synchronize() unconditionally,
which fails with "Torch not compiled with CUDA enabled" on non-CUDA builds.
"""
# Check if we're on a device that doesn't have full CUDA support
device_type = DEVICE.type

if device_type == "cuda":
# True CUDA - no fallback needed
return

logger.info(f"Setup CUDA fallback for device type: {device_type}")
@lmangani
Copy link
Copy Markdown
Author

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link
Copy Markdown

Copilot AI commented Mar 16, 2026

@lmangani I've opened a new pull request, #15, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 3 commits March 16, 2026 01:02
- 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 <[email protected]>
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 <[email protected]>
Gate CUDA fallback patch on non-CUDA PyTorch build, not active device type
@lmangani lmangani marked this pull request as ready for review March 16, 2026 01:10
@lmangani lmangani merged commit 8911042 into main Mar 16, 2026
4 checks passed
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.

3 participants