forked from NVIDIA/TensorRT-LLM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssm.py
223 lines (197 loc) · 8.28 KB
/
ssm.py
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
# SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
from dataclasses import dataclass
from typing import Optional
from .._common import default_net
from ..functional import (ACT2FN, Tensor, concat, conv2d, gather, mamba_conv1d,
permute, selective_scan, shape, split, view)
from ..module import Module
from ..parameter import Parameter
from .linear import Linear
@dataclass
class MambaParameters:
d_state: int = 16
d_conv: int = 4
expand: int = 2
dt_rank: str = "auto"
conv_bias: bool = True
bias: bool = False
class MambaConv1d(Module):
def __init__(self, d_inner, d_conv=4, dtype=None):
super().__init__()
self.d_inner = d_inner
self.d_conv = d_conv
self.dtype = dtype
self.weight = Parameter(shape=(self.d_inner, 1, self.d_conv, 1),
dtype=dtype)
self.bias = Parameter(shape=(self.d_inner, ), dtype=dtype)
def forward(self,
x: Tensor,
conv_state: Tensor,
host_request_types: Tensor,
last_token_ids: Tensor,
host_context_lengths: Optional[Tensor] = None,
slot_mapping: Optional[Tensor] = None,
conv_indices: Optional[Tensor] = None):
'''
Parameters:
x: [B, L, D] or [T, D]
conv_state: [B, W, D] or [1] of type int64 for paged state
host_request_types: [B]
last_token_ids: [B]
host_context_lengths: [B]
slot_mapping: [B]
conv_indices: [B]
'''
if default_net().plugin_config.mamba_conv1d_plugin:
transposed_weight = permute(
view(self.weight.value, shape=[self.d_inner, 1, self.d_conv]),
(1, 2, 0))
x_conv, conv_state = mamba_conv1d(
x, conv_state, transposed_weight, self.bias.value,
host_request_types, last_token_ids, self.d_inner, self.d_conv,
self.dtype, host_context_lengths, slot_mapping)
else:
assert len(
x.shape
) == 3, "remove_input_padding is not supported by OOTB for Mamba."
x = x.permute([0, 2, 1])
# In context phase, conv_state is a zero tensor, and it is used for padding
# In generation phase, conv_state is a tensor of the past x
x_pad = concat([conv_state, x], dim=2)
# Update conv_state
conv_state = gather(x_pad, 2, conv_indices)
# Convolution
x_pad = x_pad.view(
concat([shape(x_pad, 0),
shape(x_pad, 1),
shape(x_pad, 2), 1]))
x_conv = conv2d(x_pad,
self.weight.value,
self.bias.value,
groups=self.d_inner)
x_conv = ACT2FN['silu'](x_conv)
x_conv = x_conv.view(
concat([shape(x_conv, 0),
shape(x_conv, 1),
shape(x_conv, 2)]))
# Get dt, B and C
x_conv = x_conv.permute([0, 2, 1])
return x_conv, conv_state
class Mamba(Module):
def __init__(self,
d_model,
d_state=16,
d_conv=4,
expand=2,
dt_rank="auto",
conv_bias=True,
bias=False,
dtype=None):
super().__init__()
self.d_model = d_model
self.d_state = d_state
self.d_conv = d_conv
self.expand = expand
self.d_inner = int(self.expand * self.d_model)
self.dt_rank = math.ceil(self.d_model /
16) if dt_rank == "auto" else dt_rank
self.dtype = dtype
self.A = Parameter(shape=(self.d_state, self.d_inner), dtype="float32")
self.D = Parameter(shape=(self.d_inner, ), dtype="float32")
self.dt_bias = Parameter(shape=(self.d_inner, ), dtype="float32")
self.in_proj_x = Linear(self.d_model,
self.d_inner,
bias=bias,
dtype=dtype,
gather_output=False)
self.in_proj_z = Linear(self.d_model,
self.d_inner,
bias=bias,
dtype=dtype,
gather_output=False)
self.conv1d = MambaConv1d(self.d_inner, self.d_conv, self.dtype)
self.x_proj = Linear(self.d_inner,
self.dt_rank + self.d_state * 2,
bias=False,
dtype=dtype,
gather_output=False)
self.dt_proj = Linear(self.dt_rank,
self.d_inner,
bias=False,
dtype=dtype,
gather_output=False,
pad_lda=self.d_state * 2)
self.out_proj = Linear(self.d_inner,
self.d_model,
bias=bias,
dtype=dtype,
gather_output=False)
def forward(self,
hidden_states: Tensor,
conv_state: Tensor,
ssm_state: Tensor,
host_request_types: Tensor,
last_token_ids: Tensor,
host_context_lengths: Optional[Tensor] = None,
slot_mapping: Optional[Tensor] = None,
conv_indices: Optional[Tensor] = None):
'''
Parameters:
hidden_states: [B, L, D] or [T, D]
conv_state: [B, W, D] or [1] of type int64 for paged state
ssm_state: [B, N, D] or [1] of type int64 for paged state
host_request_types: [B]
last_token_ids: [B]
host_context_lengths: [B]
slot_mapping: [B]
conv_indices: [B]
'''
# in_proj
x = self.in_proj_x(hidden_states)
z = self.in_proj_z(hidden_states)
x_conv, conv_state = self.conv1d(x, conv_state, host_request_types,
last_token_ids, host_context_lengths,
slot_mapping, conv_indices)
# Get dt, B and C
x_dbl = self.x_proj(x_conv)
if default_net().plugin_config.gemm_plugin:
dt = self.dt_proj(x_dbl)
else:
dt, _ = split(x_dbl, [self.dt_rank, self.d_state * 2], dim=-1)
dt = self.dt_proj(dt)
# selective scan
y, ssm_state = selective_scan(x_conv,
ssm_state,
dt,
self.dt_bias.value,
self.A.value,
x_dbl,
self.D.value,
z,
host_request_types,
last_token_ids,
self.d_inner,
self.d_state,
self.dt_rank,
is_variable_B=True,
is_variable_C=True,
delta_softplus=True,
dtype=self.dtype,
slot_mapping=slot_mapping)
# out_proj
out = self.out_proj(y)
return out, conv_state, ssm_state