fix: make torch.cuda.empty_cache() and flex_attention device-agnostic (Intel XPU, Apple MPS)#336
Open
EduardoSolanas wants to merge 1 commit into
Conversation
- layers.py: Guard torch.cuda.empty_cache() with device checks (CUDA → XPU → MPS) Prevents RuntimeError on Intel Arc GPUs and Apple Silicon where CUDA is unavailable. - moondream.py: Add use_flex_decoding constructor parameter with auto-detection Defaults to True on CUDA, False otherwise. Allows explicit override for non-CUDA devices (Intel XPU, Apple MPS, AMD ROCm). Fixes m87-labs#316 and m87-labs#335. - sample.py: Guard CUDA memory stats calls with is_available() check Prevents crashes when running the demo script on non-NVIDIA hardware. Closes m87-labs#335
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.
Problem
Moondream3-preview crashes on non-CUDA devices (Intel Arc GPU, Apple MPS, AMD ROCm) due to two CUDA-specific code paths:
1.
torch.cuda.empty_cache()crashes in production model codeQuantizedLinear.unpack()callstorch.cuda.empty_cache()unconditionally. On Intel XPU and Apple MPS, this raises RuntimeError or AttributeError.2.
flex_attention+create_block_maskis CUDA-only (fixes #316)MoondreamModel.__init__hardcodesself.use_flex_decoding = True, triggeringcreate_block_maskandflex_attentionimports that are CUDA-only APIs.Changes
layers.py— Device-agnostic cache clearingmoondream.py— Configurable flex_decoding with auto-detectionF.scaled_dot_product_attention✓MoondreamModel(config, use_flex_decoding=False)sample.py— Guard benchmark memory statsCUDA-specific memory stat calls gated behind
torch.cuda.is_available().Why this approach (vs alternatives)
Testing
Tested on Intel Arc A770 with PyTorch 2.6.0+xpu (Intel oneAPI 2025.0.2):
use_flex_decodingauto-set toFalseQuantizedLinear.unpack()callstorch.xpu.empty_cache()correctlyF.scaled_dot_product_attentionfallbackRelated
use_flex_decodingissue)torch.cuda.empty_cache()+ flex_attention)