Skip to content

Conversation

@AMohamedAakhil
Copy link
Contributor

@AMohamedAakhil AMohamedAakhil commented Mar 3, 2024

What does this PR do?

This PR adds the original IP Adapter training scripts, and also updates the documentation with instructions on how to use it.

Fixes # (issue)

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@AMohamedAakhil
Copy link
Contributor Author

#7194

@yiyixuxu yiyixuxu requested a review from sayakpaul March 4, 2024 01:31
@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.

Comment on lines +53 to +69
#### Usage Example:
```
accelerate launch --num_processes 8 --multi_gpu --mixed_precision "fp16" \
tutorial_train_ip-adapter.py \
--pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5/" \
--image_encoder_path="{image_encoder_path}" \
--data_json_file="{data.json}" \
--data_root_path="{image_path}" \
--mixed_precision="fp16" \
--resolution=512 \
--train_batch_size=8 \
--dataloader_num_workers=4 \
--learning_rate=1e-04 \
--weight_decay=0.01 \
--output_dir="{output_dir}" \
--save_steps=10000
```
Copy link
Member

Choose a reason for hiding this comment

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

Lets first provide a single-GPU example command and then proceed to multi-GPU.

import torch

# Load the trained model checkpoint
ckpt = "checkpoint-50000/pytorch_model.bin"
Copy link
Member

Choose a reason for hiding this comment

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

Let's default to serializing in .safetensors please as it's a more secure file-format.

- `map_location="cpu"`: Specifies that the model should be loaded onto the CPU.
- `image_proj_sd`: Dictionary to store the components related to image projection.
- `ip_sd`: Dictionary to store the components related to the IP adapter.
- `"unet"`, `"image_proj_model"`, `"adapter_modules"`: Prefixes indicating components of the model. No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

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

But we are not showing actual inference here no? I think we are just showing a part of the process. How to run inference with "ip_adapter.bin" is missing.

block_id = int(name[len("down_blocks.")])
hidden_size = unet.config.block_out_channels[block_id]
if cross_attention_dim is None:
attn_procs[name] = AttnProcessor()
Copy link
Member

Choose a reason for hiding this comment

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

We should use AttnProcessor2_0() when using PyTorch 2.0 as it's more memory and compute efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's imported AttnProcessor2_0 as AttnProcessor

"to_k_ip.weight": unet_sd[layer_name + ".to_k.weight"],
"to_v_ip.weight": unet_sd[layer_name + ".to_v.weight"],
}
attn_procs[name] = IPAttnProcessor(hidden_size=hidden_size, cross_attention_dim=cross_attention_dim, num_tokens=num_tokens)
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also imported IPAttnProcessor2_0 as IPAttnProcessor

Copy link
Member

Choose a reason for hiding this comment

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

Better would be to first check if it's using PyTorch 2. and then dynamically selecting it.

An example:

AttnProcessor2_0() if hasattr(F, "scaled_dot_product_attention") and self.scale_qk else AttnProcessor()

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

Thanks! I think the structure of the training scripts deviate quite a bit from how our official training scripts are written. For now, let's maybe put these under research_projects?

@AMohamedAakhil
Copy link
Contributor Author

Sure!

@AMohamedAakhil
Copy link
Contributor Author

AMohamedAakhil commented Mar 4, 2024

And I'll work on the suggested changes

Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
@AMohamedAakhil AMohamedAakhil mentioned this pull request Mar 8, 2024
@kadirnar
Copy link
Contributor

@AMohamedAakhil ,

Can you give information about the json file? Can you share a sample json file? How should I prepare a dataset?

@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot added the stale Issues that haven't received updates label Apr 20, 2024
@yiyixuxu yiyixuxu removed the stale Issues that haven't received updates label Apr 22, 2024
@sayakpaul
Copy link
Member

To start, maybe we could add this to research_projects?

@sayakpaul
Copy link
Member

@AMohamedAakhil we would very much like to add this to research_projects. Can we revive this PR?

@AMohamedAakhil
Copy link
Contributor Author

AMohamedAakhil commented Jul 26, 2024

Sure @sayakpaul

@sayakpaul
Copy link
Member

Thanks! Please let us know once done!

@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot added the stale Issues that haven't received updates label Sep 14, 2024
@yiyixuxu yiyixuxu removed stale Issues that haven't received updates labels Sep 17, 2024
@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot added the stale Issues that haven't received updates label Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Issues that haven't received updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants