Skip to content

support Wan/fp8 #145

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

Merged
merged 3 commits into from
Aug 15, 2025
Merged
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
14 changes: 13 additions & 1 deletion diffsynth_engine/pipelines/wan_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def __call__(
cfg_scale_ = cfg_scale if isinstance(cfg_scale, float) else cfg_scale[0]

timestep = timestep * mask[:, :, :, ::2, ::2].flatten() # seq_len
timestep = timestep.to(dtype=self.config.model_dtype, device=self.device)
timestep = timestep.to(dtype=self.dtype, device=self.device)
# Classifier-free guidance
noise_pred = self.predict_noise_with_cfg(
model=model,
Expand Down Expand Up @@ -574,6 +574,18 @@ def from_pretrained(cls, model_path_or_config: WanPipelineConfig) -> "WanVideoPi
if config.offload_mode is not None:
pipe.enable_cpu_offload(config.offload_mode)

if config.model_dtype == torch.float8_e4m3fn:
pipe.dtype = torch.bfloat16 # compute dtype
pipe.enable_fp8_autocast(
model_names=["dit"], compute_dtype=pipe.dtype, use_fp8_linear=config.use_fp8_linear
)

if config.t5_dtype == torch.float8_e4m3fn:
pipe.dtype = torch.bfloat16 # compute dtype
pipe.enable_fp8_autocast(
model_names=["text_encoder"], compute_dtype=pipe.dtype, use_fp8_linear=config.use_fp8_linear
)
Comment on lines +577 to +587
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for enabling fp8 autocast is duplicated for dit and text_encoder. This can be refactored to reduce code duplication and improve maintainability. You can collect the model names that require fp8 and then call enable_fp8_autocast once.

Suggested change
if config.model_dtype == torch.float8_e4m3fn:
pipe.dtype = torch.bfloat16 # compute dtype
pipe.enable_fp8_autocast(
model_names=["dit"], compute_dtype=pipe.dtype, use_fp8_linear=config.use_fp8_linear
)
if config.t5_dtype == torch.float8_e4m3fn:
pipe.dtype = torch.bfloat16 # compute dtype
pipe.enable_fp8_autocast(
model_names=["text_encoder"], compute_dtype=pipe.dtype, use_fp8_linear=config.use_fp8_linear
)
fp8_models = []
if config.model_dtype == torch.float8_e4m3fn:
fp8_models.append("dit")
if config.t5_dtype == torch.float8_e4m3fn:
fp8_models.append("text_encoder")
if fp8_models:
pipe.dtype = torch.bfloat16 # compute dtype
pipe.enable_fp8_autocast(
model_names=fp8_models, compute_dtype=pipe.dtype, use_fp8_linear=config.use_fp8_linear
)


if config.parallelism > 1:
return ParallelWrapper(
pipe,
Expand Down