Fix (skip) cuda cache flush when origin device is cpu
and offloaded to meta
#3796
+1
−1
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.
What does this PR do?
Fix
disk_offload()
api causingtorch.cuda.empty_cache()
to be called when the module origin device iscpu
and offloaded tometa
(disk).Secondarily this also resolves a performance issue as
torch.cuda.empty_cache()
is slow and calling it with no effect in a forwardig env where modules are dynamically (manually) offloaded is suboptimal.Given above nn.Module (linear) which is on
cpu
and following this call, I did not expectaccelerate
code paths to call anything cuda related. This (in-directly) triggered a cuda assert error in my GIL=0 env with multiple gpu and threads. I probably have thread ctx bug somehwere above the code but the main objective is that in this scenerio,torch.cuda.empty_cache()
should never be called bydisk_offload
paths.Stacktrace: Please note the crash is not caused by
accelerate
. The stack shows the paths that triggered the invalidtorch.cuda.empty_cache()
call.Who can review?
@SunMarc @zach-huggingface @BenjaminBossan