06-models_deepseek_v4_1_attention.py.diff 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. --- baked-ours/models/deepseek_v4_1/attention.py 2026-09-10 20:13:20.000000000 +0200
  2. +++ baked/models/deepseek_v4_1/attention.py 2026-09-11 10:13:47.000000000 +0200
  3. @@ -42,7 +42,7 @@
  4. VllmConfig,
  5. get_current_vllm_config,
  6. )
  7. -from vllm.distributed import get_tensor_model_parallel_world_size
  8. +from vllm.distributed import get_dcp_group, get_tensor_model_parallel_world_size
  9. from vllm.forward_context import get_forward_context
  10. from vllm.logger import init_logger
  11. from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase
  12. @@ -172,6 +172,13 @@
  13. # workspace allocated in _forward_prefill and is also read by the dummy-run
  14. # path to pre-reserve that workspace.
  15. PREFILL_CHUNK_SIZE: ClassVar[int] = 4
  16. + # DCP contract, mirroring AttentionImpl (v1/attention/backend.py): the
  17. + # V4.1 layer runs its own forward (no AttentionImpl), so
  18. + # check_attention_cp_compatibility reads these from the layer. A platform
  19. + # subclass that implements the per-rank partial attention + LSE combine
  20. + # sets can_return_lse_for_decode = True (SM120 FlashInfer).
  21. + can_return_lse_for_decode: ClassVar[bool] = False
  22. + supports_mtp_with_cp_non_trivial_interleave_size: ClassVar[bool] = False
  23. @classmethod
  24. @abstractmethod
  25. @@ -226,6 +233,22 @@
  26. self.n_heads = config.num_attention_heads
  27. assert self.n_heads % tp_size == 0
  28. self.n_local_heads = self.n_heads // tp_size
  29. + # Decode context parallelism: compressed/indexer caches are sharded over
  30. + # compressed states across the DCP group (a subset of the TP ranks),
  31. + # SWA caches are replicated; the platform subclass gathers queries and
  32. + # combines per-rank partial attention (DCP2-PLAN.md).
  33. + parallel_config = vllm_config.parallel_config
  34. + self.dcp_world_size = parallel_config.decode_context_parallel_size
  35. + self.dcp_rank = get_dcp_group().rank_in_group if self.dcp_world_size > 1 else 0
  36. + self.cp_kv_cache_interleave_size = parallel_config.cp_kv_cache_interleave_size
  37. + self.need_to_return_lse_for_decode = (
  38. + self.dcp_world_size > 1 and self.can_return_lse_for_decode
  39. + )
  40. + if self.dcp_world_size > 1 and self.cp_kv_cache_interleave_size != 1:
  41. + raise NotImplementedError(
  42. + "DeepSeek-V4.1 DCP supports cp_kv_cache_interleave_size=1 only "
  43. + f"(got {self.cp_kv_cache_interleave_size})."
  44. + )
  45. self.q_lora_rank = config.q_lora_rank
  46. self.o_lora_rank = config.o_lora_rank
  47. self.head_dim = config.head_dim