22-v1_worker_gpu_model_runner.py.diff 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. --- baked-ours/v1/worker/gpu/model_runner.py 2026-09-10 19:56:14.000000000 +0200
  2. +++ baked/v1/worker/gpu/model_runner.py 2026-09-11 10:13:47.000000000 +0200
  3. @@ -70,6 +70,7 @@
  4. KVCacheConfig,
  5. MambaSpec,
  6. UniformTypeKVCacheSpecs,
  7. + is_dcp_replicated_spec,
  8. )
  9. from vllm.v1.outputs import (
  10. DraftTokenIds,
  11. @@ -170,6 +171,82 @@
  12. logger = init_logger(__name__)
  13. +# Stage 7 (DSV41_PREFILL_TIMING=1): per-phase budget of eager prefill steps.
  14. +import os as _dsv41_os # noqa: E402
  15. +
  16. +_DSV41_PREFILL_TIMING = _dsv41_os.environ.get("DSV41_PREFILL_TIMING", "0") == "1"
  17. +
  18. +
  19. +def _dsv41_prefill_timer_start(input_batch, batch_desc, dummy_run):
  20. + if (
  21. + not _DSV41_PREFILL_TIMING
  22. + or dummy_run
  23. + or input_batch.num_tokens < 1024
  24. + or batch_desc.cg_mode == CUDAGraphMode.FULL
  25. + or torch.cuda.is_current_stream_capturing()
  26. + ):
  27. + return None
  28. + try: # drop phase time accumulated outside this forward (e.g. the DSpark drafter of the previous step)
  29. + from vllm.model_executor.kernels.attention.dsa.candidate_blocks import prefill_acc_take
  30. +
  31. + prefill_acc_take()
  32. + except Exception: # noqa: BLE001
  33. + pass
  34. + e0 = torch.cuda.Event(enable_timing=True)
  35. + e0.record()
  36. + return e0
  37. +
  38. +
  39. +def _dsv41_prefill_timer_end(e0, input_batch, model_state):
  40. + if e0 is None:
  41. + return
  42. + e1 = torch.cuda.Event(enable_timing=True)
  43. + e1.record()
  44. + e1.synchronize()
  45. + fwd = e0.elapsed_time(e1)
  46. + try:
  47. + from vllm.model_executor.kernels.attention.dsa.candidate_blocks import (
  48. + prefill_acc_take,
  49. + )
  50. +
  51. + acc = prefill_acc_take()
  52. + except Exception: # noqa: BLE001
  53. + acc = {}
  54. + g = acc.get("attn_gather", 0.0)
  55. + k = acc.get("attn_kernel", 0.0)
  56. + c = acc.get("attn_combine", 0.0)
  57. + idx = acc.get("indexer", 0.0)
  58. + cand = acc.get("candidates", 0.0)
  59. + stager = getattr(model_state, "engram_stager", None)
  60. + eng = float(getattr(stager, "_t_last_total_ms", 0.0) or 0.0) if stager is not None else 0.0
  61. + n = int(input_batch.num_tokens)
  62. + moe = acc.get("moe", 0.0)
  63. + ar = acc.get("allreduce", 0.0)
  64. + egf = acc.get("engram_fwd", 0.0)
  65. + rest = fwd - (g + k + c) - idx
  66. + logger.info(
  67. + "Prefill step timing: %d tokens / %d reqs: forward %.0f ms (%.0f tok/s) = "
  68. + "attention %.0f (gather %.0f, kernel %.0f, combine %.0f) + indexer %.0f "
  69. + "(incl. candidates %.0f) + rest[MoE/dense/other] %.0f [moe %.0f, allreduce %.0f, "
  70. + "engram_fwd %.0f, other %.0f]; Engram staging before the step %.0f ms",
  71. + n,
  72. + int(input_batch.num_reqs),
  73. + fwd,
  74. + 1e3 * n / max(fwd, 1e-6),
  75. + g + k + c,
  76. + g,
  77. + k,
  78. + c,
  79. + idx,
  80. + cand,
  81. + rest,
  82. + moe,
  83. + ar,
  84. + egf,
  85. + rest - moe - ar - egf,
  86. + eng,
  87. + )
  88. +
  89. class GPUModelRunner(LoRAModelRunnerMixin):
  90. def __init__(self, vllm_config: VllmConfig, device: torch.device):
  91. @@ -558,9 +635,13 @@
  92. block_sizes = []
  93. max_num_blocks_per_group = []
  94. slot_mapping_enabled = []
  95. + cp_sharded = []
  96. for kv_cache_group in kv_cache_config.kv_cache_groups:
  97. spec = kv_cache_group.kv_cache_spec
  98. block_sizes.append(spec.block_size)
  99. + # DCP-replicated groups (V4.1 SWA cache, compressor ring) store
  100. + # every position on every rank; only sharded groups round-robin.
  101. + cp_sharded.append(not is_dcp_replicated_spec(spec))
  102. layer_spec = (
  103. spec.first_spec if isinstance(spec, UniformTypeKVCacheSpecs) else spec
  104. )
  105. @@ -623,7 +704,15 @@
  106. cp_size=self.dcp_size,
  107. cp_rank=self.dcp_rank,
  108. cp_interleave=self.cp_interleave,
  109. + cp_sharded=cp_sharded,
  110. )
  111. + if self.use_dcp:
  112. + logger.info(
  113. + "DCP%d KV cache groups: sharded=%s (block sizes %s)",
  114. + self.dcp_size,
  115. + cp_sharded,
  116. + block_sizes,
  117. + )
  118. self.pcp_manager = pcp.maybe_build_pcp_manager(
  119. self.vllm_config,
  120. self.device,
  121. @@ -1752,6 +1841,7 @@
  122. input_batch, batch_desc.cg_mode == CUDAGraphMode.FULL
  123. )
  124. self.step_timing.forward_start()
  125. + _dsv41_pf = _dsv41_prefill_timer_start(input_batch, batch_desc, dummy_run)
  126. # Run model.
  127. if batch_desc.cg_mode == CUDAGraphMode.FULL:
  128. @@ -1794,6 +1884,7 @@
  129. else:
  130. # Eager (NONE): call the raw model directly.
  131. model_output = self.model(**model_inputs)
  132. + _dsv41_prefill_timer_end(_dsv41_pf, input_batch, self.model_state)
  133. if self.is_last_pp_rank:
  134. if self.use_aux_hidden_state_outputs: