[diffusion] fix: fix video model sp when resolution is not specified (#15047)

Co-authored-by: Brain97 <Brain97@users.noreply.github.com>
This commit is contained in:
Mick
2025-12-15 20:25:43 +08:00
committed by GitHub
parent abd3e048f2
commit b098b1ae24
2 changed files with 29 additions and 15 deletions

View File

@@ -104,7 +104,8 @@ def shard_rotary_emb_for_sp(emb):
def maybe_unpad_latents(latents, batch):
# If SP padding was applied, remove extra tokens before reshaping
target_tokens = batch.raw_latent_shape[-1] * batch.raw_latent_shape[-2]
width, height = batch.raw_latent_shape[-1], batch.raw_latent_shape[-2]
target_tokens = width * height
if latents.shape[1] > target_tokens:
latents = latents[:, :target_tokens, :]
return latents
@@ -309,13 +310,23 @@ class PipelineConfig:
if latents.dim() != 5:
return latents, False
time_dim = latents.shape[2]
if time_dim > 0 and time_dim % sp_world_size == 0:
sharded_tensor = rearrange(
latents, "b c (n t) h w -> b c n t h w", n=sp_world_size
).contiguous()
sharded_tensor = sharded_tensor[:, :, rank_in_sp_group, :, :, :]
return sharded_tensor, True
return latents, False
# Pad to next multiple of SP degree if needed
if time_dim > 0 and time_dim % sp_world_size != 0:
pad_len = sp_world_size - (time_dim % sp_world_size)
pad = torch.zeros(
(*latents.shape[:2], pad_len, *latents.shape[3:]),
dtype=latents.dtype,
device=latents.device,
)
latents = torch.cat([latents, pad], dim=2)
assert latents.shape[2] % sp_world_size == 0
sharded_tensor = rearrange(
latents, "b c (n t) h w -> b c n t h w", n=sp_world_size
).contiguous()
sharded_tensor = sharded_tensor[:, :, rank_in_sp_group, :, :, :]
return sharded_tensor, True
def get_pos_prompt_embeds(self, batch):
return batch.prompt_embeds
@@ -636,6 +647,7 @@ class ImagePipelineConfig(PipelineConfig):
return sigmas
def shard_latents_for_sp(self, batch, latents):
# latents: [B, H * W, C]
sp_world_size, rank_in_sp_group = get_sp_world_size(), get_sp_parallel_rank()
seq_len = latents.shape[1]
@@ -648,8 +660,6 @@ class ImagePipelineConfig(PipelineConfig):
device=latents.device,
)
latents = torch.cat([latents, pad], dim=1)
# Record padding length for later unpad
batch.sp_seq_pad = int(getattr(batch, "sp_seq_pad", 0)) + pad_len
sharded_tensor = rearrange(
latents, "b (n s) d -> b n s d", n=sp_world_size

View File

@@ -293,12 +293,16 @@ class InputValidationStage(PipelineStage):
result = VerificationResult()
result.add_check("height", batch.height, V.positive_int)
result.add_check("width", batch.width, V.positive_int)
# Validate height and width
if batch.height % 8 != 0 or batch.width % 8 != 0:
raise ValueError(
f"Height and width must be divisible by 8 but are {batch.height} and {batch.width}."
)
# Validate height and width
def check_size(value: int, name: str):
if value % (8 * server_args.num_gpus) != 0:
raise ValueError(
f"{name} must be divisible by (8 x num_gpus) but {value} % (8 * {server_args.num_gpus}) != 0."
)
check_size(batch.height, "Height")
check_size(batch.width, "Width")
result.add_check("seeds", batch.seeds, V.list_not_empty)
result.add_check("generator", batch.generator, V.generator_or_list_generators)
return result