Skip to content

Open-Sora: Democratizing Efficient Video Production for All

Python

fix #188

Zangwei Zhengf9ef774360b6
diff --git a/configs/opensora/inference/16x256x256.py b/configs/opensora/inference/16x256x256.py
index db6f2e4..7679e0b 100644
--- a/configs/opensora/inference/16x256x256.py
+++ b/configs/opensora/inference/16x256x256.py
@@ -25,6 +25,7 @@ scheduler = dict(
type="iddpm",
num_sampling_steps=100,
cfg_scale=7.0,
+ cfg_channel=3, # or None
)
dtype = "fp16"
diff --git a/docs/structure.md b/docs/structure.md
index 3eb7c69..9978c33 100644
--- a/docs/structure.md
+++ b/docs/structure.md
@@ -107,6 +107,7 @@ scheduler = dict(
type="iddpm", # Select scheduler type (iddpm, dpm-solver)
num_sampling_steps=100, # Number of sampling steps
cfg_scale=7.0, # hyper-parameter for classifier-free diffusion
+ cfg_channel=3, # how many channels to use for classifier-free diffusion, if None, use all channels
)
dtype = "fp16" # Computation type (fp16, fp32, bf16)
diff --git a/opensora/schedulers/iddpm/__init__.py b/opensora/schedulers/iddpm/__init__.py
index b9806ad..cb2d19b 100644
--- a/opensora/schedulers/iddpm/__init__.py
+++ b/opensora/schedulers/iddpm/__init__.py
@@ -82,13 +82,15 @@ class IDDPM(SpacedDiffusion):
return samples
-def forward_with_cfg(model, x, timestep, y, cfg_scale, **kwargs):
+def forward_with_cfg(model, x, timestep, y, cfg_scale, cfg_channel=None, **kwargs):
# https://github.com/openai/glide-text2im/blob/main/notebooks/text2im.ipynb
half = x[: len(x) // 2]
combined = torch.cat([half, half], dim=0)
model_out = model.forward(combined, timestep, y, **kwargs)
model_out = model_out["x"] if isinstance(model_out, dict) else model_out
- eps, rest = model_out[:, :3], model_out[:, 3:]
+ if cfg_channel is None:
+ cfg_channel = model_out.shape[1] // 2
+ eps, rest = model_out[:, :cfg_channel], model_out[:, cfg_channel:]
cond_eps, uncond_eps = torch.split(eps, len(eps) // 2, dim=0)
half_eps = uncond_eps + cfg_scale * (cond_eps - uncond_eps)
eps = torch.cat([half_eps, half_eps], dim=0)