-
Notifications
You must be signed in to change notification settings - Fork 119
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
Re-network the DIT, fix some parameters, and simplify the model networking code #632
Merged
nemonameless
merged 28 commits into
PaddlePaddle:develop
from
chang-wenbin:DIT_PaddleMIX_729
Aug 28, 2024
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
59f23a0
modified the dit
chang-wenbin 5fee64b
add zkk_facebook
chang-wenbin f653a66
update zkk_facebook_dit.py
chang-wenbin 3b29d9d
update transformer_2d
chang-wenbin a88caea
update dit optimize
chang-wenbin 54eeec2
update transformer_2d
chang-wenbin 28a62c0
rename facebook_dit
chang-wenbin 884e29a
merge PR
chang-wenbin 15d08b6
merge from develop
chang-wenbin 7d49c49
Fixed the original dynamic image bug
chang-wenbin b03aa8e
update triton op import paddlemix
chang-wenbin cb86d17
update dit
chang-wenbin dc0c45c
update transformer_2d & simplified_facebook_dit
chang-wenbin 42f61bc
update demo & implified_facebook_dit & transformer_2d
chang-wenbin 000dd80
update Inference_Optimize
chang-wenbin 9bb9cde
update demo & simplified_facebook_dit
chang-wenbin d3de838
update demo
chang-wenbin 400ab19
update demo simplified_facebook_dit transformer_2d
chang-wenbin bfe8c41
update demo transformer_2d & simplified_facebook_dit
chang-wenbin 8896057
test
chang-wenbin e9aa47d
add format
chang-wenbin c8916f7
add format
chang-wenbin a87f81b
add Argument to the demo
chang-wenbin 0a09bf2
update Argument to the demo
chang-wenbin 10e8c1f
Merge remote-tracking branch 'upstream/develop' into DIT_PaddleMIX_729
chang-wenbin 10953b5
update transformer_2d
chang-wenbin 922d7d0
update DIT_demo
chang-wenbin c4f8242
Merge branch 'develop' into DIT_PaddleMIX_729
nemonameless File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
ppdiffusers/ppdiffusers/models/simplified_facebook_dit.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import math | ||
import os | ||
|
||
import paddle | ||
import paddle.nn.functional as F | ||
from paddle import nn | ||
|
||
|
||
class SimplifiedFacebookDIT(nn.Layer): | ||
def __init__(self, num_layers: int, dim: int, num_attention_heads: int, attention_head_dim: int): | ||
super().__init__() | ||
self.num_layers = num_layers | ||
self.dim = dim | ||
self.heads_num = num_attention_heads | ||
self.head_dim = attention_head_dim | ||
self.timestep_embedder_in_channels = 256 | ||
self.timestep_embedder_time_embed_dim = 1152 | ||
self.timestep_embedder_time_embed_dim_out = self.timestep_embedder_time_embed_dim | ||
self.LabelEmbedding_num_classes = 1001 | ||
self.LabelEmbedding_num_hidden_size = 1152 | ||
|
||
self.fcs0 = nn.LayerList( | ||
[ | ||
nn.Linear(self.timestep_embedder_in_channels, self.timestep_embedder_time_embed_dim) | ||
for i in range(num_layers) | ||
] | ||
) | ||
|
||
self.fcs1 = nn.LayerList( | ||
[ | ||
nn.Linear(self.timestep_embedder_time_embed_dim, self.timestep_embedder_time_embed_dim_out) | ||
for i in range(num_layers) | ||
] | ||
) | ||
|
||
self.fcs2 = nn.LayerList( | ||
[ | ||
nn.Linear(self.timestep_embedder_time_embed_dim, 6 * self.timestep_embedder_time_embed_dim) | ||
for i in range(num_layers) | ||
] | ||
) | ||
|
||
self.embs = nn.LayerList( | ||
[ | ||
nn.Embedding(self.LabelEmbedding_num_classes, self.LabelEmbedding_num_hidden_size) | ||
for i in range(num_layers) | ||
] | ||
) | ||
|
||
self.q = nn.LayerList([nn.Linear(dim, dim) for i in range(num_layers)]) | ||
self.k = nn.LayerList([nn.Linear(dim, dim) for i in range(num_layers)]) | ||
self.v = nn.LayerList([nn.Linear(dim, dim) for i in range(num_layers)]) | ||
self.out_proj = nn.LayerList([nn.Linear(dim, dim) for i in range(num_layers)]) | ||
self.ffn1 = nn.LayerList([nn.Linear(dim, dim * 4) for i in range(num_layers)]) | ||
self.ffn2 = nn.LayerList([nn.Linear(dim * 4, dim) for i in range(num_layers)]) | ||
self.norm = nn.LayerNorm(1152, epsilon=1e-06, weight_attr=False, bias_attr=False) | ||
self.norm1 = nn.LayerNorm(1152, epsilon=1e-05, weight_attr=False, bias_attr=False) | ||
|
||
def forward(self, hidden_states, timesteps, class_labels): | ||
|
||
# below code are copied from PaddleMIX/ppdiffusers/ppdiffusers/models/embeddings.py | ||
num_channels = 256 | ||
max_period = 10000 | ||
downscale_freq_shift = 1 | ||
half_dim = num_channels // 2 | ||
exponent = -math.log(max_period) * paddle.arange(start=0, end=half_dim, dtype="float32") | ||
exponent = exponent / (half_dim - downscale_freq_shift) | ||
emb = paddle.exp(exponent) | ||
emb = timesteps[:, None].cast("float32") * emb[None, :] | ||
emb = paddle.concat([paddle.cos(emb), paddle.sin(emb)], axis=-1) | ||
common_emb = emb.cast(hidden_states.dtype) | ||
|
||
for i in range(self.num_layers): | ||
emb = self.fcs0[i](common_emb) | ||
emb = F.silu(emb) | ||
emb = self.fcs1[i](emb) | ||
emb = emb + self.embs[i](class_labels) | ||
emb = F.silu(emb) | ||
emb = self.fcs2[i](emb) | ||
shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = emb.chunk(6, axis=1) | ||
import paddlemix | ||
|
||
if os.getenv("INFERENCE_OPTIMIZE_TRITON"): | ||
norm_hidden_states = paddlemix.triton_ops.adaptive_layer_norm( | ||
hidden_states, scale_msa, shift_msa, epsilon=1e-06 | ||
) | ||
else: | ||
norm_hidden_states = self.norm( | ||
hidden_states, | ||
) | ||
norm_hidden_states = norm_hidden_states * (1 + scale_msa[:, None]) + shift_msa[:, None] | ||
|
||
q = self.q[i](norm_hidden_states).reshape([0, 0, self.heads_num, self.head_dim]) | ||
k = self.k[i](norm_hidden_states).reshape([0, 0, self.heads_num, self.head_dim]) | ||
v = self.v[i](norm_hidden_states).reshape([0, 0, self.heads_num, self.head_dim]) | ||
|
||
norm_hidden_states = F.scaled_dot_product_attention_(q, k, v, scale=self.head_dim**-0.5) | ||
norm_hidden_states = norm_hidden_states.reshape( | ||
[norm_hidden_states.shape[0], norm_hidden_states.shape[1], self.dim] | ||
) | ||
norm_hidden_states = self.out_proj[i](norm_hidden_states) | ||
if os.getenv("INFERENCE_OPTIMIZE_TRITON"): | ||
hidden_states, norm_hidden_states = paddlemix.triton_ops.fused_adaLN_scale_residual( | ||
hidden_states, norm_hidden_states, gate_msa, scale_mlp, shift_mlp, epsilon=1e-05 | ||
) | ||
else: | ||
hidden_states = hidden_states + norm_hidden_states * gate_msa.reshape( | ||
[norm_hidden_states.shape[0], 1, self.dim] | ||
) | ||
norm_hidden_states = self.norm1( | ||
hidden_states, | ||
) | ||
norm_hidden_states = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] | ||
|
||
norm_hidden_states = self.ffn1[i](norm_hidden_states) | ||
norm_hidden_states = F.gelu(norm_hidden_states, approximate=True) | ||
norm_hidden_states = self.ffn2[i](norm_hidden_states) | ||
|
||
hidden_states = hidden_states + norm_hidden_states * gate_mlp.reshape( | ||
[norm_hidden_states.shape[0], 1, self.dim] | ||
) | ||
|
||
return hidden_states |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
必须一定要简化这个模块吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
手工优化需要
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
手工优化需要对原动态图模型组网 做高性能精简重组,这一模块还将transformer循环中的冗余计算部分提出,减少了部分计算量。
感谢提供修改意见,辛苦!