mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-08-21 03:50:04 +08:00
Make EasyCache not crash with Cosmos Predict ImagToVideo latents, but does not work well at all
This commit is contained in:
parent
31821a90a4
commit
4bc3e217d6
@ -27,6 +27,8 @@ from torchvision import transforms
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import comfy.patcher_extension
|
||||||
|
|
||||||
from .blocks import (
|
from .blocks import (
|
||||||
FinalLayer,
|
FinalLayer,
|
||||||
GeneralDITTransformerBlock,
|
GeneralDITTransformerBlock,
|
||||||
@ -435,6 +437,42 @@ class GeneralDIT(nn.Module):
|
|||||||
latent_condition_sigma: Optional[torch.Tensor] = None,
|
latent_condition_sigma: Optional[torch.Tensor] = None,
|
||||||
condition_video_augment_sigma: Optional[torch.Tensor] = None,
|
condition_video_augment_sigma: Optional[torch.Tensor] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
):
|
||||||
|
return comfy.patcher_extension.WrapperExecutor.new_class_executor(
|
||||||
|
self._forward,
|
||||||
|
self,
|
||||||
|
comfy.patcher_extension.get_all_wrappers(comfy.patcher_extension.WrappersMP.DIFFUSION_MODEL, kwargs.get("transformer_options", {}))
|
||||||
|
).execute(x,
|
||||||
|
timesteps,
|
||||||
|
context,
|
||||||
|
attention_mask,
|
||||||
|
fps,
|
||||||
|
image_size,
|
||||||
|
padding_mask,
|
||||||
|
scalar_feature,
|
||||||
|
data_type,
|
||||||
|
latent_condition,
|
||||||
|
latent_condition_sigma,
|
||||||
|
condition_video_augment_sigma,
|
||||||
|
**kwargs)
|
||||||
|
|
||||||
|
def _forward(
|
||||||
|
self,
|
||||||
|
x: torch.Tensor,
|
||||||
|
timesteps: torch.Tensor,
|
||||||
|
context: torch.Tensor,
|
||||||
|
attention_mask: Optional[torch.Tensor] = None,
|
||||||
|
# crossattn_emb: torch.Tensor,
|
||||||
|
# crossattn_mask: Optional[torch.Tensor] = None,
|
||||||
|
fps: Optional[torch.Tensor] = None,
|
||||||
|
image_size: Optional[torch.Tensor] = None,
|
||||||
|
padding_mask: Optional[torch.Tensor] = None,
|
||||||
|
scalar_feature: Optional[torch.Tensor] = None,
|
||||||
|
data_type: Optional[DataType] = DataType.VIDEO,
|
||||||
|
latent_condition: Optional[torch.Tensor] = None,
|
||||||
|
latent_condition_sigma: Optional[torch.Tensor] = None,
|
||||||
|
condition_video_augment_sigma: Optional[torch.Tensor] = None,
|
||||||
|
**kwargs,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
@ -185,6 +185,8 @@ class EasyCacheHolder:
|
|||||||
self.output_change_rates = []
|
self.output_change_rates = []
|
||||||
self.approx_output_change_rates = []
|
self.approx_output_change_rates = []
|
||||||
self.total_steps_skipped = 0
|
self.total_steps_skipped = 0
|
||||||
|
# how to deal with mismatched dims
|
||||||
|
self.cut_from_start = True
|
||||||
|
|
||||||
def is_past_end_timestep(self, timestep: float) -> bool:
|
def is_past_end_timestep(self, timestep: float) -> bool:
|
||||||
return not (timestep[0] > self.end_t).item()
|
return not (timestep[0] > self.end_t).item()
|
||||||
@ -227,10 +229,39 @@ class EasyCacheHolder:
|
|||||||
self.total_steps_skipped += 1
|
self.total_steps_skipped += 1
|
||||||
batch_offset = x.shape[0] // len(uuids)
|
batch_offset = x.shape[0] // len(uuids)
|
||||||
for i, uuid in enumerate(uuids):
|
for i, uuid in enumerate(uuids):
|
||||||
x[i*batch_offset:(i+1)*batch_offset, ...] += self.uuid_cache_diffs[uuid].to(x.device)
|
# if cached dims don't match x dims, cut off excess and hope for the best (cosmos world2video)
|
||||||
|
if x.shape != self.uuid_cache_diffs[uuid].shape:
|
||||||
|
slicing = []
|
||||||
|
skip_this_dim = True
|
||||||
|
for dim_u, dim_x in zip(self.uuid_cache_diffs[uuid].shape, x.shape):
|
||||||
|
if skip_this_dim:
|
||||||
|
skip_this_dim = False
|
||||||
|
continue
|
||||||
|
if dim_u != dim_x:
|
||||||
|
if self.cut_from_start:
|
||||||
|
slicing.append(slice(dim_x-dim_u, None))
|
||||||
|
else:
|
||||||
|
slicing.append(slice(None, dim_u))
|
||||||
|
else:
|
||||||
|
slicing.append(slice(None))
|
||||||
|
slicing = [slice(i*batch_offset,(i+1)*batch_offset)] + slicing
|
||||||
|
x = x[slicing]
|
||||||
|
x += self.uuid_cache_diffs[uuid].to(x.device)
|
||||||
return x
|
return x
|
||||||
|
|
||||||
def update_cache_diff(self, output: torch.Tensor, x: torch.Tensor, uuids: list[UUID]):
|
def update_cache_diff(self, output: torch.Tensor, x: torch.Tensor, uuids: list[UUID]):
|
||||||
|
# if output dims don't match x dims, cut off excess and hope for the best (cosmos world2video)
|
||||||
|
if output.shape != x.shape:
|
||||||
|
slicing = []
|
||||||
|
for dim_o, dim_x in zip(output.shape, x.shape):
|
||||||
|
if dim_o != dim_x:
|
||||||
|
if self.cut_from_start:
|
||||||
|
slicing.append(slice(dim_x-dim_o, None))
|
||||||
|
else:
|
||||||
|
slicing.append(slice(None, dim_o))
|
||||||
|
else:
|
||||||
|
slicing.append(slice(None))
|
||||||
|
x = x[slicing]
|
||||||
diff = output - x
|
diff = output - x
|
||||||
batch_offset = diff.shape[0] // len(uuids)
|
batch_offset = diff.shape[0] // len(uuids)
|
||||||
for i, uuid in enumerate(uuids):
|
for i, uuid in enumerate(uuids):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user