Skip to content

Unet fix #4769

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

Merged
merged 5 commits into from
Aug 26, 2023
Merged

Unet fix #4769

merged 5 commits into from
Aug 26, 2023

Conversation

canberk17
Copy link
Contributor

@canberk17 canberk17 commented Aug 24, 2023

What does this PR do?

I was doing the CustomDiffusion tutorial, and I started receiving an UnboundLocalError at the end of the training.

File "/content/diffusers/examples/custom_diffusion/train_custom_diffusion.py", line 1330, in <module>
    main(args)
  File "/content/diffusers/examples/custom_diffusion/train_custom_diffusion.py", line 1312, in main
    images=images,
UnboundLocalError: local variable 'images' referenced before assignment

The line (1312) is referring inside the conditional block if args.push_to_hub:. This error error occurs because the images variable is defined inside two nested conditional blocks (lines 1232 and 1290), but is later referenced without being assigned if certain conditions are not met.

Here's how images is defined:

line 1232

if accelerator.is_main_process:
    if args.validation_prompt is not None and global_step % args.validation_steps == 0:
        # ...

line 1290

if args.validation_prompt and args.num_validation_images > 0:
    images = [
        # ...
    ]

This issue arises if you try to push your model to the hub without, setting the num_validation_images, and it will lead to a "UnboundLocalError" when the code tries to access images without having defined it first.

To ensure that the images variable is always initialized, we can declare an empty list at the beginning of the code block where the main process is checked. This will prevent the UnboundLocalError when you reference images when pushing your model to the hub, and don't define the num_validation_images

Referring to these: #4704 #4707

Before submitting

Who can review?

@sayakpaul @patrickvonplaten

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!

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@sayakpaul
Copy link
Member

Will merge once the CI is green :-)

Thanks a lot for spotting out the bug and for your contribution.

@canberk17
Copy link
Contributor Author

Would you like me to apply the black code formatter, and push it again?

@sayakpaul
Copy link
Member

Once the CI is green, we can merge :-)

@patrickvonplaten patrickvonplaten merged commit 0d81e54 into huggingface:main Aug 26, 2023
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
* Optional images variable train_custom_diffusion.py

* Fixed train_custom_diffusion.py

* Revert accidental changes to unet_2d_condition.py

* "Format code with black"
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.

4 participants