| """ |
| Taken and modified from alxndrTL's othello_mamba repository: |
| https://github.com/alxndrTL/othello_mamba |
| """ |
|
|
| import math |
| import inspect |
| from dataclasses import dataclass |
| from typing import Union |
|
|
| import torch |
| import torch.nn as nn |
| import torch.nn.functional as F |
|
|
| |
| |
| |
| try: |
| from mamba_ssm.ops.selective_scan_interface import selective_scan_fn |
| _HAS_SELECTIVE_SCAN = True |
| except Exception: |
| selective_scan_fn = None |
| _HAS_SELECTIVE_SCAN = False |
|
|
|
|
| class PScan(torch.autograd.Function): |
| @staticmethod |
| def pscan(A, X): |
| |
| |
|
|
| |
| |
| |
| |
|
|
| B, D, L, _ = A.size() |
| num_steps = int(math.log2(L)) |
|
|
| |
| Aa = A |
| Xa = X |
| for k in range(num_steps): |
| T = 2 * (Xa.size(2) // 2) |
|
|
| Aa = Aa[:, :, :T].view(B, D, T // 2, 2, -1) |
| Xa = Xa[:, :, :T].view(B, D, T // 2, 2, -1) |
|
|
| Xa[:, :, :, 1].add_(Aa[:, :, :, 1].mul(Xa[:, :, :, 0])) |
| Aa[:, :, :, 1].mul_(Aa[:, :, :, 0]) |
|
|
| Aa = Aa[:, :, :, 1] |
| Xa = Xa[:, :, :, 1] |
|
|
| |
| for k in range(num_steps - 1, -1, -1): |
| Aa = A[:, :, 2**k - 1 : L : 2**k] |
| Xa = X[:, :, 2**k - 1 : L : 2**k] |
|
|
| T = 2 * (Xa.size(2) // 2) |
|
|
| if T < Xa.size(2): |
| Xa[:, :, -1].add_(Aa[:, :, -1].mul(Xa[:, :, -2])) |
| Aa[:, :, -1].mul_(Aa[:, :, -2]) |
|
|
| Aa = Aa[:, :, :T].view(B, D, T // 2, 2, -1) |
| Xa = Xa[:, :, :T].view(B, D, T // 2, 2, -1) |
|
|
| Xa[:, :, 1:, 0].add_(Aa[:, :, 1:, 0].mul(Xa[:, :, :-1, 1])) |
| Aa[:, :, 1:, 0].mul_(Aa[:, :, :-1, 1]) |
|
|
| @staticmethod |
| def forward(ctx, A_in, X_in): |
| """ |
| Applies the parallel scan operation, as defined above. Returns a new tensor. |
| |
| Args: |
| A_in : (B, L, D, N) |
| X_in : (B, L, D, N) |
| |
| Returns: |
| H : (B, L, D, N) |
| """ |
|
|
| |
| A = A_in.clone() |
| X = X_in.clone() |
|
|
| |
| A = A.transpose(2, 1) |
| X = X.transpose(2, 1) |
|
|
| |
| PScan.pscan(A, X) |
|
|
| ctx.save_for_backward(A_in, X) |
|
|
| return X.transpose(2, 1) |
|
|
| @staticmethod |
| def backward(ctx, grad_output_in): |
| """ |
| Flows the gradient from the output to the input. Returns two new tensors. |
| |
| Args: |
| ctx : A_in : (B, L, D, N), X : (B, D, L, N) |
| grad_output_in : (B, L, D, N) |
| |
| Returns: |
| gradA : (B, L, D, N), gradX : (B, L, D, N) |
| """ |
|
|
| A_in, X = ctx.saved_tensors |
|
|
| |
| A = A_in.clone() |
| |
|
|
| |
| A = A.transpose(2, 1) |
| A = torch.cat((A[:, :, :1], A[:, :, 1:].flip(2)), dim=2) |
| grad_output_b = grad_output_in.transpose(2, 1) |
|
|
| |
| grad_output_b = grad_output_b.flip(2) |
| PScan.pscan(A, grad_output_b) |
| grad_output_b = grad_output_b.flip(2) |
|
|
| Q = torch.zeros_like(X) |
| Q[:, :, 1:].add_(X[:, :, :-1] * grad_output_b[:, :, 1:]) |
|
|
| return Q.transpose(2, 1), grad_output_b.transpose(2, 1) |
|
|
|
|
| @dataclass |
| class MambaConfig: |
| n_embd: int |
| n_layer: int |
| dt_rank: Union[int, str] = "auto" |
| d_state: int = 16 |
| expand_factor: int = 2 |
| d_conv: int = 4 |
| vocab_size: int = 64 |
|
|
| dt_min: float = 0.001 |
| dt_max: float = 0.1 |
| dt_init: str = "random" |
| dt_scale: float = 1.0 |
| dt_init_floor = 1e-4 |
|
|
| rms_norm_eps: float = 1e-5 |
|
|
| bias: bool = False |
| conv_bias: bool = True |
| inner_layernorms: bool = False |
|
|
| pscan: bool = True |
| use_cuda: bool = True |
|
|
| model_type: str = "mamba" |
|
|
| |
| num_states: int = 64 |
| num_state_dimensions: int = 1 |
| predict_type: str = "next_token" |
| pad_id: int = -1 |
| freeze_reps: bool = False |
|
|
| def __post_init__(self): |
| self.d_inner = self.expand_factor * self.n_embd |
|
|
| if self.dt_rank == "auto": |
| self.dt_rank = math.ceil(self.n_embd / 16) |
|
|
|
|
| class Mamba(nn.Module): |
| def __init__(self, config: MambaConfig): |
| super().__init__() |
|
|
| self.config = config |
| self.embedding = nn.Embedding(config.vocab_size, config.n_embd, padding_idx=0) |
| self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False) |
| self.lm_head.weight = self.embedding.weight |
|
|
| if config.model_type in ("mamba", "mamba2"): |
| self.layers = nn.ModuleList( |
| [ResidualBlock(config) for _ in range(config.n_layer)] |
| ) |
| self.out_norm = RMSNorm(config.n_embd, config.rms_norm_eps) |
| elif config.model_type == "lstm": |
| self.layers = nn.LSTM( |
| config.n_embd, config.n_embd, config.n_layer, batch_first=True |
| ) |
| elif config.model_type == "rnn": |
| self.layers = nn.RNN( |
| config.n_embd, config.n_embd, config.n_layer, batch_first=True |
| ) |
| else: |
| raise ValueError("Invalid model_type") |
|
|
| if config.predict_type == "state": |
| self.state_predictor = nn.Linear( |
| config.n_embd, |
| config.num_states * config.num_state_dimensions, |
| bias=True, |
| ) |
|
|
| self.apply(self._init_weights) |
| for pn, p in self.named_parameters(): |
| if pn.endswith(("fc_3.weight", "c_proj.weight")): |
| torch.nn.init.normal_( |
| p, mean=0.0, std=0.02 / math.sqrt(2 * self.config.n_layer) |
| ) |
|
|
| if self.config.freeze_reps: |
| for name, param in self.named_parameters(): |
| if "lm_head" not in name and "state_predictor" not in name: |
| param.requires_grad = False |
|
|
| print(f"number of parameters: {self.get_num_params() / 1e6:.2f}M") |
|
|
| def forward(self, idx, targets=None): |
| |
|
|
| |
| b, t = idx.size() |
| x = self.embedding(idx) |
|
|
| if self.config.model_type in ("mamba", "mamba2"): |
| for layer in self.layers: |
| x = layer(x) |
| x = self.out_norm(x) |
| elif self.config.model_type in ("lstm", "rnn"): |
| x, _ = self.layers(x) |
|
|
| if self.config.freeze_reps: |
| x = x.detach() |
|
|
| if self.config.predict_type == "state": |
| logits = self.state_predictor(x) |
| if self.config.num_state_dimensions > 1: |
| logits = logits.view( |
| b, t, self.config.num_state_dimensions, self.config.num_states |
| ) |
| loss = F.cross_entropy( |
| logits.view(-1, logits.size(-1)), |
| targets.view(-1), |
| reduction="none", |
| ) |
| mask = idx != self.config.pad_id |
| if self.config.num_state_dimensions > 1: |
| loss = loss.view(b, t, self.config.num_state_dimensions).sum(-1) |
| else: |
| loss = loss.view(b, t) |
| loss = (loss * mask).sum() / mask.sum() |
| else: |
| if targets is not None: |
| |
| logits = self.lm_head(x) |
| loss = F.cross_entropy( |
| logits.view(-1, logits.size(-1)), |
| targets.view(-1), |
| ignore_index=self.config.pad_id, |
| ) |
| else: |
| |
| logits = self.lm_head( |
| x[:, [-1], :] |
| ) |
| loss = None |
|
|
| return logits, loss |
|
|
| @torch.no_grad() |
| def generate(self, idx, max_new_tokens, temperature=1.0, top_k=None, return_confidence=False): |
| """Autoregressively complete idx (B, T) by re-forwarding the full sequence each step. |
| |
| Mamba is recurrent and has no fixed context window, so no cropping is needed. |
| Matches the return contract of model.transformer.GPT.generate: |
| return_confidence=False -> idx |
| return_confidence=True -> (idx, confidences, top3_tokens, top3_probs) |
| For B == 1 the confidence outputs are flat lists indexed by time step; for |
| B > 1 they are per-sample lists of shape (B, T[, 3]). |
| """ |
| confidences = [] if return_confidence else None |
| top3_tokens = [] if return_confidence else None |
| top3_probs = [] if return_confidence else None |
| B = idx.size(0) |
|
|
| for _ in range(max_new_tokens): |
| logits, _ = self(idx) |
| if temperature <= 0: |
| |
| probs = F.softmax(logits[:, -1, :], dim=-1) |
| idx_next = probs.argmax(dim=-1, keepdim=True) |
| else: |
| logits = logits[:, -1, :] / temperature |
| if top_k is not None: |
| v, _ = torch.topk(logits, min(top_k, logits.size(-1))) |
| logits[logits < v[:, [-1]]] = -float('Inf') |
| probs = F.softmax(logits, dim=-1) |
| idx_next = torch.multinomial(probs, num_samples=1) |
|
|
| if return_confidence: |
| sampled_probs = probs.gather(1, idx_next).squeeze(-1) |
| confidences.append(sampled_probs.cpu().tolist()) |
| top3_prob_vals, top3_token_ids = torch.topk(probs, 3, dim=-1) |
| top3_tokens.append(top3_token_ids.cpu().tolist()) |
| top3_probs.append(top3_prob_vals.cpu().tolist()) |
|
|
| idx = torch.cat((idx, idx_next), dim=1) |
|
|
| if return_confidence: |
| if B == 1: |
| return (idx, |
| [c[0] for c in confidences], |
| [t[0] for t in top3_tokens], |
| [p[0] for p in top3_probs]) |
| T = len(confidences) |
| conf_bs = [[confidences[t][b] for t in range(T)] for b in range(B)] |
| tok_bs = [[top3_tokens[t][b] for t in range(T)] for b in range(B)] |
| prob_bs = [[top3_probs[t][b] for t in range(T)] for b in range(B)] |
| return idx, conf_bs, tok_bs, prob_bs |
| return idx |
|
|
| def step(self, x, caches): |
| |
| |
|
|
| |
| |
|
|
| for i, layer in enumerate(self.layers): |
| x, caches[i] = layer.step(x, caches[i]) |
|
|
| return x, caches |
|
|
| def _init_weights(self, module): |
| if isinstance(module, nn.Linear): |
| torch.nn.init.normal_(module.weight, mean=0.0, std=0.02) |
| if module.bias is not None: |
| torch.nn.init.zeros_(module.bias) |
| elif isinstance(module, nn.Embedding): |
| torch.nn.init.normal_(module.weight, mean=0.0, std=0.02) |
|
|
| def configure_optimizers(self, weight_decay, learning_rate, betas, device_type): |
| |
| param_dict = {pn: p for pn, p in self.named_parameters()} |
| |
| param_dict = {pn: p for pn, p in param_dict.items() if p.requires_grad} |
| |
| |
| decay_params = [p for n, p in param_dict.items() if p.dim() >= 2] |
| nodecay_params = [p for n, p in param_dict.items() if p.dim() < 2] |
| optim_groups = [ |
| {"params": decay_params, "weight_decay": weight_decay}, |
| {"params": nodecay_params, "weight_decay": 0.0}, |
| ] |
| num_decay_params = sum(p.numel() for p in decay_params) |
| num_nodecay_params = sum(p.numel() for p in nodecay_params) |
| print( |
| f"num decayed parameter tensors: {len(decay_params)}, with {num_decay_params:,} parameters" |
| ) |
| print( |
| f"num non-decayed parameter tensors: {len(nodecay_params)}, with {num_nodecay_params:,} parameters" |
| ) |
| |
| fused_available = "fused" in inspect.signature(torch.optim.AdamW).parameters |
| use_fused = fused_available and device_type == "cuda" |
| extra_args = dict(fused=True) if use_fused else {} |
| optimizer = torch.optim.AdamW( |
| optim_groups, lr=learning_rate, betas=betas, **extra_args |
| ) |
| print(f"using fused AdamW: {use_fused}") |
| return optimizer |
|
|
| def estimate_mfu(self, fwdbwd_per_iter, dt): |
| return -1 |
|
|
| def get_num_params(self, non_embedding=True): |
| """ |
| Return the number of parameters in the model. |
| For non-embedding count (default), the position embeddings get subtracted. |
| The token embeddings would too, except due to the parameter sharing these |
| params are actually used as weights in the final layer, so we include them. |
| """ |
| n_params = sum(p.numel() for p in self.parameters()) |
| if non_embedding: |
| n_params -= self.embedding.weight.numel() |
| return n_params |
|
|
|
|
| class ResidualBlock(nn.Module): |
| def __init__(self, config: MambaConfig): |
| super().__init__() |
|
|
| if config.model_type == "mamba": |
| self.mixer = MambaBlock(config) |
| elif config.model_type == "mamba2": |
| from mamba_ssm import Mamba2 as Mamba2SSM |
|
|
| self.mixer = Mamba2SSM( |
| |
| d_model=config.n_embd, |
| d_state=config.d_state, |
| d_conv=config.d_conv, |
| expand=config.expand_factor, |
| ) |
|
|
| self.norm = RMSNorm(config.n_embd, config.rms_norm_eps) |
|
|
| def forward(self, x): |
| |
|
|
| |
|
|
| output = self.mixer(self.norm(x)) + x |
| return output |
|
|
| def step(self, x, cache): |
| |
| |
| |
| |
|
|
| |
| |
|
|
| output, cache = self.mixer.step(self.norm(x), cache) |
| output = output + x |
| return output, cache |
|
|
|
|
| class MambaBlock(nn.Module): |
| def __init__(self, config: MambaConfig): |
| super().__init__() |
|
|
| self.config = config |
| assert isinstance(config.dt_rank, int) |
| assert isinstance(self.config.dt_rank, int) |
|
|
| |
| self.in_proj = nn.Linear(config.n_embd, 2 * config.d_inner, bias=config.bias) |
|
|
| self.conv1d = nn.Conv1d( |
| in_channels=config.d_inner, |
| out_channels=config.d_inner, |
| kernel_size=config.d_conv, |
| bias=config.conv_bias, |
| groups=config.d_inner, |
| padding=config.d_conv - 1, |
| ) |
|
|
| |
| self.x_proj = nn.Linear( |
| config.d_inner, config.dt_rank + 2 * config.d_state, bias=False |
| ) |
|
|
| |
| self.dt_proj = nn.Linear(config.dt_rank, config.d_inner, bias=True) |
|
|
| |
| |
| dt_init_std = config.dt_rank**-0.5 * config.dt_scale |
| if config.dt_init == "constant": |
| nn.init.constant_(self.dt_proj.weight, dt_init_std) |
| elif config.dt_init == "random": |
| nn.init.uniform_(self.dt_proj.weight, -dt_init_std, dt_init_std) |
| else: |
| raise NotImplementedError |
|
|
| |
| dt = torch.exp( |
| torch.rand(config.d_inner) |
| * (math.log(config.dt_max) - math.log(config.dt_min)) |
| + math.log(config.dt_min) |
| ).clamp(min=config.dt_init_floor) |
| inv_dt = dt + torch.log( |
| -torch.expm1(-dt) |
| ) |
| with torch.no_grad(): |
| self.dt_proj.bias.copy_(inv_dt) |
| |
| |
|
|
| |
| A = torch.arange(1, config.d_state + 1, dtype=torch.float32).repeat( |
| config.d_inner, 1 |
| ) |
| self.A_log = nn.Parameter( |
| torch.log(A) |
| ) |
| self.A_log._no_weight_decay = True |
|
|
| self.D = nn.Parameter(torch.ones(config.d_inner)) |
|
|
| |
| self.out_proj = nn.Linear(config.d_inner, config.n_embd, bias=config.bias) |
|
|
| self.dt_layernorm: RMSNorm | None = None |
| self.B_layernorm: RMSNorm | None = None |
| self.C_layernorm: RMSNorm | None = None |
|
|
| if self.config.inner_layernorms: |
| self.dt_layernorm = RMSNorm(self.config.dt_rank, config.rms_norm_eps) |
| self.B_layernorm = RMSNorm(self.config.d_state, config.rms_norm_eps) |
| self.C_layernorm = RMSNorm(self.config.d_state, config.rms_norm_eps) |
|
|
| if self.config.use_cuda: |
| if not _HAS_SELECTIVE_SCAN: |
| raise ImportError( |
| "config.use_cuda=True but the official mamba_ssm selective-scan " |
| "kernel is not available. Install mamba-ssm, or set use_cuda=False " |
| "to use the pure-PyTorch parallel scan.") |
| self.selective_scan_cuda = selective_scan_fn |
|
|
| def _apply_layernorms(self, dt, B, C): |
| if self.dt_layernorm is not None: |
| dt = self.dt_layernorm(dt) |
| if self.B_layernorm is not None: |
| B = self.B_layernorm(B) |
| if self.C_layernorm is not None: |
| C = self.C_layernorm(C) |
| return dt, B, C |
|
|
| def forward(self, x): |
| |
|
|
| |
|
|
| _, L, _ = x.shape |
|
|
| xz = self.in_proj(x) |
| x, z = xz.chunk(2, dim=-1) |
|
|
| |
| x = x.transpose(1, 2) |
| x = self.conv1d(x)[ |
| :, :, :L |
| ] |
| x = x.transpose(1, 2) |
|
|
| x = F.silu(x) |
| y = self.ssm(x, z) |
|
|
| if self.config.use_cuda: |
| output = self.out_proj(y) |
| return output |
|
|
| |
| z = F.silu(z) |
|
|
| output = y * z |
| output = self.out_proj(output) |
|
|
| return output |
|
|
| def ssm(self, x, z): |
| |
|
|
| |
|
|
| A = -torch.exp(self.A_log.float()) |
| D = self.D.float() |
|
|
| deltaBC = self.x_proj(x) |
| delta, B, C = torch.split( |
| deltaBC, |
| [self.config.dt_rank, self.config.d_state, self.config.d_state], |
| dim=-1, |
| ) |
| delta, B, C = self._apply_layernorms(delta, B, C) |
| delta = self.dt_proj.weight @ delta.transpose( |
| 1, 2 |
| ) |
|
|
| if self.config.use_cuda: |
| x = x.transpose(1, 2) |
| B = B.transpose(1, 2).to(x.dtype) |
| C = C.transpose(1, 2).to(x.dtype) |
| z = z.transpose(1, 2).to(x.dtype) |
| y = self.selective_scan_cuda( |
| x, |
| delta, |
| A, |
| B, |
| C, |
| D, |
| z=z, |
| delta_softplus=True, |
| delta_bias=self.dt_proj.bias.float(), |
| ) |
| y = y.transpose(1, 2) |
|
|
| else: |
| delta = delta.transpose(1, 2) |
| delta = F.softplus(delta + self.dt_proj.bias) |
|
|
| if self.config.pscan: |
| y = self.selective_scan(x, delta, A, B, C, D) |
| else: |
| y = self.selective_scan_seq(x, delta, A, B, C, D) |
|
|
| return y |
|
|
| def selective_scan(self, x, delta, A, B, C, D): |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| deltaA = torch.exp(delta.unsqueeze(-1) * A) |
| deltaB = delta.unsqueeze(-1) * B.unsqueeze(2) |
|
|
| BX = deltaB * (x.unsqueeze(-1)) |
|
|
| hs = PScan.apply(deltaA, BX) |
|
|
| y = (hs @ C.unsqueeze(-1)).squeeze( |
| 3 |
| ) |
|
|
| y = y + D * x |
|
|
| return y |
|
|
| def selective_scan_seq(self, x, delta, A, B, C, D): |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| _, L, _ = x.shape |
|
|
| deltaA = torch.exp(delta.unsqueeze(-1) * A) |
| deltaB = delta.unsqueeze(-1) * B.unsqueeze(2) |
|
|
| BX = deltaB * (x.unsqueeze(-1)) |
|
|
| h = torch.zeros( |
| x.size(0), self.config.d_inner, self.config.d_state, device=deltaA.device |
| ) |
| hs = [] |
|
|
| for t in range(0, L): |
| h = deltaA[:, t] * h + BX[:, t] |
| hs.append(h) |
|
|
| hs = torch.stack(hs, dim=1) |
|
|
| y = (hs @ C.unsqueeze(-1)).squeeze( |
| 3 |
| ) |
|
|
| y = y + D * x |
|
|
| return y |
|
|
| def step(self, x, cache): |
| |
| |
| |
| |
|
|
| |
| |
|
|
| h, inputs = cache |
|
|
| xz = self.in_proj(x) |
| x, z = xz.chunk(2, dim=1) |
|
|
| |
| x_cache = x.unsqueeze(2) |
| x = self.conv1d(torch.cat([inputs, x_cache], dim=2))[ |
| :, :, self.config.d_conv - 1 |
| ] |
|
|
| x = F.silu(x) |
| y, h = self.ssm_step(x, h) |
|
|
| |
| z = F.silu(z) |
|
|
| output = y * z |
| output = self.out_proj(output) |
|
|
| |
| inputs = torch.cat([inputs[:, :, 1:], x_cache], dim=2) |
| cache = (h, inputs) |
|
|
| return output, cache |
|
|
| def ssm_step(self, x, h): |
| |
| |
|
|
| |
| |
|
|
| A = -torch.exp( |
| self.A_log.float() |
| ) |
| D = self.D.float() |
|
|
| deltaBC = self.x_proj(x) |
|
|
| delta, B, C = torch.split( |
| deltaBC, |
| [self.config.dt_rank, self.config.d_state, self.config.d_state], |
| dim=-1, |
| ) |
| delta, B, C = self._apply_layernorms(delta, B, C) |
| delta = F.softplus(self.dt_proj(delta)) |
|
|
| deltaA = torch.exp(delta.unsqueeze(-1) * A) |
| deltaB = delta.unsqueeze(-1) * B.unsqueeze(1) |
|
|
| BX = deltaB * (x.unsqueeze(-1)) |
|
|
| if h is None: |
| h = torch.zeros( |
| x.size(0), |
| self.config.d_inner, |
| self.config.d_state, |
| device=deltaA.device, |
| ) |
|
|
| h = deltaA * h + BX |
|
|
| y = (h @ C.unsqueeze(-1)).squeeze(2) |
|
|
| y = y + D * x |
|
|
| return y, h |
|
|
|
|
| |
| class RMSNorm(nn.Module): |
| def __init__(self, n_embd: int, eps: float = 1e-5): |
| super().__init__() |
|
|
| self.eps = eps |
| self.weight = nn.Parameter(torch.ones(n_embd)) |
|
|
| def forward(self, x): |
| output = ( |
| x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) * self.weight |
| ) |
|
|
| return output |
|
|