13-v1_attention_backends_mla_compressor_utils.py.diff 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. --- baked-ours/v1/attention/backends/mla/compressor_utils.py 2026-09-10 19:56:14.000000000 +0200
  2. +++ baked/v1/attention/backends/mla/compressor_utils.py 2026-09-11 10:13:47.000000000 +0200
  3. @@ -38,6 +38,9 @@
  4. compress_ratio: int
  5. triton_block_size: int
  6. block_size: int
  7. + dcp_world: int = 1
  8. + dcp_rank: int = 0
  9. + dcp_interleave: int = 1
  10. @staticmethod
  11. @triton.jit(do_not_specialize=["block_table_stride"])
  12. @@ -51,10 +54,14 @@
  13. # [num_reqs, max_num_blocks]
  14. block_table_ptr,
  15. block_table_stride,
  16. + # states per page on THIS rank (kv_cache_spec.num_states)
  17. block_size,
  18. COMPRESS_RATIO: tl.constexpr,
  19. PAD_ID: tl.constexpr,
  20. TRITON_BLOCK_SIZE: tl.constexpr,
  21. + DCP_WORLD: tl.constexpr,
  22. + DCP_RANK: tl.constexpr,
  23. + DCP_INTERLEAVE: tl.constexpr,
  24. ):
  25. batch_idx = tl.program_id(0)
  26. @@ -71,14 +78,32 @@
  27. pos = start_pos + i + tl.arange(0, TRITON_BLOCK_SIZE)
  28. is_valid = (pos + 1) % COMPRESS_RATIO == 0
  29. - pos_after_compress = pos // COMPRESS_RATIO
  30. + # Global compressed state index of the group this token closes.
  31. + state = pos // COMPRESS_RATIO
  32. + if DCP_WORLD > 1:
  33. + # DCP shards the compressed cache over STATES with the runner's
  34. + # token-space formula (worker/gpu/block_table.py) applied in
  35. + # state space: one block-table entry spans block_size * DCP_WORLD
  36. + # states, this rank owns the states whose interleave group is
  37. + # congruent to DCP_RANK, and stores them contiguously per entry.
  38. + virtual_block_size = block_size * DCP_WORLD
  39. + virtual_block_idx = state // virtual_block_size
  40. + virtual_off = state % virtual_block_size
  41. + is_local = (virtual_off // DCP_INTERLEAVE) % DCP_WORLD == DCP_RANK
  42. + local_off = (virtual_off // (DCP_INTERLEAVE * DCP_WORLD)) * DCP_INTERLEAVE + (
  43. + virtual_off % DCP_INTERLEAVE
  44. + )
  45. + local_state = virtual_block_idx * block_size + local_off
  46. + is_valid = is_valid & is_local
  47. + else:
  48. + local_state = state
  49. - block_ids = pos_after_compress // block_size
  50. + block_ids = local_state // block_size
  51. block_numbers = tl.load(
  52. block_table_ptr + batch_idx * block_table_stride + block_ids,
  53. mask=mask & is_valid,
  54. )
  55. - slot_ids = block_numbers * block_size + pos_after_compress % block_size
  56. + slot_ids = block_numbers * block_size + local_state % block_size
  57. # NOTE
  58. slot_ids = tl.where(is_valid, slot_ids, PAD_ID)
  59. @@ -89,15 +114,29 @@
  60. *,
  61. compress_ratio: int,
  62. block_size: int,
  63. + dcp_world_size: int = 1,
  64. + dcp_rank: int = 0,
  65. + cp_kv_cache_interleave_size: int = 1,
  66. ) -> CompileKey:
  67. return self.CompileKey(
  68. compress_ratio=compress_ratio,
  69. triton_block_size=self.TRITON_BLOCK_SIZE,
  70. block_size=triton_scalar_specialization_rep(block_size),
  71. + dcp_world=dcp_world_size,
  72. + dcp_rank=dcp_rank,
  73. + dcp_interleave=cp_kv_cache_interleave_size,
  74. )
  75. def get_warmup_keys(self, vllm_config: Any) -> list[CompileKey]:
  76. hf_config = vllm_config.model_config.hf_config
  77. + parallel_config = vllm_config.parallel_config
  78. + dcp_world_size = parallel_config.decode_context_parallel_size
  79. + cp_interleave = parallel_config.cp_kv_cache_interleave_size
  80. + dcp_rank = 0
  81. + if dcp_world_size > 1:
  82. + from vllm.distributed import get_dcp_group
  83. +
  84. + dcp_rank = get_dcp_group().rank_in_group
  85. configured_ratios = (
  86. *(getattr(hf_config, "compress_ratios", None) or ()),
  87. getattr(hf_config, "index_kpool", 1) or 1,
  88. @@ -113,6 +152,9 @@
  89. dict(
  90. compress_ratio=ratio,
  91. block_size=vllm_config.cache_config.block_size // ratio,
  92. + dcp_world_size=dcp_world_size,
  93. + dcp_rank=dcp_rank,
  94. + cp_kv_cache_interleave_size=cp_interleave,
  95. )
  96. for ratio in compress_ratios
  97. )
  98. @@ -128,6 +170,9 @@
  99. block_table=int32_ptr,
  100. block_size=compile_key.block_size,
  101. compress_ratio=compile_key.compress_ratio,
  102. + dcp_world_size=compile_key.dcp_world,
  103. + dcp_rank=compile_key.dcp_rank,
  104. + cp_kv_cache_interleave_size=compile_key.dcp_interleave,
  105. )
  106. @kernel_launcher
  107. @@ -139,12 +184,22 @@
  108. block_table: torch.Tensor,
  109. block_size: int,
  110. compress_ratio: int,
  111. + dcp_world_size: int = 1,
  112. + dcp_rank: int = 0,
  113. + cp_kv_cache_interleave_size: int = 1,
  114. ) -> LaunchSpec:
  115. + assert block_size % cp_kv_cache_interleave_size == 0, (
  116. + f"states per page ({block_size}) must be a multiple of "
  117. + f"cp_kv_cache_interleave_size ({cp_kv_cache_interleave_size})"
  118. + )
  119. return (block_table.shape[0],), dict(
  120. block_table_stride=block_table.stride(0),
  121. COMPRESS_RATIO=compress_ratio,
  122. PAD_ID=-1,
  123. TRITON_BLOCK_SIZE=self.TRITON_BLOCK_SIZE,
  124. + DCP_WORLD=dcp_world_size,
  125. + DCP_RANK=dcp_rank,
  126. + DCP_INTERLEAVE=cp_kv_cache_interleave_size,
  127. )
  128. @@ -156,7 +211,17 @@
  129. block_size: int,
  130. compress_ratio: int,
  131. out: torch.Tensor | None = None,
  132. + dcp_world_size: int = 1,
  133. + dcp_rank: int = 0,
  134. + cp_kv_cache_interleave_size: int = 1,
  135. ) -> torch.Tensor:
  136. + """Slot of the compressed state each token closes (PAD otherwise).
  137. +
  138. + ``block_size`` is the number of states per page on this rank. Under DCP
  139. + (``dcp_world_size > 1``) states are sharded across ranks and only this
  140. + rank's states get a slot; the block table is the shared (manager) table
  141. + whose entries each span ``block_size * dcp_world_size`` states.
  142. + """
  143. if out is not None:
  144. # Guard: for padded / invalid sequences.
  145. # Negative positions produce bogus block indices that lead to illegal memory
  146. @@ -176,6 +241,9 @@
  147. block_table,
  148. block_size,
  149. compress_ratio,
  150. + dcp_world_size,
  151. + dcp_rank,
  152. + cp_kv_cache_interleave_size,
  153. )
  154. return slot_mapping