| 32 | from perturb_style.SeqTokenAug import SeqTokenAug |
| 33 | |
| 34 | class Mamba(nn.Module): |
| 35 | def __init__( |
| 36 | self, |
| 37 | d_model, |
| 38 | d_state=16, |
| 39 | d_conv=4, |
| 40 | expand=2, |
| 41 | dt_rank="auto", |
| 42 | dt_min=0.001, |
| 43 | dt_max=0.1, |
| 44 | dt_init="random", |
| 45 | dt_scale=1.0, |
| 46 | dt_init_floor=1e-4, |
| 47 | conv_bias=True, |
| 48 | bias=False, |
| 49 | use_fast_path=False, # Fused kernel options |
| 50 | layer_idx=None, |
| 51 | device=None, |
| 52 | dtype=None, |
| 53 | START_flag=0, |
| 54 | ): |
| 55 | factory_kwargs = {"device": device, "dtype": dtype} |
| 56 | super().__init__() |
| 57 | self.d_model = d_model |
| 58 | self.d_state = d_state |
| 59 | self.d_conv = d_conv |
| 60 | self.expand = expand |
| 61 | self.d_inner = int(self.expand * self.d_model) |
| 62 | self.dt_rank = math.ceil(self.d_model / 16) if dt_rank == "auto" else dt_rank |
| 63 | self.use_fast_path = use_fast_path |
| 64 | self.layer_idx = layer_idx |
| 65 | |
| 66 | self.in_proj = nn.Linear(self.d_model, self.d_inner * 2, bias=bias, **factory_kwargs) |
| 67 | |
| 68 | self.conv1d = nn.Conv1d( |
| 69 | in_channels=self.d_inner, |
| 70 | out_channels=self.d_inner, |
| 71 | bias=conv_bias, |
| 72 | kernel_size=d_conv, |
| 73 | groups=self.d_inner, |
| 74 | padding=d_conv - 1, |
| 75 | **factory_kwargs, |
| 76 | ) |
| 77 | |
| 78 | self.activation = "silu" |
| 79 | self.act = nn.SiLU() |
| 80 | |
| 81 | self.x_proj = nn.Linear( |
| 82 | self.d_inner, self.dt_rank + self.d_state * 2, bias=False, **factory_kwargs |
| 83 | ) |
| 84 | self.dt_proj = nn.Linear(self.dt_rank, self.d_inner, bias=True, **factory_kwargs) |
| 85 | |
| 86 | # Initialize special dt projection to preserve variance at initialization |
| 87 | dt_init_std = self.dt_rank**-0.5 * dt_scale |
| 88 | if dt_init == "constant": |
| 89 | nn.init.constant_(self.dt_proj.weight, dt_init_std) |
| 90 | elif dt_init == "random": |
| 91 | nn.init.uniform_(self.dt_proj.weight, -dt_init_std, dt_init_std) |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…