Add CUDA fallback handling for non-CUDA builds#14
Merged
Conversation
Implement CUDA fallback handling for non-CUDA PyTorch builds, including safe no-op implementations for CUDA functions and logging.
There was a problem hiding this comment.
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 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() |
| except Exception: | ||
| pass | ||
|
|
||
| def safe_cuda_memory_reserved() -> int: |
| """Return 0 for memory reserved on non-CUDA devices.""" | ||
| return 0 | ||
|
|
||
| def safe_cuda_memory_allocated() -> int: |
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 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}") |
Author
|
@copilot open a new pull request to apply changes based on the comments in this thread |
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement CUDA fallback handling for non-CUDA PyTorch builds, including safe no-op implementations for CUDA functions and logging. Based on #12