MCPcopy Create free account
hub / github.com/Netflix/void-model / TeaCache

Class TeaCache

videox_fun/models/cache_utils.py:19–74  ·  view source on GitHub ↗

Timestep Embedding Aware Cache, a training-free caching approach that estimates and leverages the fluctuating differences among model outputs across timesteps, thereby accelerating the inference. Please refer to: 1. https://github.com/ali-vilab/TeaCache. 2. Liu, Feng, et al. "Ti

Source from the content-addressed store, hash-verified

17
18
19class TeaCache():
20 """
21 Timestep Embedding Aware Cache, a training-free caching approach that estimates and leverages
22 the fluctuating differences among model outputs across timesteps, thereby accelerating the inference.
23 Please refer to:
24 1. https://github.com/ali-vilab/TeaCache.
25 2. Liu, Feng, et al. "Timestep Embedding Tells: It's Time to Cache for Video Diffusion Model." arXiv preprint arXiv:2411.19108 (2024).
26 """
27 def __init__(
28 self,
29 coefficients: list[float],
30 num_steps: int,
31 rel_l1_thresh: float = 0.0,
32 num_skip_start_steps: int = 0,
33 offload: bool = True,
34 ):
35 if num_steps < 1:
36 raise ValueError(f"`num_steps` must be greater than 0 but is {num_steps}.")
37 if rel_l1_thresh < 0:
38 raise ValueError(f"`rel_l1_thresh` must be greater than or equal to 0 but is {rel_l1_thresh}.")
39 if num_skip_start_steps < 0 or num_skip_start_steps > num_steps:
40 raise ValueError(
41 "`num_skip_start_steps` must be great than or equal to 0 and "
42 f"less than or equal to `num_steps={num_steps}` but is {num_skip_start_steps}."
43 )
44 self.coefficients = coefficients
45 self.num_steps = num_steps
46 self.rel_l1_thresh = rel_l1_thresh
47 self.num_skip_start_steps = num_skip_start_steps
48 self.offload = offload
49 self.rescale_func = np.poly1d(self.coefficients)
50
51 self.cnt = 0
52 self.should_calc = True
53 self.accumulated_rel_l1_distance = 0
54 self.previous_modulated_input = None
55 # Some pipelines concatenate the unconditional and text guide in forward.
56 self.previous_residual = None
57 # Some pipelines perform forward propagation separately on the unconditional and text guide.
58 self.previous_residual_cond = None
59 self.previous_residual_uncond = None
60
61 @staticmethod
62 def compute_rel_l1_distance(prev: torch.Tensor, cur: torch.Tensor) -> torch.Tensor:
63 rel_l1_distance = (torch.abs(cur - prev).mean()) / torch.abs(prev).mean()
64
65 return rel_l1_distance.cpu().item()
66
67 def reset(self):
68 self.cnt = 0
69 self.should_calc = True
70 self.accumulated_rel_l1_distance = 0
71 self.previous_modulated_input = None
72 self.previous_residual = None
73 self.previous_residual_cond = None
74 self.previous_residual_uncond = None

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected