Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LoRA] fix: lora loading when using with a device_mapped model. #9449

Merged
merged 29 commits into from
Oct 31, 2024

Conversation

sayakpaul
Copy link
Member

@sayakpaul sayakpaul commented Sep 17, 2024

What does this PR do?

Fixes LoRA loading behaviour when used with a model that is sharded into multiple devices.

Minimal code
"""
Minimal example to show how to load a LoRA into the Flux transformer
that is sharded in two GPUs. 

Limitation:
* Latency
* If the LoRA has text encoder layers then this needs to be revisited.
"""

from diffusers import FluxTransformer2DModel, FluxPipeline 
import torch 

ckpt_id = "black-forest-labs/FLUX.1-dev"
dtype = torch.bfloat16
transformer = FluxTransformer2DModel.from_pretrained(
    ckpt_id, 
    subfolder="transformer",
    device_map="auto",
    max_memory={0: "16GB", 1: "16GB"},
    torch_dtype=dtype
)
print(transformer.hf_device_map)
pipeline = FluxPipeline.from_pretrained(
    ckpt_id,
    text_encoder=None,
    text_encoder_2=None,
    tokenizer=None,
    tokenizer_2=None,
    vae=None,
    transformer=transformer,
    torch_dtype=dtype
)
pipeline.load_lora_weights("TheLastBen/Jon_Snow_Flux_LoRA", weight_name="jon_snow.safetensors")
# print(pipeline.transformer.hf_device_map)

# Essentially you'd pre-compute these embeddings beforehand.
# Reference: https://gist.github.com/sayakpaul/a9266fe2d0d510ec44a9cdc385b3dd74. 
example_inputs = {
    "prompt_embeds": torch.randn(1, 512, 4096, dtype=dtype, device="cuda"),
    "pooled_projections": torch.randn(1, 768, dtype=dtype, device="cuda"),
}

_ =  pipeline(
    prompt_embeds=example_inputs["prompt_embeds"],
    pooled_prompt_embeds=example_inputs["pooled_projections"],
    num_inference_steps=50,
    guidance_scale=3.5,
    height=1024,
    width=1024,
    output_type="latent",
)

Some internal discussions:

Cc: @philschmid for awareness as you were interested in this feature.

TODOs

  • Tests
  • Docs

Once I get a sanity review from Marc and Benjamin, will request a review from Yiyi.

@BenjaminBossan
Copy link
Member

Does diffusers have multi GPU tests? If yes, would it make sense to add a test there and check that after LoRA loading, no parameter was transferred to meta device?

@sayakpaul
Copy link
Member Author

That is a TODO ;)

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a TODO ;)

I see. In that case, I have just some nits, otherwise I'd defer to Marc as I'm not an expert on device maps.

src/diffusers/pipelines/pipeline_utils.py Outdated Show resolved Hide resolved
src/diffusers/pipelines/pipeline_utils.py Outdated Show resolved Hide resolved
@sayakpaul
Copy link
Member Author

Does diffusers have multi GPU tests?

@BenjaminBossan yes, we do: https://github.com/search?q=repo%3Ahuggingface%2Fdiffusers%20require_torch_multi_gpu&type=code

But not for the use case, being described here. Will add them as a part of this PR.

@sayakpaul
Copy link
Member Author

@SunMarc a gentle ping when you find a moment.

@HuggingFaceDocBuilderDev

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.

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ! Just a few suggestions !

src/diffusers/loaders/lora_base.py Outdated Show resolved Hide resolved
src/diffusers/loaders/lora_base.py Outdated Show resolved Hide resolved
@sayakpaul
Copy link
Member Author

@yiyixuxu can you give this an initial look and once we agree, I will work on adding testing, docs, etc.

@sayakpaul
Copy link
Member Author

@yiyixuxu a gentle ping for a first review as it touches pipeline_utils.py.

@sayakpaul sayakpaul requested a review from DN6 October 15, 2024 09:51
@@ -398,9 +399,18 @@ def _optionally_disable_offloading(cls, _pipeline):
is_model_cpu_offload = False
is_sequential_cpu_offload = False

def model_has_device_map(model):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After-effects of make fix-copies.

@@ -387,6 +387,11 @@ def to(self, *args, **kwargs):

device = device or device_arg

def model_has_device_map(model):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DN6 it would make sense to make this a separate utility instead of having redefine three times. WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, you can add as a util function inside pipeline_utils.

@slow
@nightly
def test_calling_to_raises_error_device_mapped_components(self):
if "Combined" in self.pipeline_class.__name__:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because for connected pipelines, we don't support device mapping in the first place.

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, LGTM.

@sayakpaul
Copy link
Member Author

Failing tests are unrelated.

@sayakpaul sayakpaul merged commit 41e4779 into main Oct 31, 2024
17 of 18 checks passed
@sayakpaul sayakpaul deleted the lora-device-map branch October 31, 2024 15:47
@yiyixuxu yiyixuxu restored the lora-device-map branch October 31, 2024 17:59
yiyixuxu added a commit that referenced this pull request Oct 31, 2024
yiyixuxu added a commit that referenced this pull request Oct 31, 2024
#9823)

Revert "[LoRA] fix: lora loading when using with a device_mapped model. (#9449)"

This reverts commit 41e4779.
@yiyixuxu yiyixuxu deleted the lora-device-map branch October 31, 2024 18:20
a-r-r-o-w pushed a commit that referenced this pull request Nov 1, 2024
* fix: lora loading when using with a device_mapped model.

* better attibutung

* empty

Co-authored-by: Benjamin Bossan <[email protected]>

* Apply suggestions from code review

Co-authored-by: Marc Sun <[email protected]>

* minors

* better error messages.

* fix-copies

* add: tests, docs.

* add hardware note.

* quality

* Update docs/source/en/training/distributed_inference.md

Co-authored-by: Steven Liu <[email protected]>

* fixes

* skip properly.

* fixes

---------

Co-authored-by: Benjamin Bossan <[email protected]>
Co-authored-by: Marc Sun <[email protected]>
Co-authored-by: Steven Liu <[email protected]>
a-r-r-o-w pushed a commit that referenced this pull request Nov 1, 2024
#9823)

Revert "[LoRA] fix: lora loading when using with a device_mapped model. (#9449)"

This reverts commit 41e4779.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants