[Refactor] Extend CFG Parallel to support 3 or 4 branch dispatch across M GPUs (#2423)
This commit is contained in:
1 parent
ef230ac720
commit
16041ab550
8 files changed
+670
-122
No files matched your search
@@ -15,11 +15,6 @@ from tqdm import tqdm
|
||||
|
||||
from vllm_omni.diffusion.data import DiffusionOutput, OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.distributed.cfg_parallel import CFGParallelMixin
|
||||
from vllm_omni.diffusion.distributed.parallel_state import (
|
||||
get_cfg_group,
|
||||
get_classifier_free_guidance_rank,
|
||||
get_classifier_free_guidance_world_size,
|
||||
)
|
||||
from vllm_omni.diffusion.distributed.utils import get_local_device
|
||||
from vllm_omni.diffusion.models.interface import SupportAudioInput, SupportImageInput
|
||||
from vllm_omni.diffusion.request import OmniDiffusionRequest
|
||||
@@ -249,6 +244,28 @@ class DreamIDOmniPipeline(nn.Module, CFGParallelMixin, SupportImageInput, Suppor
|
||||
|
||||
return sample_scheduler, timesteps
|
||||
|
||||
def predict_noise(self, **kwargs):
|
||||
pred_vid, pred_audio = self.model(**kwargs)
|
||||
return (pred_vid[0], pred_audio[0])
|
||||
|
||||
def combine_multi_branch_cfg_noise(self, predictions, true_cfg_scale, cfg_normalize=False):
|
||||
vid_pos, audio_pos = predictions[0]
|
||||
vid_neg, audio_neg = predictions[1]
|
||||
vid_ip_neg, _ = predictions[2]
|
||||
_, refaudio_neg = predictions[3]
|
||||
|
||||
pred_video = (
|
||||
vid_neg
|
||||
+ true_cfg_scale["video_cfg_scale"] * (vid_pos - vid_neg)
|
||||
+ true_cfg_scale["video_ref_cfg_scale"] * (vid_pos - vid_ip_neg)
|
||||
)
|
||||
pred_audio = (
|
||||
audio_neg
|
||||
+ true_cfg_scale["audio_cfg_scale"] * (audio_pos - audio_neg)
|
||||
+ true_cfg_scale["audio_ref_cfg_scale"] * (audio_pos - refaudio_neg)
|
||||
)
|
||||
return (pred_video, pred_audio)
|
||||
|
||||
def diffuse(
|
||||
self,
|
||||
video_noise: torch.Tensor,
|
||||
@@ -306,72 +323,22 @@ class DreamIDOmniPipeline(nn.Module, CFGParallelMixin, SupportImageInput, Suppor
|
||||
"vid_context": [text_embeddings_video_neg],
|
||||
}
|
||||
|
||||
if get_classifier_free_guidance_world_size() > 1:
|
||||
# Enable CFG-parallel: rank0 computes positive, rank1 computes negative.
|
||||
cfg_group = get_cfg_group()
|
||||
cfg_rank = get_classifier_free_guidance_rank()
|
||||
branches_kwargs = [
|
||||
{"vid": [model_input_video], "audio": [model_input_audio], "t": timestep_input, **pos_args},
|
||||
{"vid": [model_input_video], "audio": [model_input_audio], "t": timestep_input, **neg_args},
|
||||
{"vid": [model_input_video_neg], "audio": [model_input_audio], "t": timestep_input, **pos_args},
|
||||
{"vid": [model_input_video], "audio": [model_input_audio_neg], "t": timestep_input, **pos_args},
|
||||
]
|
||||
|
||||
if cfg_rank == 0:
|
||||
pred_vid, pred_audio = self.model(
|
||||
vid=[model_input_video], audio=[model_input_audio], t=timestep_input, **pos_args
|
||||
)
|
||||
pre_vid_ip_neg, _ = self.model(
|
||||
vid=[model_input_video_neg], audio=[model_input_audio], t=timestep_input, **pos_args
|
||||
)
|
||||
pred_vid_0 = pred_vid[0]
|
||||
pred_audio_0 = pred_audio[0]
|
||||
pre_vid_ip_0 = pre_vid_ip_neg[0]
|
||||
pred_refaudio_0 = torch.zeros_like(pred_audio_0) # dummy tensor
|
||||
else:
|
||||
pred_vid, pred_audio = self.model(
|
||||
vid=[model_input_video], audio=[model_input_audio], t=timestep_input, **neg_args
|
||||
)
|
||||
_, pred_refaudio_neg = self.model(
|
||||
vid=[model_input_video], audio=[model_input_audio_neg], t=timestep_input, **pos_args
|
||||
)
|
||||
pred_vid_0 = pred_vid[0]
|
||||
pred_audio_0 = pred_audio[0]
|
||||
pre_vid_ip_0 = torch.zeros_like(pred_vid_0) # dummy tensor
|
||||
pred_refaudio_0 = pred_refaudio_neg[0]
|
||||
|
||||
pred_vid_gathered = cfg_group.all_gather(pred_vid_0, separate_tensors=True)
|
||||
pred_audio_gathered = cfg_group.all_gather(pred_audio_0, separate_tensors=True)
|
||||
pre_vid_ip_gathered = cfg_group.all_gather(pre_vid_ip_0, separate_tensors=True)
|
||||
pred_refaudio_gathered = cfg_group.all_gather(pred_refaudio_0, separate_tensors=True)
|
||||
|
||||
pred_vid_pos = [pred_vid_gathered[0]]
|
||||
pred_vid_neg = [pred_vid_gathered[1]]
|
||||
pred_audio_pos = [pred_audio_gathered[0]]
|
||||
pred_audio_neg = [pred_audio_gathered[1]]
|
||||
pre_vid_ip_neg = [pre_vid_ip_gathered[0]]
|
||||
pred_refaudio_neg = [pred_refaudio_gathered[1]]
|
||||
else:
|
||||
pred_vid_pos, pred_audio_pos = self.model(
|
||||
vid=[model_input_video], audio=[model_input_audio], t=timestep_input, **pos_args
|
||||
)
|
||||
|
||||
pred_vid_neg, pred_audio_neg = self.model(
|
||||
vid=[model_input_video], audio=[model_input_audio], t=timestep_input, **neg_args
|
||||
)
|
||||
|
||||
pre_vid_ip_neg, _ = self.model(
|
||||
vid=[model_input_video_neg], audio=[model_input_audio], t=timestep_input, **pos_args
|
||||
)
|
||||
|
||||
_, pred_refaudio_neg = self.model(
|
||||
vid=[model_input_video], audio=[model_input_audio_neg], t=timestep_input, **pos_args
|
||||
)
|
||||
|
||||
pred_video_guided = (
|
||||
pred_vid_neg[0]
|
||||
+ self.video_cfg_scale * (pred_vid_pos[0] - pred_vid_neg[0])
|
||||
+ self.video_ref_cfg_scale * (pred_vid_pos[0] - pre_vid_ip_neg[0])
|
||||
)
|
||||
|
||||
pred_audio_guided = (
|
||||
pred_audio_neg[0]
|
||||
+ self.audio_cfg_scale * (pred_audio_pos[0] - pred_audio_neg[0])
|
||||
+ self.audio_ref_cfg_scale * (pred_audio_pos[0] - pred_refaudio_neg[0])
|
||||
pred_video_guided, pred_audio_guided = self.predict_noise_with_multi_branch_cfg(
|
||||
do_true_cfg=True,
|
||||
true_cfg_scale={
|
||||
"video_cfg_scale": self.video_cfg_scale,
|
||||
"video_ref_cfg_scale": self.video_ref_cfg_scale,
|
||||
"audio_cfg_scale": self.audio_cfg_scale,
|
||||
"audio_ref_cfg_scale": self.audio_ref_cfg_scale,
|
||||
},
|
||||
branches_kwargs=branches_kwargs,
|
||||
)
|
||||
video_noise = scheduler_video.step(
|
||||
pred_video_guided.unsqueeze(0), t_v, video_noise.unsqueeze(0), return_dict=False
|
||||
|
||||
@@ -29,6 +29,7 @@ from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2_5_VLProcessor
|
||||
from vllm.model_executor.models.utils import AutoWeightsLoader
|
||||
|
||||
from vllm_omni.diffusion.data import DiffusionOutput, OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.distributed.cfg_parallel import CFGParallelMixin
|
||||
from vllm_omni.diffusion.distributed.utils import get_local_device
|
||||
from vllm_omni.diffusion.model_loader.diffusers_loader import DiffusersPipelineLoader
|
||||
from vllm_omni.diffusion.models.omnigen2.omnigen2_transformer import (
|
||||
@@ -619,7 +620,7 @@ def retrieve_timesteps(
|
||||
return timesteps, num_inference_steps
|
||||
|
||||
|
||||
class OmniGen2Pipeline(nn.Module):
|
||||
class OmniGen2Pipeline(CFGParallelMixin, nn.Module):
|
||||
"""
|
||||
Pipeline for text-to-image generation using OmniGen2.
|
||||
|
||||
@@ -1171,14 +1172,6 @@ class OmniGen2Pipeline(nn.Module):
|
||||
self._num_timesteps = len(timesteps)
|
||||
|
||||
for i, t in enumerate(timesteps):
|
||||
model_pred = self.predict(
|
||||
t=t,
|
||||
latents=latents,
|
||||
prompt_embeds=prompt_embeds,
|
||||
freqs_cis=freqs_cis,
|
||||
prompt_attention_mask=prompt_attention_mask,
|
||||
ref_image_hidden_states=ref_latents,
|
||||
)
|
||||
text_guidance_scale = (
|
||||
self.text_guidance_scale if self.cfg_range[0] <= i / len(timesteps) <= self.cfg_range[1] else 1.0
|
||||
)
|
||||
@@ -1186,8 +1179,26 @@ class OmniGen2Pipeline(nn.Module):
|
||||
self.image_guidance_scale if self.cfg_range[0] <= i / len(timesteps) <= self.cfg_range[1] else 1.0
|
||||
)
|
||||
|
||||
positive_kwargs = dict(
|
||||
t=t,
|
||||
latents=latents,
|
||||
prompt_embeds=prompt_embeds,
|
||||
freqs_cis=freqs_cis,
|
||||
prompt_attention_mask=prompt_attention_mask,
|
||||
ref_image_hidden_states=ref_latents,
|
||||
)
|
||||
uncond_kwargs = dict(
|
||||
t=t,
|
||||
latents=latents,
|
||||
prompt_embeds=negative_prompt_embeds,
|
||||
freqs_cis=freqs_cis,
|
||||
prompt_attention_mask=negative_prompt_attention_mask,
|
||||
ref_image_hidden_states=None,
|
||||
)
|
||||
|
||||
if text_guidance_scale > 1.0 and image_guidance_scale > 1.0:
|
||||
model_pred_ref = self.predict(
|
||||
# 3-branch CFG: pos + ref_neg + uncond
|
||||
ref_neg_kwargs = dict(
|
||||
t=t,
|
||||
latents=latents,
|
||||
prompt_embeds=negative_prompt_embeds,
|
||||
@@ -1195,31 +1206,24 @@ class OmniGen2Pipeline(nn.Module):
|
||||
prompt_attention_mask=negative_prompt_attention_mask,
|
||||
ref_image_hidden_states=ref_latents,
|
||||
)
|
||||
|
||||
model_pred_uncond = self.predict(
|
||||
t=t,
|
||||
latents=latents,
|
||||
prompt_embeds=negative_prompt_embeds,
|
||||
freqs_cis=freqs_cis,
|
||||
prompt_attention_mask=negative_prompt_attention_mask,
|
||||
ref_image_hidden_states=None,
|
||||
)
|
||||
|
||||
model_pred = (
|
||||
model_pred_uncond
|
||||
+ image_guidance_scale * (model_pred_ref - model_pred_uncond)
|
||||
+ text_guidance_scale * (model_pred - model_pred_ref)
|
||||
model_pred = self.predict_noise_with_multi_branch_cfg(
|
||||
do_true_cfg=True,
|
||||
true_cfg_scale={
|
||||
"text": text_guidance_scale,
|
||||
"image": image_guidance_scale,
|
||||
},
|
||||
branches_kwargs=[positive_kwargs, ref_neg_kwargs, uncond_kwargs],
|
||||
)
|
||||
elif text_guidance_scale > 1.0:
|
||||
model_pred_uncond = self.predict(
|
||||
t=t,
|
||||
latents=latents,
|
||||
prompt_embeds=negative_prompt_embeds,
|
||||
freqs_cis=freqs_cis,
|
||||
prompt_attention_mask=negative_prompt_attention_mask,
|
||||
ref_image_hidden_states=None,
|
||||
# 2-branch CFG: pos + uncond
|
||||
model_pred = self.predict_noise_with_multi_branch_cfg(
|
||||
do_true_cfg=True,
|
||||
true_cfg_scale=text_guidance_scale,
|
||||
branches_kwargs=[positive_kwargs, uncond_kwargs],
|
||||
)
|
||||
model_pred = model_pred_uncond + text_guidance_scale * (model_pred - model_pred_uncond)
|
||||
else:
|
||||
# No CFG
|
||||
model_pred = self.predict_noise(**positive_kwargs)
|
||||
|
||||
latents = self.scheduler.step(model_pred, t, latents, return_dict=False)[0]
|
||||
|
||||
@@ -1265,6 +1269,21 @@ class OmniGen2Pipeline(nn.Module):
|
||||
)
|
||||
return model_pred
|
||||
|
||||
def predict_noise(self, **kwargs):
|
||||
"""Override CFGParallelMixin.predict_noise to use self.predict."""
|
||||
return self.predict(**kwargs)
|
||||
|
||||
def combine_multi_branch_cfg_noise(self, predictions, true_cfg_scale, cfg_normalize=False):
|
||||
"""Override: 3-branch dual scale or 2-branch standard CFG."""
|
||||
if len(predictions) == 3:
|
||||
text_scale = true_cfg_scale["text"]
|
||||
image_scale = true_cfg_scale["image"]
|
||||
pos, ref, uncond = predictions[0], predictions[1], predictions[2]
|
||||
return uncond + image_scale * (ref - uncond) + text_scale * (pos - ref)
|
||||
# 2-branch: standard CFG
|
||||
pos, neg = predictions[0], predictions[1]
|
||||
return neg + true_cfg_scale * (pos - neg)
|
||||
|
||||
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
|
||||
loader = AutoWeightsLoader(self)
|
||||
return loader.load_weights(weights)
|
||||
Reference in new issue
Block a user