Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions comfy/latent_formats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import torch
import comfy.nested_tensor

class LatentFormat:
scale_factor = 1.0
Expand All @@ -17,6 +18,9 @@ def process_in(self, latent):
def process_out(self, latent):
return latent / self.scale_factor

def fix_empty_latent(self, latent):
return latent

class SD15(LatentFormat):
def __init__(self, scale_factor=0.18215):
self.scale_factor = scale_factor
Expand Down Expand Up @@ -606,6 +610,19 @@ class MiniMaxH3AV(MiniMaxH3Video):
# max channels across the two streams (video 24, audio 32) so per-stream slices keep both streams whole
latent_channels = 32

def fix_empty_latent(self, latent):
video_latent_channels = MiniMaxH3Video.latent_channels
audio_latent_channels = 32
audio_channels = 2
frames_per_token = (1, 4, 4, 4, 4)
audio_frame_rescale = 5.0 / 3.0

video = latent[:, :video_latent_channels].clone()
frame_count = sum(frames_per_token[i % len(frames_per_token)] for i in range(video.shape[2]))
audio_t = round(frame_count * audio_frame_rescale)
audio = latent.new_zeros((latent.shape[0], audio_latent_channels, audio_channels, audio_t))
return comfy.nested_tensor.NestedTensor((video, audio))

class HunyuanVideo(LatentFormat):
latent_channels = 16
latent_dimensions = 3
Expand Down
5 changes: 4 additions & 1 deletion comfy/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def prepare_empty_noise(latent_image):
def fix_empty_latent_channels(model, latent_image, downscale_ratio_spacial=None, downscale_ratio_temporal=None):
if latent_image.is_nested:
return latent_image
latent_format = model.get_model_object("latent_format") #Resize the empty latent image so it has the right number of channels
latent_format = model.get_model_object("latent_format")
is_empty = torch.count_nonzero(latent_image) == 0
if is_empty:
if latent_format.latent_channels != latent_image.shape[1]:
Expand All @@ -64,6 +64,9 @@ def fix_empty_latent_channels(model, latent_image, downscale_ratio_spacial=None,
new_t = max(1, round(latent_image.shape[2] * ratio))
latent_image = comfy.utils.repeat_to_batch_size(latent_image, new_t, dim=2)

if is_empty:
latent_image = latent_format.fix_empty_latent(latent_image)

return latent_image

def prepare_sampling(model, noise_shape, positive, negative, noise_mask):
Expand Down
Loading