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

Improve the performance and suitable for NPU computing #9642

Merged
merged 7 commits into from
Oct 14, 2024

Conversation

leisuzz
Copy link
Contributor

@leisuzz leisuzz commented Oct 11, 2024

What does this PR do?

Improve the performance (FPS) while training, and suitable for NPU computing.
Selection for free memory for CUDA or NPU

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.

@leisuzz leisuzz changed the title Improve the performance and suitable for NPU Improve the performance and suitable for NPU computing Oct 11, 2024
@leisuzz
Copy link
Contributor Author

leisuzz commented Oct 11, 2024

@sayakpaul Please refer to this one, thanks!

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! Just a couple comments.

@@ -540,7 +541,7 @@ def compute_vae_encodings(batch, vae):
with torch.no_grad():
model_input = vae.encode(pixel_values).latent_dist.sample()
model_input = model_input * vae.config.scaling_factor
return {"model_input": model_input.cpu()}
return {"model_input": accelerator.gather(model_input)}
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By using the accelerator, the communication time can be reduced

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the reason may caused by the pixel_values, as it is in vae.device (accelerator). Therefore, by changing the code, the accelerator can distribute and reduce the time cost.

Copy link
Member

Choose a reason for hiding this comment

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

But isn't an all-gather a more expensive op?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, I tested three different approaches, the first one is accelerator.gather(model_input), the average FPS is 29.13 with training duration 530; the second one is model_input.to(accelerator.device), the average FPS is 27.41 with training duration 544; the last one is the original model_input.cpu(), the average FPS is 28.56 with training duration 537. Overall, with same hardware, the FPS will increase a little with accelerator.gather. I tested multiple times with accelerator.gather and model_input.cpu(), the average FPS in accelerator.gather is larger than model_input.cpu().

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, thanks! Since the performance improvement seems to be minor, do you think it makes sense to not change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sense, I will change it back to the .cpu

Copy link
Member

Choose a reason for hiding this comment

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

But I welcome you to also add a note on your findings on accelerate.gather() so that users are aware. I think that'd still be quite valuable.

@@ -1091,8 +1095,7 @@ def compute_time_ids(original_size, crops_coords_top_left):
# Adapted from pipeline.StableDiffusionXLPipeline._get_add_time_ids
target_size = (args.resolution, args.resolution)
add_time_ids = list(original_size + crops_coords_top_left + target_size)
add_time_ids = torch.tensor([add_time_ids])
add_time_ids = add_time_ids.to(accelerator.device, dtype=weight_dtype)
add_time_ids = torch.tensor([add_time_ids], device=accelerator.device, dtype=weight_dtype)
Copy link
Member

Choose a reason for hiding this comment

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

Nice, this makes sense!

@leisuzz
Copy link
Contributor Author

leisuzz commented Oct 14, 2024

@sayakpaul I've changed the code based on your suggestions, thanks!

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!

@leisuzz
Copy link
Contributor Author

leisuzz commented Oct 14, 2024

@sayakpaul Is there anything I need to change to merge this PR? I saw there is something wrong with the 'Build PR Documentation', but I didn't change this section I think

@sayakpaul
Copy link
Member

Code quality check needs to pass.

@leisuzz
Copy link
Contributor Author

leisuzz commented Oct 14, 2024

@sayakpaul I couldn't see which line is needed to be changed to pass

@sayakpaul
Copy link
Member

Can you follow https://github.com/huggingface/diffusers/actions/runs/11319895166/job/31477993314?pr=9642 and follow the instructions from the logs?

@leisuzz
Copy link
Contributor Author

leisuzz commented Oct 14, 2024

@sayakpaul It didn't show the specific problem, but I figured it would be the formatting issue about my comment in line 544. I've changed it. Thanks

@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.

@sayakpaul sayakpaul merged commit 5956b68 into huggingface:main Oct 14, 2024
8 checks passed
@sayakpaul
Copy link
Member

Thanks for your contributions!

sayakpaul added a commit that referenced this pull request Dec 23, 2024
* Improve the performance and suitable for NPU

* Improve the performance and suitable for NPU computing

* Improve the performance and suitable for NPU

* Improve the performance and suitable for NPU

* Improve the performance and suitable for NPU

* Improve the performance and suitable for NPU

---------

Co-authored-by: 蒋硕 <[email protected]>
Co-authored-by: Sayak Paul <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants