Skip to content

Commit

Permalink
[UNet2DConditionModel] add cross_attention_dim as an argument (apple#155
Browse files Browse the repository at this point in the history
)

add cross_attention_dim as an argument
  • Loading branch information
patil-suraj authored Aug 5, 2022
1 parent 616c3a4 commit c4a3b09
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/diffusers/models/unet_2d_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(
act_fn="silu",
norm_num_groups=32,
norm_eps=1e-5,
cross_attention_dim=1280,
attention_head_dim=8,
):
super().__init__()
Expand Down Expand Up @@ -64,6 +65,7 @@ def __init__(
add_downsample=not is_final_block,
resnet_eps=norm_eps,
resnet_act_fn=act_fn,
cross_attention_dim=cross_attention_dim,
attn_num_head_channels=attention_head_dim,
downsample_padding=downsample_padding,
)
Expand All @@ -77,6 +79,7 @@ def __init__(
resnet_act_fn=act_fn,
output_scale_factor=mid_block_scale_factor,
resnet_time_scale_shift="default",
cross_attention_dim=cross_attention_dim,
attn_num_head_channels=attention_head_dim,
resnet_groups=norm_num_groups,
)
Expand All @@ -101,6 +104,7 @@ def __init__(
add_upsample=not is_final_block,
resnet_eps=norm_eps,
resnet_act_fn=act_fn,
cross_attention_dim=cross_attention_dim,
attn_num_head_channels=attention_head_dim,
)
self.up_blocks.append(up_block)
Expand Down
8 changes: 8 additions & 0 deletions src/diffusers/models/unet_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def get_down_block(
resnet_eps,
resnet_act_fn,
attn_num_head_channels,
cross_attention_dim=None,
downsample_padding=None,
):
down_block_type = down_block_type[7:] if down_block_type.startswith("UNetRes") else down_block_type
Expand Down Expand Up @@ -58,6 +59,8 @@ def get_down_block(
attn_num_head_channels=attn_num_head_channels,
)
elif down_block_type == "CrossAttnDownBlock2D":
if cross_attention_dim is None:
raise ValueError("cross_attention_dim must be specified for CrossAttnUpBlock2D")
return CrossAttnDownBlock2D(
num_layers=num_layers,
in_channels=in_channels,
Expand All @@ -67,6 +70,7 @@ def get_down_block(
resnet_eps=resnet_eps,
resnet_act_fn=resnet_act_fn,
downsample_padding=downsample_padding,
cross_attention_dim=cross_attention_dim,
attn_num_head_channels=attn_num_head_channels,
)
elif down_block_type == "SkipDownBlock2D":
Expand Down Expand Up @@ -115,6 +119,7 @@ def get_up_block(
resnet_eps,
resnet_act_fn,
attn_num_head_channels,
cross_attention_dim=None,
):
up_block_type = up_block_type[7:] if up_block_type.startswith("UNetRes") else up_block_type
if up_block_type == "UpBlock2D":
Expand All @@ -129,6 +134,8 @@ def get_up_block(
resnet_act_fn=resnet_act_fn,
)
elif up_block_type == "CrossAttnUpBlock2D":
if cross_attention_dim is None:
raise ValueError("cross_attention_dim must be specified for CrossAttnUpBlock2D")
return CrossAttnUpBlock2D(
num_layers=num_layers,
in_channels=in_channels,
Expand All @@ -138,6 +145,7 @@ def get_up_block(
add_upsample=add_upsample,
resnet_eps=resnet_eps,
resnet_act_fn=resnet_act_fn,
cross_attention_dim=cross_attention_dim,
attn_num_head_channels=attn_num_head_channels,
)
elif up_block_type == "AttnUpBlock2D":
Expand Down

0 comments on commit c4a3b09

Please sign in to comment.