Pass-through cache slot for hybrid models' linear-attention layers. HF's ``LinearAttentionLayer`` is already cudagraph-safe by construction: static shapes, ``mark_static_address`` buffers, and in-place ``copy_`` state updates (its own comments say "to preserve the static address for
| 206 | |
| 207 | |
| 208 | class DSStaticGDNSlot: |
| 209 | """Pass-through cache slot for hybrid models' linear-attention layers. |
| 210 | |
| 211 | HF's ``LinearAttentionLayer`` is already cudagraph-safe by construction: |
| 212 | static shapes, ``mark_static_address`` buffers, and in-place ``copy_`` |
| 213 | state updates (its own comments say "to preserve the static address for |
| 214 | cudagraphs"). Duplicating that state management here would only drift; |
| 215 | the graph path therefore binds and reuses the prefill cache's slot |
| 216 | object directly - zero copies, and decode-time updates land in the very |
| 217 | buffers the captured graph reads.""" |
| 218 | |
| 219 | is_compileable = True |
| 220 | supports_early_init = False |
| 221 | |
| 222 | def __init__(self, max_cache_len: int): |
| 223 | self._slot = None |
| 224 | self.max_cache_len = max_cache_len |
| 225 | self._write_position: torch.Tensor | None = None |
| 226 | |
| 227 | def bind(self, hf_slot) -> None: |
| 228 | """Adopt a prefill cache's linear-attention slot (by reference).""" |
| 229 | self._slot = hf_slot |
| 230 | |
| 231 | def __getattr__(self, name): |
| 232 | # Forward the HF slot API (conv_states, recurrent_states, |
| 233 | # has_previous_state, update_*, ...) so the model's GDN modules see |
| 234 | # the exact object semantics they were written against. |
| 235 | slot = object.__getattribute__(self, "_slot") |
| 236 | if slot is None: |
| 237 | raise AttributeError(f"DSStaticGDNSlot not bound: {name}") |
| 238 | return getattr(slot, name) |
| 239 | |
| 240 | def set_write_position(self, pos: torch.Tensor): |
| 241 | self._write_position = pos # tracked for symmetry; GDN state is position-free |
| 242 | |
| 243 | def get_seq_length(self) -> int: |
| 244 | return 0 |
| 245 | |
| 246 | def get_max_cache_shape(self) -> int: |
| 247 | return 0 |
| 248 | |
| 249 | def get_mask_sizes(self, query_length: int) -> tuple[int, int]: |
| 250 | return query_length, 0 |
| 251 | |
| 252 | def reset(self) -> None: |
| 253 | if self._slot is not None: |
| 254 | self._slot.reset() |
| 255 | |
| 256 | |
| 257 | class DeepSpeedStaticCache: |