Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ def __init__(
(self.max_requests,), -1, dtype=torch.int32, device=torch.cuda.current_device()
)

# Map from requests to slots in the static Mamba state buffer for active decode requests
# Map from requests to slots in the static Mamba state buffer for active decode requests.
# int64 so selective_state_update can index directly without a per-layer upcast kernel;
self._batch_indices_decode_buffer = torch.full(
(self.max_requests,), -1, dtype=torch.int32, device=self.device
(self.max_requests,), -1, dtype=torch.int64, device=self.device
)

# Map from requests to slots in the static Mamba state buffer for active prefill requests
Expand Down
25 changes: 19 additions & 6 deletions megatron/core/ssm/mamba_mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,24 @@ def _ssm_prefill(

return y

def _get_decode_A_neg_exp(self) -> torch.Tensor:
"""Cached ``-exp(A_log.float())`` for the decode path.

A_log is frozen during inference; recomputing it per token otherwise
launches three small elementwise kernels (float cast, exp, neg) that
rival ``selective_state_update`` itself in the decode profile.
"""
cached = getattr(self, "_A_neg_exp_cache", None)
if cached is None:
cached = -torch.exp(self.A_log.float())
self._A_neg_exp_cache = cached
return cached

def train(self, mode: bool = True):
"""Invalidate the decode A cache when switching modes (weights may update)."""
self._A_neg_exp_cache = None
return super().train(mode)

def _ssm_decode(
self,
zxBCdt: torch.Tensor,
Expand Down Expand Up @@ -1093,7 +1111,7 @@ def _ssm_decode(
],
dim=-1,
)
A = -torch.exp(self.A_log.float())
A = self._get_decode_A_neg_exp()

# SSM step
if selective_state_update is None:
Expand Down Expand Up @@ -1166,11 +1184,6 @@ def _ssm_decode(
if not self.rmsnorm:
z = rearrange(z, "b s (h p) -> b s h p", p=self.headdim)

# Upcast the batch_indices to prevent integer overflow errors in the case of
# large max request counts.
if batch_indices is not None:
batch_indices = batch_indices.to(torch.int64)

y = selective_state_update(
ssm_state,
x_reshaped,
Expand Down
Loading