Skip to content

[Type Hint] Unet Models #330

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 2 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/diffusers/models/unet_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(
attention_head_dim=8,
norm_num_groups=32,
norm_eps=1e-5,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
norm_eps=1e-5,
norm_eps: float =1e-5,

Copy link
Contributor

Choose a reason for hiding this comment

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

Hello

From issue #287, I see that type hints are required for all the variables. So would you be adding the type hints for other variables as well? Or is this PR out of scope for all of them?

Here are some examples:

flip_sin_to_cos: bool=True,
down_block_types: tuple[str]=("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D"),
block_out_channels: tuple[int]=(224, 448, 672, 896),
act_fn: str="silu"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the examples @vishnu-anirudh and @patrickvonplaten - I added type hints for other __init__ variables as well.

):
) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice None is the correct return type here according to https://stackoverflow.com/questions/46779046/correct-type-annotation-for-init (no @anton-l ?)

@sidthekidder, could you maybe also give all the init values some type hints? I've shown one example above :-)

Copy link
Member

Choose a reason for hiding this comment

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

@patrickvonplaten yeah, but it has become more of a soft requirement for __init__, as it can only return None. E.g. in mypy: python/mypy#604
So no worries if we don't annotate them :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah agree here think it's also cleaner to not have a -> None type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, no return annotation for the constructor is cleaner - removed None!

super().__init__()

self.sample_size = sample_size
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/models/unet_2d_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(
norm_eps=1e-5,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
norm_eps=1e-5,
norm_eps: float =1e-5,

Could you give all those values some type hints as well? :-)

cross_attention_dim=1280,
attention_head_dim=8,
):
) -> None:
super().__init__()

self.sample_size = sample_size
Expand Down