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

Fixed several errors in StableDiffusion adapter conversion script #1281

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions examples/stable_diffusion/convert_sd_adapter_to_peft.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def peft_state_dict(self) -> Dict[str, torch.Tensor]:
if self.lora_A is None or self.lora_B is None:
raise ValueError("At least one of lora_A or lora_B is None, they must both be provided")
return {
f"base_model.model{self.peft_key}.lora_A.weight": self.lora_A,
f"base_model.model.{self.peft_key}.lora_A.weight": self.lora_A,
f"base_model.model.{self.peft_key}.lora_B.weight": self.lora_B,
}

Expand Down Expand Up @@ -483,6 +483,10 @@ def detect_adapter_type(keys: List[str]) -> PeftType:

# Process each model sequentially
for model, model_name in [(text_encoder, "text_encoder"), (unet, "unet")]:
# Skip model if no data was provided
if len(adapter_info[model_name]) == 0:
continue

config = construct_config_fn(adapter_info[model_name], decompose_factor=decompose_factor)

# Output warning for LoHa with use_effective_conv2d
Expand All @@ -497,7 +501,11 @@ def detect_adapter_type(keys: List[str]) -> PeftType:
)

model = get_peft_model(model, config)
set_peft_model_state_dict(model, combine_peft_state_dict(adapter_info[model_name]))
missing_keys, unexpected_keys = set_peft_model_state_dict(
model, combine_peft_state_dict(adapter_info[model_name])
)
if len(unexpected_keys) > 0:
raise ValueError(f"Unexpected keys {unexpected_keys} found during conversion")

if args.half:
model.to(torch.float16)
Expand Down
Loading