mirror of
https://github.com/ml-explore/mlx-examples.git
synced 2025-06-28 12:13:25 +08:00
fixing cache
This commit is contained in:
parent
dd4957f3da
commit
531ac96481
@ -117,6 +117,7 @@ class Mamba2Block(nn.Module):
|
|||||||
shape=(self.n_heads,)
|
shape=(self.n_heads,)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
dt = mx.clip(dt, args.time_step_floor, float('inf'))
|
dt = mx.clip(dt, args.time_step_floor, float('inf'))
|
||||||
inv_dt = dt + mx.log(-mx.exp(-dt) + 1) # Inverse softplus
|
inv_dt = dt + mx.log(-mx.exp(-dt) + 1) # Inverse softplus
|
||||||
self.dt_bias = mx.array(inv_dt)
|
self.dt_bias = mx.array(inv_dt)
|
||||||
@ -148,39 +149,37 @@ class Mamba2Block(nn.Module):
|
|||||||
batch_size, seq_len, _ = u.shape
|
batch_size, seq_len, _ = u.shape
|
||||||
|
|
||||||
# Project input
|
# Project input
|
||||||
zxbcdt = self.in_proj(u) # (B, L, d_in_proj)
|
zxbcdt = self.in_proj(u)
|
||||||
|
|
||||||
# Split projections
|
|
||||||
z = zxbcdt[..., :self.d_inner]
|
z = zxbcdt[..., :self.d_inner]
|
||||||
xBC = zxbcdt[..., self.d_inner:self.d_inner + (self.d_inner + 2 * self.n_groups * self.d_state)]
|
xBC = zxbcdt[..., self.d_inner:self.d_inner + (self.d_inner + 2 * self.n_groups * self.d_state)]
|
||||||
dt = zxbcdt[..., -self.n_heads:]
|
dt = zxbcdt[..., -self.n_heads:]
|
||||||
|
|
||||||
# Process time steps - simplified to match PyTorch
|
# Process dt
|
||||||
dt = nn.softplus(dt + self.dt_bias) # (B, L, nheads)
|
dt = nn.softplus(dt + self.dt_bias)
|
||||||
|
|
||||||
xBC, conv_state = self.conv1d(xBC, cache[0] if cache else None) # (B, L, self.d_inner + 2 * ngroups * d_state)
|
# Conv1d and activation
|
||||||
|
xBC, conv_state = self.conv1d(xBC, cache[0] if cache else None)
|
||||||
if cache is not None:
|
if cache is not None:
|
||||||
cache[0] = conv_state
|
cache[0] = conv_state
|
||||||
xBC = silu(xBC)
|
xBC = silu(xBC)
|
||||||
|
|
||||||
xBC = xBC[:, :seq_len, :]
|
xBC = xBC[:, :seq_len, :]
|
||||||
|
|
||||||
# Split conv output and reshape
|
# Split conv output and reshape
|
||||||
x = xBC[..., :self.d_inner]
|
x = xBC[..., :self.d_inner]
|
||||||
B = mx.reshape(xBC[..., self.d_inner:self.d_inner + self.n_groups * self.d_state], (batch_size, seq_len, self.n_groups, -1))
|
B = mx.reshape(xBC[..., self.d_inner:self.d_inner + self.n_groups * self.d_state],
|
||||||
C = mx.reshape(xBC[..., -self.n_groups * self.d_state:], (batch_size, seq_len, self.n_groups, -1))
|
(batch_size, seq_len, self.n_groups, -1))
|
||||||
|
C = mx.reshape(xBC[..., -self.n_groups * self.d_state:],
|
||||||
|
(batch_size, seq_len, self.n_groups, -1))
|
||||||
|
|
||||||
# Reshape for SSM processing
|
|
||||||
x = mx.reshape(x, (batch_size, seq_len, self.n_heads, self.d_head))
|
x = mx.reshape(x, (batch_size, seq_len, self.n_heads, self.d_head))
|
||||||
|
|
||||||
# Initialize state
|
# Initialize state
|
||||||
if cache and cache[1] is not None:
|
if cache and cache[1] is not None:
|
||||||
# State initialization might need proper scaling
|
|
||||||
prev_state = cache[1]
|
prev_state = cache[1]
|
||||||
else:
|
else:
|
||||||
prev_state = mx.zeros((batch_size, self.n_heads, self.d_head, self.d_state))
|
prev_state = mx.zeros((batch_size, self.n_heads, self.d_head, self.d_state))
|
||||||
|
|
||||||
# Compute dA - simplified to match PyTorch
|
# Compute dA
|
||||||
A = -mx.exp(self.A_log)
|
A = -mx.exp(self.A_log)
|
||||||
dt = mx.reshape(dt, (batch_size, seq_len, self.n_heads))
|
dt = mx.reshape(dt, (batch_size, seq_len, self.n_heads))
|
||||||
dA = mx.exp(dt * mx.expand_dims(A, axis=(0, 1)))
|
dA = mx.exp(dt * mx.expand_dims(A, axis=(0, 1)))
|
||||||
@ -190,19 +189,16 @@ class Mamba2Block(nn.Module):
|
|||||||
outputs = []
|
outputs = []
|
||||||
|
|
||||||
for t in range(seq_len):
|
for t in range(seq_len):
|
||||||
# Get current step tensors
|
xt = x[:, t]
|
||||||
xt = x[:, t] # [batch, n_heads, d_head]
|
Bt = B[:, t]
|
||||||
Bt = B[:, t] # [batch, n_heads, d_state]
|
Ct = C[:, t]
|
||||||
Ct = C[:, t] # [batch, n_heads, d_state]
|
dAt = dA[:, t]
|
||||||
dAt = dA[:, t] # [batch, n_heads]
|
|
||||||
|
|
||||||
# Compute dBx using einsum to match PyTorch
|
|
||||||
dBx = mx.einsum('bh,bgd,bhp->bhpd', dAt, Bt, xt) # dAt: (b,h), Bt: (b,g,d), xt: (b,h,p) -> (b,h,p,d)
|
|
||||||
|
|
||||||
# Update state
|
# Update state
|
||||||
|
dBx = mx.einsum('bh,bgd,bhp->bhpd', dAt, Bt, xt)
|
||||||
next_state = next_state * mx.expand_dims(dAt, axis=(-1, -2)) + dBx
|
next_state = next_state * mx.expand_dims(dAt, axis=(-1, -2)) + dBx
|
||||||
|
|
||||||
# Compute output with groups
|
# Compute output
|
||||||
yt = mx.einsum('bhpd,bgd->bhp', next_state, Ct)
|
yt = mx.einsum('bhpd,bgd->bhp', next_state, Ct)
|
||||||
yt = yt + xt * mx.expand_dims(self.D, -1)
|
yt = yt + xt * mx.expand_dims(self.D, -1)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user