Add torch.cuda.synchronize() before empty_cache() to fix Blackwell crash#116
Open
cuzelac wants to merge 1 commit intovisualbruno:mainfrom
Open
Add torch.cuda.synchronize() before empty_cache() to fix Blackwell crash#116cuzelac wants to merge 1 commit intovisualbruno:mainfrom
cuzelac wants to merge 1 commit intovisualbruno:mainfrom
Conversation
torch.cuda.empty_cache() does not synchronize — it can free memory that in-flight async kernels are still using, causing "illegal memory access" errors on high-concurrency GPUs (e.g. RTX 5090 / Blackwell). Add synchronize() before each empty_cache() to ensure all GPU work completes before memory is released. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
torch.cuda.empty_cache()can free GPU memory that still has pending async work, causingCUDA error: illegal memory accesson Blackwell GPUs (RTX 5090, sm_120). Addingtorch.cuda.synchronize()before eachempty_cache()call ensures all GPU work completes before memory is released.Related: CuMesh also needs stream-awareness fixes — see visualbruno/CuMesh#2. Both fixes are required; neither alone is sufficient.
Why this is needed
torch.cuda.empty_cache()releases unused cached memory from PyTorch's allocator back to the GPU. However, it does not synchronize pending GPU operations first. On Blackwell GPUs, where PyTorch usescudaStreamNonBlockingstreams, this can race with in-flight kernels that are still using that memory.Changes
trellis2/pipelines/trellis2_image_to_3d.py— addtorch.cuda.synchronize()before alltorch.cuda.empty_cache()calls in the pipelinetrellis2/models/sc_vaes/sparse_unet_vae.py— addtorch.cuda.synchronize()beforetorch.cuda.empty_cache()in the VAE decoderTesting