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

Add Diffusion Policy for Reinforcement Learning #9824

Merged
merged 34 commits into from
Nov 2, 2024

Conversation

DorsaRoh
Copy link
Contributor

@DorsaRoh DorsaRoh commented Oct 31, 2024

What does this PR do?

Adds Diffusion Policy, a diffusion model to predict action sequences in reinforcement learning tasks, using the HuggingFace diffusers library.

Before submitting

Who can review?

@sayakpaul @yiyixuxu @DN6 @a-r-r-o-w


@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
Copy link
Member

It seems like we're putting in the content of some repository within diffusers. I think it might be better off as a standalone repository than an example here as we're not a training focused library.

@DorsaRoh
Copy link
Contributor Author

DorsaRoh commented Nov 1, 2024

It seems like we're putting in the content of some repository within diffusers. I think it might be better off as a standalone repository than an example here as we're not a training focused library.

@sayakpaul Thank you for the feedback! I have made the changes. Now, it includes only an inference example of using diffusers for diffusion policy

return action.transpose(1, 2) # [batch_size, sequence_length, action_dim]

if __name__ == "__main__":
policy = DiffusionPolicy()
Copy link
Member

Choose a reason for hiding this comment

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

Should we load any pre-trained model here?

Copy link
Contributor Author

@DorsaRoh DorsaRoh Nov 1, 2024

Choose a reason for hiding this comment

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

Thanks for the valuable thought!
Diffusion policies are frequently tailored to specific use cases, and incorporating pretrained weights into the inference example could highly limit its general applicability and confuse users working on different tasks. Although I have pretrained weights available for a specific task that I can add here, to maintain the example’s universality, I recommend initializing the model without loading them. This will allow users to train their own models or integrate relevant pretrained weights based on their own applications!

Copy link
Member

Choose a reason for hiding this comment

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

I beg to differ. I think if we can document it sufficiently it would make more sense to showcase this with a pre-trained model.

Copy link
Contributor Author

@DorsaRoh DorsaRoh Nov 1, 2024

Choose a reason for hiding this comment

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

Sounds good! I have made the changes. Now, the example loads from a pretrained model and contains comprehensive documentation

@DorsaRoh DorsaRoh requested a review from sayakpaul November 1, 2024 14:56
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! Left some further comments.

examples/reinforcement_learning/README.md Outdated Show resolved Hide resolved
examples/reinforcement_learning/diffusion_policy.py Outdated Show resolved Hide resolved
examples/reinforcement_learning/diffusion_policy.py Outdated Show resolved Hide resolved
@DorsaRoh DorsaRoh requested a review from sayakpaul November 1, 2024 15:15
from diffusers import DDPMScheduler, UNet1DModel


add_safe_globals(
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 it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After setting weights_only=True (from False), an error occurs for any pretrained model unless we use add_safe_globals to make custom or third-party methods available (since weights_only=True skips the configuration loading). I believe it is a preventative measure by HuggingFace for security reasons, because it explicitly states that if we use weights_only=False, we must trust the authors of the model

Copy link
Member

Choose a reason for hiding this comment

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

It is happening at torch.load() so, I don't think it has anything to do with Hugging Face. Which torch version are you using?

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 see, thank you - I am using 2.5.1

@DorsaRoh DorsaRoh requested a review from sayakpaul November 2, 2024 02:31
@DorsaRoh
Copy link
Contributor Author

DorsaRoh commented Nov 2, 2024

It appears the 1 failing check is unrelated to the changes in this PR and may be due to external factors. Do they need to be addressed?

@sayakpaul
Copy link
Member

Indeed. That is not merge-blocking. Thanks for the PR!

@sayakpaul sayakpaul merged commit c10f875 into huggingface:main Nov 2, 2024
7 of 8 checks passed
sayakpaul added a commit that referenced this pull request Dec 23, 2024
* enable cpu ability

* model creation + comprehensive testing

* training + tests

* all tests working

* remove unneeded files + clarify docs

* update train tests

* update readme.md

* remove data from gitignore

* undo cpu enabled option

* Update README.md

* update readme

* code quality fixes

* diffusion policy example

* update readme

* add pretrained model weights + doc

* add comment

* add documentation

* add docstrings

* update comments

* update readme

* fix code quality

* Update examples/reinforcement_learning/README.md

Co-authored-by: Sayak Paul <[email protected]>

* Update examples/reinforcement_learning/diffusion_policy.py

Co-authored-by: Sayak Paul <[email protected]>

* suggestions + safe globals for weights_only=True

* suggestions + safe weights loading

* fix code quality

* reformat file

---------

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