Loading safetensors on GPU is slower than when loading on CPU
Describe the bug
Loading models directly on GPU with safetensors is slower than when first loading on CPU and then on GPU.
Note that diffusers depends on both transformers and accelerate for fast loading so the problem might very well be there.
Note: Also please make sure to use this branch: https://github.com/huggingface/accelerate/pull/1121 when reproducing this bug as otherwise there will be a failure.
Reproduction
Run the following file:
from diffusers import StableDiffusionPipeline, UniPCMultistepScheduler
import time
import torch
import sys
path = sys.argv[1]
use_device_map = bool(int(sys.argv[2]))
start_time = time.time()
if use_device_map:
print("Load directly on GPU")
pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16, device_map="auto")
else:
print("Load directly on CPU")
pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
prompt = "a highly realistic photo of green turtle"
print("Loading Time", time.time() - start_time)
generator = torch.Generator(device="cuda").manual_seed(0)
image = pipe(prompt, generator=generator, num_inference_steps=15).images[0]
print("Time", time.time() - start_time)
with:
./bench_safetensors.py CompVis/stable-diffusion-v1-4 0
and then with
./bench_safetensors.py CompVis/stable-diffusion-v1-4 1
Logs
The logs show the following:
Load directly on CPU
...
Loading Time 6.805649042129517
...
Time 9.034255504608154
and
Load directly on GPU
...
Loading Time 8.363195180892944
...
Time 11.03556203842163
### System Info
diffusersversion: 0.14.0.dev0- Platform: Linux-4.19.0-23-cloud-amd64-x86_64-with-debian-10.13
- Python version: 3.7.3
- PyTorch version (GPU?): 1.13.1+cu116 (True)
- Huggingface_hub version: 0.12.0
- Transformers version: 4.27.0.dev0
- Accelerate version: 0.17.0.dev0
- xFormers version: 0.0.16
- Using GPU in script?:
- Using distributed or parallel set-up in script?:
and:
safetensors == 0.2.8
and running on a V100
cc @Narsil - any idea what could be going on here? Think debugging is a bit of a rabbit hole between accelerate and diffusers, but would be cool to figure out what's going on here.
Thanks for the catch. I need to dive a little more. This is at odds with past bench I have done (benching this stuff correctly is sooo tricky)
Also related: https://github.com/huggingface/safetensors/pull/184
@Narsil any updates?
Well it's true.
My guess is that it all links to how the ops are orchestrated.
With First load on CPU then to GPU, what happens is that you're creating all the shapes and whatnot on CPU, and it happens extremely fast since you're not creating CPU buffers for the tensors. (Please make sure to use safetensors == 0.3 to check numbers now.
Whereas when loading on GPU, you are still not allocating on CPU, but you're doing some CPU work, then allocating GPU buffer and feeding it from CPU, then doing another bit of work on CPU etc.. And the ordering seems to be slower.
The observations hold true in pure torch code, https://gist.github.com/Narsil/3edeec2669a5e94e4707aa0f901d2282
So there's really not a lot to be done about it. But since 0.3 the situation should be much better in all cases.
Also not that in complex apps, every subsequent movement of tensors is hard to account for, load_state_dict seemed to be than moving the dict on GPU first (I'm guessing for the same reasons)
So, if I understand correctly there is no point of loading safetensors directly to gpu?
It depends. Try it for yourself. It really depends on how the rest of the code works I feel. (but basically, making the GPU allocations as close together as possible seems to be the optimal thing)
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
Please note that issues that do not follow the contributing guidelines are likely to be ignored.
is copy to cpu followed by streaming into GPU from CPU async? that can explain why loading to cpu is faster than gpu directly. Because load to CPU and transfer to GPU can be async