File size: 6,896 Bytes
78f9417
 
 
 
 
 
 
 
 
 
 
 
 
 
d11d0a9
78f9417
 
d11d0a9
 
78f9417
 
 
 
d11d0a9
78f9417
 
 
 
 
 
d11d0a9
78f9417
 
d11d0a9
78f9417
 
 
 
 
 
 
 
 
 
 
d11d0a9
78f9417
 
d11d0a9
78f9417
 
 
 
d11d0a9
78f9417
 
 
 
d11d0a9
78f9417
 
 
 
 
 
 
 
 
 
 
 
 
 
d11d0a9
78f9417
 
 
 
 
d11d0a9
78f9417
 
 
 
 
 
 
 
 
 
 
d11d0a9
78f9417
 
 
 
 
 
 
d11d0a9
78f9417
 
d11d0a9
78f9417
 
d11d0a9
78f9417
 
 
 
 
 
 
 
d11d0a9
78f9417
 
 
d11d0a9
78f9417
 
d11d0a9
78f9417
d11d0a9
78f9417
 
d11d0a9
78f9417
 
 
 
 
 
 
d11d0a9
 
78f9417
d11d0a9
 
 
 
78f9417
 
 
 
d11d0a9
78f9417
 
 
d11d0a9
78f9417
 
 
 
d11d0a9
78f9417
d11d0a9
78f9417
d11d0a9
78f9417
d11d0a9
78f9417
e787de7
d11d0a9
78f9417
 
 
 
 
 
 
 
 
d11d0a9
78f9417
d11d0a9
 
78f9417
d11d0a9
78f9417
 
 
d11d0a9
78f9417
 
 
 
 
 
d11d0a9
78f9417
 
 
d11d0a9
 
78f9417
d11d0a9
78f9417
e787de7
78f9417
 
 
 
 
 
d11d0a9
78f9417
d11d0a9
78f9417
 
 
 
 
d11d0a9
 
78f9417
 
d11d0a9
78f9417
 
 
d11d0a9
 
78f9417
d11d0a9
78f9417
d11d0a9
78f9417
 
 
 
d11d0a9
78f9417
 
 
e787de7
78f9417
d11d0a9
e787de7
78f9417
 
 
 
 
d11d0a9
78f9417
d11d0a9
78f9417
d11d0a9
78f9417
d11d0a9
78f9417
d11d0a9
 
78f9417
d11d0a9
 
78f9417
d11d0a9
78f9417
 
d11d0a9
78f9417
d11d0a9
78f9417
d11d0a9
78f9417
 
 
d11d0a9
78f9417
 
d11d0a9
78f9417
 
 
 
 
 
 
 
 
 
 
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
import math
from dataclasses import dataclass
from typing import Union

import torch
import torch.nn as nn
import torch.nn.functional as F

from pscan import pscan



@dataclass
class MambaConfig:
    d_model: int 
    n_layers: int
    dt_rank: Union[int, str] = 'auto'
    d_state: int = 16 
    expand_factor: int = 2 
    d_conv: int = 4

    dt_min: float = 0.001
    dt_max: float = 0.1
    dt_init: str = "random" 
    dt_scale: float = 1.0
    dt_init_floor = 1e-4

    bias: bool = False
    conv_bias: bool = True

    pscan: bool = True 

    def __post_init__(self):
        self.d_inner = self.expand_factor * self.d_model 

        if self.dt_rank == 'auto':
            self.dt_rank = math.ceil(self.d_model / 16)

class Mamba(nn.Module):
    def __init__(self, config: MambaConfig):
        super().__init__()

        self.config = config

        self.layers = nn.ModuleList([ResidualBlock(config) for _ in range(config.n_layers)])
       

    def forward(self, x):
       

        for layer in self.layers:
            x = layer(x)

    

        return x
    
    def step(self, x, caches):
        

        for i, layer in enumerate(self.layers):
            x, caches[i] = layer.step(x, caches[i])

        return x, caches

class ResidualBlock(nn.Module):
    def __init__(self, config: MambaConfig):
        super().__init__()

        self.mixer = MambaBlock(config)
        self.norm = RMSNorm(config.d_model)

    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

       
        self.in_proj = nn.Linear(config.d_model, 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.D = nn.Parameter(torch.ones(config.d_inner))

      
        self.out_proj = nn.Linear(config.d_inner, config.d_model, bias=config.bias)

    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 = F.silu(z)

        output = y * z
        output = self.out_proj(output) 

        return output
    
    def ssm(self, x):
       

        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 = F.softplus(self.dt_proj(delta)) 

        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(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 = 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.squeeze(1)


class RMSNorm(nn.Module):
    def __init__(self, d_model: int, eps: float = 1e-5):
        super().__init__()

        self.eps = eps
        self.weight = nn.Parameter(torch.ones(d_model))

    def forward(self, x):
        output = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) * self.weight

        return output