Fix mamba checkpoint depth under dcp (#34808)
This commit is contained in:
@@ -8,6 +8,7 @@ from sglang.srt.runtime_context import (
|
||||
get_serving,
|
||||
get_spec,
|
||||
mamba_cache_chunk_size,
|
||||
mamba_checkpoint_grid,
|
||||
mamba_extra_buffer_enabled,
|
||||
mamba_extra_buffer_lazy_enabled,
|
||||
)
|
||||
@@ -2630,6 +2631,10 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
|
||||
req: Req,
|
||||
) -> _MambaRadixCacheV2TrackEntry:
|
||||
chunk_size = mamba_cache_chunk_size()
|
||||
# The donated depth has to be a radix node boundary. Read the tree's own
|
||||
# page rather than re-deriving how DCP widens it; the kernel still
|
||||
# snapshots on the chunk_size grid.
|
||||
checkpoint_grid = mamba_checkpoint_grid(self.tree_cache.page_size)
|
||||
|
||||
def _force_track_h(i: int) -> int:
|
||||
assert i % chunk_size == 0
|
||||
@@ -2642,7 +2647,7 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
|
||||
# to force the math calculation to retrieve the correct mamba state from h.
|
||||
return i + 1
|
||||
|
||||
mask = req.extend_range.length >= chunk_size
|
||||
mask = req.extend_range.length >= checkpoint_grid
|
||||
track_index = req.mamba_ping_pong_track_buffer[req.mamba_next_track_idx].item()
|
||||
mamba_track_seqlen = -1
|
||||
if mask:
|
||||
@@ -2659,13 +2664,13 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
|
||||
# mamba radix cache to track which seqlen this mamba state should store at.
|
||||
mamba_track_seqlen_aligned = (
|
||||
len(req.prefix_indices)
|
||||
+ (req.extend_range.length // chunk_size) * chunk_size
|
||||
+ (req.extend_range.length // checkpoint_grid) * checkpoint_grid
|
||||
)
|
||||
|
||||
# mamba_track_fla_chunk_aligned is the aligned seqlen based on chunk_size
|
||||
# If mamba_track_fla_chunk_aligned != mamba_track_seqlen_aligned, which can be true when
|
||||
# page_size > chunk_size, we need to force the math calculation to retrieve the correct mamba state from h
|
||||
# by _force_track_h()
|
||||
# If mamba_track_fla_chunk_aligned != mamba_track_seqlen_aligned, which is true when
|
||||
# checkpoint_grid is coarser than chunk_size, we need to force the math calculation to
|
||||
# retrieve the correct mamba state from h by _force_track_h()
|
||||
mamba_track_fla_chunk_aligned = (
|
||||
len(req.prefix_indices)
|
||||
+ (req.extend_range.length // chunk_size) * chunk_size
|
||||
|
||||
@@ -38,6 +38,7 @@ from sglang.srt.mem_cache.unified_cache.components.tree_component import (
|
||||
from sglang.srt.runtime_context import (
|
||||
get_exec,
|
||||
mamba_cache_chunk_size,
|
||||
mamba_checkpoint_grid,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -69,6 +70,9 @@ class MambaComponent(TreeComponent):
|
||||
), f"MambaComponent requires page_size=1 when mamba_extra_buffer is disabled, got {params.page_size}"
|
||||
super().__init__(cache, params)
|
||||
self.mamba_cache_chunk_size = mamba_cache_chunk_size()
|
||||
# params.page_size is the tree page the allocator actually uses, already
|
||||
# widened by dcp_size, so it is the one grid a checkpoint depth can land on.
|
||||
self.mamba_checkpoint_grid = mamba_checkpoint_grid(params.page_size)
|
||||
self.mamba_max_states_per_path = get_exec().mamba.mamba_max_states_per_path
|
||||
# HiCache state
|
||||
self._mamba_pool_host = None # set to host mamba pool when HiCache enabled
|
||||
@@ -164,8 +168,8 @@ class MambaComponent(TreeComponent):
|
||||
# persistence of a new branching state is currently write-through only;
|
||||
# write-back eviction may discard the device-only state.
|
||||
aligned_seqlen = (
|
||||
result.full_kv_hit_length // self.mamba_cache_chunk_size
|
||||
) * self.mamba_cache_chunk_size
|
||||
result.full_kv_hit_length // self.mamba_checkpoint_grid
|
||||
) * self.mamba_checkpoint_grid
|
||||
branching_seqlen = (
|
||||
aligned_seqlen if aligned_seqlen > mamba_boundary_len else None
|
||||
)
|
||||
|
||||
@@ -48,6 +48,7 @@ from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import functools
|
||||
import math
|
||||
import os
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
@@ -1435,6 +1436,14 @@ def mamba_cache_chunk_size() -> int:
|
||||
return get_server_args().mamba_cache_chunk_size
|
||||
|
||||
|
||||
def mamba_checkpoint_grid(tree_page: int) -> int:
|
||||
"""The granularity a donated mamba checkpoint's depth must land on so the
|
||||
radix tree can name it. Pass the page the tree actually allocates on: DCP
|
||||
widens it past ``mamba_cache_chunk_size``, and deriving that here would be a
|
||||
second copy of a predicate that already lives in the cache builder."""
|
||||
return math.lcm(mamba_cache_chunk_size(), tree_page)
|
||||
|
||||
|
||||
def max_speculative_num_draft_tokens() -> int | None:
|
||||
"""The largest draft-token count speculative decoding may use.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user