17-v1_core_kv_cache_utils.py.diff 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. --- baked-ours/v1/core/kv_cache_utils.py 2026-09-10 19:56:14.000000000 +0200
  2. +++ baked/v1/core/kv_cache_utils.py 2026-09-11 10:13:47.000000000 +0200
  3. @@ -37,6 +37,7 @@
  4. SlidingWindowSpec,
  5. UniformTypeKVCacheSpecs,
  6. compute_layout_strides,
  7. + is_dcp_sharded_spec,
  8. iter_layer_specs,
  9. replace_as,
  10. )
  11. @@ -651,13 +652,15 @@
  12. def resolve_dcp_kv_block_size(spec: KVCacheSpec, dcp_world_size: int) -> int:
  13. - """Return the token span of a cache block under DCP."""
  14. - layer_specs = iter_layer_specs(spec)
  15. - if len(layer_specs) > 0 and all(
  16. - isinstance(layer_spec, AttentionSpec) for layer_spec in layer_specs
  17. - ):
  18. - return spec.block_size * dcp_world_size
  19. - return spec.block_size
  20. + """Return the token span of a cache block under DCP.
  21. +
  22. + Only DCP-sharded groups span ``dcp_world_size`` blocks of tokens per
  23. + manager block; replicated attention groups (``dcp_replicated``), Mamba and
  24. + other per-rank state keep their plain block size. Must agree with
  25. + ``dcp_world_size_for_kv_cache_spec`` (the coordinator scales manager block
  26. + sizes with that helper).
  27. + """
  28. + return spec.block_size * dcp_world_size_for_kv_cache_spec(spec, dcp_world_size)
  29. def resolve_dcp_kv_cache_spec(spec: KVCacheSpec, dcp_world_size: int) -> KVCacheSpec:
  30. @@ -686,15 +689,13 @@
  31. must keep ``dcp_world_size=1`` even when the process runs with DCP > 1.
  32. Draft MLA groups on the sharded DSpark path are ``FullAttentionSpec`` /
  33. - ``MLAAttentionSpec`` and therefore keep the process DCP size. A replicated
  34. - draft group would need a different spec, not this helper.
  35. + ``MLAAttentionSpec`` and therefore keep the process DCP size. Attention
  36. + groups whose layers set ``dcp_replicated`` (DeepSeek-V4.1 SWA cache and
  37. + compressor ring) hold the full KV on every rank and keep ``1``.
  38. """
  39. if dcp_world_size <= 1:
  40. return 1
  41. - inner = spec
  42. - if isinstance(spec, UniformTypeKVCacheSpecs):
  43. - inner = next(iter(spec.kv_cache_specs.values()))
  44. - if isinstance(inner, FullAttentionSpec):
  45. + if is_dcp_sharded_spec(spec):
  46. return dcp_world_size
  47. return 1