kashif HF Staff commited on
Commit
16de481
·
verified ·
1 Parent(s): 01ac9c0

v5-align: remove store_kv; always update() on cached forward

Browse files
Files changed (1) hide show
  1. modeling_sdar.py +4 -33
modeling_sdar.py CHANGED
@@ -261,37 +261,12 @@ class SDARAttention(nn.Module):
261
  query_states, key_states = apply_rotary_pos_emb(
262
  query_states, key_states, cos, sin)
263
 
264
- # I-DLM / strict-causal mode (`config.use_regular_causal=True`) uses the standard HF cache
265
- # convention: every cached forward commits its KVs. In SDAR block-diffusion mode, callers pass
266
- # `store_kv=True` for commit forwards and `store_kv=False` for retrieve-only denoising passes.
267
- # Upstream forwards thread `store_kv: Optional[bool] = None` through, so a missing caller kwarg
268
- # still arrives here as `None` — treat that as "use config default" rather than False.
269
- store_kv = kwargs.get("store_kv", None)
270
- if store_kv is None:
271
- store_kv = bool(getattr(self.config, "use_regular_causal", False))
272
- if past_key_value is not None and store_kv:
273
- # Store new kv into the cache (and get concatenated result).
274
  key_states, value_states = past_key_value.update(
275
  key_states, value_states, self.layer_idx)
276
- elif past_key_value is not None and not store_kv and len(past_key_value) > self.layer_idx:
277
- # Retrieve only (don't store): read cached kv and concatenate with current block kv.
278
- # transformers v5 DynamicCache uses `.layers[idx].keys/.values`; v4 supports `cache[idx]`.
279
- past_key_states = past_value_states = None
280
- if hasattr(past_key_value, "layers"):
281
- layer = past_key_value.layers[self.layer_idx]
282
- past_key_states, past_value_states = layer.keys, layer.values
283
- else:
284
- try:
285
- past_key_states, past_value_states = past_key_value[self.layer_idx]
286
- except (IndexError, TypeError, KeyError):
287
- past_key_states = past_value_states = None
288
- # Guard: v5 DynamicCache has a layer slot from construction time but its `.keys` is None
289
- # until the first `.update()` call. On the initial cached forward (e.g. plain `generate()`
290
- # that hasn't stored this layer's KV yet), there's nothing to concatenate — just use the
291
- # current block's KVs as if no cache.
292
- if past_key_states is not None and past_value_states is not None:
293
- key_states = torch.cat([past_key_states, key_states], dim=-2)
294
- value_states = torch.cat([past_value_states, value_states], dim=-2)
295
 
296
  attention_mask = attention_mask.bool() if attention_mask is not None else None
297
 
@@ -373,7 +348,6 @@ class SDARDecoderLayer(GradientCheckpointingLayer):
373
  past_key_value: Optional[Cache] = None,
374
  output_attentions: Optional[bool] = False,
375
  use_cache: Optional[bool] = False,
376
- store_kv: Optional[bool] = False,
377
  cache_position: Optional[torch.LongTensor] = None,
378
  # necessary, but kept here for BC
379
  position_embeddings: Optional[Tuple[torch.Tensor,
@@ -391,7 +365,6 @@ class SDARDecoderLayer(GradientCheckpointingLayer):
391
  past_key_value=past_key_value,
392
  output_attentions=output_attentions,
393
  use_cache=use_cache,
394
- store_kv=store_kv,
395
  cache_position=cache_position,
396
  position_embeddings=position_embeddings,
397
  **kwargs,
@@ -543,7 +516,6 @@ class SDARModel(SDARPreTrainedModel):
543
  past_key_values: Optional[Cache] = None,
544
  inputs_embeds: Optional[torch.FloatTensor] = None,
545
  use_cache: Optional[bool] = None,
546
- store_kv: Optional[bool] = None,
547
  output_attentions: Optional[bool] = None,
548
  output_hidden_states: Optional[bool] = None,
549
  cache_position: Optional[torch.LongTensor] = None,
@@ -610,7 +582,6 @@ class SDARModel(SDARPreTrainedModel):
610
  past_key_value=past_key_values,
611
  output_attentions=output_attentions,
612
  use_cache=use_cache,
613
- store_kv=store_kv,
614
  cache_position=cache_position,
615
  position_embeddings=position_embeddings,
616
  **flash_attn_kwargs,
 
261
  query_states, key_states = apply_rotary_pos_emb(
262
  query_states, key_states, cos, sin)
263
 
264
+ # Standard transformers v5 cache convention: when a cache is provided, always `.update()` it.
265
+ # Callers that want a read-only forward should pass `past_key_values=None`, or use
266
+ # `DynamicCache.crop(prev_seq_len)` to roll back the append after reading the logits.
267
+ if past_key_value is not None:
 
 
 
 
 
 
268
  key_states, value_states = past_key_value.update(
269
  key_states, value_states, self.layer_idx)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
 
271
  attention_mask = attention_mask.bool() if attention_mask is not None else None
272
 
 
348
  past_key_value: Optional[Cache] = None,
349
  output_attentions: Optional[bool] = False,
350
  use_cache: Optional[bool] = False,
 
351
  cache_position: Optional[torch.LongTensor] = None,
352
  # necessary, but kept here for BC
353
  position_embeddings: Optional[Tuple[torch.Tensor,
 
365
  past_key_value=past_key_value,
366
  output_attentions=output_attentions,
367
  use_cache=use_cache,
 
368
  cache_position=cache_position,
369
  position_embeddings=position_embeddings,
370
  **kwargs,
 
516
  past_key_values: Optional[Cache] = None,
517
  inputs_embeds: Optional[torch.FloatTensor] = None,
518
  use_cache: Optional[bool] = None,
 
519
  output_attentions: Optional[bool] = None,
520
  output_hidden_states: Optional[bool] = None,
521
  cache_position: Optional[torch.LongTensor] = None,
 
582
  past_key_value=past_key_values,
583
  output_attentions=output_attentions,
584
  use_cache=use_cache,
 
585
  cache_position=cache_position,
586
  position_embeddings=position_embeddings,
587
  **flash_attn_kwargs,