Uh oh!
There was an error while loading. Please reload this page.
Improve load_ip_adapter RAM Usage - #10948
Conversation
asomoza
commented
Mar 3, 2025
nice! do you measure the ram savings? |
HuggingFaceDocBuilderDev
commented
Mar 3, 2025
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
hlky
commented
Mar 3, 2025
@bot /style |
Style fixes have been applied. View the workflow run here. |
Yep! The peak CPU RAM usage decreases from 8.9G to 2.1G when I load an FP16 SDXL IP Adapter to GPU. |
It's very easy to measure RAM savings in Colab. Here are two minimal reproductions. Now fromtransformersimportCLIPVisionModelWithProjectionimporttorchmodel=CLIPVisionModelWithProjection.from_pretrained("eramth/ip-adapter",subfolder="sdxl_models/image_encoder",torch_dtype=torch.float16)
# The CPU RAM usage is about 2.3G.model=model.to("cuda")
# The CPU RAM usage is still about 2.3G. And the VRAM usage is about 3.8G# The below code can return the released CPU RAM to the system, so that we can observe the CPU RAM usage easily.importctypesctypes.CDLL("libc.so.6").malloc_trim(0)
# The CPU RAM usage is about 2.1G.Before fromtransformersimportCLIPVisionModelWithProjectionimporttorchmodel=CLIPVisionModelWithProjection.from_pretrained("eramth/ip-adapter",subfolder="sdxl_models/image_encoder")
# The CPU RAM usage is about 8.9G.model=model.to("cuda",dtype=torch.float16)
# The CPU RAM usage is still about 6.4G. And the VRAM usage is about 3.8G# The below code can return the released CPU RAM to the system, so that we can observe the CPU RAM usage easily.importctypesctypes.CDLL("libc.so.6").malloc_trim(0)
# The CPU RAM usage is about 2.2G.Also you can get the same memory usage results directly loading an IP Adapter in |
thanks a lot, I don't have a low RAM system so this is hard to catch for me at least. We have some errors in the tests which I believe aren't related to this PR. @hlky can you review this too please, there's a lot of failed tests so just to be sure. |
Loading a model with
torch_dtypeisNoneusingtransformers.modeling_utils.PreTrainedModelwill result in additional RAM usage because of data type conversion.There is no conversion if the data type of the model weights is the same as
torch_dtype.And different conversions have different impacts on RAM usage which is mentioned in #10679.
When
torch_dtypeisNone,PreTrainedModelwill regard it astorch.float32, which causes a lot more RAM usage.This PR is to improve this issue.
@asomoza