forked from dirac-run/dirac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode_refactor_DynamicCache
More file actions
226 lines (203 loc) · 11.5 KB
/
opencode_refactor_DynamicCache
File metadata and controls
226 lines (203 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
diff --git a/src/transformers/cache_utils.py b/src/transformers/cache_utils.py
index ac324ebb62..ccb2aa867b 100644
--- a/src/transformers/cache_utils.py
+++ b/src/transformers/cache_utils.py
@@ -1263,6 +1263,7 @@ class DynamicCache(Cache):
)
else:
super().__init__(layers=layers, offloading=offloading, offload_only_non_sliding=offload_only_non_sliding)
+ self.is_stale = False
def __iter__(self):
for layer in self.layers:
diff --git a/src/transformers/models/cohere2/modular_cohere2.py b/src/transformers/models/cohere2/modular_cohere2.py
index d19055a1b7..f790e87dfd 100644
--- a/src/transformers/models/cohere2/modular_cohere2.py
+++ b/src/transformers/models/cohere2/modular_cohere2.py
@@ -194,7 +194,15 @@ class Cohere2Attention(CohereAttention):
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
if past_key_values is not None:
- key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
+ if isinstance(past_key_values, DynamicCache) and past_key_values.is_stale:
+ import warnings
+
+ warnings.warn(
+ "The provided `past_key_values` is marked as stale. Bypassing the internal cache update.",
+ UserWarning,
+ )
+ else:
+ key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface(
self.config._attn_implementation, eager_attention_forward
diff --git a/src/transformers/models/deepseek_v3/modular_deepseek_v3.py b/src/transformers/models/deepseek_v3/modular_deepseek_v3.py
index 3c62a564a3..03307ff24f 100644
--- a/src/transformers/models/deepseek_v3/modular_deepseek_v3.py
+++ b/src/transformers/models/deepseek_v3/modular_deepseek_v3.py
@@ -6,7 +6,7 @@ import torch.nn.functional as F
from torch import nn
from ... import initialization as init
-from ...cache_utils import Cache
+from ...cache_utils import Cache, DynamicCache
from ...modeling_flash_attention_utils import FlashAttentionKwargs
from ...modeling_layers import GenericForSequenceClassification, GenericForTokenClassification
from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel
@@ -256,7 +256,15 @@ class DeepseekV3Attention(nn.Module):
key_states = torch.cat((k_pass, k_rot), dim=-1)
if past_key_values is not None:
- key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
+ if isinstance(past_key_values, DynamicCache) and past_key_values.is_stale:
+ import warnings
+
+ warnings.warn(
+ "The provided `past_key_values` is marked as stale. Bypassing the internal cache update.",
+ UserWarning,
+ )
+ else:
+ key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
if is_flash_attention_requested(self.config) and self.qk_head_dim != self.v_head_dim:
value_states = F.pad(value_states, [0, self.qk_head_dim - self.v_head_dim])
diff --git a/src/transformers/models/gemma4/modular_gemma4.py b/src/transformers/models/gemma4/modular_gemma4.py
index a972738022..1965fd31cd 100644
--- a/src/transformers/models/gemma4/modular_gemma4.py
+++ b/src/transformers/models/gemma4/modular_gemma4.py
@@ -985,12 +985,20 @@ class Gemma4TextAttention(nn.Module):
value_states = value_states.transpose(1, 2)
if past_key_values is not None:
- if not self.is_kv_shared_layer:
- key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
- if self.store_full_length_kv:
- if not hasattr(past_key_values, "shared_layers"):
- past_key_values.shared_layers = {}
- past_key_values.shared_layers[self.layer_idx] = key_states, value_states
+ if isinstance(past_key_values, DynamicCache) and past_key_values.is_stale:
+ import warnings
+
+ warnings.warn(
+ "The provided `past_key_values` is marked as stale. Bypassing the internal cache update.",
+ UserWarning,
+ )
+ else:
+ if not self.is_kv_shared_layer:
+ key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
+ if self.store_full_length_kv:
+ if not hasattr(past_key_values, "shared_layers"):
+ past_key_values.shared_layers = {}
+ past_key_values.shared_layers[self.layer_idx] = key_states, value_states
attention_interface: Callable = eager_attention_forward
if self.config._attn_implementation != "eager":
diff --git a/src/transformers/models/llama4/modeling_llama4.py b/src/transformers/models/llama4/modeling_llama4.py
index 08d50bd63f..d1cf81064f 100644
--- a/src/transformers/models/llama4/modeling_llama4.py
+++ b/src/transformers/models/llama4/modeling_llama4.py
@@ -389,7 +389,15 @@ class Llama4TextAttention(nn.Module):
key_states = key_states.transpose(1, 2)
if past_key_values is not None:
- key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
+ if isinstance(past_key_values, DynamicCache) and past_key_values.is_stale:
+ import warnings
+
+ warnings.warn(
+ "The provided `past_key_values` is marked as stale. Bypassing the internal cache update.",
+ UserWarning,
+ )
+ else:
+ key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface(
self.config._attn_implementation, eager_attention_forward
diff --git a/src/transformers/models/ministral3/modular_ministral3.py b/src/transformers/models/ministral3/modular_ministral3.py
index 0a925f5d40..8eeada4f38 100644
--- a/src/transformers/models/ministral3/modular_ministral3.py
+++ b/src/transformers/models/ministral3/modular_ministral3.py
@@ -2,7 +2,7 @@ from collections.abc import Callable
import torch
-from ...cache_utils import Cache
+from ...cache_utils import Cache, DynamicCache
from ...modeling_flash_attention_utils import FlashAttentionKwargs
from ...modeling_layers import (
GenericForQuestionAnswering,
@@ -57,7 +57,15 @@ class Ministral3Attention(MistralAttention):
).to(query_states.dtype)
if past_key_values is not None:
- key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
+ if isinstance(past_key_values, DynamicCache) and past_key_values.is_stale:
+ import warnings
+
+ warnings.warn(
+ "The provided `past_key_values` is marked as stale. Bypassing the internal cache update.",
+ UserWarning,
+ )
+ else:
+ key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface(
self.config._attn_implementation, eager_attention_forward
diff --git a/src/transformers/models/mistral4/modular_mistral4.py b/src/transformers/models/mistral4/modular_mistral4.py
index acd9f1f601..80eeb51e32 100644
--- a/src/transformers/models/mistral4/modular_mistral4.py
+++ b/src/transformers/models/mistral4/modular_mistral4.py
@@ -18,7 +18,7 @@ import torch.nn.functional as F
from torch import nn
from ... import initialization as init
-from ...cache_utils import Cache
+from ...cache_utils import Cache, DynamicCache
from ...modeling_flash_attention_utils import FlashAttentionKwargs
from ...modeling_layers import GenericForSequenceClassification, GenericForTokenClassification
from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel
@@ -191,7 +191,15 @@ class Mistral4Attention(DeepseekV3Attention):
).to(query_states.dtype)
if past_key_values is not None:
- key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
+ if isinstance(past_key_values, DynamicCache) and past_key_values.is_stale:
+ import warnings
+
+ warnings.warn(
+ "The provided `past_key_values` is marked as stale. Bypassing the internal cache update.",
+ UserWarning,
+ )
+ else:
+ key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
if is_flash_attention_requested(self.config) and self.qk_head_dim != self.v_head_dim:
value_states = F.pad(value_states, [0, self.qk_head_dim - self.v_head_dim])
diff --git a/src/transformers/models/olmo3/modular_olmo3.py b/src/transformers/models/olmo3/modular_olmo3.py
index 81325db7cc..15e570baaa 100644
--- a/src/transformers/models/olmo3/modular_olmo3.py
+++ b/src/transformers/models/olmo3/modular_olmo3.py
@@ -126,7 +126,15 @@ class Olmo3Attention(Olmo2Attention):
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
if past_key_values is not None:
- key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
+ if isinstance(past_key_values, DynamicCache) and past_key_values.is_stale:
+ import warnings
+
+ warnings.warn(
+ "The provided `past_key_values` is marked as stale. Bypassing the internal cache update.",
+ UserWarning,
+ )
+ else:
+ key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface(
self.config._attn_implementation, eager_attention_forward
diff --git a/src/transformers/models/qwen3/modular_qwen3.py b/src/transformers/models/qwen3/modular_qwen3.py
index 73cde6d89a..6934deea14 100644
--- a/src/transformers/models/qwen3/modular_qwen3.py
+++ b/src/transformers/models/qwen3/modular_qwen3.py
@@ -17,7 +17,7 @@ from collections.abc import Callable
import torch
-from ...cache_utils import Cache
+from ...cache_utils import Cache, DynamicCache
from ...modeling_flash_attention_utils import FlashAttentionKwargs
from ...modeling_outputs import CausalLMOutputWithPast
from ...modeling_utils import ALL_ATTENTION_FUNCTIONS
@@ -84,7 +84,15 @@ class Qwen3Attention(LlamaAttention):
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
if past_key_values is not None:
- key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
+ if isinstance(past_key_values, DynamicCache) and past_key_values.is_stale:
+ import warnings
+
+ warnings.warn(
+ "The provided `past_key_values` is marked as stale. Bypassing the internal cache update.",
+ UserWarning,
+ )
+ else:
+ key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx)
attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface(
self.config._attn_implementation, eager_attention_forward