[Mamba] Support configurable conv-window layouts (#31059)
This commit is contained in:
@@ -311,6 +311,10 @@ class ReqToTokenPool:
|
||||
|
||||
|
||||
class MambaPool:
|
||||
# Axis of each two-dimensional conv state that represents the sliding window.
|
||||
# Upstream states use (dim, K-1); subclasses may preserve another layout.
|
||||
conv_window_axis = -1
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class State:
|
||||
conv: List[torch.Tensor]
|
||||
@@ -352,6 +356,46 @@ class MambaPool:
|
||||
intermediate_ssm: torch.Tensor
|
||||
intermediate_conv_window: List[torch.Tensor]
|
||||
|
||||
def _allocate_deduplicated_conv_window(
|
||||
self,
|
||||
*,
|
||||
conv_shape: Tuple[int, int],
|
||||
num_mamba_layers: int,
|
||||
spec_state_size: int,
|
||||
speculative_num_draft_tokens: int,
|
||||
conv_dtype: torch.dtype,
|
||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||
window_axis = self.conv_window_axis % len(conv_shape)
|
||||
win = conv_shape[window_axis]
|
||||
physical_conv_shape = list(conv_shape)
|
||||
physical_conv_shape[window_axis] = speculative_num_draft_tokens + win - 1
|
||||
phys = torch.zeros(
|
||||
(
|
||||
num_mamba_layers,
|
||||
spec_state_size + 1,
|
||||
*physical_conv_shape,
|
||||
),
|
||||
dtype=conv_dtype,
|
||||
device="cuda",
|
||||
)
|
||||
physical_conv_strides = phys.stride()[2:]
|
||||
window_stride = physical_conv_strides[window_axis]
|
||||
view = phys.as_strided(
|
||||
(
|
||||
phys.shape[0],
|
||||
phys.shape[1],
|
||||
speculative_num_draft_tokens,
|
||||
*conv_shape,
|
||||
),
|
||||
(
|
||||
phys.stride(0),
|
||||
phys.stride(1),
|
||||
window_stride,
|
||||
*physical_conv_strides,
|
||||
),
|
||||
)
|
||||
return phys, view
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
@@ -538,36 +582,12 @@ class MambaPool:
|
||||
if dedup_conv_window:
|
||||
intermediate_conv_window_cache = []
|
||||
for conv_shape in conv_state_shape:
|
||||
conv_dim, win = conv_shape # win == conv_kernel - 1 == K-1
|
||||
shared_win = (
|
||||
speculative_num_draft_tokens + win - 1
|
||||
) # D + (K-1) - 1
|
||||
phys = torch.zeros(
|
||||
size=(
|
||||
num_mamba_layers,
|
||||
spec_state_size + 1,
|
||||
conv_dim,
|
||||
shared_win,
|
||||
),
|
||||
dtype=conv_dtype,
|
||||
device="cuda",
|
||||
)
|
||||
# view[l, s, step, d, w] = phys[l, s, d, step + w]
|
||||
view = phys.as_strided(
|
||||
(
|
||||
phys.shape[0],
|
||||
phys.shape[1],
|
||||
speculative_num_draft_tokens,
|
||||
conv_dim,
|
||||
win,
|
||||
),
|
||||
(
|
||||
phys.stride(0),
|
||||
phys.stride(1),
|
||||
phys.stride(3), # step -> shared-win axis (stride 1)
|
||||
phys.stride(2), # dim
|
||||
phys.stride(3), # win -> shared-win axis (stride 1)
|
||||
),
|
||||
phys, view = self._allocate_deduplicated_conv_window(
|
||||
conv_shape=conv_shape,
|
||||
num_mamba_layers=num_mamba_layers,
|
||||
spec_state_size=spec_state_size,
|
||||
speculative_num_draft_tokens=speculative_num_draft_tokens,
|
||||
conv_dtype=conv_dtype,
|
||||
)
|
||||
self._intermediate_conv_window_phys.append(phys)
|
||||
intermediate_conv_window_cache.append(view)
|
||||
@@ -818,6 +838,8 @@ class MambaPool:
|
||||
class HybridReqToTokenPool(ReqToTokenPool):
|
||||
"""A memory pool that maps a request to its token locations."""
|
||||
|
||||
mamba_pool_cls = MambaPool
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
@@ -880,7 +902,7 @@ class HybridReqToTokenPool(ReqToTokenPool):
|
||||
linear_replayssm_cache_len: int = 16,
|
||||
mamba_envelope_layout: bool = False,
|
||||
):
|
||||
self.mamba_pool = MambaPool(
|
||||
self.mamba_pool = self.mamba_pool_cls(
|
||||
size=mamba_size,
|
||||
spec_state_size=mamba_spec_state_size,
|
||||
cache_params=cache_params,
|
||||
|
||||
Reference in New Issue
Block a user