-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathtransformer_wan.py
More file actions
640 lines (572 loc) · 22.7 KB
/
transformer_wan.py
File metadata and controls
640 lines (572 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
"""
Copyright 2025 Google LLC
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
https://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.
"""
from typing import Tuple, Optional, Dict, Union, Any
import contextlib
import math
import jax
import jax.numpy as jnp
from jax.sharding import PartitionSpec
from jax.ad_checkpoint import checkpoint_name
from flax import nnx
import flax.linen as nn
import numpy as np
from .... import common_types
from ...modeling_flax_utils import FlaxModelMixin, get_activation
from ....configuration_utils import ConfigMixin, register_to_config
from ...embeddings_flax import (
get_1d_rotary_pos_embed,
NNXFlaxTimesteps,
NNXTimestepEmbedding,
NNXPixArtAlphaTextProjection,
)
from ...normalization_flax import FP32LayerNorm
from ...attention_flax import FlaxWanAttention
from ...gradient_checkpoint import GradientCheckpointType
BlockSizes = common_types.BlockSizes
def get_frequencies(max_seq_len: int, theta: int, attention_head_dim: int):
h_dim = w_dim = 2 * (attention_head_dim // 6)
t_dim = attention_head_dim - h_dim - w_dim
freqs = []
for dim in [t_dim, h_dim, w_dim]:
freq = get_1d_rotary_pos_embed(dim, max_seq_len, theta, freqs_dtype=jnp.float32, use_real=False)
freqs.append(freq)
freqs = jnp.concatenate(freqs, axis=1)
t_size = attention_head_dim // 2 - 2 * (attention_head_dim // 6)
hw_size = attention_head_dim // 6
dims = [t_size, hw_size, hw_size]
# Calculate split indices as a static list of integers
cumulative_sizes = np.cumsum(dims)
split_indices = cumulative_sizes[:-1].tolist()
freqs_split = jnp.split(freqs, split_indices, axis=1)
return freqs_split
class WanRotaryPosEmbed(nnx.Module):
def __init__(self, attention_head_dim: int, patch_size: Tuple[int, int, int], max_seq_len: int, theta: float = 10000.0):
self.attention_head_dim = attention_head_dim
self.patch_size = patch_size
self.max_seq_len = max_seq_len
self.theta = theta
def __call__(self, hidden_states: jax.Array) -> jax.Array:
_, num_frames, height, width, _ = hidden_states.shape
p_t, p_h, p_w = self.patch_size
ppf, pph, ppw = num_frames // p_t, height // p_h, width // p_w
freqs_split = get_frequencies(self.max_seq_len, self.theta, self.attention_head_dim)
freqs_f = jnp.expand_dims(jnp.expand_dims(freqs_split[0][:ppf], axis=1), axis=1)
freqs_f = jnp.broadcast_to(freqs_f, (ppf, pph, ppw, freqs_split[0].shape[-1]))
freqs_h = jnp.expand_dims(jnp.expand_dims(freqs_split[1][:pph], axis=0), axis=2)
freqs_h = jnp.broadcast_to(freqs_h, (ppf, pph, ppw, freqs_split[1].shape[-1]))
freqs_w = jnp.expand_dims(jnp.expand_dims(freqs_split[2][:ppw], axis=0), axis=1)
freqs_w = jnp.broadcast_to(freqs_w, (ppf, pph, ppw, freqs_split[2].shape[-1]))
freqs_concat = jnp.concatenate([freqs_f, freqs_h, freqs_w], axis=-1)
freqs_final = jnp.reshape(freqs_concat, (1, 1, ppf * pph * ppw, -1))
return freqs_final
class WanTimeTextImageEmbedding(nnx.Module):
def __init__(
self,
rngs: nnx.Rngs,
dim: int,
time_freq_dim: int,
time_proj_dim: int,
text_embed_dim: int,
image_embed_dim: Optional[int] = None,
pos_embed_seq_len: Optional[int] = None,
dtype: jnp.dtype = jnp.float32,
weights_dtype: jnp.dtype = jnp.float32,
precision: jax.lax.Precision = None,
):
self.timesteps_proj = NNXFlaxTimesteps(dim=time_freq_dim, flip_sin_to_cos=True, freq_shift=0)
self.time_embedder = NNXTimestepEmbedding(
rngs=rngs,
in_channels=time_freq_dim,
time_embed_dim=dim,
dtype=dtype,
weights_dtype=weights_dtype,
precision=precision,
)
self.act_fn = get_activation("silu")
self.time_proj = nnx.Linear(
rngs=rngs,
in_features=dim,
out_features=time_proj_dim,
dtype=jnp.float32,
param_dtype=weights_dtype,
precision=precision,
kernel_init=nnx.with_partitioning(
nnx.initializers.xavier_uniform(),
(
"embed",
"mlp",
),
),
bias_init=nnx.with_partitioning(nnx.initializers.zeros, ("mlp",)),
)
self.text_embedder = NNXPixArtAlphaTextProjection(
rngs=rngs,
in_features=text_embed_dim,
hidden_size=dim,
act_fn="gelu_tanh",
)
def __call__(
self, timestep: jax.Array, encoder_hidden_states: jax.Array, encoder_hidden_states_image: Optional[jax.Array] = None
):
timestep = self.timesteps_proj(timestep)
temb = self.time_embedder(timestep)
timestep_proj = self.time_proj(self.act_fn(temb))
encoder_hidden_states = self.text_embedder(encoder_hidden_states)
if encoder_hidden_states_image is not None:
raise NotImplementedError("currently img2vid is not supported")
return temb, timestep_proj, encoder_hidden_states, encoder_hidden_states_image
class ApproximateGELU(nnx.Module):
r"""
The approximate form of the Gaussian Error Linear Unit (GELU). For more details, see section 2 of this
[paper](https://arxiv.org/abs/1606.08415).
"""
def __init__(
self,
rngs: nnx.Rngs,
dim_in: int,
dim_out: int,
bias: bool,
dtype: jnp.dtype = jnp.float32,
weights_dtype: jnp.dtype = jnp.float32,
precision: jax.lax.Precision = None,
):
self.proj = nnx.Linear(
rngs=rngs,
in_features=dim_in,
out_features=dim_out,
use_bias=bias,
dtype=dtype,
param_dtype=weights_dtype,
precision=precision,
kernel_init=nnx.with_partitioning(
nnx.initializers.xavier_uniform(),
(
"mlp",
"embed",
),
),
bias_init=nnx.with_partitioning(nnx.initializers.zeros, ("embed",)),
)
def __call__(self, x: jax.Array) -> jax.Array:
x = self.proj(x)
return nnx.gelu(x)
class WanFeedForward(nnx.Module):
def __init__(
self,
rngs: nnx.Rngs,
dim: int,
dim_out: Optional[int] = None,
mult: int = 4,
dropout: float = 0.0,
activation_fn: str = "geglu",
final_dropout: bool = False,
inner_dim: int = None,
bias: bool = True,
dtype: jnp.dtype = jnp.float32,
weights_dtype: jnp.dtype = jnp.float32,
precision: jax.lax.Precision = None,
enable_jax_named_scopes: bool = False,
):
if inner_dim is None:
inner_dim = int(dim * mult)
dim_out = dim_out if dim_out is not None else dim
self.enable_jax_named_scopes = enable_jax_named_scopes
self.act_fn = nnx.data(None)
if activation_fn == "gelu-approximate":
self.act_fn = ApproximateGELU(
rngs=rngs, dim_in=dim, dim_out=inner_dim, bias=bias, dtype=dtype, weights_dtype=weights_dtype, precision=precision
)
else:
raise NotImplementedError(f"{activation_fn} is not implemented.")
self.drop_out = nnx.Dropout(dropout)
self.proj_out = nnx.Linear(
rngs=rngs,
in_features=inner_dim,
out_features=dim_out,
use_bias=bias,
dtype=dtype,
param_dtype=weights_dtype,
precision=precision,
kernel_init=nnx.with_partitioning(
nnx.initializers.xavier_uniform(),
(
"embed",
"mlp",
),
),
)
def conditional_named_scope(self, name: str):
"""Return a JAX named scope if enabled, otherwise a null context."""
return jax.named_scope(name) if self.enable_jax_named_scopes else contextlib.nullcontext()
def __call__(self, hidden_states: jax.Array, deterministic: bool = True, rngs: nnx.Rngs = None) -> jax.Array:
with self.conditional_named_scope("mlp_up_proj_and_gelu"):
hidden_states = self.act_fn(hidden_states) # Output is (4, 75600, 13824)
hidden_states = checkpoint_name(hidden_states, "ffn_activation")
hidden_states = self.drop_out(hidden_states, deterministic=deterministic, rngs=rngs)
with self.conditional_named_scope("mlp_down_proj"):
return self.proj_out(hidden_states) # output is (4, 75600, 5120)
class WanTransformerBlock(nnx.Module):
def __init__(
self,
rngs: nnx.Rngs,
dim: int,
ffn_dim: int,
num_heads: int,
qk_norm: str = "rms_norm_across_heads",
cross_attn_norm: bool = False,
eps: float = 1e-6,
# In torch, this is none, so it can be ignored.
# added_kv_proj_dim: Optional[int] = None,
flash_min_seq_length: int = 4096,
flash_block_sizes: BlockSizes = None,
mesh: jax.sharding.Mesh = None,
dtype: jnp.dtype = jnp.float32,
weights_dtype: jnp.dtype = jnp.float32,
precision: jax.lax.Precision = None,
attention: str = "dot_product",
dropout: float = 0.0,
mask_padding_tokens: bool = True,
enable_jax_named_scopes: bool = False,
):
self.enable_jax_named_scopes = enable_jax_named_scopes
# 1. Self-attention
self.norm1 = FP32LayerNorm(rngs=rngs, dim=dim, eps=eps, elementwise_affine=False)
self.attn1 = FlaxWanAttention(
rngs=rngs,
query_dim=dim,
heads=num_heads,
dim_head=dim // num_heads,
qk_norm=qk_norm,
eps=eps,
flash_min_seq_length=flash_min_seq_length,
flash_block_sizes=flash_block_sizes,
mesh=mesh,
dtype=dtype,
weights_dtype=weights_dtype,
precision=precision,
attention_kernel=attention,
dropout=dropout,
is_self_attention=True,
mask_padding_tokens=mask_padding_tokens,
residual_checkpoint_name="self_attn",
enable_jax_named_scopes=enable_jax_named_scopes,
)
# 1. Cross-attention
self.attn2 = FlaxWanAttention(
rngs=rngs,
query_dim=dim,
heads=num_heads,
dim_head=dim // num_heads,
qk_norm=qk_norm,
eps=eps,
flash_min_seq_length=flash_min_seq_length,
flash_block_sizes=flash_block_sizes,
mesh=mesh,
dtype=dtype,
weights_dtype=weights_dtype,
precision=precision,
attention_kernel=attention,
dropout=dropout,
is_self_attention=False,
mask_padding_tokens=mask_padding_tokens,
residual_checkpoint_name="cross_attn",
enable_jax_named_scopes=enable_jax_named_scopes,
)
assert cross_attn_norm is True
self.norm2 = FP32LayerNorm(rngs=rngs, dim=dim, eps=eps, elementwise_affine=True)
# 3. Feed-forward
self.ffn = WanFeedForward(
rngs=rngs,
dim=dim,
inner_dim=ffn_dim,
activation_fn="gelu-approximate",
dtype=dtype,
weights_dtype=weights_dtype,
precision=precision,
dropout=dropout,
enable_jax_named_scopes=enable_jax_named_scopes,
)
self.norm3 = FP32LayerNorm(rngs=rngs, dim=dim, eps=eps, elementwise_affine=False)
key = rngs.params()
self.adaln_scale_shift_table = nnx.Param(
jax.random.normal(key, (1, 6, dim)) / dim**0.5,
)
def conditional_named_scope(self, name: str):
"""Return a JAX named scope if enabled, otherwise a null context."""
return jax.named_scope(name) if self.enable_jax_named_scopes else contextlib.nullcontext()
def __call__(
self,
hidden_states: jax.Array,
encoder_hidden_states: jax.Array,
temb: jax.Array,
rotary_emb: jax.Array,
deterministic: bool = True,
rngs: nnx.Rngs = None,
):
with self.conditional_named_scope("transformer_block"):
with self.conditional_named_scope("adaln"):
shift_msa, scale_msa, gate_msa, c_shift_msa, c_scale_msa, c_gate_msa = jnp.split(
(self.adaln_scale_shift_table + temb.astype(jnp.float32)), 6, axis=1
)
hidden_states = jax.lax.with_sharding_constraint(hidden_states, PartitionSpec("data", "fsdp", "tensor"))
hidden_states = checkpoint_name(hidden_states, "hidden_states")
encoder_hidden_states = jax.lax.with_sharding_constraint(encoder_hidden_states, PartitionSpec("data", "fsdp", None))
# 1. Self-attention
with self.conditional_named_scope("self_attn"):
with self.conditional_named_scope("self_attn_norm"):
norm_hidden_states = (self.norm1(hidden_states.astype(jnp.float32)) * (1 + scale_msa) + shift_msa).astype(
hidden_states.dtype
)
with self.conditional_named_scope("self_attn_attn"):
attn_output = self.attn1(
hidden_states=norm_hidden_states,
encoder_hidden_states=norm_hidden_states,
rotary_emb=rotary_emb,
deterministic=deterministic,
rngs=rngs,
)
with self.conditional_named_scope("self_attn_residual"):
hidden_states = (hidden_states.astype(jnp.float32) + attn_output * gate_msa).astype(hidden_states.dtype)
# 2. Cross-attention
with self.conditional_named_scope("cross_attn"):
with self.conditional_named_scope("cross_attn_norm"):
norm_hidden_states = self.norm2(hidden_states.astype(jnp.float32)).astype(hidden_states.dtype)
with self.conditional_named_scope("cross_attn_attn"):
attn_output = self.attn2(
hidden_states=norm_hidden_states,
encoder_hidden_states=encoder_hidden_states,
deterministic=deterministic,
rngs=rngs,
)
with self.conditional_named_scope("cross_attn_residual"):
hidden_states = hidden_states + attn_output
# 3. Feed-forward
with self.conditional_named_scope("mlp"):
with self.conditional_named_scope("mlp_norm"):
norm_hidden_states = (self.norm3(hidden_states.astype(jnp.float32)) * (1 + c_scale_msa) + c_shift_msa).astype(
hidden_states.dtype
)
with self.conditional_named_scope("mlp_ffn"):
ff_output = self.ffn(norm_hidden_states, deterministic=deterministic, rngs=rngs)
with self.conditional_named_scope("mlp_residual"):
hidden_states = (hidden_states.astype(jnp.float32) + ff_output.astype(jnp.float32) * c_gate_msa).astype(
hidden_states.dtype
)
return hidden_states
class WanModel(nnx.Module, FlaxModelMixin, ConfigMixin):
@register_to_config
def __init__(
self,
rngs: nnx.Rngs,
model_type="t2v",
patch_size: Tuple[int] = (1, 2, 2),
num_attention_heads: int = 40,
attention_head_dim: int = 128,
in_channels: int = 16,
out_channels: int = 16,
text_dim: int = 4096,
freq_dim: int = 256,
ffn_dim: int = 13824,
num_layers: int = 40,
dropout: float = 0.0,
cross_attn_norm: bool = True,
qk_norm: Optional[str] = "rms_norm_across_heads",
eps: float = 1e-6,
image_dim: Optional[int] = None,
added_kv_proj_dim: Optional[int] = None,
rope_max_seq_len: int = 1024,
pos_embed_seq_len: Optional[int] = None,
flash_min_seq_length: int = 4096,
flash_block_sizes: BlockSizes = None,
mesh: jax.sharding.Mesh = None,
dtype: jnp.dtype = jnp.float32,
weights_dtype: jnp.dtype = jnp.float32,
precision: jax.lax.Precision = None,
attention: str = "dot_product",
remat_policy: str = "None",
names_which_can_be_saved: list = [],
names_which_can_be_offloaded: list = [],
mask_padding_tokens: bool = True,
scan_layers: bool = True,
enable_jax_named_scopes: bool = False,
):
inner_dim = num_attention_heads * attention_head_dim
out_channels = out_channels or in_channels
self.num_layers = num_layers
self.scan_layers = scan_layers
self.enable_jax_named_scopes = enable_jax_named_scopes
# 1. Patch & position embedding
self.rope = WanRotaryPosEmbed(attention_head_dim, patch_size, rope_max_seq_len)
self.patch_embedding = nnx.Conv(
in_channels,
inner_dim,
rngs=rngs,
kernel_size=patch_size,
strides=patch_size,
dtype=dtype,
param_dtype=weights_dtype,
precision=precision,
kernel_init=nnx.with_partitioning(
nnx.initializers.xavier_uniform(),
(None, None, None, None, "conv_out"),
),
)
# 2. Condition embeddings
# image_embedding_dim=1280 for I2V model
self.condition_embedder = WanTimeTextImageEmbedding(
rngs=rngs,
dim=inner_dim,
time_freq_dim=freq_dim,
time_proj_dim=inner_dim * 6,
text_embed_dim=text_dim,
image_embed_dim=image_dim,
pos_embed_seq_len=pos_embed_seq_len,
)
# 3. Transformer blocks
@nnx.split_rngs(splits=num_layers)
@nnx.vmap(in_axes=0, out_axes=0, transform_metadata={nnx.PARTITION_NAME: "layers_per_stage"})
def init_block(rngs):
return WanTransformerBlock(
rngs=rngs,
dim=inner_dim,
ffn_dim=ffn_dim,
num_heads=num_attention_heads,
qk_norm=qk_norm,
cross_attn_norm=cross_attn_norm,
eps=eps,
flash_min_seq_length=flash_min_seq_length,
flash_block_sizes=flash_block_sizes,
mesh=mesh,
dtype=dtype,
weights_dtype=weights_dtype,
precision=precision,
attention=attention,
dropout=dropout,
mask_padding_tokens=mask_padding_tokens,
enable_jax_named_scopes=enable_jax_named_scopes,
)
self.gradient_checkpoint = GradientCheckpointType.from_str(remat_policy)
self.names_which_can_be_offloaded = names_which_can_be_offloaded
self.names_which_can_be_saved = names_which_can_be_saved
if scan_layers:
self.blocks = init_block(rngs)
else:
blocks = nnx.List([])
for _ in range(num_layers):
block = WanTransformerBlock(
rngs=rngs,
dim=inner_dim,
ffn_dim=ffn_dim,
num_heads=num_attention_heads,
qk_norm=qk_norm,
cross_attn_norm=cross_attn_norm,
eps=eps,
flash_min_seq_length=flash_min_seq_length,
flash_block_sizes=flash_block_sizes,
mesh=mesh,
dtype=dtype,
weights_dtype=weights_dtype,
precision=precision,
attention=attention,
enable_jax_named_scopes=enable_jax_named_scopes,
)
blocks.append(block)
self.blocks = blocks
self.norm_out = FP32LayerNorm(rngs=rngs, dim=inner_dim, eps=eps, elementwise_affine=False)
self.proj_out = nnx.Linear(
rngs=rngs,
in_features=inner_dim,
out_features=out_channels * math.prod(patch_size),
dtype=dtype,
param_dtype=weights_dtype,
precision=precision,
kernel_init=nnx.with_partitioning(nnx.initializers.xavier_uniform(), ("embed", None)),
)
key = rngs.params()
self.scale_shift_table = nnx.Param(
jax.random.normal(key, (1, 2, inner_dim)) / inner_dim**0.5,
kernel_init=nnx.with_partitioning(nnx.initializers.xavier_uniform(), (None, None, "embed")),
)
def conditional_named_scope(self, name: str):
"""Return a JAX named scope if enabled, otherwise a null context."""
return jax.named_scope(name) if self.enable_jax_named_scopes else contextlib.nullcontext()
def __call__(
self,
hidden_states: jax.Array,
timestep: jax.Array,
encoder_hidden_states: jax.Array,
encoder_hidden_states_image: Optional[jax.Array] = None,
return_dict: bool = True,
attention_kwargs: Optional[Dict[str, Any]] = None,
deterministic: bool = True,
rngs: nnx.Rngs = None,
) -> Union[jax.Array, Dict[str, jax.Array]]:
hidden_states = nn.with_logical_constraint(hidden_states, ("batch", None, None, None, None))
batch_size, _, num_frames, height, width = hidden_states.shape
p_t, p_h, p_w = self.config.patch_size
post_patch_num_frames = num_frames // p_t
post_patch_height = height // p_h
post_patch_width = width // p_w
hidden_states = jnp.transpose(hidden_states, (0, 2, 3, 4, 1))
with self.conditional_named_scope("rotary_embedding"):
rotary_emb = self.rope(hidden_states)
with self.conditional_named_scope("patch_embedding"):
hidden_states = self.patch_embedding(hidden_states)
hidden_states = jax.lax.collapse(hidden_states, 1, -1)
with self.conditional_named_scope("condition_embedder"):
temb, timestep_proj, encoder_hidden_states, encoder_hidden_states_image = self.condition_embedder(
timestep, encoder_hidden_states, encoder_hidden_states_image
)
timestep_proj = timestep_proj.reshape(timestep_proj.shape[0], 6, -1)
if encoder_hidden_states_image is not None:
raise NotImplementedError("img2vid is not yet implemented.")
if self.scan_layers:
def scan_fn(carry, block):
hidden_states_carry, rngs_carry = carry
hidden_states = block(
hidden_states_carry, encoder_hidden_states, timestep_proj, rotary_emb, deterministic, rngs_carry
)
new_carry = (hidden_states, rngs_carry)
return new_carry, None
rematted_block_forward = self.gradient_checkpoint.apply(
scan_fn, self.names_which_can_be_saved, self.names_which_can_be_offloaded, prevent_cse=not self.scan_layers
)
initial_carry = (hidden_states, rngs)
final_carry, _ = nnx.scan(
rematted_block_forward,
length=self.num_layers,
in_axes=(nnx.Carry, 0),
out_axes=(nnx.Carry, 0),
)(initial_carry, self.blocks)
hidden_states, _ = final_carry
else:
for block in self.blocks:
def layer_forward(hidden_states):
return block(hidden_states, encoder_hidden_states, timestep_proj, rotary_emb, deterministic, rngs)
rematted_layer_forward = self.gradient_checkpoint.apply(
layer_forward, self.names_which_can_be_saved, self.names_which_can_be_offloaded, prevent_cse=not self.scan_layers
)
hidden_states = rematted_layer_forward(hidden_states)
shift, scale = jnp.split(self.scale_shift_table + jnp.expand_dims(temb, axis=1), 2, axis=1)
with self.conditional_named_scope("output_norm"):
hidden_states = (self.norm_out(hidden_states.astype(jnp.float32)) * (1 + scale) + shift).astype(hidden_states.dtype)
with self.conditional_named_scope("output_proj"):
hidden_states = self.proj_out(hidden_states)
hidden_states = hidden_states.reshape(
batch_size, post_patch_num_frames, post_patch_height, post_patch_width, p_t, p_h, p_w, -1
)
hidden_states = jnp.transpose(hidden_states, (0, 7, 1, 4, 2, 5, 3, 6))
hidden_states = jax.lax.collapse(hidden_states, 6, None)
hidden_states = jax.lax.collapse(hidden_states, 4, 6)
hidden_states = jax.lax.collapse(hidden_states, 2, 4)
return hidden_states