MCPcopy Create free account
hub / github.com/OpenImagingLab/FlashVSR / update

Method update

diffsynth/pipelines/omnigen_image.py:74–118  ·  view source on GitHub ↗

Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`. Parameters: key_states (`torch.Tensor`): The new key states to cache. value_states (`torch.Tensor`): The new value states to cache.

(
        self,
        key_states: torch.Tensor, 
        value_states: torch.Tensor,
        layer_idx: int,
        cache_kwargs: Optional[Dict[str, Any]] = None,
    )

Source from the content-addressed store, hash-verified

72
73
74 def update(
75 self,
76 key_states: torch.Tensor,
77 value_states: torch.Tensor,
78 layer_idx: int,
79 cache_kwargs: Optional[Dict[str, Any]] = None,
80 ) -> Tuple[torch.Tensor, torch.Tensor]:
81 """
82 Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`.
83 Parameters:
84 key_states (`torch.Tensor`):
85 The new key states to cache.
86 value_states (`torch.Tensor`):
87 The new value states to cache.
88 layer_idx (`int`):
89 The index of the layer to cache the states for.
90 cache_kwargs (`Dict[str, Any]`, `optional`):
91 Additional arguments for the cache subclass. No additional arguments are used in `OffloadedCache`.
92 Return:
93 A tuple containing the updated key and value states.
94 """
95 # Update the cache
96 if len(self.key_cache) < layer_idx:
97 raise ValueError("OffloadedCache does not support model usage where layers are skipped. Use DynamicCache.")
98 elif len(self.key_cache) == layer_idx:
99 # only cache the states for condition tokens
100 key_states = key_states[..., :-(self.num_tokens_for_img+1), :]
101 value_states = value_states[..., :-(self.num_tokens_for_img+1), :]
102
103 # Update the number of seen tokens
104 if layer_idx == 0:
105 self._seen_tokens += key_states.shape[-2]
106
107 self.key_cache.append(key_states)
108 self.value_cache.append(value_states)
109 self.original_device.append(key_states.device)
110 if self.offload_kv_cache:
111 self.evict_previous_layer(layer_idx)
112 return self.key_cache[layer_idx], self.value_cache[layer_idx]
113 else:
114 # only cache the states for condition tokens
115 key_tensor, value_tensor = self[layer_idx]
116 k = torch.cat([key_tensor, key_states], dim=-2)
117 v = torch.cat([value_tensor, value_states], dim=-2)
118 return k, v
119
120
121

Callers

nothing calls this directly

Calls 1

evict_previous_layerMethod · 0.95

Tested by

no test coverage detected