ai-edge-torch-nightly 0.1.dev202405131930__py3-none-any.whl → 0.2.0.dev20240601__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ai-edge-torch-nightly might be problematic. Click here for more details.
- ai_edge_torch/convert/fx_passes/build_aten_composite_pass.py +5 -2
- ai_edge_torch/convert/test/test_convert_composites.py +3 -0
- ai_edge_torch/generative/examples/stable_diffusion/__init__.py +14 -0
- ai_edge_torch/generative/examples/stable_diffusion/attention.py +106 -0
- ai_edge_torch/generative/examples/stable_diffusion/clip.py +79 -0
- ai_edge_torch/generative/examples/stable_diffusion/convert_to_tflite.py +107 -0
- ai_edge_torch/generative/examples/stable_diffusion/decoder.py +113 -0
- ai_edge_torch/generative/examples/stable_diffusion/diffusion.py +499 -0
- ai_edge_torch/generative/examples/stable_diffusion/encoder.py +67 -0
- ai_edge_torch/generative/examples/stable_diffusion/pipeline.py +222 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/__init__.py +19 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/k_euler.py +61 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/k_euler_ancestral.py +65 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/k_lms.py +73 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/sampler.py +38 -0
- ai_edge_torch/generative/examples/stable_diffusion/tokenizer.py +108 -0
- ai_edge_torch/generative/examples/stable_diffusion/util.py +71 -0
- ai_edge_torch/generative/test/loader_test.py +80 -0
- ai_edge_torch/generative/utilities/loader.py +8 -4
- {ai_edge_torch_nightly-0.1.dev202405131930.dist-info → ai_edge_torch_nightly-0.2.0.dev20240601.dist-info}/METADATA +2 -2
- {ai_edge_torch_nightly-0.1.dev202405131930.dist-info → ai_edge_torch_nightly-0.2.0.dev20240601.dist-info}/RECORD +24 -8
- {ai_edge_torch_nightly-0.1.dev202405131930.dist-info → ai_edge_torch_nightly-0.2.0.dev20240601.dist-info}/LICENSE +0 -0
- {ai_edge_torch_nightly-0.1.dev202405131930.dist-info → ai_edge_torch_nightly-0.2.0.dev20240601.dist-info}/WHEEL +0 -0
- {ai_edge_torch_nightly-0.1.dev202405131930.dist-info → ai_edge_torch_nightly-0.2.0.dev20240601.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
|
|
16
|
+
import torch
|
|
17
|
+
from torch import nn
|
|
18
|
+
from torch.nn import functional as F
|
|
19
|
+
|
|
20
|
+
from ai_edge_torch.generative.examples.stable_diffusion.attention import CrossAttention # NOQA
|
|
21
|
+
from ai_edge_torch.generative.examples.stable_diffusion.attention import SelfAttention # NOQA
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class TimeEmbedding(nn.Module):
|
|
25
|
+
|
|
26
|
+
def __init__(self, n_embd):
|
|
27
|
+
super().__init__()
|
|
28
|
+
self.linear_1 = nn.Linear(n_embd, 4 * n_embd)
|
|
29
|
+
self.linear_2 = nn.Linear(4 * n_embd, 4 * n_embd)
|
|
30
|
+
|
|
31
|
+
def forward(self, x):
|
|
32
|
+
x = self.linear_1(x)
|
|
33
|
+
x = F.silu(x)
|
|
34
|
+
x = self.linear_2(x)
|
|
35
|
+
return x
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class ResidualBlock(nn.Module):
|
|
39
|
+
|
|
40
|
+
def __init__(self, in_channels, out_channels, n_time=1280):
|
|
41
|
+
super().__init__()
|
|
42
|
+
self.groupnorm_feature = nn.GroupNorm(32, in_channels)
|
|
43
|
+
self.conv_feature = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)
|
|
44
|
+
self.linear_time = nn.Linear(n_time, out_channels)
|
|
45
|
+
|
|
46
|
+
self.groupnorm_merged = nn.GroupNorm(32, out_channels)
|
|
47
|
+
self.conv_merged = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)
|
|
48
|
+
|
|
49
|
+
if in_channels == out_channels:
|
|
50
|
+
self.residual_layer = nn.Identity()
|
|
51
|
+
else:
|
|
52
|
+
self.residual_layer = nn.Conv2d(
|
|
53
|
+
in_channels, out_channels, kernel_size=1, padding=0
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
def forward(self, feature, time):
|
|
57
|
+
residue = feature
|
|
58
|
+
|
|
59
|
+
feature = self.groupnorm_feature(feature)
|
|
60
|
+
feature = F.silu(feature)
|
|
61
|
+
feature = self.conv_feature(feature)
|
|
62
|
+
|
|
63
|
+
time = F.silu(time)
|
|
64
|
+
time = self.linear_time(time)
|
|
65
|
+
|
|
66
|
+
merged = feature + time.unsqueeze(-1).unsqueeze(-1)
|
|
67
|
+
merged = self.groupnorm_merged(merged)
|
|
68
|
+
merged = F.silu(merged)
|
|
69
|
+
merged = self.conv_merged(merged)
|
|
70
|
+
|
|
71
|
+
return merged + self.residual_layer(residue)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class AttentionBlock(nn.Module):
|
|
75
|
+
|
|
76
|
+
def __init__(self, n_head: int, n_embd: int, d_context=768):
|
|
77
|
+
super().__init__()
|
|
78
|
+
channels = n_head * n_embd
|
|
79
|
+
|
|
80
|
+
self.groupnorm = nn.GroupNorm(32, channels, eps=1e-6)
|
|
81
|
+
self.conv_input = nn.Conv2d(channels, channels, kernel_size=1, padding=0)
|
|
82
|
+
|
|
83
|
+
self.layernorm_1 = nn.LayerNorm(channels)
|
|
84
|
+
self.attention_1 = SelfAttention(n_head, channels, in_proj_bias=False)
|
|
85
|
+
self.layernorm_2 = nn.LayerNorm(channels)
|
|
86
|
+
self.attention_2 = CrossAttention(n_head, channels, d_context, in_proj_bias=False)
|
|
87
|
+
self.layernorm_3 = nn.LayerNorm(channels)
|
|
88
|
+
self.linear_geglu_1 = nn.Linear(channels, 4 * channels * 2)
|
|
89
|
+
self.linear_geglu_2 = nn.Linear(4 * channels, channels)
|
|
90
|
+
|
|
91
|
+
self.conv_output = nn.Conv2d(channels, channels, kernel_size=1, padding=0)
|
|
92
|
+
|
|
93
|
+
def forward(self, x, context):
|
|
94
|
+
residue_long = x
|
|
95
|
+
|
|
96
|
+
x = self.groupnorm(x)
|
|
97
|
+
x = self.conv_input(x)
|
|
98
|
+
|
|
99
|
+
n, c, h, w = x.shape
|
|
100
|
+
x = x.view((n, c, h * w)) # (n, c, hw)
|
|
101
|
+
x = x.transpose(-1, -2) # (n, hw, c)
|
|
102
|
+
|
|
103
|
+
residue_short = x
|
|
104
|
+
x = self.layernorm_1(x)
|
|
105
|
+
x = self.attention_1(x)
|
|
106
|
+
x += residue_short
|
|
107
|
+
|
|
108
|
+
residue_short = x
|
|
109
|
+
x = self.layernorm_2(x)
|
|
110
|
+
x = self.attention_2(x, context)
|
|
111
|
+
x += residue_short
|
|
112
|
+
|
|
113
|
+
residue_short = x
|
|
114
|
+
x = self.layernorm_3(x)
|
|
115
|
+
x, gate = self.linear_geglu_1(x).chunk(2, dim=-1)
|
|
116
|
+
x = x * F.gelu(gate)
|
|
117
|
+
x = self.linear_geglu_2(x)
|
|
118
|
+
x += residue_short
|
|
119
|
+
|
|
120
|
+
x = x.transpose(-1, -2) # (n, c, hw)
|
|
121
|
+
x = x.view((n, c, h, w)) # (n, c, h, w)
|
|
122
|
+
|
|
123
|
+
return self.conv_output(x) + residue_long
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class Upsample(nn.Module):
|
|
127
|
+
|
|
128
|
+
def __init__(self, channels):
|
|
129
|
+
super().__init__()
|
|
130
|
+
self.conv = nn.Conv2d(channels, channels, kernel_size=3, padding=1)
|
|
131
|
+
|
|
132
|
+
def forward(self, x):
|
|
133
|
+
x = F.interpolate(x, scale_factor=2, mode='nearest')
|
|
134
|
+
return self.conv(x)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class SwitchSequential(nn.Sequential):
|
|
138
|
+
|
|
139
|
+
def forward(self, x, context, time):
|
|
140
|
+
for layer in self:
|
|
141
|
+
if isinstance(layer, AttentionBlock):
|
|
142
|
+
x = layer(x, context)
|
|
143
|
+
elif isinstance(layer, ResidualBlock):
|
|
144
|
+
x = layer(x, time)
|
|
145
|
+
else:
|
|
146
|
+
x = layer(x)
|
|
147
|
+
return x
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class UNet(nn.Module):
|
|
151
|
+
|
|
152
|
+
def __init__(self):
|
|
153
|
+
super().__init__()
|
|
154
|
+
self.encoders = nn.ModuleList(
|
|
155
|
+
[
|
|
156
|
+
SwitchSequential(nn.Conv2d(4, 320, kernel_size=3, padding=1)),
|
|
157
|
+
SwitchSequential(ResidualBlock(320, 320), AttentionBlock(8, 40)),
|
|
158
|
+
SwitchSequential(ResidualBlock(320, 320), AttentionBlock(8, 40)),
|
|
159
|
+
SwitchSequential(nn.Conv2d(320, 320, kernel_size=3, stride=2, padding=1)),
|
|
160
|
+
SwitchSequential(ResidualBlock(320, 640), AttentionBlock(8, 80)),
|
|
161
|
+
SwitchSequential(ResidualBlock(640, 640), AttentionBlock(8, 80)),
|
|
162
|
+
SwitchSequential(nn.Conv2d(640, 640, kernel_size=3, stride=2, padding=1)),
|
|
163
|
+
SwitchSequential(ResidualBlock(640, 1280), AttentionBlock(8, 160)),
|
|
164
|
+
SwitchSequential(ResidualBlock(1280, 1280), AttentionBlock(8, 160)),
|
|
165
|
+
SwitchSequential(nn.Conv2d(1280, 1280, kernel_size=3, stride=2, padding=1)),
|
|
166
|
+
SwitchSequential(ResidualBlock(1280, 1280)),
|
|
167
|
+
SwitchSequential(ResidualBlock(1280, 1280)),
|
|
168
|
+
]
|
|
169
|
+
)
|
|
170
|
+
self.bottleneck = SwitchSequential(
|
|
171
|
+
ResidualBlock(1280, 1280),
|
|
172
|
+
AttentionBlock(8, 160),
|
|
173
|
+
ResidualBlock(1280, 1280),
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
self.decoders = nn.ModuleList(
|
|
177
|
+
[
|
|
178
|
+
SwitchSequential(ResidualBlock(2560, 1280)),
|
|
179
|
+
SwitchSequential(ResidualBlock(2560, 1280)),
|
|
180
|
+
SwitchSequential(ResidualBlock(2560, 1280), Upsample(1280)),
|
|
181
|
+
SwitchSequential(ResidualBlock(2560, 1280), AttentionBlock(8, 160)),
|
|
182
|
+
SwitchSequential(ResidualBlock(2560, 1280), AttentionBlock(8, 160)),
|
|
183
|
+
SwitchSequential(
|
|
184
|
+
ResidualBlock(1920, 1280), AttentionBlock(8, 160), Upsample(1280)
|
|
185
|
+
),
|
|
186
|
+
SwitchSequential(ResidualBlock(1920, 640), AttentionBlock(8, 80)),
|
|
187
|
+
SwitchSequential(ResidualBlock(1280, 640), AttentionBlock(8, 80)),
|
|
188
|
+
SwitchSequential(
|
|
189
|
+
ResidualBlock(960, 640), AttentionBlock(8, 80), Upsample(640)
|
|
190
|
+
),
|
|
191
|
+
SwitchSequential(ResidualBlock(960, 320), AttentionBlock(8, 40)),
|
|
192
|
+
SwitchSequential(ResidualBlock(640, 320), AttentionBlock(8, 40)),
|
|
193
|
+
SwitchSequential(ResidualBlock(640, 320), AttentionBlock(8, 40)),
|
|
194
|
+
]
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
def forward(self, x, context, time):
|
|
198
|
+
skip_connections = []
|
|
199
|
+
for layers in self.encoders:
|
|
200
|
+
x = layers(x, context, time)
|
|
201
|
+
skip_connections.append(x)
|
|
202
|
+
|
|
203
|
+
x = self.bottleneck(x, context, time)
|
|
204
|
+
|
|
205
|
+
# print('x shape:')
|
|
206
|
+
# print(list(x.shape))
|
|
207
|
+
# print('time shape:')
|
|
208
|
+
# print(list(time.shape))
|
|
209
|
+
|
|
210
|
+
for layers in self.decoders:
|
|
211
|
+
x = torch.cat((x, skip_connections.pop()), dim=1)
|
|
212
|
+
x = layers(x, context, time)
|
|
213
|
+
|
|
214
|
+
return x
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
# The encoder component.
|
|
218
|
+
class UNetEncoder(nn.Module):
|
|
219
|
+
|
|
220
|
+
def __init__(self):
|
|
221
|
+
super().__init__()
|
|
222
|
+
self.time_embedding = TimeEmbedding(320)
|
|
223
|
+
self.encoders = nn.ModuleList(
|
|
224
|
+
[
|
|
225
|
+
SwitchSequential(nn.Conv2d(4, 320, kernel_size=3, padding=1)),
|
|
226
|
+
SwitchSequential(ResidualBlock(320, 320), AttentionBlock(8, 40)),
|
|
227
|
+
SwitchSequential(ResidualBlock(320, 320), AttentionBlock(8, 40)),
|
|
228
|
+
SwitchSequential(nn.Conv2d(320, 320, kernel_size=3, stride=2, padding=1)),
|
|
229
|
+
SwitchSequential(ResidualBlock(320, 640), AttentionBlock(8, 80)),
|
|
230
|
+
SwitchSequential(ResidualBlock(640, 640), AttentionBlock(8, 80)),
|
|
231
|
+
SwitchSequential(nn.Conv2d(640, 640, kernel_size=3, stride=2, padding=1)),
|
|
232
|
+
SwitchSequential(ResidualBlock(640, 1280), AttentionBlock(8, 160)),
|
|
233
|
+
SwitchSequential(ResidualBlock(1280, 1280), AttentionBlock(8, 160)),
|
|
234
|
+
SwitchSequential(nn.Conv2d(1280, 1280, kernel_size=3, stride=2, padding=1)),
|
|
235
|
+
SwitchSequential(ResidualBlock(1280, 1280)),
|
|
236
|
+
SwitchSequential(ResidualBlock(1280, 1280)),
|
|
237
|
+
]
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
def forward(self, x, context, time):
|
|
241
|
+
time_embedding = self.time_embedding(time)
|
|
242
|
+
skip_connections = []
|
|
243
|
+
for layers in self.encoders:
|
|
244
|
+
x = layers(x, context, time_embedding)
|
|
245
|
+
skip_connections.append(x)
|
|
246
|
+
|
|
247
|
+
return x, skip_connections, time_embedding
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
class UNetBottleNeck(nn.Module):
|
|
251
|
+
|
|
252
|
+
def __init__(self):
|
|
253
|
+
super().__init__()
|
|
254
|
+
self.bottleneck = SwitchSequential(
|
|
255
|
+
ResidualBlock(1280, 1280),
|
|
256
|
+
AttentionBlock(8, 160),
|
|
257
|
+
ResidualBlock(1280, 1280),
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
def forward(self, x, context, time):
|
|
261
|
+
x = self.bottleneck(x, context, time)
|
|
262
|
+
# print('shape')
|
|
263
|
+
# print(list(x.shape))
|
|
264
|
+
return x
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
# Unet decoder.
|
|
268
|
+
class UNetDecoder1(nn.Module):
|
|
269
|
+
|
|
270
|
+
def __init__(self):
|
|
271
|
+
super().__init__()
|
|
272
|
+
self.decoders = nn.ModuleList(
|
|
273
|
+
[
|
|
274
|
+
SwitchSequential(ResidualBlock(2560, 1280)),
|
|
275
|
+
SwitchSequential(ResidualBlock(2560, 1280)),
|
|
276
|
+
SwitchSequential(ResidualBlock(2560, 1280), Upsample(1280)),
|
|
277
|
+
SwitchSequential(ResidualBlock(2560, 1280), AttentionBlock(8, 160)),
|
|
278
|
+
]
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
def forward(self, x, context, time, s9, s10, s11, s12):
|
|
282
|
+
x = torch.cat((x, s12), dim=1)
|
|
283
|
+
x = self.decoders[0](x, context, time)
|
|
284
|
+
x = torch.cat((x, s11), dim=1)
|
|
285
|
+
x = self.decoders[1](x, context, time)
|
|
286
|
+
x = torch.cat((x, s10), dim=1)
|
|
287
|
+
x = self.decoders[2](x, context, time)
|
|
288
|
+
x = torch.cat((x, s9), dim=1)
|
|
289
|
+
x = self.decoders[3](x, context, time)
|
|
290
|
+
|
|
291
|
+
return x
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
class UNetDecoder2(nn.Module):
|
|
295
|
+
|
|
296
|
+
def __init__(self):
|
|
297
|
+
super().__init__()
|
|
298
|
+
self.decoders = nn.ModuleList(
|
|
299
|
+
[
|
|
300
|
+
SwitchSequential(ResidualBlock(2560, 1280), AttentionBlock(8, 160)),
|
|
301
|
+
SwitchSequential(
|
|
302
|
+
ResidualBlock(1920, 1280), AttentionBlock(8, 160), Upsample(1280)
|
|
303
|
+
),
|
|
304
|
+
SwitchSequential(ResidualBlock(1920, 640), AttentionBlock(8, 80)),
|
|
305
|
+
SwitchSequential(ResidualBlock(1280, 640), AttentionBlock(8, 80)),
|
|
306
|
+
]
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
def forward(self, x, context, time, s5, s6, s7, s8):
|
|
310
|
+
x = torch.cat((x, s8), dim=1)
|
|
311
|
+
x = self.decoders[0](x, context, time)
|
|
312
|
+
x = torch.cat((x, s7), dim=1)
|
|
313
|
+
x = self.decoders[1](x, context, time)
|
|
314
|
+
x = torch.cat((x, s6), dim=1)
|
|
315
|
+
x = self.decoders[2](x, context, time)
|
|
316
|
+
x = torch.cat((x, s5), dim=1)
|
|
317
|
+
x = self.decoders[3](x, context, time)
|
|
318
|
+
return x
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
class UNetDecoder3(nn.Module):
|
|
322
|
+
|
|
323
|
+
def __init__(self):
|
|
324
|
+
super().__init__()
|
|
325
|
+
self.decoders = nn.ModuleList(
|
|
326
|
+
[
|
|
327
|
+
SwitchSequential(
|
|
328
|
+
ResidualBlock(960, 640), AttentionBlock(8, 80), Upsample(640)
|
|
329
|
+
),
|
|
330
|
+
SwitchSequential(ResidualBlock(960, 320), AttentionBlock(8, 40)),
|
|
331
|
+
SwitchSequential(ResidualBlock(640, 320), AttentionBlock(8, 40)),
|
|
332
|
+
SwitchSequential(ResidualBlock(640, 320), AttentionBlock(8, 40)),
|
|
333
|
+
]
|
|
334
|
+
)
|
|
335
|
+
self.final = FinalLayer(320, 4)
|
|
336
|
+
|
|
337
|
+
def forward(self, x, context, time, s1, s2, s3, s4):
|
|
338
|
+
x = torch.cat((x, s4), dim=1)
|
|
339
|
+
x = self.decoders[0](x, context, time)
|
|
340
|
+
x = torch.cat((x, s3), dim=1)
|
|
341
|
+
x = self.decoders[1](x, context, time)
|
|
342
|
+
x = torch.cat((x, s2), dim=1)
|
|
343
|
+
x = self.decoders[2](x, context, time)
|
|
344
|
+
x = torch.cat((x, s1), dim=1)
|
|
345
|
+
x = self.decoders[3](x, context, time)
|
|
346
|
+
|
|
347
|
+
x = self.final(x)
|
|
348
|
+
return x
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
class UNetDecoder(nn.Module):
|
|
352
|
+
|
|
353
|
+
def __init__(self):
|
|
354
|
+
super().__init__()
|
|
355
|
+
self.decoders = nn.ModuleList(
|
|
356
|
+
[
|
|
357
|
+
SwitchSequential(ResidualBlock(2560, 1280)),
|
|
358
|
+
SwitchSequential(ResidualBlock(2560, 1280)),
|
|
359
|
+
SwitchSequential(ResidualBlock(2560, 1280), Upsample(1280)),
|
|
360
|
+
SwitchSequential(ResidualBlock(2560, 1280), AttentionBlock(8, 160)),
|
|
361
|
+
SwitchSequential(ResidualBlock(2560, 1280), AttentionBlock(8, 160)),
|
|
362
|
+
SwitchSequential(
|
|
363
|
+
ResidualBlock(1920, 1280), AttentionBlock(8, 160), Upsample(1280)
|
|
364
|
+
),
|
|
365
|
+
SwitchSequential(ResidualBlock(1920, 640), AttentionBlock(8, 80)),
|
|
366
|
+
SwitchSequential(ResidualBlock(1280, 640), AttentionBlock(8, 80)),
|
|
367
|
+
SwitchSequential(
|
|
368
|
+
ResidualBlock(960, 640), AttentionBlock(8, 80), Upsample(640)
|
|
369
|
+
),
|
|
370
|
+
SwitchSequential(ResidualBlock(960, 320), AttentionBlock(8, 40)),
|
|
371
|
+
SwitchSequential(ResidualBlock(640, 320), AttentionBlock(8, 40)),
|
|
372
|
+
SwitchSequential(ResidualBlock(640, 320), AttentionBlock(8, 40)),
|
|
373
|
+
]
|
|
374
|
+
)
|
|
375
|
+
self.final = FinalLayer(320, 4)
|
|
376
|
+
|
|
377
|
+
def forward(
|
|
378
|
+
self, x, context, time, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12
|
|
379
|
+
):
|
|
380
|
+
x = torch.cat((x, s12), dim=1)
|
|
381
|
+
x = self.decoders[0](x, context, time)
|
|
382
|
+
x = torch.cat((x, s11), dim=1)
|
|
383
|
+
x = self.decoders[1](x, context, time)
|
|
384
|
+
x = torch.cat((x, s10), dim=1)
|
|
385
|
+
x = self.decoders[2](x, context, time)
|
|
386
|
+
x = torch.cat((x, s9), dim=1)
|
|
387
|
+
x = self.decoders[3](x, context, time)
|
|
388
|
+
x = torch.cat((x, s8), dim=1)
|
|
389
|
+
x = self.decoders[4](x, context, time)
|
|
390
|
+
x = torch.cat((x, s7), dim=1)
|
|
391
|
+
x = self.decoders[5](x, context, time)
|
|
392
|
+
x = torch.cat((x, s6), dim=1)
|
|
393
|
+
x = self.decoders[6](x, context, time)
|
|
394
|
+
x = torch.cat((x, s5), dim=1)
|
|
395
|
+
x = self.decoders[7](x, context, time)
|
|
396
|
+
x = torch.cat((x, s4), dim=1)
|
|
397
|
+
x = self.decoders[0](x, context, time)
|
|
398
|
+
x = torch.cat((x, s3), dim=1)
|
|
399
|
+
x = self.decoders[1](x, context, time)
|
|
400
|
+
x = torch.cat((x, s2), dim=1)
|
|
401
|
+
x = self.decoders[2](x, context, time)
|
|
402
|
+
x = torch.cat((x, s1), dim=1)
|
|
403
|
+
x = self.decoders[3](x, context, time)
|
|
404
|
+
|
|
405
|
+
x = self.final(x)
|
|
406
|
+
|
|
407
|
+
return x
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
class FinalLayer(nn.Module):
|
|
411
|
+
|
|
412
|
+
def __init__(self, in_channels, out_channels):
|
|
413
|
+
super().__init__()
|
|
414
|
+
self.groupnorm = nn.GroupNorm(32, in_channels)
|
|
415
|
+
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)
|
|
416
|
+
|
|
417
|
+
def forward(self, x):
|
|
418
|
+
x = self.groupnorm(x)
|
|
419
|
+
x = F.silu(x)
|
|
420
|
+
x = self.conv(x)
|
|
421
|
+
return x
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
class Diffusion(nn.Module):
|
|
425
|
+
|
|
426
|
+
def __init__(self):
|
|
427
|
+
super().__init__()
|
|
428
|
+
self.time_embedding = TimeEmbedding(320)
|
|
429
|
+
self.unet = UNet()
|
|
430
|
+
self.final = FinalLayer(320, 4)
|
|
431
|
+
|
|
432
|
+
@torch.inference_mode
|
|
433
|
+
def forward(self, latent, context, time):
|
|
434
|
+
time = self.time_embedding(time)
|
|
435
|
+
# print('time:')
|
|
436
|
+
# print(list(time.shape))
|
|
437
|
+
output = self.unet(latent, context, time)
|
|
438
|
+
output = self.final(output)
|
|
439
|
+
return output
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
# Calling code as if Diffusion is splitted into two parts.
|
|
443
|
+
class DiffusionSplitted(nn.Module):
|
|
444
|
+
|
|
445
|
+
def __init__(self):
|
|
446
|
+
super().__init__()
|
|
447
|
+
self.unet_encoder = UNetEncoder()
|
|
448
|
+
self.bottleneck = UNetBottleNeck()
|
|
449
|
+
self.unet_decoder1 = UNetDecoder1()
|
|
450
|
+
self.unet_decoder2 = UNetDecoder2()
|
|
451
|
+
self.unet_decoder3 = UNetDecoder3()
|
|
452
|
+
|
|
453
|
+
def get_skip_connections(self, latent, context, time):
|
|
454
|
+
_, skip_connections, _ = self.unet_encoder(latent, context, time)
|
|
455
|
+
return skip_connections
|
|
456
|
+
|
|
457
|
+
def forward(self, latent, context, time):
|
|
458
|
+
output, skip_connections, time = self.unet_encoder(latent, context, time)
|
|
459
|
+
# print("output shape of unet encoder...")
|
|
460
|
+
# print(list(output.shape))
|
|
461
|
+
# print("output shape of time...")
|
|
462
|
+
# print(list(time.shape))
|
|
463
|
+
output = self.bottleneck(output, context, time)
|
|
464
|
+
# print("output shape of bn")
|
|
465
|
+
# print(list(output.shape))
|
|
466
|
+
output = self.unet_decoder1(
|
|
467
|
+
output,
|
|
468
|
+
context,
|
|
469
|
+
time,
|
|
470
|
+
skip_connections[8],
|
|
471
|
+
skip_connections[9],
|
|
472
|
+
skip_connections[10],
|
|
473
|
+
skip_connections[11],
|
|
474
|
+
)
|
|
475
|
+
# print("output shape of d1:")
|
|
476
|
+
# print(list(output.shape))
|
|
477
|
+
|
|
478
|
+
output = self.unet_decoder2(
|
|
479
|
+
output,
|
|
480
|
+
context,
|
|
481
|
+
time,
|
|
482
|
+
skip_connections[4],
|
|
483
|
+
skip_connections[5],
|
|
484
|
+
skip_connections[6],
|
|
485
|
+
skip_connections[7],
|
|
486
|
+
)
|
|
487
|
+
|
|
488
|
+
# print("output shape of d2:")
|
|
489
|
+
# print(list(output.shape))
|
|
490
|
+
output = self.unet_decoder3(
|
|
491
|
+
output,
|
|
492
|
+
context,
|
|
493
|
+
time,
|
|
494
|
+
skip_connections[0],
|
|
495
|
+
skip_connections[1],
|
|
496
|
+
skip_connections[2],
|
|
497
|
+
skip_connections[3],
|
|
498
|
+
)
|
|
499
|
+
return output
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
|
|
16
|
+
import torch
|
|
17
|
+
from torch import nn
|
|
18
|
+
from torch.nn import functional as F
|
|
19
|
+
|
|
20
|
+
from ai_edge_torch.generative.examples.stable_diffusion.decoder import AttentionBlock # NOQA
|
|
21
|
+
from ai_edge_torch.generative.examples.stable_diffusion.decoder import ResidualBlock # NOQA
|
|
22
|
+
import ai_edge_torch.generative.utilities.loader as loading_utils
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Encoder(nn.Sequential):
|
|
26
|
+
|
|
27
|
+
def __init__(self):
|
|
28
|
+
super().__init__(
|
|
29
|
+
nn.Conv2d(3, 128, kernel_size=3, padding=1),
|
|
30
|
+
ResidualBlock(128, 128),
|
|
31
|
+
ResidualBlock(128, 128),
|
|
32
|
+
nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=0),
|
|
33
|
+
ResidualBlock(128, 256),
|
|
34
|
+
ResidualBlock(256, 256),
|
|
35
|
+
nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=0),
|
|
36
|
+
ResidualBlock(256, 512),
|
|
37
|
+
ResidualBlock(512, 512),
|
|
38
|
+
nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=0),
|
|
39
|
+
ResidualBlock(512, 512),
|
|
40
|
+
ResidualBlock(512, 512),
|
|
41
|
+
ResidualBlock(512, 512),
|
|
42
|
+
AttentionBlock(512),
|
|
43
|
+
ResidualBlock(512, 512),
|
|
44
|
+
nn.GroupNorm(32, 512),
|
|
45
|
+
nn.SiLU(),
|
|
46
|
+
nn.Conv2d(512, 8, kernel_size=3, padding=1),
|
|
47
|
+
nn.Conv2d(8, 8, kernel_size=1, padding=0),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
@torch.inference_mode
|
|
51
|
+
def forward(self, x, noise):
|
|
52
|
+
for module in self:
|
|
53
|
+
if getattr(module, 'stride', None) == (
|
|
54
|
+
2,
|
|
55
|
+
2,
|
|
56
|
+
): # Padding at downsampling should be asymmetric (see #8)
|
|
57
|
+
x = F.pad(x, (0, 1, 0, 1))
|
|
58
|
+
x = module(x)
|
|
59
|
+
|
|
60
|
+
mean, log_variance = torch.chunk(x, 2, dim=1)
|
|
61
|
+
log_variance = torch.clamp(log_variance, -30, 20)
|
|
62
|
+
variance = log_variance.exp()
|
|
63
|
+
stdev = variance.sqrt()
|
|
64
|
+
x = mean + stdev * noise
|
|
65
|
+
|
|
66
|
+
x *= 0.18215
|
|
67
|
+
return x
|