File size: 939 Bytes
5fed0fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch

def chunk_scan(X: torch.Tensor, A: torch.Tensor, B: torch.Tensor, chunk: int = 128, BD: int = 128) -> torch.Tensor:
    """
    Baseline Mamba2 chunked scan implementation using PyTorch.
    
    Args:
        X: Input tensor of shape (L, D) - input sequence
        A: Input tensor of shape (L, D) - decay factors
        B: Input tensor of shape (L, D) - input weights
        chunk: Chunk size for parallel processing (default 128)
        BD: Block dimension for feature dimension tiling (default 128) - unused in baseline
    
    Returns:
        Output tensor of shape (L, D) - scan output
    """
    # y_t = a_t * y_{t-1} + b_t * x_t
    L, D = X.shape
    y = torch.zeros(D, device=X.device, dtype=torch.float32)
    out = torch.empty(L, D, device=X.device, dtype=torch.float32)
    for t in range(L):
        y = A[t].float() * y + B[t].float() * X[t].float()
        out[t] = y
    return out.to(torch.float16)