Skip to content

Fixes bug in SelectiveScanFn.forward for when B is not variable and last_state is returned #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
11 changes: 11 additions & 0 deletions mamba_ssm/ops/selective_scan_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class SelectiveScanFn(torch.autograd.Function):
@staticmethod
def forward(ctx, u, delta, A, B, C, D=None, z=None, delta_bias=None, delta_softplus=False,
return_last_state=False):

is_variable_B = B.dim() >= 3

if u.stride(-1) != 1:
u = u.contiguous()
if delta.stride(-1) != 1:
Expand All @@ -43,6 +46,14 @@ def forward(ctx, u, delta, A, B, C, D=None, z=None, delta_bias=None, delta_softp
ctx.delta_softplus = delta_softplus
ctx.has_z = z is not None
last_state = x[:, :, -1, 1::2] # (batch, dim, dstate)

# The cuda kernel does a peculiar optimization of not multiplying the state
# by B if B is not variable! This does not impact MambaInnerFn, because it
# never returns the state. But SelectiveScanFn may needd to return the
# last state! Hence the following is needed.
if not is_variable_B:
last_state = torch.einsum('bdn,dn->bdn', last_state, B)

if not ctx.has_z:
ctx.save_for_backward(u, delta, A, B, C, D, delta_bias, x)
return out if not return_last_state else (out, last_state)
Expand Down