Skip to content

SkyReels-V2: Infinite-length Film Generative model

Python

fix df image size adaptation && Multi-GPU teacache support

hao.zhangg0d4a9a929bdc
diff --git a/generate_video_df.py b/generate_video_df.py
index 7b73cd2..aa49782 100644
--- a/generate_video_df.py
+++ b/generate_video_df.py
@@ -11,6 +11,7 @@ from diffusers.utils import load_image
from skyreels_v2_infer import DiffusionForcingPipeline
from skyreels_v2_infer.modules import download_model
from skyreels_v2_infer.pipelines import PromptEnhancer
+from skyreels_v2_infer.pipelines import resizecrop
if __name__ == "__main__":
@@ -44,11 +45,13 @@ if __name__ == "__main__":
"--teacache_thresh",
type=float,
default=0.2,
- help="Higher speedup will cause to worse quality -- 0.1 for 2.0x speedup -- 0.2 for 3.0x speedup")
+ help="Higher speedup will cause to worse quality -- 0.1 for 2.0x speedup -- 0.2 for 3.0x speedup",
+ )
parser.add_argument(
"--use_ret_steps",
action="store_true",
- help="Using Retention Steps will result in faster generation speed and better generation quality.")
+ help="Using Retention Steps will result in faster generation speed and better generation quality.",
+ )
args = parser.parse_args()
args.model_id = download_model(args.model_id)
@@ -82,14 +85,22 @@ if __name__ == "__main__":
guidance_scale = args.guidance_scale
shift = args.shift
- image = load_image(args.image).convert("RGB") if args.image else None
+ if args.image:
+ args.image = load_image(args.image)
+ image_width, image_height = args.image.size
+ if image_height > image_width:
+ height, width = width, height
+ args.image = resizecrop(args.image, height, width)
+ image = args.image.convert("RGB") if args.image else None
negative_prompt = "色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走"
save_dir = os.path.join("result", args.outdir)
os.makedirs(save_dir, exist_ok=True)
local_rank = 0
if args.use_usp:
- assert not args.prompt_enhancer, "`--prompt_enhancer` is not allowed if using `--use_usp`. We recommend running the skyreels_v2_infer/pipelines/prompt_enhancer.py script first to generate enhanced prompt before enabling the `--use_usp` parameter."
+ assert (
+ not args.prompt_enhancer
+ ), "`--prompt_enhancer` is not allowed if using `--use_usp`. We recommend running the skyreels_v2_infer/pipelines/prompt_enhancer.py script first to generate enhanced prompt before enabling the `--use_usp` parameter."
from xfuser.core.distributed import initialize_model_parallel, init_distributed_environment
import torch.distributed as dist
@@ -127,16 +138,23 @@ if __name__ == "__main__":
if args.causal_attention:
pipe.transformer.set_ar_attention(args.causal_block_size)
-
+
if args.teacache:
if args.ar_step > 0:
- num_steps = args.inference_steps + (((args.base_num_frames - 1)//4 + 1) // args.causal_block_size - 1) * args.ar_step
- print('num_steps:', num_steps)
+ num_steps = (
+ args.inference_steps
+ + (((args.base_num_frames - 1) // 4 + 1) // args.causal_block_size - 1) * args.ar_step
+ )
+ print("num_steps:", num_steps)
else:
num_steps = args.inference_steps
- pipe.transformer.initialize_teacache(enable_teacache=True, num_steps=num_steps,
- teacache_thresh=args.teacache_thresh, use_ret_steps=args.use_ret_steps,
- ckpt_dir=args.model_id)
+ pipe.transformer.initialize_teacache(
+ enable_teacache=True,
+ num_steps=num_steps,
+ teacache_thresh=args.teacache_thresh,
+ use_ret_steps=args.use_ret_steps,
+ ckpt_dir=args.model_id,
+ )
print(f"prompt:{prompt_input}")
print(f"guidance_scale:{guidance_scale}")
diff --git a/skyreels_v2_infer/distributed/xdit_context_parallel.py b/skyreels_v2_infer/distributed/xdit_context_parallel.py
index f150f6f..560df92 100644
--- a/skyreels_v2_infer/distributed/xdit_context_parallel.py
+++ b/skyreels_v2_infer/distributed/xdit_context_parallel.py
@@ -1,3 +1,4 @@
+import numpy as np
import torch
import torch.amp as amp
from torch.backends.cuda import sdp_kernel
@@ -59,6 +60,17 @@ def rope_apply(x, grid_sizes, freqs):
return torch.stack(output).float()
+def broadcast_should_calc(should_calc: bool) -> bool:
+ import torch.distributed as dist
+
+ device = torch.cuda.current_device()
+ int_should_calc = 1 if should_calc else 0
+ tensor = torch.tensor([int_should_calc], device=device, dtype=torch.int8)
+ dist.broadcast(tensor, src=0)
+ should_calc = tensor.item() == 1
+ return should_calc
+
+
def usp_dit_forward(self, x, t, context, clip_fea=None, y=None, fps=None):
"""
x: A list of videos each with shape [C, T, H, W].
@@ -135,20 +147,84 @@ def usp_dit_forward(self, x, t, context, clip_fea=None, y=None, fps=None):
e0 = torch.chunk(e0, get_sequence_parallel_world_size(), dim=2)[get_sequence_parallel_rank()]
kwargs = dict(e=e0, grid_sizes=grid_sizes, freqs=self.freqs, context=context, block_mask=self.block_mask)
- # Context Parallel
- x = torch.chunk(x, get_sequence_parallel_world_size(), dim=1)[get_sequence_parallel_rank()]
+ if self.enable_teacache:
+ modulated_inp = e0 if self.use_ref_steps else e
+ # teacache
+ if self.cnt % 2 == 0: # even -> conditon
+ self.is_even = True
+ if self.cnt < self.ret_steps or self.cnt >= self.cutoff_steps:
+ should_calc_even = True
+ self.accumulated_rel_l1_distance_even = 0
+ else:
+ rescale_func = np.poly1d(self.coefficients)
+ self.accumulated_rel_l1_distance_even += rescale_func(
+ ((modulated_inp - self.previous_e0_even).abs().mean() / self.previous_e0_even.abs().mean())
+ .cpu()
+ .item()
+ )
+ if self.accumulated_rel_l1_distance_even < self.teacache_thresh:
+ should_calc_even = False
+ else:
+ should_calc_even = True
+ self.accumulated_rel_l1_distance_even = 0
+ self.previous_e0_even = modulated_inp.clone()
+ else: # odd -> unconditon
+ self.is_even = False
+ if self.cnt < self.ret_steps or self.cnt >= self.cutoff_steps:
+ should_calc_odd = True
+ self.accumulated_rel_l1_distance_odd = 0
+ else:
+ rescale_func = np.poly1d(self.coefficients)
+ self.accumulated_rel_l1_distance_odd += rescale_func(
+ ((modulated_inp - self.previous_e0_odd).abs().mean() / self.previous_e0_odd.abs().mean())
+ .cpu()
+ .item()
+ )
+ if self.accumulated_rel_l1_distance_odd < self.teacache_thresh:
+ should_calc_odd = False
+ else:
+ should_calc_odd = True
+ self.accumulated_rel_l1_distance_odd = 0
+ self.previous_e0_odd = modulated_inp.clone()
- for block in self.blocks:
- x = block(x, **kwargs)
+ x = torch.chunk(x, get_sequence_parallel_world_size(), dim=1)[get_sequence_parallel_rank()]
+ if self.enable_teacache:
+ if self.is_even:
+ should_calc_even = broadcast_should_calc(should_calc_even)
+ if not should_calc_even:
+ x += self.previous_residual_even
+ else:
+ ori_x = x.clone()
+ for block in self.blocks:
+ x = block(x, **kwargs)
+ ori_x.mul_(-1)
+ ori_x.add_(x)
+ self.previous_residual_even = ori_x
+ else:
+ should_calc_odd = broadcast_should_calc(should_calc_odd)
+ if not should_calc_odd:
+ x += self.previous_residual_odd
+ else:
+ ori_x = x.clone()
+ for block in self.blocks:
+ x = block(x, **kwargs)
+ ori_x.mul_(-1)
+ ori_x.add_(x)
+ self.previous_residual_odd = ori_x
+ self.cnt += 1
+ if self.cnt >= self.num_steps:
+ self.cnt = 0
+ else:
+ # Context Parallel
+ for block in self.blocks:
+ x = block(x, **kwargs)
# head
if e.ndim == 3:
e = torch.chunk(e, get_sequence_parallel_world_size(), dim=1)[get_sequence_parallel_rank()]
x = self.head(x, e)
-
# Context Parallel
x = get_sp_group().all_gather(x, dim=1)
-
# unpatchify
x = self.unpatchify(x, grid_sizes)
return x.float()