Text Generation
Transformers
RWKV
PyTorch
English
maba_sparse
maba
maba-v2
maba-v2-architecture
architecture
recurrent
dgda
decoupled-gated-delta-attention
gated-deltanet
linear-attention
linear-recurrence
sparse-attention
maba-sa
mla
multi-head-latent-attention
deepseek
qwen
minicpm
mamba
mamba-2
transformer
causal-lm
llm
nlp
long-context
1m-context
sub-quadratic
state-space-model
ssm
triton
flash-attention
on-device-ai
efficient-llm
nope
dg-indexer
centroid-indexing
hca
3-stream
swiglu
rmsnorm
speculative-decoding
mtp
multi-token-prediction
needle-in-a-haystack
scaling
100m
1b
3b
7b
30b
Instructions to use AndrewThompson1233/maba-v2-architecture with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AndrewThompson1233/maba-v2-architecture with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="AndrewThompson1233/maba-v2-architecture")# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("AndrewThompson1233/maba-v2-architecture", device_map="auto") - RWKV
How to use AndrewThompson1233/maba-v2-architecture with RWKV:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use AndrewThompson1233/maba-v2-architecture with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "AndrewThompson1233/maba-v2-architecture" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "AndrewThompson1233/maba-v2-architecture", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/AndrewThompson1233/maba-v2-architecture
- SGLang
How to use AndrewThompson1233/maba-v2-architecture with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "AndrewThompson1233/maba-v2-architecture" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "AndrewThompson1233/maba-v2-architecture", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "AndrewThompson1233/maba-v2-architecture" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "AndrewThompson1233/maba-v2-architecture", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use AndrewThompson1233/maba-v2-architecture with Docker Model Runner:
docker model run hf.co/AndrewThompson1233/maba-v2-architecture
File size: 11,136 Bytes
d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 fef798e d06ee60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | from typing import Any, Optional, Tuple, Union
import torch
import torch.nn as nn
import torch.nn.functional as F
from maba_sparse.kernels.dispatcher import dispatch_dgda_prefill, dispatch_dgda_step
class ConvState(tuple):
@property
def shape(self) -> torch.Size:
if len(self) == 0:
return torch.Size([0, 0, 0, 0])
f = self[0]
return torch.Size([f.shape[0], len(self), f.shape[1], f.shape[2]])
def as_tensor(self) -> torch.Tensor:
return torch.stack(self, dim=1)
class DGDALayer(nn.Module):
def __init__(
self,
config: Optional[Any] = None,
dim: int = 640,
n_heads: int = 10,
d_head: Optional[int] = None,
kernel_size: int = 4,
eps: float = 1e-6,
) -> None:
super().__init__()
if config is not None:
dim = getattr(config, "dim", getattr(config, "d_model", dim))
n_heads = getattr(config, "n_heads", getattr(config, "num_heads", n_heads))
d_head = getattr(config, "d_head", d_head)
kernel_size = getattr(
config, "kernel_size", getattr(config, "conv_kernel_size", kernel_size)
)
eps = getattr(config, "eps", getattr(config, "rms_norm_eps", eps))
self.dim = dim
self.n_heads = n_heads
self.d_head = d_head if d_head is not None else (dim // n_heads)
self.d_k = getattr(config, "d_k", self.d_head) if config is not None else self.d_head
self.d_v = getattr(config, "d_v", self.d_head) if config is not None else self.d_head
self.kernel_size = kernel_size
self.k_size = kernel_size
self.eps = eps
self.chunk_size = getattr(config, "chunk_size", 16)
self.inversion_method = getattr(config, "inversion_method", "adaptive")
self.adaptive_tol = getattr(config, "adaptive_tol", 7e-5)
qk_dim = self.n_heads * self.d_k
v_dim = self.n_heads * self.d_v
self.q_proj = nn.Linear(self.dim, qk_dim, bias=False)
self.k_proj = nn.Linear(self.dim, qk_dim, bias=False)
self.v_proj = nn.Linear(self.dim, v_dim, bias=False)
self.conv_q = nn.Conv1d(
qk_dim, qk_dim, self.kernel_size, groups=qk_dim, bias=False, padding=0
)
self.conv_k = nn.Conv1d(
qk_dim, qk_dim, self.kernel_size, groups=qk_dim, bias=False, padding=0
)
self.conv_v = nn.Conv1d(
v_dim, v_dim, self.kernel_size, groups=v_dim, bias=False, padding=0
)
self.gate_alpha = nn.Linear(self.dim, qk_dim, bias=False)
self.gate_erase = nn.Linear(self.dim, qk_dim, bias=False)
self.gate_write = nn.Linear(self.dim, v_dim, bias=False)
self.o_proj = nn.Linear(v_dim, self.dim, bias=False)
self._reset_parameters()
@property
def alpha_proj(self) -> nn.Linear:
return self.gate_alpha
@property
def b_proj(self) -> nn.Linear:
return self.gate_erase
@property
def w_proj(self) -> nn.Linear:
return self.gate_write
def _reset_parameters(self) -> None:
nn.init.xavier_uniform_(self.q_proj.weight)
nn.init.xavier_uniform_(self.k_proj.weight)
nn.init.xavier_uniform_(self.v_proj.weight)
nn.init.xavier_uniform_(self.gate_alpha.weight)
nn.init.xavier_uniform_(self.gate_erase.weight)
nn.init.xavier_uniform_(self.gate_write.weight)
nn.init.xavier_uniform_(self.o_proj.weight)
nn.init.normal_(self.conv_q.weight, mean=0.0, std=0.02)
nn.init.normal_(self.conv_k.weight, mean=0.0, std=0.02)
nn.init.normal_(self.conv_v.weight, mean=0.0, std=0.02)
def _apply_conv(
self,
x: torch.Tensor,
conv: nn.Conv1d,
conv_state: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
k = self.kernel_size
xt = x.transpose(1, 2)
if conv_state is not None:
if k > 1 and conv_state.shape[-1] != k - 1:
if conv_state.shape[-1] < k - 1:
conv_state = F.pad(conv_state, (k - 1 - conv_state.shape[-1], 0))
else:
conv_state = conv_state[:, :, -(k - 1):]
p = torch.cat([conv_state, xt], dim=2)
else:
p = F.pad(xt, (k - 1, 0)) if k > 1 else xt
ns = p[:, :, -(k - 1):].contiguous() if k > 1 else p[:, :, :0].contiguous()
y = F.silu(conv(p)).transpose(1, 2)
return y, ns
def _unpack_conv_state(
self,
conv_state: Optional[Union[ConvState, Tuple[torch.Tensor, ...], torch.Tensor]],
b_size: int,
device: torch.device,
dtype: torch.dtype,
) -> Tuple[Optional[torch.Tensor], Optional[torch.Tensor], Optional[torch.Tensor]]:
if conv_state is None:
return None, None, None
def _cast(t: Optional[torch.Tensor]) -> Optional[torch.Tensor]:
if t is None:
return None
return t.to(device=device, dtype=dtype)
k_hist = max(self.kernel_size - 1, 0)
if isinstance(conv_state, (tuple, list)):
if len(conv_state) == 3:
return _cast(conv_state[0]), _cast(conv_state[1]), _cast(conv_state[2])
if len(conv_state) == 1 and isinstance(conv_state[0], (tuple, list)):
return _cast(conv_state[0][0]), _cast(conv_state[0][1]), _cast(conv_state[0][2])
if isinstance(conv_state, torch.Tensor):
if conv_state.dim() == 4 and conv_state.shape[1] == 3:
return _cast(conv_state[:, 0]), _cast(conv_state[:, 1]), _cast(conv_state[:, 2])
if conv_state.dim() == 5 and conv_state.shape[1] == 3:
cs = conv_state.reshape(b_size, 3, conv_state.shape[2], k_hist)
return _cast(cs[:, 0]), _cast(cs[:, 1]), _cast(cs[:, 2])
cs = conv_state.reshape(b_size, -1, k_hist)
tot_ch = cs.shape[1]
qk_ch = self.n_heads * self.d_k
v_ch = self.n_heads * self.d_v
if tot_ch == 2 * qk_ch + v_ch:
return _cast(cs[:, :qk_ch]), _cast(cs[:, qk_ch : 2 * qk_ch]), _cast(cs[:, 2 * qk_ch :])
if tot_ch == qk_ch and qk_ch == v_ch:
return _cast(cs), _cast(cs), _cast(cs)
if tot_ch == qk_ch:
zero_v = torch.zeros(b_size, v_ch, k_hist, device=device, dtype=dtype)
return _cast(cs), _cast(cs), zero_v
return _cast(cs), _cast(cs), _cast(cs)
raise ValueError(f"Unsupported conv_state shape or type: {type(conv_state)}")
def forward(
self,
x: torch.Tensor,
state: Optional[torch.Tensor] = None,
conv_state: Optional[Union[torch.Tensor, Tuple[torch.Tensor, ...]]] = None,
chunk_size: Optional[int] = None,
inversion_method: Optional[str] = None,
) -> Tuple[torch.Tensor, torch.Tensor, ConvState]:
b, l, d = x.shape
h, dk, dv = self.n_heads, self.d_k, self.d_v
qk_dim = h * dk
v_dim = h * dv
if l == 0:
eo = torch.empty(b, 0, d, dtype=x.dtype, device=x.device)
es = state.clone() if state is not None else torch.zeros(b, h, dk, dv, dtype=x.dtype, device=x.device)
k_hist = max(self.kernel_size - 1, 0)
zc_qk = torch.zeros(b, qk_dim, k_hist, dtype=x.dtype, device=x.device)
zc_v = torch.zeros(b, v_dim, k_hist, dtype=x.dtype, device=x.device)
return eo, es, ConvState((zc_qk, zc_qk, zc_v))
cq, ck, cv = self._unpack_conv_state(conv_state, b, x.device, x.dtype)
q, nq = self._apply_conv(self.q_proj(x), self.conv_q, cq)
k, nk = self._apply_conv(self.k_proj(x), self.conv_k, ck)
v, nv = self._apply_conv(self.v_proj(x), self.conv_v, cv)
ncs = ConvState((nq, nk, nv))
eb = torch.sigmoid(self.gate_erase(x))
ew = torch.sigmoid(self.gate_write(x))
la = -F.softplus(self.gate_alpha(x))
la = torch.clamp(la, min=-14.0)
alpha = torch.exp(la)
q = q.view(b, l, h, dk).transpose(1, 2)
k = k.view(b, l, h, dk).transpose(1, 2)
v = v.view(b, l, h, dv).transpose(1, 2)
eb = eb.view(b, l, h, dk).transpose(1, 2)
ew = ew.view(b, l, h, dv).transpose(1, 2)
alpha = alpha.view(b, l, h, dk).transpose(1, 2)
la = la.view(b, l, h, dk).transpose(1, 2)
k = k / (torch.linalg.vector_norm(k, dim=-1, keepdim=True) + self.eps)
k = k.to(q.dtype)
v = v.to(q.dtype)
eb = eb.to(q.dtype)
ew = ew.to(q.dtype)
alpha = alpha.to(q.dtype)
la = la.to(q.dtype)
if state is not None:
state = state.to(q.dtype)
cs_val = chunk_size if chunk_size is not None else self.chunk_size
inv_val = inversion_method if inversion_method is not None else self.inversion_method
o, cs = dispatch_dgda_prefill(
q=q,
k=k,
v=v,
alpha=alpha,
b=eb,
w=ew,
chunk_size=cs_val,
initial_state=state,
inversion_method=inv_val,
adaptive_tol=self.adaptive_tol,
log_alpha=la,
)
o = o.transpose(1, 2).contiguous().view(b, l, h * dv)
return self.o_proj(o), cs, ncs
def step(
self,
x: torch.Tensor,
state: Optional[torch.Tensor] = None,
conv_state: Optional[Union[torch.Tensor, Tuple[torch.Tensor, ...]]] = None,
) -> Tuple[torch.Tensor, torch.Tensor, ConvState]:
b, l = x.shape[0], x.shape[1]
assert l == 1
h, dk, dv = self.n_heads, self.d_k, self.d_v
cq, ck, cv = self._unpack_conv_state(conv_state, b, x.device, x.dtype)
q, nq = self._apply_conv(self.q_proj(x), self.conv_q, cq)
k, nk = self._apply_conv(self.k_proj(x), self.conv_k, ck)
v, nv = self._apply_conv(self.v_proj(x), self.conv_v, cv)
ncs = ConvState((nq, nk, nv))
eb = torch.sigmoid(self.gate_erase(x)).view(b, 1, h, dk).transpose(1, 2)
ew = torch.sigmoid(self.gate_write(x)).view(b, 1, h, dv).transpose(1, 2)
la = -F.softplus(self.gate_alpha(x)).view(b, 1, h, dk).transpose(1, 2)
la = torch.clamp(la, min=-14.0)
alpha = torch.exp(la)
q = q.view(b, 1, h, dk).transpose(1, 2)
k = k.view(b, 1, h, dk).transpose(1, 2)
v = v.view(b, 1, h, dv).transpose(1, 2)
k = k / (torch.linalg.vector_norm(k, dim=-1, keepdim=True) + self.eps)
k = k.to(q.dtype)
v = v.to(q.dtype)
eb = eb.to(q.dtype)
ew = ew.to(q.dtype)
alpha = alpha.to(q.dtype)
la = la.to(q.dtype)
if state is not None:
state = state.to(q.dtype)
oh, st = dispatch_dgda_step(
q=q,
k=k,
v=v,
alpha=alpha,
b=eb,
w=ew,
state=state,
log_alpha=la,
)
o = self.o_proj(oh.reshape(b, 1, h * dv))
return o, st, ncs
DecoupledGatedDeltaAttention = DGDALayer
|