Instructions to use dynatrace-oss/chunkable-mamba2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use dynatrace-oss/chunkable-mamba2 with Transformers:
# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("dynatrace-oss/chunkable-mamba2", device_map="auto") - Notebooks
- Google Colab
- Kaggle
chore: clean up masking
Browse files- chunkable_ssd_combined.py +12 -17
- modeling_chunkable_mamba2.py +0 -1
- safe_layernorm_gated.py +0 -16
chunkable_ssd_combined.py
CHANGED
|
@@ -189,10 +189,13 @@ class ChunkableMambaSplitConv1dScanCombinedFn(torch.autograd.Function):
|
|
| 189 |
)
|
| 190 |
assert dt_bias.shape == (nheads,)
|
| 191 |
assert A.shape == (nheads,)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
zx0, z, xBC, dt = torch.split(
|
| 193 |
zxbcdt, [2 * d_nonssm, dim, dim + ngroups * dstate * 2, nheads], dim=-1
|
| 194 |
)
|
| 195 |
-
seq_idx = seq_idx.contiguous() if seq_idx is not None else None
|
| 196 |
final_conv_states = (
|
| 197 |
torch.empty(
|
| 198 |
(batch, conv1d_weight.shape[1] - 1, dim + ngroups * dstate * 2),
|
|
@@ -206,8 +209,6 @@ class ChunkableMambaSplitConv1dScanCombinedFn(torch.autograd.Function):
|
|
| 206 |
# Additionally, there is a bug in causal_conv1d_fwd_function when seq_idx is used causing illegal memory access:
|
| 207 |
# - Issue: https://github.com/Dao-AILab/causal-conv1d/issues/67
|
| 208 |
# - PR: https://github.com/Dao-AILab/causal-conv1d/pull/101
|
| 209 |
-
if seq_idx is not None and initial_conv_states is not None:
|
| 210 |
-
xBC = xBC * (seq_idx.unsqueeze(-1) >= 0).to(xBC.dtype)
|
| 211 |
xBC_conv = rearrange(
|
| 212 |
causal_conv1d_fwd_function(
|
| 213 |
rearrange_and_update_stride(xBC, "b s d -> b d s"),
|
|
@@ -220,8 +221,8 @@ class ChunkableMambaSplitConv1dScanCombinedFn(torch.autograd.Function):
|
|
| 220 |
),
|
| 221 |
"b d s -> b s d",
|
| 222 |
)
|
| 223 |
-
if
|
| 224 |
-
xBC_conv = xBC_conv *
|
| 225 |
x, B, C = torch.split(
|
| 226 |
xBC_conv, [dim, ngroups * dstate, ngroups * dstate], dim=-1
|
| 227 |
)
|
|
@@ -229,12 +230,6 @@ class ChunkableMambaSplitConv1dScanCombinedFn(torch.autograd.Function):
|
|
| 229 |
B = rearrange(B, "b l (g n) -> b l g n", g=ngroups)
|
| 230 |
C = rearrange(C, "b l (g n) -> b l g n", g=ngroups)
|
| 231 |
z = rearrange(z, "b l (h p) -> b l h p", h=nheads) if z is not None else None
|
| 232 |
-
# Moved bias addition and softplus calculation out of kernel to mask padding dt values.
|
| 233 |
-
if dt_bias is not None:
|
| 234 |
-
dt = dt + dt_bias[None, None, :]
|
| 235 |
-
dt = torch.where(dt <= 20.0, torch.log(dt.exp() + 1), dt)
|
| 236 |
-
if seq_idx is not None:
|
| 237 |
-
dt = dt * (seq_idx.unsqueeze(-1) >= 0).to(dt.dtype)
|
| 238 |
if rmsnorm_weight is None:
|
| 239 |
out, out_x, dt_out, dA_cumsum, states, final_ssm_states = (
|
| 240 |
_mamba_chunk_scan_combined_fwd(
|
|
@@ -246,10 +241,10 @@ class ChunkableMambaSplitConv1dScanCombinedFn(torch.autograd.Function):
|
|
| 246 |
chunk_size=chunk_size,
|
| 247 |
D=D,
|
| 248 |
z=z,
|
| 249 |
-
dt_bias=
|
| 250 |
initial_states=initial_ssm_states,
|
| 251 |
seq_idx=seq_idx,
|
| 252 |
-
dt_softplus=
|
| 253 |
dt_limit=dt_limit,
|
| 254 |
)
|
| 255 |
)
|
|
@@ -268,10 +263,10 @@ class ChunkableMambaSplitConv1dScanCombinedFn(torch.autograd.Function):
|
|
| 268 |
chunk_size=chunk_size,
|
| 269 |
D=D,
|
| 270 |
z=None,
|
| 271 |
-
dt_bias=
|
| 272 |
initial_states=initial_ssm_states,
|
| 273 |
seq_idx=seq_idx,
|
| 274 |
-
dt_softplus=
|
| 275 |
dt_limit=dt_limit,
|
| 276 |
)
|
| 277 |
)
|
|
@@ -317,8 +312,8 @@ class ChunkableMambaSplitConv1dScanCombinedFn(torch.autograd.Function):
|
|
| 317 |
out = F.linear(out, outproj_weight, outproj_bias)
|
| 318 |
else:
|
| 319 |
assert outproj_bias is None
|
| 320 |
-
if out is not None and
|
| 321 |
-
out = out *
|
| 322 |
ctx.save_for_backward(
|
| 323 |
zxbcdt,
|
| 324 |
conv1d_weight,
|
|
|
|
| 189 |
)
|
| 190 |
assert dt_bias.shape == (nheads,)
|
| 191 |
assert A.shape == (nheads,)
|
| 192 |
+
seq_idx = seq_idx.contiguous() if seq_idx is not None else None
|
| 193 |
+
mask = (seq_idx.unsqueeze(-1) >= 0).to(zxbcdt.dtype) if seq_idx is not None else None
|
| 194 |
+
if mask is not None:
|
| 195 |
+
zxbcdt = zxbcdt * mask
|
| 196 |
zx0, z, xBC, dt = torch.split(
|
| 197 |
zxbcdt, [2 * d_nonssm, dim, dim + ngroups * dstate * 2, nheads], dim=-1
|
| 198 |
)
|
|
|
|
| 199 |
final_conv_states = (
|
| 200 |
torch.empty(
|
| 201 |
(batch, conv1d_weight.shape[1] - 1, dim + ngroups * dstate * 2),
|
|
|
|
| 209 |
# Additionally, there is a bug in causal_conv1d_fwd_function when seq_idx is used causing illegal memory access:
|
| 210 |
# - Issue: https://github.com/Dao-AILab/causal-conv1d/issues/67
|
| 211 |
# - PR: https://github.com/Dao-AILab/causal-conv1d/pull/101
|
|
|
|
|
|
|
| 212 |
xBC_conv = rearrange(
|
| 213 |
causal_conv1d_fwd_function(
|
| 214 |
rearrange_and_update_stride(xBC, "b s d -> b d s"),
|
|
|
|
| 221 |
),
|
| 222 |
"b d s -> b s d",
|
| 223 |
)
|
| 224 |
+
if mask is not None:
|
| 225 |
+
xBC_conv = xBC_conv * mask
|
| 226 |
x, B, C = torch.split(
|
| 227 |
xBC_conv, [dim, ngroups * dstate, ngroups * dstate], dim=-1
|
| 228 |
)
|
|
|
|
| 230 |
B = rearrange(B, "b l (g n) -> b l g n", g=ngroups)
|
| 231 |
C = rearrange(C, "b l (g n) -> b l g n", g=ngroups)
|
| 232 |
z = rearrange(z, "b l (h p) -> b l h p", h=nheads) if z is not None else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
if rmsnorm_weight is None:
|
| 234 |
out, out_x, dt_out, dA_cumsum, states, final_ssm_states = (
|
| 235 |
_mamba_chunk_scan_combined_fwd(
|
|
|
|
| 241 |
chunk_size=chunk_size,
|
| 242 |
D=D,
|
| 243 |
z=z,
|
| 244 |
+
dt_bias=dt_bias,
|
| 245 |
initial_states=initial_ssm_states,
|
| 246 |
seq_idx=seq_idx,
|
| 247 |
+
dt_softplus=True,
|
| 248 |
dt_limit=dt_limit,
|
| 249 |
)
|
| 250 |
)
|
|
|
|
| 263 |
chunk_size=chunk_size,
|
| 264 |
D=D,
|
| 265 |
z=None,
|
| 266 |
+
dt_bias=dt_bias,
|
| 267 |
initial_states=initial_ssm_states,
|
| 268 |
seq_idx=seq_idx,
|
| 269 |
+
dt_softplus=True,
|
| 270 |
dt_limit=dt_limit,
|
| 271 |
)
|
| 272 |
)
|
|
|
|
| 312 |
out = F.linear(out, outproj_weight, outproj_bias)
|
| 313 |
else:
|
| 314 |
assert outproj_bias is None
|
| 315 |
+
if out is not None and mask is not None:
|
| 316 |
+
out = out * mask
|
| 317 |
ctx.save_for_backward(
|
| 318 |
zxbcdt,
|
| 319 |
conv1d_weight,
|
modeling_chunkable_mamba2.py
CHANGED
|
@@ -41,7 +41,6 @@ class ChunkableMamba2Mixer(Mamba2Mixer):
|
|
| 41 |
)
|
| 42 |
|
| 43 |
# 1. Gated MLP's linear projection
|
| 44 |
-
hidden_states = apply_mask_to_padding_states(hidden_states, attention_mask[:, -hidden_states.size(1):])
|
| 45 |
projected_states = self.in_proj(hidden_states)
|
| 46 |
|
| 47 |
A = -torch.exp(self.A_log.float()) # (num_heads) or (intermediate_size, state_size)
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
# 1. Gated MLP's linear projection
|
|
|
|
| 44 |
projected_states = self.in_proj(hidden_states)
|
| 45 |
|
| 46 |
A = -torch.exp(self.A_log.float()) # (num_heads) or (intermediate_size, state_size)
|
safe_layernorm_gated.py
DELETED
|
@@ -1,16 +0,0 @@
|
|
| 1 |
-
# Copyright (c) 2024, Tri Dao.
|
| 2 |
-
# Based on the Triton LayerNorm tutorial: https://triton-lang.org/main/getting-started/tutorials/05-layer-norm.html
|
| 3 |
-
# For the backward pass, we keep weight_grad and bias_grad in registers and accumulate.
|
| 4 |
-
# This backward pass is faster for dimensions up to 8k, but after that it's much slower due to register spilling.
|
| 5 |
-
# The models we train have hidden dim up to 8k anyway (e.g. Llama 70B), so this is fine.
|
| 6 |
-
|
| 7 |
-
import math
|
| 8 |
-
|
| 9 |
-
import torch
|
| 10 |
-
import torch.nn.functional as F
|
| 11 |
-
|
| 12 |
-
import triton
|
| 13 |
-
import triton.language as tl
|
| 14 |
-
|
| 15 |
-
from einops import rearrange
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|