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 efficient_unet and update others #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/diffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
Transformer2DModel,
UNet1DModel,
UNet2DConditionModel,
EfficientUNet2DConditionModel,
UNet2DModel,
UNet3DConditionModel,
VQModel,
Expand Down
1 change: 1 addition & 0 deletions src/diffusers/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .unet_1d import UNet1DModel
from .unet_2d import UNet2DModel
from .unet_2d_condition import UNet2DConditionModel
from .unet_2d_condition_efficient import EfficientUNet2DConditionModel
from .unet_3d_condition import UNet3DConditionModel
from .vq_model import VQModel

Expand Down
12 changes: 7 additions & 5 deletions src/diffusers/models/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ def __init__(self, num_heads, embed_dim, dtype=None):
super().__init__()
self.dtype = dtype
self.positional_embedding = nn.Parameter(torch.randn(1, embed_dim) / embed_dim**0.5)
self.k_proj = nn.Linear(embed_dim, embed_dim, dtype=self.dtype)
self.q_proj = nn.Linear(embed_dim, embed_dim, dtype=self.dtype)
self.v_proj = nn.Linear(embed_dim, embed_dim, dtype=self.dtype)
self.k_proj = nn.Linear(embed_dim, embed_dim)
self.q_proj = nn.Linear(embed_dim, embed_dim)
self.v_proj = nn.Linear(embed_dim, embed_dim)
self.num_heads = num_heads
self.dim_per_head = embed_dim // self.num_heads

Expand Down Expand Up @@ -433,11 +433,13 @@ def shape(x):

# (bs*n_heads, class_token_length, length+class_token_length):
scale = 1 / math.sqrt(math.sqrt(self.dim_per_head))
weight = torch.einsum("bct,bcs->bts", q * scale, k * scale) # More stable with f16 than dividing afterwards
#weight = torch.einsum("bct,bcs->bts", q * scale, k * scale) # More stable with f16 than dividing afterwards
weight = torch.matmul((q * scale).transpose(1,2), k * scale) # More stable with f16 than dividing afterwards
weight = torch.softmax(weight.float(), dim=-1).type(weight.dtype)

# (bs*n_heads, dim_per_head, class_token_length)
a = torch.einsum("bts,bcs->bct", weight, v)
#a = torch.einsum("bts,bcs->bct", weight, v)
a = torch.matmul(weight, v.transpose(1,2)).transpose(1,2) # More stable with f16 than dividing afterwards

# (bs, length+1, width)
a = a.reshape(bs, -1, 1).transpose(1, 2)
Expand Down
2 changes: 2 additions & 0 deletions src/diffusers/models/transformer_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def forward(
self,
hidden_states,
encoder_hidden_states=None,
encoder_attention_mask=None,
timestep=None,
class_labels=None,
cross_attention_kwargs=None,
Expand Down Expand Up @@ -265,6 +266,7 @@ def forward(
hidden_states = block(
hidden_states,
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=encoder_attention_mask,
timestep=timestep,
cross_attention_kwargs=cross_attention_kwargs,
class_labels=class_labels,
Expand Down
Loading
Loading