isemmanuelolowe commited on
Commit
b322b37
1 Parent(s): 974f8dd

Upload 5 files

Browse files
Files changed (5) hide show
  1. KAN.py +304 -0
  2. berkant_layers.py +1075 -0
  3. berkant_padding.py +159 -0
  4. configuration_berkant.py +25 -0
  5. flash_attn_triton.py +1112 -0
KAN.py ADDED
@@ -0,0 +1,304 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn.functional as F
3
+ import math
4
+
5
+
6
+ class KANLinear(torch.nn.Module):
7
+ """
8
+ Kolmogorov-Arnold Neural Network (KAN) layer.
9
+
10
+ Args:
11
+ in_features (int): Number of input features.
12
+ out_features (int): Number of output features.
13
+ grid_size (int): Number of grid points.
14
+ spline_order (int): Order of the spline.
15
+ scale_noise (float): Scale of the noise.
16
+ scale_base (float): Scale of the base weight.
17
+ scale_spline (float): Scale of the spline weight.
18
+ enable_standalone_scale_spline (bool): Whether to enable standalone scale for spline weight.
19
+ base_activation (torch.nn.Module): Activation function for the base weight.
20
+ grid_eps (float): Epsilon for the grid.
21
+ grid_range (list): Range of the grid.
22
+ """
23
+ def __init__(
24
+ self,
25
+ in_features,
26
+ out_features,
27
+ grid_size=5,
28
+ spline_order=3,
29
+ scale_noise=0.1,
30
+ scale_base=1.0,
31
+ scale_spline=1.0,
32
+ enable_standalone_scale_spline=True,
33
+ base_activation=torch.nn.SiLU,
34
+ grid_eps=0.02,
35
+ grid_range=[-1, 1],
36
+ ):
37
+ super(KANLinear, self).__init__()
38
+ self.in_features = in_features
39
+ self.out_features = out_features
40
+ self.grid_size = grid_size
41
+ self.spline_order = spline_order
42
+
43
+ h = (grid_range[1] - grid_range[0]) / grid_size
44
+ grid = (
45
+ (
46
+ torch.arange(-spline_order, grid_size + spline_order + 1) * h
47
+ + grid_range[0]
48
+ )
49
+ .expand(in_features, -1)
50
+ .contiguous()
51
+ )
52
+ self.register_buffer("grid", grid)
53
+
54
+ self.base_weight = torch.nn.Parameter(torch.Tensor(out_features, in_features))
55
+ self.spline_weight = torch.nn.Parameter(
56
+ torch.Tensor(out_features, in_features, grid_size + spline_order)
57
+ )
58
+ if enable_standalone_scale_spline:
59
+ self.spline_scaler = torch.nn.Parameter(
60
+ torch.Tensor(out_features, in_features)
61
+ )
62
+
63
+ self.scale_noise = scale_noise
64
+ self.scale_base = scale_base
65
+ self.scale_spline = scale_spline
66
+ self.enable_standalone_scale_spline = enable_standalone_scale_spline
67
+ self.base_activation = base_activation()
68
+ self.grid_eps = grid_eps
69
+
70
+ self.reset_parameters()
71
+
72
+ def reset_parameters(self):
73
+ torch.nn.init.kaiming_uniform_(self.base_weight, a=math.sqrt(5) * self.scale_base)
74
+ with torch.no_grad():
75
+ noise = (
76
+ (
77
+ torch.rand(self.grid_size + 1, self.in_features, self.out_features)
78
+ - 1 / 2
79
+ )
80
+ * self.scale_noise
81
+ / self.grid_size
82
+ )
83
+ self.spline_weight.data.copy_(
84
+ (self.scale_spline if not self.enable_standalone_scale_spline else 1.0)
85
+ * self.curve2coeff(
86
+ self.grid.T[self.spline_order : -self.spline_order],
87
+ noise,
88
+ )
89
+ )
90
+ if self.enable_standalone_scale_spline:
91
+ # torch.nn.init.constant_(self.spline_scaler, self.scale_spline)
92
+ torch.nn.init.kaiming_uniform_(self.spline_scaler, a=math.sqrt(5) * self.scale_spline)
93
+
94
+ def b_splines(self, x: torch.Tensor):
95
+ """
96
+ Compute the B-spline bases for the given input tensor.
97
+
98
+ Args:
99
+ x (torch.Tensor): Input tensor of shape (batch_size, in_features).
100
+
101
+ Returns:
102
+ torch.Tensor: B-spline bases tensor of shape (batch_size, in_features, grid_size + spline_order).
103
+ """
104
+ assert x.dim() == 2 and x.size(1) == self.in_features
105
+
106
+ grid: torch.Tensor = (
107
+ self.grid
108
+ ) # (in_features, grid_size + 2 * spline_order + 1)
109
+ x = x.unsqueeze(-1)
110
+ bases = ((x >= grid[:, :-1]) & (x < grid[:, 1:])).to(x.dtype)
111
+ for k in range(1, self.spline_order + 1):
112
+ bases = (
113
+ (x - grid[:, : -(k + 1)])
114
+ / (grid[:, k:-1] - grid[:, : -(k + 1)])
115
+ * bases[:, :, :-1]
116
+ ) + (
117
+ (grid[:, k + 1 :] - x)
118
+ / (grid[:, k + 1 :] - grid[:, 1:(-k)])
119
+ * bases[:, :, 1:]
120
+ )
121
+
122
+ assert bases.size() == (
123
+ x.size(0),
124
+ self.in_features,
125
+ self.grid_size + self.spline_order,
126
+ )
127
+ return bases.contiguous()
128
+
129
+ def curve2coeff(self, x: torch.Tensor, y: torch.Tensor):
130
+ """
131
+ Compute the coefficients of the curve that interpolates the given points.
132
+
133
+ Args:
134
+ x (torch.Tensor): Input tensor of shape (batch_size, in_features).
135
+ y (torch.Tensor): Output tensor of shape (batch_size, in_features, out_features).
136
+
137
+ Returns:
138
+ torch.Tensor: Coefficients tensor of shape (out_features, in_features, grid_size + spline_order).
139
+ """
140
+ assert x.dim() == 2 and x.size(1) == self.in_features
141
+ assert y.size() == (x.size(0), self.in_features, self.out_features)
142
+
143
+ A = self.b_splines(x).transpose(
144
+ 0, 1
145
+ ) # (in_features, batch_size, grid_size + spline_order)
146
+ B = y.transpose(0, 1) # (in_features, batch_size, out_features)
147
+ # cast to float32 to avoid torch.linalg.lstsq error
148
+ if A.dtype != torch.float32:
149
+ original_dtype = A.dtype
150
+ A = A.to(torch.float32)
151
+ B = B.to(torch.float32)
152
+ solution = torch.linalg.lstsq(
153
+ A, B
154
+ ).solution # (in_features, grid_size + spline_order, out_features)
155
+ # cast back to original dtype
156
+ if A.dtype != solution.dtype:
157
+ solution = solution.to(original_dtype)
158
+ result = solution.permute(
159
+ 2, 0, 1
160
+ ) # (out_features, in_features, grid_size + spline_order)
161
+
162
+ assert result.size() == (
163
+ self.out_features,
164
+ self.in_features,
165
+ self.grid_size + self.spline_order,
166
+ )
167
+ return result.contiguous()
168
+
169
+ @property
170
+ def scaled_spline_weight(self):
171
+ return self.spline_weight * (
172
+ self.spline_scaler.unsqueeze(-1)
173
+ if self.enable_standalone_scale_spline
174
+ else 1.0
175
+ )
176
+
177
+ def forward(self, x: torch.Tensor):
178
+ assert x.dim() == 2 and x.size(1) == self.in_features
179
+
180
+ base_output = F.linear(self.base_activation(x), self.base_weight)
181
+ spline_output = F.linear(
182
+ self.b_splines(x).view(x.size(0), -1),
183
+ self.scaled_spline_weight.view(self.out_features, -1),
184
+ )
185
+ return base_output + spline_output
186
+
187
+ @torch.no_grad()
188
+ def update_grid(self, x: torch.Tensor, margin=0.01):
189
+ assert x.dim() == 2 and x.size(1) == self.in_features
190
+ batch = x.size(0)
191
+
192
+ splines = self.b_splines(x) # (batch, in, coeff)
193
+ splines = splines.permute(1, 0, 2) # (in, batch, coeff)
194
+ orig_coeff = self.scaled_spline_weight # (out, in, coeff)
195
+ orig_coeff = orig_coeff.permute(1, 2, 0) # (in, coeff, out)
196
+ unreduced_spline_output = torch.bmm(splines, orig_coeff) # (in, batch, out)
197
+ unreduced_spline_output = unreduced_spline_output.permute(
198
+ 1, 0, 2
199
+ ) # (batch, in, out)
200
+
201
+ # sort each channel individually to collect data distribution
202
+ x_sorted = torch.sort(x, dim=0)[0]
203
+ grid_adaptive = x_sorted[
204
+ torch.linspace(
205
+ 0, batch - 1, self.grid_size + 1, dtype=torch.int64, device=x.device
206
+ )
207
+ ]
208
+
209
+ uniform_step = (x_sorted[-1] - x_sorted[0] + 2 * margin) / self.grid_size
210
+ grid_uniform = (
211
+ torch.arange(
212
+ self.grid_size + 1, dtype=torch.float32, device=x.device
213
+ ).unsqueeze(1)
214
+ * uniform_step
215
+ + x_sorted[0]
216
+ - margin
217
+ )
218
+
219
+ grid = self.grid_eps * grid_uniform + (1 - self.grid_eps) * grid_adaptive
220
+ grid = torch.concatenate(
221
+ [
222
+ grid[:1]
223
+ - uniform_step
224
+ * torch.arange(self.spline_order, 0, -1, device=x.device).unsqueeze(1),
225
+ grid,
226
+ grid[-1:]
227
+ + uniform_step
228
+ * torch.arange(1, self.spline_order + 1, device=x.device).unsqueeze(1),
229
+ ],
230
+ dim=0,
231
+ )
232
+
233
+ self.grid.copy_(grid.T)
234
+ self.spline_weight.data.copy_(self.curve2coeff(x, unreduced_spline_output))
235
+
236
+ def regularization_loss(self, regularize_activation=1.0, regularize_entropy=1.0):
237
+ """
238
+ Compute the regularization loss.
239
+
240
+ This is a dumb simulation of the original L1 regularization as stated in the
241
+ paper, since the original one requires computing absolutes and entropy from the
242
+ expanded (batch, in_features, out_features) intermediate tensor, which is hidden
243
+ behind the F.linear function if we want an memory efficient implementation.
244
+
245
+ The L1 regularization is now computed as mean absolute value of the spline
246
+ weights. The authors implementation also includes this term in addition to the
247
+ sample-based regularization.
248
+ """
249
+ l1_fake = self.spline_weight.abs().mean(-1)
250
+ regularization_loss_activation = l1_fake.sum()
251
+ p = l1_fake / regularization_loss_activation
252
+ regularization_loss_entropy = -torch.sum(p * p.log())
253
+ return (
254
+ regularize_activation * regularization_loss_activation
255
+ + regularize_entropy * regularization_loss_entropy
256
+ )
257
+
258
+
259
+ class KAN(torch.nn.Module):
260
+ def __init__(
261
+ self,
262
+ layers_hidden,
263
+ grid_size=5,
264
+ spline_order=3,
265
+ scale_noise=0.1,
266
+ scale_base=1.0,
267
+ scale_spline=1.0,
268
+ base_activation=torch.nn.SiLU,
269
+ grid_eps=0.02,
270
+ grid_range=[-1, 1],
271
+ ):
272
+ super(KAN, self).__init__()
273
+ self.grid_size = grid_size
274
+ self.spline_order = spline_order
275
+
276
+ self.layers = torch.nn.ModuleList()
277
+ for in_features, out_features in zip(layers_hidden, layers_hidden[1:]):
278
+ self.layers.append(
279
+ KANLinear(
280
+ in_features,
281
+ out_features,
282
+ grid_size=grid_size,
283
+ spline_order=spline_order,
284
+ scale_noise=scale_noise,
285
+ scale_base=scale_base,
286
+ scale_spline=scale_spline,
287
+ base_activation=base_activation,
288
+ grid_eps=grid_eps,
289
+ grid_range=grid_range,
290
+ )
291
+ )
292
+
293
+ def forward(self, x: torch.Tensor, update_grid=False):
294
+ for layer in self.layers:
295
+ if update_grid:
296
+ layer.update_grid(x)
297
+ x = layer(x)
298
+ return x
299
+
300
+ def regularization_loss(self, regularize_activation=1.0, regularize_entropy=1.0):
301
+ return sum(
302
+ layer.regularization_loss(regularize_activation, regularize_entropy)
303
+ for layer in self.layers
304
+ )
berkant_layers.py ADDED
@@ -0,0 +1,1075 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 MosaicML Examples authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
5
+ # Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved.
6
+ # Copyright (c) 2022, Tri Dao.
7
+
8
+ """Implements Mosaic BERT, with an eye towards the Hugging Face API.
9
+
10
+ Mosaic BERT improves performance over Hugging Face BERT through the following:
11
+
12
+ 1. ALiBi. This architectural change removes positional embeddings and instead encodes positional
13
+ information through attention biases based on query-key position distance. It improves the effectiveness
14
+ of training with shorter sequence lengths by enabling extrapolation to longer sequences.
15
+
16
+ 2. Gated Linear Units (GLU). This architectural change replaces the FFN component of the BERT layer
17
+ to improve overall expressiveness, providing better convergence properties.
18
+
19
+ 3. Flash Attention. The Mosaic BERT's self-attention layer makes use of Flash Attention, which dramatically
20
+ improves the speed of self-attention. Our implementation utilizes a bleeding edge implementation that
21
+ supports attention biases, which allows us to use Flash Attention with ALiBi.
22
+
23
+ 4. Unpadding. Padding is often used to simplify batching across sequences of different lengths. Standard BERT
24
+ implementations waste computation on padded tokens. Mosaic BERT internally unpads to reduce unnecessary computation
25
+ and improve speed. It does this without changing how the user interfaces with the model, thereby
26
+ preserving the simple API of standard implementations.
27
+
28
+
29
+ Currently, Mosaic BERT is available for masked language modeling :class:`BertForMaskedLM` and sequence
30
+ classification :class:`BertForSequenceClassification`. We aim to expand this catalogue in future releases.
31
+
32
+ See :file:`./mosaic_bert.py` for utilities to simplify working with Mosaic BERT in Composer, and for example usage
33
+ of the core Mosaic BERT classes.
34
+ """
35
+
36
+ import copy
37
+ import logging
38
+ import math
39
+ import warnings
40
+ from typing import List, Optional, Tuple, Union
41
+
42
+ import torch
43
+ import torch.nn as nn
44
+ from einops import rearrange
45
+ from torch.nn.modules.utils import consume_prefix_in_state_dict_if_present
46
+ from transformers.activations import ACT2FN
47
+ from transformers.modeling_outputs import (MaskedLMOutput,
48
+ SequenceClassifierOutput)
49
+ from transformers.models.bert.modeling_bert import BertPreTrainedModel
50
+
51
+ from .berkant_padding import (index_first_axis,
52
+ index_put_first_axis, pad_input,
53
+ unpad_input, unpad_input_only)
54
+
55
+ from .KAN import KANLinear
56
+ try:
57
+ from .flash_attn_triton import flash_attn_qkvpacked_func
58
+ except ImportError as e:
59
+ flash_attn_qkvpacked_func = None
60
+
61
+ logger = logging.getLogger(__name__)
62
+
63
+ class BerKANTEmbeddings(nn.Module):
64
+ """Construct the embeddings for words, ignoring position.
65
+
66
+ There are no positional embeddings since we use ALiBi and token_type
67
+ embeddings.
68
+
69
+ This module is modeled after the Hugging Face BERT's
70
+ :class:`~transformers.model.bert.modeling_bert.BertEmbeddings`, but is
71
+ modified as part of Mosaic BERT's ALiBi implementation. The key change is
72
+ that position embeddings are removed. Position information instead comes
73
+ from attention biases that scale linearly with the position distance
74
+ between query and key tokens.
75
+
76
+ This module ignores the `position_ids` input to the `forward` method.
77
+ """
78
+
79
+ def __init__(self, config):
80
+ super().__init__()
81
+ self.word_embeddings = nn.Embedding(config.vocab_size,
82
+ config.hidden_size,
83
+ padding_idx=config.pad_token_id)
84
+ # ALiBi doesn't use position embeddings
85
+ self.token_type_embeddings = nn.Embedding(config.type_vocab_size,
86
+ config.hidden_size)
87
+
88
+ # self.LayerNorm is not snake-cased to stick with TensorFlow model
89
+ # variable name and be able to load any TensorFlow checkpoint file
90
+ self.LayerNorm = nn.LayerNorm(config.hidden_size,
91
+ eps=config.layer_norm_eps)
92
+ self.dropout = nn.Dropout(config.hidden_dropout_prob)
93
+ self.register_buffer('token_type_ids',
94
+ torch.zeros(config.max_position_embeddings,
95
+ dtype=torch.long),
96
+ persistent=False)
97
+
98
+ def forward(
99
+ self,
100
+ input_ids: Optional[torch.LongTensor] = None,
101
+ token_type_ids: Optional[torch.LongTensor] = None,
102
+ position_ids: Optional[torch.LongTensor] = None,
103
+ inputs_embeds: Optional[torch.FloatTensor] = None,
104
+ past_key_values_length: int = 0,
105
+ ) -> torch.Tensor:
106
+ if (input_ids is not None) == (inputs_embeds is not None):
107
+ raise ValueError('Must specify either input_ids or input_embeds!')
108
+ if input_ids is not None:
109
+ input_shape = input_ids.size()
110
+ else:
111
+ assert inputs_embeds is not None # just for type checking
112
+ input_shape = inputs_embeds.size()[:-1]
113
+
114
+ seq_length = input_shape[1]
115
+
116
+ if position_ids is None:
117
+ # great! ALiBi
118
+ pass
119
+
120
+ # Setting the token_type_ids to the registered buffer in constructor
121
+ # where it is all zeros, which usually occurs when it's auto-generated;
122
+ # registered buffer helps users when tracing the model without passing
123
+ # token_type_ids, solves issue #5664
124
+ if token_type_ids is None:
125
+ if hasattr(self, 'token_type_ids'):
126
+ assert isinstance(self.token_type_ids, torch.LongTensor)
127
+ buffered_token_type_ids = self.token_type_ids[:, :seq_length]
128
+ buffered_token_type_ids_expanded = buffered_token_type_ids.expand(
129
+ input_shape[0], seq_length)
130
+ token_type_ids = buffered_token_type_ids_expanded # type: ignore
131
+ else:
132
+ token_type_ids = torch.zeros(input_shape, # type: ignore
133
+ dtype=torch.long,
134
+ device=self.word_embeddings.device) # type: ignore # yapf: disable
135
+
136
+ if inputs_embeds is None:
137
+ inputs_embeds = self.word_embeddings(input_ids)
138
+ token_type_embeddings = self.token_type_embeddings(token_type_ids)
139
+
140
+ embeddings = inputs_embeds + token_type_embeddings
141
+ # no position embeddings! ALiBi
142
+ embeddings = self.LayerNorm(embeddings)
143
+ embeddings = self.dropout(embeddings)
144
+ return embeddings
145
+
146
+
147
+ class BerKANTUnpadSelfAttention(nn.Module):
148
+ """Performs multi-headed self attention on a batch of unpadded sequences.
149
+
150
+ If Triton is installed, this module uses Flash Attention to greatly improve throughput.
151
+ The Flash Attention implementation used in Mosaic BERT supports arbitrary attention biases (which
152
+ we use to implement ALiBi), but does not support attention dropout. If either Triton is not installed
153
+ or `config.attention_probs_dropout_prob > 0`, the implementation will default to a
154
+ math-equivalent pytorch version, which is much slower.
155
+
156
+ See `forward` method for additional detail.
157
+ """
158
+
159
+ def __init__(self, config):
160
+ super().__init__()
161
+ if config.hidden_size % config.num_attention_heads != 0 and not hasattr(
162
+ config, 'embedding_size'):
163
+ raise ValueError(
164
+ f'The hidden size ({config.hidden_size}) is not a multiple of the number of attention '
165
+ f'heads ({config.num_attention_heads})')
166
+
167
+ self.num_attention_heads = config.num_attention_heads
168
+ self.attention_head_size = int(config.hidden_size /
169
+ config.num_attention_heads)
170
+ self.all_head_size = self.num_attention_heads * self.attention_head_size
171
+ self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
172
+ self.p_dropout = config.attention_probs_dropout_prob
173
+ self.Wqkv = KANLinear(self.all_head_size, 3 * config.hidden_size)
174
+
175
+ # Warn if defaulting to pytorch because of import issues
176
+ if flash_attn_qkvpacked_func is None:
177
+ warnings.warn(
178
+ 'Unable to import Triton; defaulting MosaicBERT attention implementation to pytorch (this will reduce throughput when using this model).'
179
+ )
180
+
181
+ def forward(self, hidden_states: torch.Tensor, cu_seqlens: torch.Tensor,
182
+ max_seqlen_in_batch: int, indices: torch.Tensor,
183
+ attn_mask: torch.Tensor, bias: torch.Tensor) -> torch.Tensor:
184
+ """Perform self-attention.
185
+
186
+ If dropout is zero, then we can use the Triton kernel, so we do that. However, if not, we send through a standard PyTorch
187
+ implementation of self-attention.
188
+
189
+ The arguments are unpadded, and our implementations of attention require padded arguments,
190
+ so we first call `pad_input`. Once we compute attention, we re-unpad our outputs for the other layers.
191
+ The pad/unpad operations add overhead, but not sending pad tokens through ffs saves compute.
192
+ It is possible to write an unpadded implementation of attention (in Triton and PyTorch), which we will eventually do.
193
+
194
+ Args:
195
+ hidden_states: (total_nnz, dim)
196
+ cu_seqlens: (batch + 1,)
197
+ max_seqlen_in_batch: int
198
+ indices: (total_nnz,)
199
+ attn_mask: (batch, max_seqlen_in_batch)
200
+ bias: (batch, heads, max_seqlen_in_batch, max_seqlen_in_batch)
201
+
202
+ Returns:
203
+ attention: (total_nnz, dim)
204
+ """
205
+ qkv = self.Wqkv(hidden_states)
206
+ qkv = pad_input(qkv, indices, cu_seqlens.shape[0] - 1,
207
+ max_seqlen_in_batch) # batch, max_seqlen_in_batch, thd
208
+ qkv = rearrange(qkv,
209
+ 'b s (t h d) -> b s t h d',
210
+ t=3,
211
+ h=self.num_attention_heads)
212
+ if self.p_dropout or flash_attn_qkvpacked_func is None:
213
+ # if we have nonzero attention dropout (e.g. during fine-tuning) or no Triton, compute attention in PyTorch
214
+ q = qkv[:, :, 0, :, :].permute(0, 2, 1, 3) # b h s d
215
+ k = qkv[:, :, 1, :, :].permute(0, 2, 3, 1) # b h d s
216
+ v = qkv[:, :, 2, :, :].permute(0, 2, 1, 3) # b h s d
217
+ attention_scores = torch.matmul(q, k) / math.sqrt(
218
+ self.attention_head_size)
219
+ attention_scores = attention_scores + bias
220
+ attention_probs = nn.functional.softmax(attention_scores, dim=-1)
221
+ attention_probs = self.dropout(attention_probs)
222
+ attention = torch.matmul(attention_probs, v).permute(0, 2, 1,
223
+ 3) # b s h d
224
+ else:
225
+ # Triton implementation only supports 0 attention dropout
226
+ convert_dtype = qkv.dtype not in [torch.float16, torch.bfloat16]
227
+ if convert_dtype:
228
+ # Triton implementation only supports fp16 and bf16
229
+ orig_dtype = qkv.dtype
230
+ qkv = qkv.to(torch.float16)
231
+ bias_dtype = bias.dtype
232
+ bias = bias.to(torch.float16)
233
+ attention = flash_attn_qkvpacked_func(qkv, bias)
234
+ attention = attention.to(orig_dtype)
235
+ bias = bias.to(bias_dtype)
236
+ else:
237
+ attention = flash_attn_qkvpacked_func(qkv, bias)
238
+
239
+ # attn_mask is 1 for attend and 0 for don't
240
+ attention = unpad_input_only(attention, torch.squeeze(attn_mask) == 1)
241
+ return rearrange(attention, 'nnz h d -> nnz (h d)')
242
+
243
+
244
+ # Copy of transformer's library BertSelfOutput that will not be caught by surgery methods looking for HF BERT modules.
245
+ class BerKANTSelfOutput(nn.Module):
246
+ """Computes the output of the attention layer.
247
+
248
+ This module is modeled after the Hugging Face BERT's
249
+ :class:`~transformers.model.bert.modeling_bert.BertSelfOutput`.
250
+ The implementation is identical. Rather than use the original module
251
+ directly, we re-implement it here so that Mosaic BERT's modules will not
252
+ be affected by any Composer surgery algorithm that modifies Hugging Face
253
+ BERT modules.
254
+ """
255
+
256
+ def __init__(self, config):
257
+ super().__init__()
258
+ self.dense = KANLinear(config.hidden_size, config.hidden_size)
259
+ self.LayerNorm = nn.LayerNorm(config.hidden_size,
260
+ eps=config.layer_norm_eps)
261
+ self.dropout = nn.Dropout(config.hidden_dropout_prob)
262
+
263
+ def forward(self, hidden_states: torch.Tensor,
264
+ input_tensor: torch.Tensor) -> torch.Tensor:
265
+ hidden_states = self.dense(hidden_states)
266
+ hidden_states = self.dropout(hidden_states)
267
+ hidden_states = self.LayerNorm(hidden_states + input_tensor)
268
+ return hidden_states
269
+
270
+
271
+ class BerKANTUnpadAttention(nn.Module):
272
+ """Chains attention, Dropout, and LayerNorm for Mosaic BERT."""
273
+
274
+ def __init__(self, config):
275
+ super().__init__()
276
+ self.self = BerKANTUnpadSelfAttention(config)
277
+ self.output = BerKANTSelfOutput(config)
278
+
279
+ def forward(
280
+ self,
281
+ input_tensor: torch.Tensor,
282
+ cu_seqlens: torch.Tensor,
283
+ max_s: int,
284
+ subset_idx: Optional[torch.Tensor] = None,
285
+ indices: Optional[torch.Tensor] = None,
286
+ attn_mask: Optional[torch.Tensor] = None,
287
+ bias: Optional[torch.Tensor] = None,
288
+ ) -> torch.Tensor:
289
+ """Forward pass for scaled self-attention without padding.
290
+
291
+ Arguments:
292
+ input_tensor: (total_nnz, dim)
293
+ cu_seqlens: (batch + 1,)
294
+ max_s: int
295
+ subset_idx: () set of indices whose values we care about at the end of the layer
296
+ (e.g., the masked tokens, if this is the final layer).
297
+ indices: None or (total_nnz,)
298
+ attn_mask: None or (batch, max_seqlen_in_batch)
299
+ bias: None or (batch, heads, max_seqlen_in_batch, max_seqlen_in_batch)
300
+ """
301
+ self_output = self.self(input_tensor, cu_seqlens, max_s, indices,
302
+ attn_mask, bias)
303
+ if subset_idx is not None:
304
+ return self.output(index_first_axis(self_output, subset_idx),
305
+ index_first_axis(input_tensor, subset_idx))
306
+ else:
307
+ return self.output(self_output, input_tensor)
308
+
309
+
310
+ class BerKANTGatedLinearUnitMLP(nn.Module):
311
+ """Applies the FFN at the end of each Mosaic BERT layer.
312
+
313
+ Compared to the default BERT architecture, this block replaces :class:`~transformers.model.bert.modeling_bert.BertIntermediate`
314
+ and :class:`~transformers.model.bert.modeling_bert.SelfOutput` with a single module that has similar functionality, but
315
+ introduces Gated Linear Units.
316
+
317
+ Note: Mosaic BERT adds parameters in order to implement Gated Linear Units. To keep parameter count consistent with that of a
318
+ standard Hugging Face BERT, scale down `config.intermediate_size` by 2/3. For example, a Mosaic BERT constructed with
319
+ `config.intermediate_size=2048` will have the same parameter footprint as its Hugging Face BERT counterpart constructed
320
+ with the `config.intermediate_size=3072`.
321
+ However, in most cases it will not be necessary to adjust `config.intermediate_size` since, despite the increased
322
+ parameter size, Mosaic BERT typically offers a net higher throughput than a Hugging Face BERT built from the same `config`.
323
+ """
324
+
325
+ def __init__(self, config):
326
+ super().__init__()
327
+ self.config = config
328
+ self.gated_layers = KANLinear(config.hidden_size,
329
+ config.intermediate_size * 2)
330
+ self.act = nn.GELU(approximate='none')
331
+ self.wo = KANLinear(config.intermediate_size, config.hidden_size)
332
+ self.dropout = nn.Dropout(config.hidden_dropout_prob)
333
+ self.layernorm = nn.LayerNorm(config.hidden_size,
334
+ eps=config.layer_norm_eps)
335
+
336
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
337
+ """Compute new hidden states from current hidden states.
338
+
339
+ Args:
340
+ hidden_states (torch.Tensor): The (unpadded) hidden states from
341
+ the attention layer [nnz, dim].
342
+ """
343
+ residual_connection = hidden_states
344
+ # compute the activation
345
+ hidden_states = self.gated_layers(hidden_states)
346
+ gated = hidden_states[:, :self.config.intermediate_size]
347
+ non_gated = hidden_states[:, self.config.intermediate_size:]
348
+ hidden_states = self.act(gated) * non_gated
349
+ hidden_states = self.dropout(hidden_states)
350
+ # multiply by the second matrix
351
+ hidden_states = self.wo(hidden_states)
352
+ # add the residual connection and post-LN
353
+ hidden_states = self.layernorm(hidden_states + residual_connection)
354
+ return hidden_states
355
+
356
+
357
+ class BerKANTLayer(nn.Module):
358
+ """Composes the Mosaic BERT attention and FFN blocks into a single layer."""
359
+
360
+ def __init__(self, config):
361
+ super(BerKANTLayer, self).__init__()
362
+ self.attention = BerKANTUnpadAttention(config)
363
+ self.mlp = BerKANTGatedLinearUnitMLP(config)
364
+
365
+ def forward(
366
+ self,
367
+ hidden_states: torch.Tensor,
368
+ cu_seqlens: torch.Tensor,
369
+ seqlen: int,
370
+ subset_idx: Optional[torch.Tensor] = None,
371
+ indices: Optional[torch.Tensor] = None,
372
+ attn_mask: Optional[torch.Tensor] = None,
373
+ bias: Optional[torch.Tensor] = None,
374
+ ) -> torch.Tensor:
375
+ """Forward pass for a BERT layer, including both attention and MLP.
376
+
377
+ Args:
378
+ hidden_states: (total_nnz, dim)
379
+ cu_seqlens: (batch + 1,)
380
+ seqlen: int
381
+ subset_idx: () set of indices whose values we care about at the end of the layer
382
+ (e.g., the masked tokens, if this is the final layer).
383
+ indices: None or (total_nnz,)
384
+ attn_mask: None or (batch, max_seqlen_in_batch)
385
+ bias: None or (batch, heads, max_seqlen_in_batch, max_seqlen_in_batch)
386
+ """
387
+ attention_output = self.attention(hidden_states, cu_seqlens, seqlen,
388
+ subset_idx, indices, attn_mask, bias)
389
+ layer_output = self.mlp(attention_output)
390
+ return layer_output
391
+
392
+
393
+ class BerKANTEncoder(nn.Module):
394
+ """A stack of BERT layers providing the backbone of Mosaic BERT.
395
+
396
+ This module is modeled after the Hugging Face BERT's :class:`~transformers.model.bert.modeling_bert.BertEncoder`,
397
+ but with substantial modifications to implement unpadding and ALiBi.
398
+
399
+ Compared to the analogous Hugging Face BERT module, this module handles unpadding to reduce unnecessary computation
400
+ at padded tokens, and pre-computes attention biases to implement ALiBi.
401
+ """
402
+
403
+ def __init__(self, config):
404
+ super().__init__()
405
+ layer = BerKANTLayer(config)
406
+ self.layer = nn.ModuleList(
407
+ [copy.deepcopy(layer) for _ in range(config.num_hidden_layers)])
408
+
409
+ self.num_attention_heads = config.num_attention_heads
410
+
411
+ # The alibi mask will be dynamically expanded if it is too small for
412
+ # the input the model receives. But it generally helps to initialize it
413
+ # to a reasonably large size to help pre-allocate CUDA memory.
414
+ # The default `alibi_starting_size` is 512.
415
+ self._current_alibi_size = int(config.alibi_starting_size)
416
+ self.alibi = torch.zeros(
417
+ (1, self.num_attention_heads, self._current_alibi_size,
418
+ self._current_alibi_size))
419
+ self.rebuild_alibi_tensor(size=config.alibi_starting_size)
420
+
421
+ def rebuild_alibi_tensor(self,
422
+ size: int,
423
+ device: Optional[Union[torch.device, str]] = None):
424
+ # Alibi
425
+ # Following https://github.com/ofirpress/attention_with_linear_biases/issues/5 (Implementation 1)
426
+ # In the causal case, you can exploit the fact that softmax is invariant to a uniform translation
427
+ # of the logits, which makes the math work out *after* applying causal masking. If no causal masking
428
+ # will be applied, it is necessary to construct the diagonal mask.
429
+ n_heads = self.num_attention_heads
430
+
431
+ def _get_alibi_head_slopes(n_heads: int) -> List[float]:
432
+
433
+ def get_slopes_power_of_2(n_heads: int) -> List[float]:
434
+ start = (2**(-2**-(math.log2(n_heads) - 3)))
435
+ ratio = start
436
+ return [start * ratio**i for i in range(n_heads)]
437
+
438
+ # In the paper, they only train models that have 2^a heads for some a. This function
439
+ # has some good properties that only occur when the input is a power of 2. To
440
+ # maintain that even when the number of heads is not a power of 2, we use a
441
+ # workaround.
442
+ if math.log2(n_heads).is_integer():
443
+ return get_slopes_power_of_2(n_heads)
444
+
445
+ closest_power_of_2 = 2**math.floor(math.log2(n_heads))
446
+ slopes_a = get_slopes_power_of_2(closest_power_of_2)
447
+ slopes_b = _get_alibi_head_slopes(2 * closest_power_of_2)
448
+ slopes_b = slopes_b[0::2][:n_heads - closest_power_of_2]
449
+ return slopes_a + slopes_b
450
+
451
+ context_position = torch.arange(size, device=device)[:, None]
452
+ memory_position = torch.arange(size, device=device)[None, :]
453
+ relative_position = torch.abs(memory_position - context_position)
454
+ # [n_heads, max_token_length, max_token_length]
455
+ relative_position = relative_position.unsqueeze(0).expand(
456
+ n_heads, -1, -1)
457
+ slopes = torch.Tensor(_get_alibi_head_slopes(n_heads)).to(device)
458
+ alibi = slopes.unsqueeze(1).unsqueeze(1) * -relative_position
459
+ # [1, n_heads, max_token_length, max_token_length]
460
+ alibi = alibi.unsqueeze(0)
461
+ assert alibi.shape == torch.Size([1, n_heads, size, size])
462
+
463
+ self._current_alibi_size = size
464
+ self.alibi = alibi
465
+
466
+ def forward(
467
+ self,
468
+ hidden_states: torch.Tensor,
469
+ attention_mask: torch.Tensor,
470
+ output_all_encoded_layers: Optional[bool] = True,
471
+ subset_mask: Optional[torch.Tensor] = None,
472
+ ) -> List[torch.Tensor]:
473
+
474
+ extended_attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
475
+ extended_attention_mask = extended_attention_mask.to(
476
+ dtype=next(self.parameters()).dtype) # fp16 compatibility
477
+ extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0
478
+
479
+ attention_mask_bool = attention_mask.bool()
480
+ batch, seqlen = hidden_states.shape[:2]
481
+ # Unpad inputs and mask. It will remove tokens that are padded.
482
+ # Assume ntokens is total number of tokens (padded and non-padded)
483
+ # and ntokens_unpad is total number of non-padded tokens.
484
+ # Then unpadding performs the following compression of the inputs:
485
+ # hidden_states[ntokens,hidden] -> hidden_states[ntokens_unpad,hidden]
486
+ hidden_states, indices, cu_seqlens, _ = unpad_input(
487
+ hidden_states, attention_mask_bool)
488
+
489
+ # Add alibi matrix to extended_attention_mask
490
+ if self._current_alibi_size < seqlen:
491
+ # Rebuild the alibi tensor when needed
492
+ warnings.warn(
493
+ f'Increasing alibi size from {self._current_alibi_size} to {seqlen}'
494
+ )
495
+ self.rebuild_alibi_tensor(size=seqlen, device=hidden_states.device)
496
+ elif self.alibi.device != hidden_states.device:
497
+ # Device catch-up
498
+ self.alibi = self.alibi.to(hidden_states.device)
499
+ alibi_bias = self.alibi[:, :, :seqlen, :seqlen]
500
+ attn_bias = extended_attention_mask[:, :, :seqlen, :seqlen]
501
+ alibi_attn_mask = attn_bias + alibi_bias
502
+
503
+ all_encoder_layers = []
504
+ if subset_mask is None:
505
+ for layer_module in self.layer:
506
+ hidden_states = layer_module(hidden_states,
507
+ cu_seqlens,
508
+ seqlen,
509
+ None,
510
+ indices,
511
+ attn_mask=attention_mask,
512
+ bias=alibi_attn_mask)
513
+ if output_all_encoded_layers:
514
+ all_encoder_layers.append(hidden_states)
515
+ # Pad inputs and mask. It will insert back zero-padded tokens.
516
+ # Assume ntokens is total number of tokens (padded and non-padded)
517
+ # and ntokens_unpad is total number of non-padded tokens.
518
+ # Then padding performs the following de-compression:
519
+ # hidden_states[ntokens_unpad,hidden] -> hidden_states[ntokens,hidden]
520
+ hidden_states = pad_input(hidden_states, indices, batch, seqlen)
521
+ else:
522
+ for i in range(len(self.layer) - 1):
523
+ layer_module = self.layer[i]
524
+ hidden_states = layer_module(hidden_states,
525
+ cu_seqlens,
526
+ seqlen,
527
+ None,
528
+ indices,
529
+ attn_mask=attention_mask,
530
+ bias=alibi_attn_mask)
531
+ if output_all_encoded_layers:
532
+ all_encoder_layers.append(hidden_states)
533
+ subset_idx = torch.nonzero(subset_mask[attention_mask_bool],
534
+ as_tuple=False).flatten()
535
+ hidden_states = self.layer[-1](hidden_states,
536
+ cu_seqlens,
537
+ seqlen,
538
+ subset_idx=subset_idx,
539
+ indices=indices,
540
+ attn_mask=attention_mask,
541
+ bias=alibi_attn_mask)
542
+
543
+ if not output_all_encoded_layers:
544
+ all_encoder_layers.append(hidden_states)
545
+ return all_encoder_layers
546
+
547
+
548
+ class BerKANTPooler(nn.Module):
549
+
550
+ def __init__(self, config):
551
+ super(BerKANTPooler, self).__init__()
552
+ self.dense = KANLinear(config.hidden_size, config.hidden_size)
553
+ self.activation = nn.Tanh()
554
+
555
+ def forward(self,
556
+ hidden_states: torch.Tensor,
557
+ pool: Optional[bool] = True) -> torch.Tensor:
558
+ # We "pool" the model by simply taking the hidden state corresponding
559
+ # to the first token.
560
+ first_token_tensor = hidden_states[:, 0] if pool else hidden_states
561
+ pooled_output = self.dense(first_token_tensor)
562
+ pooled_output = self.activation(pooled_output)
563
+ return pooled_output
564
+
565
+
566
+ class BerKANTPredictionHeadTransform(nn.Module):
567
+
568
+ def __init__(self, config):
569
+ super().__init__()
570
+ self.dense = nn.Linear(config.hidden_size, config.hidden_size)
571
+ if isinstance(config.hidden_act, str):
572
+ self.transform_act_fn = ACT2FN[config.hidden_act]
573
+ else:
574
+ self.transform_act_fn = config.hidden_act
575
+ self.LayerNorm = torch.nn.LayerNorm(config.hidden_size, eps=1e-12)
576
+
577
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
578
+ hidden_states = self.dense(hidden_states)
579
+ hidden_states = self.transform_act_fn(hidden_states)
580
+ hidden_states = self.LayerNorm(hidden_states)
581
+ return hidden_states
582
+
583
+
584
+ class BerKANTModel(BertPreTrainedModel):
585
+ """Overall BERT model.
586
+
587
+ Args:
588
+ config: a BertConfig class instance with the configuration to build a new model
589
+
590
+ Inputs:
591
+ `input_ids`: a torch.LongTensor of shape [batch_size, sequence_length]
592
+ with the word token indices in the vocabulary(see the tokens preprocessing logic in the scripts
593
+ `extract_features.py`, `run_classifier.py` and `run_squad.py`)
594
+ `token_type_ids`: an optional torch.LongTensor of shape [batch_size, sequence_length] with the token
595
+ types indices selected in [0, 1]. Type 0 corresponds to a `sentence A` and type 1 corresponds to
596
+ a `sentence B` token (see BERT paper for more details).
597
+ `attention_mask`: an optional torch.LongTensor of shape [batch_size, sequence_length] with indices
598
+ selected in [0, 1]. It's a mask to be used if the input sequence length is smaller than the max
599
+ input sequence length in the current batch. It's the mask that we typically use for attention when
600
+ a batch has varying length sentences.
601
+ `output_all_encoded_layers`: boolean which controls the content of the `encoded_layers` output as described below. Default: `True`.
602
+
603
+ Outputs: Tuple of (encoded_layers, pooled_output)
604
+ `encoded_layers`: controlled by `output_all_encoded_layers` argument:
605
+ - `output_all_encoded_layers=True`: outputs a list of the full sequences of encoded-hidden-states at the end
606
+ of each attention block (i.e. 12 full sequences for BERT-base, 24 for BERT-large), each
607
+ encoded-hidden-state is a torch.FloatTensor of size [batch_size, sequence_length, hidden_size],
608
+ - `output_all_encoded_layers=False`: outputs only the full sequence of hidden-states corresponding
609
+ to the last attention block of shape [batch_size, sequence_length, hidden_size],
610
+ `pooled_output`: a torch.FloatTensor of size [batch_size, hidden_size] which is the output of a
611
+ classifier pretrained on top of the hidden state associated to the first character of the
612
+ input (`CLS`) to train on the Next-Sentence task (see BERT's paper).
613
+
614
+ Example usage:
615
+ ```python
616
+ # Already been converted into WordPiece token ids
617
+ input_ids = torch.LongTensor([[31, 51, 99], [15, 5, 0]])
618
+ input_mask = torch.LongTensor([[1, 1, 1], [1, 1, 0]])
619
+ token_type_ids = torch.LongTensor([[0, 0, 1], [0, 1, 0]])
620
+ config = modeling.BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768,
621
+ num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072)
622
+ model = BertModel(config=config)
623
+ all_encoder_layers, pooled_output = model(input_ids, token_type_ids, input_mask)
624
+ ```
625
+ """
626
+
627
+ def __init__(self, config, add_pooling_layer=True):
628
+ super(BerKANTModel, self).__init__(config)
629
+ self.embeddings = BerKANTEmbeddings(config)
630
+ self.encoder = BerKANTEncoder(config)
631
+ self.pooler = BerKANTPooler(config) if add_pooling_layer else None
632
+ self.post_init()
633
+
634
+ def get_input_embeddings(self):
635
+ return self.embeddings.word_embeddings
636
+
637
+ def set_input_embeddings(self, value):
638
+ self.embeddings.word_embeddings = value
639
+
640
+ def forward(
641
+ self,
642
+ input_ids: torch.Tensor,
643
+ token_type_ids: Optional[torch.Tensor] = None,
644
+ attention_mask: Optional[torch.Tensor] = None,
645
+ position_ids: Optional[torch.Tensor] = None,
646
+ output_all_encoded_layers: Optional[bool] = False,
647
+ masked_tokens_mask: Optional[torch.Tensor] = None,
648
+ **kwargs
649
+ ) -> Tuple[Union[List[torch.Tensor], torch.Tensor], Optional[torch.Tensor]]:
650
+ if attention_mask is None:
651
+ attention_mask = torch.ones_like(input_ids)
652
+ if token_type_ids is None:
653
+ token_type_ids = torch.zeros_like(input_ids)
654
+
655
+ embedding_output = self.embeddings(input_ids, token_type_ids,
656
+ position_ids)
657
+
658
+ subset_mask = []
659
+ first_col_mask = []
660
+
661
+ if masked_tokens_mask is None:
662
+ subset_mask = None
663
+ else:
664
+ first_col_mask = torch.zeros_like(masked_tokens_mask)
665
+ first_col_mask[:, 0] = True
666
+ subset_mask = masked_tokens_mask | first_col_mask
667
+
668
+ encoder_outputs = self.encoder(
669
+ embedding_output,
670
+ attention_mask,
671
+ output_all_encoded_layers=output_all_encoded_layers,
672
+ subset_mask=subset_mask)
673
+
674
+ if masked_tokens_mask is None:
675
+ sequence_output = encoder_outputs[-1]
676
+ pooled_output = self.pooler(
677
+ sequence_output) if self.pooler is not None else None
678
+ else:
679
+ # TD [2022-03-01]: the indexing here is very tricky.
680
+ attention_mask_bool = attention_mask.bool()
681
+ subset_idx = subset_mask[attention_mask_bool] # type: ignore
682
+ sequence_output = encoder_outputs[-1][
683
+ masked_tokens_mask[attention_mask_bool][subset_idx]]
684
+ if self.pooler is not None:
685
+ pool_input = encoder_outputs[-1][
686
+ first_col_mask[attention_mask_bool][subset_idx]]
687
+ pooled_output = self.pooler(pool_input, pool=False)
688
+ else:
689
+ pooled_output = None
690
+
691
+ if not output_all_encoded_layers:
692
+ encoder_outputs = sequence_output
693
+
694
+ if self.pooler is not None:
695
+ return encoder_outputs, pooled_output
696
+
697
+ return encoder_outputs, None
698
+
699
+
700
+ ###################
701
+ # Bert Heads
702
+ ###################
703
+ class BerKANTLMPredictionHead(nn.Module):
704
+
705
+ def __init__(self, config, bert_model_embedding_weights):
706
+ super().__init__()
707
+ self.transform = BerKANTPredictionHeadTransform(config)
708
+ # The output weights are the same as the input embeddings, but there is
709
+ # an output-only bias for each token.
710
+ self.decoder = nn.Linear(bert_model_embedding_weights.size(1),
711
+ bert_model_embedding_weights.size(0))
712
+ self.decoder.weight = bert_model_embedding_weights
713
+
714
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
715
+ hidden_states = self.transform(hidden_states)
716
+ hidden_states = self.decoder(hidden_states)
717
+ return hidden_states
718
+
719
+
720
+ class BerKANTOnlyMLMHead(nn.Module):
721
+
722
+ def __init__(self, config, bert_model_embedding_weights):
723
+ super().__init__()
724
+ self.predictions = BerKANTLMPredictionHead(config,
725
+ bert_model_embedding_weights)
726
+
727
+ def forward(self, sequence_output: torch.Tensor) -> torch.Tensor:
728
+ prediction_scores = self.predictions(sequence_output)
729
+ return prediction_scores
730
+
731
+
732
+ class BerKANTOnlyNSPHead(nn.Module):
733
+
734
+ def __init__(self, config):
735
+ super().__init__()
736
+ self.seq_relationship = KANLinear(config.hidden_size, 2)
737
+
738
+ def forward(self, pooled_output: torch.Tensor) -> torch.Tensor:
739
+ seq_relationship_score = self.seq_relationship(pooled_output)
740
+ return seq_relationship_score
741
+
742
+
743
+ #####################
744
+ # Various Bert models
745
+ #####################
746
+
747
+
748
+ class BerKANTForPreTraining(BertPreTrainedModel):
749
+ #TBD: Coming in Future Commit
750
+ pass
751
+
752
+
753
+ class BerKANTLMHeadModel(BertPreTrainedModel):
754
+ #TBD: Coming in Future Commit
755
+ pass
756
+
757
+
758
+ class BerKANTForMaskedLM(BertPreTrainedModel):
759
+
760
+ def __init__(self, config):
761
+ super().__init__(config)
762
+
763
+ if config.is_decoder:
764
+ warnings.warn(
765
+ 'If you want to use `BertForMaskedLM` make sure `config.is_decoder=False` for '
766
+ 'bi-directional self-attention.')
767
+
768
+ self.bert = BerKANTModel(config, add_pooling_layer=False)
769
+ self.cls = BerKANTOnlyMLMHead(config,
770
+ self.bert.embeddings.word_embeddings.weight)
771
+
772
+ # Initialize weights and apply final processing
773
+ self.post_init()
774
+
775
+ @classmethod
776
+ def from_composer(cls,
777
+ pretrained_checkpoint,
778
+ state_dict=None,
779
+ cache_dir=None,
780
+ from_tf=False,
781
+ config=None,
782
+ *inputs,
783
+ **kwargs):
784
+ """Load from pre-trained."""
785
+ model = cls(config, *inputs, **kwargs)
786
+ if from_tf:
787
+ raise ValueError(
788
+ 'Mosaic BERT does not support loading TensorFlow weights.')
789
+
790
+ state_dict = torch.load(pretrained_checkpoint)
791
+ # If the state_dict was saved after wrapping with `composer.HuggingFaceModel`, it takes on the `model` prefix
792
+ consume_prefix_in_state_dict_if_present(state_dict, prefix='model.')
793
+ missing_keys, unexpected_keys = model.load_state_dict(state_dict,
794
+ strict=False)
795
+
796
+ if len(missing_keys) > 0:
797
+ logger.warning(
798
+ f"Found these missing keys in the checkpoint: {', '.join(missing_keys)}"
799
+ )
800
+ if len(unexpected_keys) > 0:
801
+ logger.warning(
802
+ f"Found these unexpected keys in the checkpoint: {', '.join(unexpected_keys)}"
803
+ )
804
+
805
+ return model
806
+
807
+ def get_output_embeddings(self):
808
+ return self.cls.predictions.decoder
809
+
810
+ def set_output_embeddings(self, new_embeddings):
811
+ self.cls.predictions.decoder = new_embeddings
812
+
813
+ def forward(
814
+ self,
815
+ input_ids: Optional[torch.Tensor] = None,
816
+ attention_mask: Optional[torch.Tensor] = None,
817
+ token_type_ids: Optional[torch.Tensor] = None,
818
+ position_ids: Optional[torch.Tensor] = None,
819
+ head_mask: Optional[torch.Tensor] = None,
820
+ inputs_embeds: Optional[torch.Tensor] = None,
821
+ encoder_hidden_states: Optional[torch.Tensor] = None,
822
+ encoder_attention_mask: Optional[torch.Tensor] = None,
823
+ labels: Optional[torch.Tensor] = None,
824
+ output_attentions: Optional[bool] = None,
825
+ output_hidden_states: Optional[bool] = None,
826
+ return_dict: Optional[bool] = None,
827
+ ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]:
828
+ # labels should be a `torch.LongTensor` of shape
829
+ # `(batch_size, sequence_length)`. These are used for computing the
830
+ # masked language modeling loss.
831
+ #
832
+ # Indices should be in `[-100, 0, ..., config.vocab_size]` (see
833
+ # `input_ids` docstring) Tokens with indices set to `-100` are ignored
834
+ # (masked), the loss is only computed for the tokens with labels in `[0,
835
+ # ..., config.vocab_size]`
836
+ #
837
+ # Prediction scores are only computed for masked tokens and the (bs,
838
+ # seqlen) dimensions are flattened
839
+ if (input_ids is not None) == (inputs_embeds is not None):
840
+ raise ValueError('Must specify either input_ids or input_embeds!')
841
+
842
+ if labels is None:
843
+ masked_tokens_mask = None
844
+ else:
845
+ masked_tokens_mask = labels > 0
846
+
847
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
848
+
849
+ outputs = self.bert(
850
+ input_ids,
851
+ attention_mask=attention_mask,
852
+ token_type_ids=token_type_ids,
853
+ position_ids=position_ids,
854
+ head_mask=head_mask,
855
+ inputs_embeds=inputs_embeds,
856
+ encoder_hidden_states=encoder_hidden_states,
857
+ encoder_attention_mask=encoder_attention_mask,
858
+ output_attentions=output_attentions,
859
+ output_hidden_states=output_hidden_states,
860
+ return_dict=return_dict,
861
+ masked_tokens_mask=masked_tokens_mask,
862
+ )
863
+
864
+ sequence_output = outputs[0]
865
+ prediction_scores = self.cls(sequence_output)
866
+
867
+ loss = None
868
+ if labels is not None:
869
+ # Compute loss
870
+ loss_fct = nn.CrossEntropyLoss()
871
+ masked_token_idx = torch.nonzero(labels.flatten() > 0,
872
+ as_tuple=False).flatten()
873
+ loss = loss_fct(prediction_scores,
874
+ labels.flatten()[masked_token_idx])
875
+
876
+ assert input_ids is not None, 'Coding error; please open an issue'
877
+ batch, seqlen = input_ids.shape[:2]
878
+ prediction_scores = rearrange(index_put_first_axis(
879
+ prediction_scores, masked_token_idx, batch * seqlen),
880
+ '(b s) d -> b s d',
881
+ b=batch)
882
+
883
+ if not return_dict:
884
+ output = (prediction_scores,) + outputs[2:]
885
+ return ((loss,) + output) if loss is not None else output
886
+
887
+ return MaskedLMOutput(
888
+ loss=loss,
889
+ logits=prediction_scores,
890
+ hidden_states=None,
891
+ attentions=None,
892
+ )
893
+
894
+ def prepare_inputs_for_generation(self, input_ids: torch.Tensor,
895
+ attention_mask: torch.Tensor,
896
+ **model_kwargs):
897
+ input_shape = input_ids.shape
898
+ effective_batch_size = input_shape[0]
899
+
900
+ # add a dummy token
901
+ if self.config.pad_token_id is None:
902
+ raise ValueError('The PAD token should be defined for generation')
903
+
904
+ attention_mask = torch.cat([
905
+ attention_mask,
906
+ attention_mask.new_zeros((attention_mask.shape[0], 1))
907
+ ],
908
+ dim=-1)
909
+ dummy_token = torch.full((effective_batch_size, 1),
910
+ self.config.pad_token_id,
911
+ dtype=torch.long,
912
+ device=input_ids.device)
913
+ input_ids = torch.cat([input_ids, dummy_token], dim=1)
914
+
915
+ return {'input_ids': input_ids, 'attention_mask': attention_mask}
916
+
917
+
918
+ class BerKANTForNextSentencePrediction(BertPreTrainedModel):
919
+ #TBD: Push in future commit
920
+ pass
921
+
922
+
923
+ class BerKANTForSequenceClassification(BertPreTrainedModel):
924
+ """Bert Model transformer with a sequence classification/regression head.
925
+
926
+ This head is just a linear layer on top of the pooled output. Used for,
927
+ e.g., GLUE tasks.
928
+ """
929
+
930
+ def __init__(self, config):
931
+ super().__init__(config)
932
+ self.num_labels = config.num_labels
933
+ self.config = config
934
+
935
+ self.bert = BerKANTModel(config)
936
+ classifier_dropout = (config.classifier_dropout
937
+ if config.classifier_dropout is not None else
938
+ config.hidden_dropout_prob)
939
+ self.dropout = nn.Dropout(classifier_dropout)
940
+ self.classifier = KANLinear(config.hidden_size, config.num_labels)
941
+
942
+ # Initialize weights and apply final processing
943
+ self.post_init()
944
+
945
+ @classmethod
946
+ def from_composer(cls,
947
+ pretrained_checkpoint,
948
+ state_dict=None,
949
+ cache_dir=None,
950
+ from_tf=False,
951
+ config=None,
952
+ *inputs,
953
+ **kwargs):
954
+ """Load from pre-trained."""
955
+ model = cls(config, *inputs, **kwargs)
956
+ if from_tf:
957
+ raise ValueError(
958
+ 'Mosaic BERT does not support loading TensorFlow weights.')
959
+
960
+ state_dict = torch.load(pretrained_checkpoint)
961
+ # If the state_dict was saved after wrapping with `composer.HuggingFaceModel`, it takes on the `model` prefix
962
+ consume_prefix_in_state_dict_if_present(state_dict, prefix='model.')
963
+ missing_keys, unexpected_keys = model.load_state_dict(state_dict,
964
+ strict=False)
965
+
966
+ if len(missing_keys) > 0:
967
+ logger.warning(
968
+ f"Found these missing keys in the checkpoint: {', '.join(missing_keys)}"
969
+ )
970
+ if len(unexpected_keys) > 0:
971
+ logger.warning(
972
+ f"Found these unexpected keys in the checkpoint: {', '.join(unexpected_keys)}"
973
+ )
974
+
975
+ return model
976
+
977
+ def forward(
978
+ self,
979
+ input_ids: Optional[torch.Tensor] = None,
980
+ attention_mask: Optional[torch.Tensor] = None,
981
+ token_type_ids: Optional[torch.Tensor] = None,
982
+ position_ids: Optional[torch.Tensor] = None,
983
+ head_mask: Optional[torch.Tensor] = None,
984
+ inputs_embeds: Optional[torch.Tensor] = None,
985
+ labels: Optional[torch.Tensor] = None,
986
+ output_attentions: Optional[bool] = None,
987
+ output_hidden_states: Optional[bool] = None,
988
+ return_dict: Optional[bool] = None,
989
+ ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]:
990
+ # labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
991
+ # Labels for computing the sequence classification/regression loss.
992
+ # Indices should be in `[0, ..., config.num_labels - 1]`.
993
+ # If `config.num_labels == 1` a regression loss is computed
994
+ # (mean-square loss). If `config.num_labels > 1` a classification loss
995
+ # is computed (cross-entropy).
996
+
997
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
998
+
999
+ outputs = self.bert(
1000
+ input_ids,
1001
+ attention_mask=attention_mask,
1002
+ token_type_ids=token_type_ids,
1003
+ position_ids=position_ids,
1004
+ head_mask=head_mask,
1005
+ inputs_embeds=inputs_embeds,
1006
+ output_attentions=output_attentions,
1007
+ output_hidden_states=output_hidden_states,
1008
+ return_dict=return_dict,
1009
+ )
1010
+
1011
+ pooled_output = outputs[1]
1012
+
1013
+ pooled_output = self.dropout(pooled_output)
1014
+ logits = self.classifier(pooled_output)
1015
+
1016
+ loss = None
1017
+ if labels is not None:
1018
+ # Compute loss
1019
+ if self.config.problem_type is None:
1020
+ if self.num_labels == 1:
1021
+ self.config.problem_type = 'regression'
1022
+ elif self.num_labels > 1 and (labels.dtype == torch.long or
1023
+ labels.dtype == torch.int):
1024
+ self.config.problem_type = 'single_label_classification'
1025
+ else:
1026
+ self.config.problem_type = 'multi_label_classification'
1027
+
1028
+ if self.config.problem_type == 'regression':
1029
+ loss_fct = nn.MSELoss()
1030
+ if self.num_labels == 1:
1031
+ loss = loss_fct(logits.squeeze(), labels.squeeze())
1032
+ else:
1033
+ loss = loss_fct(logits, labels)
1034
+ elif self.config.problem_type == 'single_label_classification':
1035
+ loss_fct = nn.CrossEntropyLoss()
1036
+ loss = loss_fct(logits.view(-1, self.num_labels),
1037
+ labels.view(-1))
1038
+ elif self.config.problem_type == 'multi_label_classification':
1039
+ loss_fct = nn.BCEWithLogitsLoss()
1040
+ loss = loss_fct(logits, labels)
1041
+
1042
+ if not return_dict:
1043
+ output = (logits,) + outputs[2:]
1044
+ return ((loss,) + output) if loss is not None else output
1045
+
1046
+ return SequenceClassifierOutput(
1047
+ loss=loss,
1048
+ logits=logits,
1049
+ hidden_states=None,
1050
+ attentions=None,
1051
+ )
1052
+
1053
+
1054
+ class BerKANTForMultipleChoice(BertPreTrainedModel):
1055
+ #TBD: Push in future commit
1056
+ pass
1057
+
1058
+
1059
+ class BerKANTForTokenClassification(BertPreTrainedModel):
1060
+ #TBD: Push in future commit
1061
+ pass
1062
+
1063
+
1064
+ class BerKANTForQuestionAnswering(BertPreTrainedModel):
1065
+ """Bert Model with a span classification head.
1066
+
1067
+ This is used for extractive question-answering tasks like SQuAD (a linear
1068
+ layers on top of the hidden states' output to compute `span start logits`
1069
+ and `span end logits`).
1070
+ """
1071
+ #TBD: Push in future commit
1072
+
1073
+ # from transformers import AutoModelForMaskedLM
1074
+
1075
+ # AutoModelForMaskedLM.register("labiium-berkant", BerKANTForMaskedLM)
berkant_padding.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 MosaicML Examples authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ # Adapted from https://github.com/HazyResearch/flash-attention/blob/main/flash_attn/bert_padding.py
5
+ # Which was adapted from https://github.com/mlcommons/training_results_v1.1/blob/main/NVIDIA/benchmarks/bert/implementations/pytorch/padding.py
6
+
7
+ """Helper functions for padding and unpadding batches.
8
+
9
+ These functions are used extensively throughout the Mosaic BERT implementation
10
+ in `bert_layers.py`.
11
+ """
12
+
13
+ from typing import Tuple, cast
14
+
15
+ import torch
16
+ import torch.nn.functional as F
17
+ from einops import rearrange, repeat
18
+
19
+
20
+ class IndexFirstAxis(torch.autograd.Function):
21
+
22
+ @staticmethod
23
+ def forward(ctx, input: torch.Tensor,
24
+ indices: torch.Tensor) -> torch.Tensor:
25
+ """Get just the values of `input` which are at `indices`.
26
+
27
+ Arguments:
28
+ ctx: the autograd context object
29
+ input: (b, ...) 2+ dimensional tensor
30
+ indices: (num_idx) 1D tensor
31
+ """
32
+ ctx.save_for_backward(indices)
33
+ assert input.ndim >= 2
34
+ ctx.first_axis_dim, other_shape = input.shape[0], input.shape[
35
+ 1:] # type: ignore
36
+ second_dim = other_shape.numel(
37
+ ) # product of sizes of all but first dimension
38
+ # TD [2022-03-04] For some reason torch.gather is a bit faster than indexing.
39
+ return torch.gather(
40
+ rearrange(input, 'b ... -> b (...)'), # (b, ...) -> (b, second_dim)
41
+ 0,
42
+ repeat(indices, 'z -> z d',
43
+ d=second_dim) # (indices,) -> (indices, second_dim)
44
+ ).reshape(-1, *other_shape) # (num_idx, ...)
45
+
46
+ @staticmethod
47
+ def backward(ctx, grad_output: torch.Tensor) -> Tuple[torch.Tensor, None]:
48
+ indices, = ctx.saved_tensors
49
+ assert grad_output.ndim >= 2
50
+ other_shape = grad_output.shape[1:]
51
+ grad_output = rearrange(grad_output, 'b ... -> b (...)')
52
+ grad_input = torch.zeros([ctx.first_axis_dim, grad_output.shape[1]],
53
+ device=grad_output.device,
54
+ dtype=grad_output.dtype)
55
+ # TD [2022-03-04] For some reason torch.scatter is a bit faster than indexing.
56
+ # grad_input[indices] = grad_output
57
+ grad_input.scatter_(0,
58
+ repeat(indices, 'z -> z d', d=grad_output.shape[1]),
59
+ grad_output)
60
+ return grad_input.reshape(ctx.first_axis_dim, *other_shape), None
61
+
62
+
63
+ index_first_axis = IndexFirstAxis.apply
64
+
65
+
66
+ class IndexPutFirstAxis(torch.autograd.Function):
67
+
68
+ @staticmethod
69
+ def forward(ctx, values: torch.Tensor, indices: torch.Tensor,
70
+ first_axis_dim) -> torch.Tensor:
71
+ ctx.save_for_backward(indices)
72
+ assert indices.ndim == 1
73
+ assert values.ndim >= 2
74
+ output = torch.zeros(first_axis_dim,
75
+ *values.shape[1:],
76
+ device=values.device,
77
+ dtype=values.dtype)
78
+ output[indices] = values
79
+ return output
80
+
81
+ @staticmethod
82
+ def backward(ctx,
83
+ grad_output: torch.Tensor) -> Tuple[torch.Tensor, None, None]:
84
+ indices, = ctx.saved_tensors
85
+ grad_values = grad_output[indices]
86
+ return grad_values, None, None
87
+
88
+
89
+ index_put_first_axis = IndexPutFirstAxis.apply
90
+
91
+
92
+ def unpad_input(
93
+ hidden_states: torch.Tensor,
94
+ attention_mask: torch.Tensor,
95
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, int]:
96
+ """Remove padding from input sequences.
97
+
98
+ Arguments:
99
+ hidden_states: (batch, seqlen, ...)
100
+ attention_mask: (batch, seqlen), bool / int, 1 means valid and 0 means not valid.
101
+
102
+ Returns:
103
+ hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask.
104
+ indices: (total_nnz)
105
+ cu_seqlens: (batch + 1), the cumulative sequence lengths, used to index into hidden_states.
106
+ max_seqlen_in_batch: int ()
107
+ """
108
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
109
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
110
+ max_seqlen_in_batch = int(seqlens_in_batch.max().item())
111
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32),
112
+ (1, 0))
113
+ # TD [2022-03-04] We don't want to index with a bool mask, because Pytorch will expand the
114
+ # bool mask, then call nonzero to get the indices, then index with those. The indices is @dim
115
+ # times larger than it needs to be, wasting memory. It's faster and more memory-efficient to
116
+ # index with integer indices. Moreover, torch's index is a bit slower than it needs to be,
117
+ # so we write custom forward and backward to make it a bit faster.
118
+ hidden_states = cast(
119
+ torch.Tensor,
120
+ index_first_axis(rearrange(hidden_states, 'b s ... -> (b s) ...'),
121
+ indices))
122
+ return hidden_states, indices, cu_seqlens, max_seqlen_in_batch
123
+
124
+
125
+ def unpad_input_only(
126
+ hidden_states: torch.Tensor,
127
+ attention_mask: torch.Tensor,
128
+ ) -> torch.Tensor:
129
+ """Like unpad_input, but only return the unpadded first tensor.
130
+
131
+ Save a small amount of overhead.
132
+
133
+ Arguments:
134
+ hidden_states: (batch, seqlen, ...)
135
+ attention_mask: (batch, seqlen), bool / int, 1 means valid and 0 means not valid.
136
+
137
+ Returns:
138
+ hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask.
139
+ """
140
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
141
+ return index_first_axis(rearrange(hidden_states, 'b s ... -> (b s) ...'),
142
+ indices)
143
+
144
+
145
+ def pad_input(hidden_states: torch.Tensor, indices: torch.Tensor, batch: int,
146
+ seqlen: int) -> torch.Tensor:
147
+ """Add padding to sequences.
148
+
149
+ Arguments:
150
+ hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask.
151
+ indices: (total_nnz)
152
+ batch: int batch_size
153
+ seqlen: int max sequence length
154
+
155
+ Returns:
156
+ hidden_states: (batch, seqlen, ...)
157
+ """
158
+ output = index_put_first_axis(hidden_states, indices, batch * seqlen)
159
+ return rearrange(output, '(b s) ... -> b s ...', b=batch)
configuration_berkant.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 MosaicML Examples authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ from transformers import BertConfig as TransformersBertConfig
5
+
6
+
7
+ class BerKANTConfig(TransformersBertConfig):
8
+
9
+ def __init__(
10
+ self,
11
+ alibi_starting_size: int = 512,
12
+ attention_probs_dropout_prob: float = 0.0,
13
+ **kwargs,
14
+ ):
15
+ """Configuration class for MosaicBert.
16
+ Args:
17
+ alibi_starting_size (int): Use `alibi_starting_size` to determine how large of an alibi tensor to
18
+ create when initializing the model. You should be able to ignore this parameter in most cases.
19
+ Defaults to 512.
20
+ attention_probs_dropout_prob (float): By default, turn off attention dropout in Mosaic BERT
21
+ (otherwise, Flash Attention will be off by default). Defaults to 0.0.
22
+ """
23
+ super().__init__(
24
+ attention_probs_dropout_prob=attention_probs_dropout_prob, **kwargs)
25
+ self.alibi_starting_size = alibi_starting_size
flash_attn_triton.py ADDED
@@ -0,0 +1,1112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 MosaicML Examples authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ """Triton implementation of Flash Attention.
5
+
6
+ # Copyright (c) 2022, Tri Dao.
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+
20
+ *Experimental* implementation of FlashAttention in Triton.
21
+ We use the FlashAttention implementation from Phil Tillet a starting point.
22
+ https://github.com/openai/triton/blob/master/python/tutorials/06-fused-attention.py
23
+
24
+ Changes:
25
+ - Implement both causal and non-causal attention.
26
+ - Implement both self-attention and cross-attention.
27
+ - Support arbitrary seqlens (not just multiples of 128), for both forward and backward.
28
+ - Support all head dimensions up to 128 (not just 16, 32, 64, 128), for both forward and backward.
29
+ - Support attention bias.
30
+ - Speed up the forward pass a bit, and only store the LSE instead of m and l.
31
+ - Make the backward for d=128 much faster by reducing register spilling.
32
+ - Optionally parallelize the backward pass across seqlen_k, to deal with the case of
33
+ small batch size * nheads.
34
+
35
+ Caution:
36
+ - If you plan to use headdim other than 64 and 128, you should test for race conditions
37
+ (due to the Triton compiler), as done in tests/test_flash_attn.py
38
+ "test_flash_attn_triton_race_condition". I've tested and fixed many race conditions
39
+ for different head dimensions (40, 48, 64, 128, 80, 88, 96), but I'm still not 100% confident
40
+ that there are none left for other head dimensions.
41
+ Differences between this Triton version and the CUDA version:
42
+ - Triton version doesn't support dropout.
43
+ - Triton forward is generally faster than CUDA forward.
44
+ - Triton backward is faster than CUDA backward when batch * nheads is small, and when headdim=64.
45
+ It is slightly slower when headdim=128 and batch * nheads is large.
46
+ - Triton version doesn't yet support different sequence lengths in a batch (i.e., RaggedTensor/NestedTensor).
47
+ """
48
+
49
+ import math
50
+
51
+ import torch
52
+ import triton # type: ignore (reportMissingImports)
53
+ import triton.language as tl # type: ignore (reportMissingImports)
54
+ from einops import repeat
55
+
56
+
57
+ @triton.autotune(
58
+ configs=[
59
+ triton.Config({
60
+ 'BLOCK_M': 128,
61
+ 'BLOCK_N': 128
62
+ },
63
+ num_warps=8,
64
+ num_stages=1),
65
+ # This config has a race condition when EVEN_M == False, disabling it for now.
66
+ # triton.Config({"BLOCK_M": 64, "BLOCK_N": 64}, num_warps=4, num_stages=1),
67
+ ],
68
+ key=[
69
+ 'CACHE_KEY_SEQLEN_Q', 'CACHE_KEY_SEQLEN_K', 'BIAS_TYPE', 'IS_CAUSAL',
70
+ 'BLOCK_HEADDIM'
71
+ ])
72
+ @triton.heuristics({
73
+ 'EVEN_M': lambda args: args['seqlen_q'] % args['BLOCK_M'] == 0,
74
+ 'EVEN_N': lambda args: args['seqlen_k'] % args['BLOCK_N'] == 0,
75
+ 'EVEN_HEADDIM': lambda args: args['headdim'] == args['BLOCK_HEADDIM'],
76
+ })
77
+ @triton.jit
78
+ def _fwd_kernel(
79
+ Q,
80
+ K,
81
+ V,
82
+ Bias,
83
+ Out,
84
+ Lse,
85
+ TMP, # NOTE: TMP is a scratchpad buffer to workaround a compiler bug
86
+ softmax_scale,
87
+ stride_qb,
88
+ stride_qh,
89
+ stride_qm,
90
+ stride_kb,
91
+ stride_kh,
92
+ stride_kn,
93
+ stride_vb,
94
+ stride_vh,
95
+ stride_vn,
96
+ stride_bb,
97
+ stride_bh,
98
+ stride_bm,
99
+ stride_ob,
100
+ stride_oh,
101
+ stride_om,
102
+ nheads,
103
+ seqlen_q,
104
+ seqlen_k,
105
+ seqlen_q_rounded,
106
+ headdim,
107
+ CACHE_KEY_SEQLEN_Q,
108
+ CACHE_KEY_SEQLEN_K,
109
+ BIAS_TYPE: tl.constexpr,
110
+ IS_CAUSAL: tl.constexpr,
111
+ BLOCK_HEADDIM: tl.constexpr,
112
+ EVEN_M: tl.constexpr,
113
+ EVEN_N: tl.constexpr,
114
+ EVEN_HEADDIM: tl.constexpr,
115
+ BLOCK_M: tl.constexpr,
116
+ BLOCK_N: tl.constexpr,
117
+ ):
118
+ start_m = tl.program_id(0)
119
+ off_hb = tl.program_id(1)
120
+ off_b = off_hb // nheads
121
+ off_h = off_hb % nheads
122
+ # off_b = tl.program_id(1)
123
+ # off_h = tl.program_id(2)
124
+ # off_hb = off_b * nheads + off_h
125
+ # initialize offsets
126
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
127
+ offs_n = tl.arange(0, BLOCK_N)
128
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
129
+ # Initialize pointers to Q, K, V
130
+ # Adding parenthesis around indexing might use int32 math instead of int64 math?
131
+ # https://github.com/openai/triton/issues/741
132
+ # I'm seeing a tiny bit of difference (5-7us)
133
+ q_ptrs = Q + off_b * stride_qb + off_h * stride_qh + (
134
+ offs_m[:, None] * stride_qm + offs_d[None, :])
135
+ k_ptrs = K + off_b * stride_kb + off_h * stride_kh + (
136
+ offs_n[:, None] * stride_kn + offs_d[None, :])
137
+ v_ptrs = V + off_b * stride_vb + off_h * stride_vh + (
138
+ offs_n[:, None] * stride_vn + offs_d[None, :])
139
+ if BIAS_TYPE == 'vector':
140
+ b_ptrs = Bias + off_b * stride_bb + off_h * stride_bh + offs_n
141
+ elif BIAS_TYPE == 'matrix':
142
+ b_ptrs = Bias + off_b * stride_bb + off_h * stride_bh + (
143
+ offs_m[:, None] * stride_bm + offs_n[None, :])
144
+ else:
145
+ raise ValueError("BIAS_TYPE must be one of {'vector', 'matrix'}")
146
+ # initialize pointer to m and l
147
+ t_ptrs = TMP + off_hb * seqlen_q_rounded + offs_m
148
+ lse_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float('inf')
149
+ m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float('inf')
150
+ acc_o = tl.zeros([BLOCK_M, BLOCK_HEADDIM], dtype=tl.float32)
151
+ # load q: it will stay in SRAM throughout
152
+ # [2022-10-30] TD: Triton bug - in the case of EVEN_M=True and EVEN_N=False, if we just call
153
+ # tl.load(q_ptrs), we get the wrong output!
154
+ if EVEN_M & EVEN_N:
155
+ if EVEN_HEADDIM:
156
+ q = tl.load(q_ptrs)
157
+ else:
158
+ q = tl.load(q_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
159
+ else:
160
+ if EVEN_HEADDIM:
161
+ q = tl.load(q_ptrs, mask=offs_m[:, None] < seqlen_q, other=0.0)
162
+ else:
163
+ q = tl.load(q_ptrs,
164
+ mask=(offs_m[:, None] < seqlen_q) &
165
+ (offs_d[None, :] < headdim),
166
+ other=0.0)
167
+ # loop over k, v and update accumulator
168
+ end_n = seqlen_k if not IS_CAUSAL else tl.minimum(
169
+ (start_m + 1) * BLOCK_M, seqlen_k)
170
+ for start_n in range(0, end_n, BLOCK_N):
171
+ start_n = tl.multiple_of(start_n, BLOCK_N)
172
+ # -- compute qk ----
173
+ if EVEN_N & EVEN_M: # If we just do "if EVEN_N", there seems to be some race condition
174
+ if EVEN_HEADDIM:
175
+ k = tl.load(k_ptrs + start_n * stride_kn)
176
+ else:
177
+ k = tl.load(k_ptrs + start_n * stride_kn,
178
+ mask=offs_d[None, :] < headdim,
179
+ other=0.0)
180
+ else:
181
+ if EVEN_HEADDIM:
182
+ k = tl.load(k_ptrs + start_n * stride_kn,
183
+ mask=(start_n + offs_n)[:, None] < seqlen_k,
184
+ other=0.0)
185
+ else:
186
+ k = tl.load(k_ptrs + start_n * stride_kn,
187
+ mask=((start_n + offs_n)[:, None] < seqlen_k) &
188
+ (offs_d[None, :] < headdim),
189
+ other=0.0)
190
+ qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32)
191
+ qk += tl.dot(q, k, trans_b=True)
192
+ # Trying to combine the two masks seem to make the result wrong
193
+ if not EVEN_N: # Need to mask out otherwise the softmax is wrong
194
+ qk += tl.where((start_n + offs_n)[None, :] < seqlen_k, 0,
195
+ float('-inf'))
196
+ if IS_CAUSAL:
197
+ qk += tl.where(offs_m[:, None] >= (start_n + offs_n)[None, :], 0,
198
+ float('-inf'))
199
+ if BIAS_TYPE != 'none':
200
+ if BIAS_TYPE == 'vector':
201
+ if EVEN_N:
202
+ bias = tl.load(b_ptrs + start_n).to(tl.float32)
203
+ else:
204
+ bias = tl.load(b_ptrs + start_n,
205
+ mask=(start_n + offs_n) < seqlen_k,
206
+ other=0.0).to(tl.float32)
207
+ bias = bias[None, :]
208
+ elif BIAS_TYPE == 'matrix':
209
+ if EVEN_M & EVEN_N:
210
+ bias = tl.load(b_ptrs + start_n).to(tl.float32)
211
+ else:
212
+ bias = tl.load(b_ptrs + start_n,
213
+ mask=(offs_m[:, None] < seqlen_q) &
214
+ ((start_n + offs_n)[None, :] < seqlen_k),
215
+ other=0.0).to(tl.float32)
216
+ else:
217
+ raise ValueError(
218
+ "BIAS_TYPE must be one of {'vector', 'matrix'}")
219
+ # Slightly faster to multiply the softmax_scale in the tl.exp below since the compiler
220
+ # can then fuse the mult and add into an fma instruction. But if we have bias we need to
221
+ # to multiply with softmax_scale here.
222
+ qk = qk * softmax_scale + bias
223
+ m_ij = tl.maximum(tl.max(qk, 1), lse_i)
224
+ p = tl.exp(qk - m_ij[:, None])
225
+ else:
226
+ m_ij = tl.maximum(tl.max(qk, 1) * softmax_scale, lse_i)
227
+ p = tl.exp(qk * softmax_scale - m_ij[:, None])
228
+ l_ij = tl.sum(p, 1)
229
+
230
+ # scale acc_o
231
+ acc_o_scale = tl.exp(m_i - m_ij)
232
+
233
+ # # -- update output accumulator --
234
+ # BUG: have to store and immediately load
235
+ tl.store(t_ptrs, acc_o_scale)
236
+ acc_o_scale = tl.load(t_ptrs)
237
+ acc_o = acc_o * acc_o_scale[:, None]
238
+ # update acc_o
239
+ if EVEN_N & EVEN_M: # If we just do "if EVEN_N", there seems to be some race condition
240
+ if EVEN_HEADDIM:
241
+ v = tl.load(v_ptrs + start_n * stride_vn)
242
+ else:
243
+ v = tl.load(v_ptrs + start_n * stride_vn,
244
+ mask=offs_d[None, :] < headdim,
245
+ other=0.0)
246
+ else:
247
+ if EVEN_HEADDIM:
248
+ v = tl.load(v_ptrs + start_n * stride_vn,
249
+ mask=(start_n + offs_n)[:, None] < seqlen_k,
250
+ other=0.0)
251
+ else:
252
+ v = tl.load(v_ptrs + start_n * stride_vn,
253
+ mask=((start_n + offs_n)[:, None] < seqlen_k) &
254
+ (offs_d[None, :] < headdim),
255
+ other=0.0)
256
+ p = p.to(v.dtype)
257
+ acc_o += tl.dot(p, v)
258
+
259
+ # -- update statistics
260
+ m_i = m_ij
261
+ l_i_new = tl.exp(lse_i - m_ij) + l_ij
262
+ lse_i = m_ij + tl.log(l_i_new)
263
+
264
+ o_scale = tl.exp(m_i - lse_i)
265
+ # BUG: have to store and immediately load
266
+ tl.store(t_ptrs, o_scale)
267
+ o_scale = tl.load(t_ptrs)
268
+ acc_o = acc_o * o_scale[:, None]
269
+ # rematerialize offsets to save registers
270
+ start_m = tl.program_id(0)
271
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
272
+ # write back l and m
273
+ lse_ptrs = Lse + off_hb * seqlen_q_rounded + offs_m
274
+ tl.store(lse_ptrs, lse_i)
275
+ # initialize pointers to output
276
+ offs_n = tl.arange(0, BLOCK_HEADDIM)
277
+ out_ptrs = Out + off_b * stride_ob + off_h * stride_oh + (
278
+ offs_m[:, None] * stride_om + offs_n[None, :])
279
+ if EVEN_M:
280
+ if EVEN_HEADDIM:
281
+ tl.store(out_ptrs, acc_o)
282
+ else:
283
+ tl.store(out_ptrs, acc_o, mask=offs_d[None, :] < headdim)
284
+ else:
285
+ if EVEN_HEADDIM:
286
+ tl.store(out_ptrs, acc_o, mask=offs_m[:, None] < seqlen_q)
287
+ else:
288
+ tl.store(out_ptrs,
289
+ acc_o,
290
+ mask=(offs_m[:, None] < seqlen_q) &
291
+ (offs_d[None, :] < headdim))
292
+
293
+
294
+ @triton.jit
295
+ def _bwd_preprocess_do_o_dot(
296
+ Out,
297
+ DO,
298
+ Delta,
299
+ stride_ob,
300
+ stride_oh,
301
+ stride_om,
302
+ stride_dob,
303
+ stride_doh,
304
+ stride_dom,
305
+ nheads,
306
+ seqlen_q,
307
+ seqlen_q_rounded,
308
+ headdim,
309
+ BLOCK_M: tl.constexpr,
310
+ BLOCK_HEADDIM: tl.constexpr,
311
+ ):
312
+ start_m = tl.program_id(0)
313
+ off_hb = tl.program_id(1)
314
+ off_b = off_hb // nheads
315
+ off_h = off_hb % nheads
316
+ # initialize offsets
317
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
318
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
319
+ # load
320
+ o = tl.load(Out + off_b * stride_ob + off_h * stride_oh +
321
+ offs_m[:, None] * stride_om + offs_d[None, :],
322
+ mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim),
323
+ other=0.0).to(tl.float32)
324
+ do = tl.load(DO + off_b * stride_dob + off_h * stride_doh +
325
+ offs_m[:, None] * stride_dom + offs_d[None, :],
326
+ mask=(offs_m[:, None] < seqlen_q) &
327
+ (offs_d[None, :] < headdim),
328
+ other=0.0).to(tl.float32)
329
+ delta = tl.sum(o * do, axis=1)
330
+ # write-back
331
+ tl.store(Delta + off_hb * seqlen_q_rounded + offs_m, delta)
332
+
333
+
334
+ @triton.jit
335
+ def _bwd_kernel_one_col_block(
336
+ start_n,
337
+ Q,
338
+ K,
339
+ V,
340
+ Bias,
341
+ DO,
342
+ DQ,
343
+ DK,
344
+ DV,
345
+ LSE,
346
+ D,
347
+ softmax_scale,
348
+ stride_qm,
349
+ stride_kn,
350
+ stride_vn,
351
+ stride_bm,
352
+ stride_dom,
353
+ stride_dqm,
354
+ stride_dkn,
355
+ stride_dvn,
356
+ seqlen_q,
357
+ seqlen_k,
358
+ headdim,
359
+ ATOMIC_ADD: tl.constexpr,
360
+ BIAS_TYPE: tl.constexpr,
361
+ IS_CAUSAL: tl.constexpr,
362
+ BLOCK_HEADDIM: tl.constexpr,
363
+ EVEN_M: tl.constexpr,
364
+ EVEN_N: tl.constexpr,
365
+ EVEN_HEADDIM: tl.constexpr,
366
+ BLOCK_M: tl.constexpr,
367
+ BLOCK_N: tl.constexpr,
368
+ ):
369
+ # We need to make sure begin_m is a multiple of BLOCK_M (not BLOCK_N)
370
+ begin_m = 0 if not IS_CAUSAL else ((start_n * BLOCK_N) // BLOCK_M) * BLOCK_M
371
+ # initialize row/col offsets
372
+ offs_qm = begin_m + tl.arange(0, BLOCK_M)
373
+ offs_n = start_n * BLOCK_N + tl.arange(0, BLOCK_N)
374
+ offs_m = tl.arange(0, BLOCK_M)
375
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
376
+ # initialize pointers to value-like data
377
+ q_ptrs = Q + (offs_qm[:, None] * stride_qm + offs_d[None, :])
378
+ k_ptrs = K + (offs_n[:, None] * stride_kn + offs_d[None, :])
379
+ v_ptrs = V + (offs_n[:, None] * stride_vn + offs_d[None, :])
380
+ do_ptrs = DO + (offs_qm[:, None] * stride_dom + offs_d[None, :])
381
+ dq_ptrs = DQ + (offs_qm[:, None] * stride_dqm + offs_d[None, :])
382
+ if BIAS_TYPE == 'vector':
383
+ b_ptrs = Bias + offs_n
384
+ elif BIAS_TYPE == 'matrix':
385
+ b_ptrs = Bias + (offs_qm[:, None] * stride_bm + offs_n[None, :])
386
+ else:
387
+ raise ValueError("BIAS_TYPE must be one of {'vector', 'matrix'}")
388
+ # initialize dv and dk
389
+ dv = tl.zeros([BLOCK_N, BLOCK_HEADDIM], dtype=tl.float32)
390
+ dk = tl.zeros([BLOCK_N, BLOCK_HEADDIM], dtype=tl.float32)
391
+ # k and v stay in SRAM throughout
392
+ # [2022-10-30] TD: Same bug as the fwd. In the case of EVEN_N=True and EVEN_M=False,
393
+ # if we just call tl.load(k_ptrs), we get the wrong output!
394
+ if EVEN_N & EVEN_M:
395
+ if EVEN_HEADDIM:
396
+ k = tl.load(k_ptrs)
397
+ v = tl.load(v_ptrs)
398
+ else:
399
+ k = tl.load(k_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
400
+ v = tl.load(v_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
401
+ else:
402
+ if EVEN_HEADDIM:
403
+ k = tl.load(k_ptrs, mask=offs_n[:, None] < seqlen_k, other=0.0)
404
+ v = tl.load(v_ptrs, mask=offs_n[:, None] < seqlen_k, other=0.0)
405
+ else:
406
+ k = tl.load(k_ptrs,
407
+ mask=(offs_n[:, None] < seqlen_k) &
408
+ (offs_d[None, :] < headdim),
409
+ other=0.0)
410
+ v = tl.load(v_ptrs,
411
+ mask=(offs_n[:, None] < seqlen_k) &
412
+ (offs_d[None, :] < headdim),
413
+ other=0.0)
414
+ # loop over rows
415
+ num_block_m = tl.cdiv(seqlen_q, BLOCK_M)
416
+ for start_m in range(begin_m, num_block_m * BLOCK_M, BLOCK_M):
417
+ start_m = tl.multiple_of(start_m, BLOCK_M)
418
+ offs_m_curr = start_m + offs_m
419
+ # load q, k, v, do on-chip
420
+ # Same bug as below. Otherwise gives wrong result for headdim=40, seqlen=(128, 117)
421
+ if EVEN_M & EVEN_HEADDIM:
422
+ q = tl.load(q_ptrs)
423
+ else:
424
+ if EVEN_HEADDIM:
425
+ q = tl.load(q_ptrs,
426
+ mask=offs_m_curr[:, None] < seqlen_q,
427
+ other=0.0)
428
+ else:
429
+ q = tl.load(q_ptrs,
430
+ mask=(offs_m_curr[:, None] < seqlen_q) &
431
+ (offs_d[None, :] < headdim),
432
+ other=0.0)
433
+ # recompute p = softmax(qk, dim=-1).T
434
+ qk = tl.dot(q, k, trans_b=True)
435
+ # Trying to combine the two masks seem to make the result wrong
436
+ if not EVEN_N: # Need to mask out otherwise the softmax is wrong
437
+ qk = tl.where(offs_n[None, :] < seqlen_k, qk, float('-inf'))
438
+ if IS_CAUSAL:
439
+ qk = tl.where(offs_m_curr[:, None] >= (offs_n[None, :]), qk,
440
+ float('-inf'))
441
+ if BIAS_TYPE != 'none':
442
+ if BIAS_TYPE == 'vector':
443
+ if EVEN_N:
444
+ bias = tl.load(b_ptrs).to(tl.float32)
445
+ else:
446
+ bias = tl.load(b_ptrs, mask=offs_n < seqlen_k,
447
+ other=0.0).to(tl.float32)
448
+ bias = bias[None, :]
449
+ elif BIAS_TYPE == 'matrix':
450
+ if EVEN_M & EVEN_N:
451
+ bias = tl.load(b_ptrs).to(tl.float32)
452
+ else:
453
+ bias = tl.load(b_ptrs,
454
+ mask=(offs_m_curr[:, None] < seqlen_q) &
455
+ (offs_n[None, :] < seqlen_k),
456
+ other=0.0).to(tl.float32)
457
+ else:
458
+ raise ValueError(
459
+ "BIAS_TYPE must be one of {'vector', 'matrix'}")
460
+ qk = qk * softmax_scale + bias
461
+ # There seems to be a race condition when headdim=48/96, and dq, dk, dv are wrong.
462
+ # Also wrong for headdim=64.
463
+ if not (EVEN_M & EVEN_HEADDIM):
464
+ tl.debug_barrier()
465
+ lse_i = tl.load(LSE + offs_m_curr)
466
+ if BIAS_TYPE == 'none':
467
+ p = tl.exp(qk * softmax_scale - lse_i[:, None])
468
+ else:
469
+ p = tl.exp(qk - lse_i[:, None])
470
+ # compute dv
471
+ # [2022-10-30] TD: A Triton bug: if EVEN_M=True and EVEN_HEADDIM=False, if we call
472
+ # do = tl.load(do_ptrs, mask=offs_d[None, :] < headdim, other=0.0), we get wrong outputs
473
+ # in the case of headdim=48/96, seqlen_q & seqlen_k >= 512. If headdim=40 or seqlen < 512,
474
+ # the output is correct.
475
+ if EVEN_M & EVEN_HEADDIM:
476
+ do = tl.load(do_ptrs)
477
+ else:
478
+ # [2022-11-01] TD: Triton bug, there's a race condition if we just use m_mask and not d_mask.
479
+ do = tl.load(do_ptrs,
480
+ mask=(offs_m_curr[:, None] < seqlen_q) &
481
+ (offs_d[None, :] < headdim),
482
+ other=0.0)
483
+ # if EVEN_M:
484
+ # if EVEN_HEADDIM:
485
+ # do = tl.load(do_ptrs)
486
+ # else:
487
+ # do = tl.load(do_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
488
+ # else:
489
+ # if EVEN_HEADDIM:
490
+ # do = tl.load(do_ptrs, mask=offs_m_curr[:, None] < seqlen_q, other=0.0)
491
+ # else:
492
+ # do = tl.load(do_ptrs, mask=(offs_m_curr[:, None] < seqlen_q)
493
+ # & (offs_d[None, :] < headdim), other=0.0)
494
+ dv += tl.dot(p.to(do.dtype), do, trans_a=True)
495
+ # compute dp = dot(v, do)
496
+ # There seems to be a race condition when headdim=48/96, and dq, dk are wrong.
497
+ # Also wrong for headdim=128, seqlen=(108, 256), and ATOMIC_ADD=True
498
+ # Also wrong for headdim=64, seqlen=(1023, 1024), and ATOMIC_ADD=False
499
+ if not (EVEN_M & EVEN_HEADDIM):
500
+ tl.debug_barrier()
501
+ dp = tl.dot(do, v, trans_b=True)
502
+ # There's a race condition for headdim=48
503
+ if not EVEN_HEADDIM:
504
+ tl.debug_barrier()
505
+ # compute ds = p * (dp - delta[:, None])
506
+ # Putting the subtraction after the dp matmul (instead of before) is slightly faster
507
+ Di = tl.load(D + offs_m_curr)
508
+ # Converting ds to q.dtype here reduces register pressure and makes it much faster
509
+ # for BLOCK_HEADDIM=128
510
+ ds = (p * (dp - Di[:, None]) * softmax_scale).to(q.dtype)
511
+ # compute dk = dot(ds.T, q)
512
+ dk += tl.dot(ds, q, trans_a=True)
513
+ # compute dq
514
+ if not ATOMIC_ADD:
515
+ if EVEN_M & EVEN_HEADDIM: # Race condition if we just do EVEN_M
516
+ dq = tl.load(dq_ptrs, eviction_policy='evict_last')
517
+ dq += tl.dot(ds, k)
518
+ tl.store(dq_ptrs, dq, eviction_policy='evict_last')
519
+ else:
520
+ if EVEN_HEADDIM:
521
+ dq = tl.load(dq_ptrs,
522
+ mask=offs_m_curr[:, None] < seqlen_q,
523
+ other=0.0,
524
+ eviction_policy='evict_last')
525
+ dq += tl.dot(ds, k)
526
+ tl.store(dq_ptrs,
527
+ dq,
528
+ mask=offs_m_curr[:, None] < seqlen_q,
529
+ eviction_policy='evict_last')
530
+ else:
531
+ dq = tl.load(dq_ptrs,
532
+ mask=(offs_m_curr[:, None] < seqlen_q) &
533
+ (offs_d[None, :] < headdim),
534
+ other=0.0,
535
+ eviction_policy='evict_last')
536
+ dq += tl.dot(ds, k)
537
+ tl.store(dq_ptrs,
538
+ dq,
539
+ mask=(offs_m_curr[:, None] < seqlen_q) &
540
+ (offs_d[None, :] < headdim),
541
+ eviction_policy='evict_last')
542
+ else: # If we're parallelizing across the seqlen_k dimension
543
+ dq = tl.dot(ds, k)
544
+ if EVEN_M & EVEN_HEADDIM: # Race condition if we just do EVEN_M
545
+ tl.atomic_add(dq_ptrs, dq)
546
+ else:
547
+ if EVEN_HEADDIM:
548
+ tl.atomic_add(dq_ptrs,
549
+ dq,
550
+ mask=offs_m_curr[:, None] < seqlen_q)
551
+ else:
552
+ tl.atomic_add(dq_ptrs,
553
+ dq,
554
+ mask=(offs_m_curr[:, None] < seqlen_q) &
555
+ (offs_d[None, :] < headdim))
556
+ # increment pointers
557
+ dq_ptrs += BLOCK_M * stride_dqm
558
+ q_ptrs += BLOCK_M * stride_qm
559
+ do_ptrs += BLOCK_M * stride_dom
560
+ if BIAS_TYPE == 'matrix':
561
+ b_ptrs += BLOCK_M * stride_bm
562
+ # write-back
563
+ dv_ptrs = DV + (offs_n[:, None] * stride_dvn + offs_d[None, :])
564
+ dk_ptrs = DK + (offs_n[:, None] * stride_dkn + offs_d[None, :])
565
+ # [2022-11-01] TD: Same bug. In the case of EVEN_N=True and EVEN_M=False,
566
+ # if we just call tl.store(dv_ptrs), there's a race condition
567
+ if EVEN_N & EVEN_M:
568
+ if EVEN_HEADDIM:
569
+ tl.store(dv_ptrs, dv)
570
+ tl.store(dk_ptrs, dk)
571
+ else:
572
+ tl.store(dv_ptrs, dv, mask=offs_d[None, :] < headdim)
573
+ tl.store(dk_ptrs, dk, mask=offs_d[None, :] < headdim)
574
+ else:
575
+ if EVEN_HEADDIM:
576
+ tl.store(dv_ptrs, dv, mask=offs_n[:, None] < seqlen_k)
577
+ tl.store(dk_ptrs, dk, mask=offs_n[:, None] < seqlen_k)
578
+ else:
579
+ tl.store(dv_ptrs,
580
+ dv,
581
+ mask=(offs_n[:, None] < seqlen_k) &
582
+ (offs_d[None, :] < headdim))
583
+ tl.store(dk_ptrs,
584
+ dk,
585
+ mask=(offs_n[:, None] < seqlen_k) &
586
+ (offs_d[None, :] < headdim))
587
+
588
+
589
+ def init_to_zero(name):
590
+ return lambda nargs: nargs[name].zero_()
591
+
592
+
593
+ @triton.autotune(
594
+ configs=[
595
+ triton.Config(
596
+ {
597
+ 'BLOCK_M': 128,
598
+ 'BLOCK_N': 128,
599
+ 'SEQUENCE_PARALLEL': False
600
+ },
601
+ num_warps=8,
602
+ num_stages=1,
603
+ pre_hook=init_to_zero('DQ')),
604
+ triton.Config(
605
+ {
606
+ 'BLOCK_M': 128,
607
+ 'BLOCK_N': 128,
608
+ 'SEQUENCE_PARALLEL': True
609
+ },
610
+ num_warps=8,
611
+ num_stages=1,
612
+ pre_hook=init_to_zero('DQ')),
613
+ # Other configs seem to give wrong results when seqlen_q % 128 != 0, disabling them for now
614
+ # # Kernel is buggy (give wrong result) if we set BLOCK_m=128, BLOCK_n=64, num_warps=*4*
615
+ # triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "SEQUENCE_PARALLEL": False}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ')),
616
+ # triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "SEQUENCE_PARALLEL": True}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ')),
617
+ # triton.Config({"BLOCK_M": 64, "BLOCK_N": 64, "SEQUENCE_PARALLEL": False}, num_warps=4, num_stages=1, pre_hook=init_to_zero('DQ')),
618
+ # triton.Config({"BLOCK_M": 64, "BLOCK_N": 64, "SEQUENCE_PARALLEL": True}, num_warps=4, num_stages=1, pre_hook=init_to_zero('DQ')),
619
+ ],
620
+ key=[
621
+ 'CACHE_KEY_SEQLEN_Q', 'CACHE_KEY_SEQLEN_K', 'BIAS_TYPE', 'IS_CAUSAL',
622
+ 'BLOCK_HEADDIM'
623
+ ],
624
+ )
625
+ @triton.heuristics({
626
+ 'EVEN_M': lambda args: args['seqlen_q'] % args['BLOCK_M'] == 0,
627
+ 'EVEN_N': lambda args: args['seqlen_k'] % args['BLOCK_N'] == 0,
628
+ 'EVEN_HEADDIM': lambda args: args['headdim'] == args['BLOCK_HEADDIM'],
629
+ })
630
+ @triton.jit
631
+ def _bwd_kernel(
632
+ Q,
633
+ K,
634
+ V,
635
+ Bias,
636
+ DO,
637
+ DQ,
638
+ DK,
639
+ DV,
640
+ LSE,
641
+ D,
642
+ softmax_scale,
643
+ stride_qb,
644
+ stride_qh,
645
+ stride_qm,
646
+ stride_kb,
647
+ stride_kh,
648
+ stride_kn,
649
+ stride_vb,
650
+ stride_vh,
651
+ stride_vn,
652
+ stride_bb,
653
+ stride_bh,
654
+ stride_bm,
655
+ stride_dob,
656
+ stride_doh,
657
+ stride_dom,
658
+ stride_dqb,
659
+ stride_dqh,
660
+ stride_dqm,
661
+ stride_dkb,
662
+ stride_dkh,
663
+ stride_dkn,
664
+ stride_dvb,
665
+ stride_dvh,
666
+ stride_dvn,
667
+ nheads,
668
+ seqlen_q,
669
+ seqlen_k,
670
+ seqlen_q_rounded,
671
+ headdim,
672
+ CACHE_KEY_SEQLEN_Q,
673
+ CACHE_KEY_SEQLEN_K,
674
+ BIAS_TYPE: tl.constexpr,
675
+ IS_CAUSAL: tl.constexpr,
676
+ BLOCK_HEADDIM: tl.constexpr,
677
+ SEQUENCE_PARALLEL: tl.constexpr,
678
+ EVEN_M: tl.constexpr,
679
+ EVEN_N: tl.constexpr,
680
+ EVEN_HEADDIM: tl.constexpr,
681
+ BLOCK_M: tl.constexpr,
682
+ BLOCK_N: tl.constexpr,
683
+ ):
684
+ off_hb = tl.program_id(1)
685
+ off_b = off_hb // nheads
686
+ off_h = off_hb % nheads
687
+ # offset pointers for batch/head
688
+ Q += off_b * stride_qb + off_h * stride_qh
689
+ K += off_b * stride_kb + off_h * stride_kh
690
+ V += off_b * stride_vb + off_h * stride_vh
691
+ DO += off_b * stride_dob + off_h * stride_doh
692
+ DQ += off_b * stride_dqb + off_h * stride_dqh
693
+ DK += off_b * stride_dkb + off_h * stride_dkh
694
+ DV += off_b * stride_dvb + off_h * stride_dvh
695
+ if BIAS_TYPE != 'none':
696
+ Bias += off_b * stride_bb + off_h * stride_bh
697
+ # pointer to row-wise quantities in value-like data
698
+ D += off_hb * seqlen_q_rounded
699
+ LSE += off_hb * seqlen_q_rounded
700
+ if not SEQUENCE_PARALLEL:
701
+ num_block_n = tl.cdiv(seqlen_k, BLOCK_N)
702
+ for start_n in range(0, num_block_n):
703
+ _bwd_kernel_one_col_block(start_n,
704
+ Q,
705
+ K,
706
+ V,
707
+ Bias,
708
+ DO,
709
+ DQ,
710
+ DK,
711
+ DV,
712
+ LSE,
713
+ D,
714
+ softmax_scale,
715
+ stride_qm,
716
+ stride_kn,
717
+ stride_vn,
718
+ stride_bm,
719
+ stride_dom,
720
+ stride_dqm,
721
+ stride_dkn,
722
+ stride_dvn,
723
+ seqlen_q,
724
+ seqlen_k,
725
+ headdim,
726
+ ATOMIC_ADD=False,
727
+ BIAS_TYPE=BIAS_TYPE,
728
+ IS_CAUSAL=IS_CAUSAL,
729
+ BLOCK_HEADDIM=BLOCK_HEADDIM,
730
+ EVEN_M=EVEN_M,
731
+ EVEN_N=EVEN_N,
732
+ EVEN_HEADDIM=EVEN_HEADDIM,
733
+ BLOCK_M=BLOCK_M,
734
+ BLOCK_N=BLOCK_N)
735
+ else:
736
+ start_n = tl.program_id(0)
737
+ _bwd_kernel_one_col_block(start_n,
738
+ Q,
739
+ K,
740
+ V,
741
+ Bias,
742
+ DO,
743
+ DQ,
744
+ DK,
745
+ DV,
746
+ LSE,
747
+ D,
748
+ softmax_scale,
749
+ stride_qm,
750
+ stride_kn,
751
+ stride_vn,
752
+ stride_bm,
753
+ stride_dom,
754
+ stride_dqm,
755
+ stride_dkn,
756
+ stride_dvn,
757
+ seqlen_q,
758
+ seqlen_k,
759
+ headdim,
760
+ ATOMIC_ADD=True,
761
+ BIAS_TYPE=BIAS_TYPE,
762
+ IS_CAUSAL=IS_CAUSAL,
763
+ BLOCK_HEADDIM=BLOCK_HEADDIM,
764
+ EVEN_M=EVEN_M,
765
+ EVEN_N=EVEN_N,
766
+ EVEN_HEADDIM=EVEN_HEADDIM,
767
+ BLOCK_M=BLOCK_M,
768
+ BLOCK_N=BLOCK_N)
769
+
770
+
771
+ def _flash_attn_forward(q, k, v, bias=None, causal=False, softmax_scale=None):
772
+ # shape constraints
773
+ batch, seqlen_q, nheads, d = q.shape
774
+ _, seqlen_k, _, _ = k.shape
775
+ assert k.shape == (batch, seqlen_k, nheads, d)
776
+ assert v.shape == (batch, seqlen_k, nheads, d)
777
+ assert d <= 128, 'FlashAttention only support head dimensions up to 128'
778
+ assert q.dtype == k.dtype == v.dtype, 'All tensors must have the same type'
779
+ assert q.dtype in [torch.float16,
780
+ torch.bfloat16], 'Only support fp16 and bf16'
781
+ assert q.is_cuda and k.is_cuda and v.is_cuda
782
+ softmax_scale = softmax_scale or 1.0 / math.sqrt(d)
783
+
784
+ has_bias = bias is not None
785
+ bias_type = 'none'
786
+ if has_bias:
787
+ assert bias.dtype in [q.dtype, torch.float]
788
+ assert bias.is_cuda
789
+ assert bias.dim() == 4
790
+ if bias.stride(-1) != 1:
791
+ bias = bias.contiguous()
792
+ if bias.shape[2:] == (1, seqlen_k):
793
+ bias_type = 'vector'
794
+ elif bias.shape[2:] == (seqlen_q, seqlen_k):
795
+ bias_type = 'matrix'
796
+ else:
797
+ raise RuntimeError('Last 2 dimensions of bias must be (1, seqlen_k)'
798
+ ' or (seqlen_q, seqlen_k)')
799
+ if bias.shape[:2] == (1, nheads):
800
+ bias = repeat(bias, '1 h ... -> b h ...', b=batch)
801
+ elif bias.shape[:2] == (batch, 1):
802
+ bias = repeat(bias, 'b 1 ... -> b h ...', h=nheads)
803
+ elif bias.shape[:2] == (1, 1):
804
+ bias = repeat(bias, '1 h ... -> b h ...', b=batch)
805
+ bias = repeat(bias, 'b 1 ... -> b h ...', h=nheads)
806
+ assert bias.shape[:2] == (
807
+ batch, nheads
808
+ ), f'First 2 dimensions of bias must be broadcastible to (batch, nheads) = ({batch, nheads}). Bias has shape: {bias.shape}'
809
+ assert bias is not None # for type checking
810
+ bias_strides = (bias.stride(0), bias.stride(1),
811
+ bias.stride(2)) if has_bias else (0, 0, 0)
812
+
813
+ seqlen_q_rounded = math.ceil(seqlen_q / 128) * 128
814
+ lse = torch.empty((batch, nheads, seqlen_q_rounded),
815
+ device=q.device,
816
+ dtype=torch.float32)
817
+ tmp = torch.empty((batch, nheads, seqlen_q_rounded),
818
+ device=q.device,
819
+ dtype=torch.float32)
820
+ o = torch.empty_like(q)
821
+
822
+ BLOCK_HEADDIM = max(triton.next_power_of_2(d), 16)
823
+ # BLOCK = 128
824
+ # num_warps = 4 if d <= 64 else 8
825
+ grid = lambda META: (triton.cdiv(seqlen_q, META['BLOCK_M']), batch * nheads)
826
+ _fwd_kernel[grid]( # type: ignore
827
+ q,
828
+ k,
829
+ v,
830
+ bias,
831
+ o,
832
+ lse,
833
+ tmp,
834
+ softmax_scale,
835
+ q.stride(0),
836
+ q.stride(2),
837
+ q.stride(1),
838
+ k.stride(0),
839
+ k.stride(2),
840
+ k.stride(1),
841
+ v.stride(0),
842
+ v.stride(2),
843
+ v.stride(1),
844
+ *bias_strides,
845
+ o.stride(0),
846
+ o.stride(2),
847
+ o.stride(1),
848
+ nheads,
849
+ seqlen_q,
850
+ seqlen_k,
851
+ seqlen_q_rounded,
852
+ d,
853
+ seqlen_q // 32,
854
+ seqlen_k // 32, # key for triton cache (limit number of compilations)
855
+ # Can't use kwargs here because triton autotune expects key to be args, not kwargs
856
+ # IS_CAUSAL=causal, BLOCK_HEADDIM=d,
857
+ bias_type,
858
+ causal,
859
+ BLOCK_HEADDIM,
860
+ # BLOCK_M=BLOCK, BLOCK_N=BLOCK,
861
+ # num_warps=num_warps,
862
+ # num_stages=1,
863
+ )
864
+ return o, lse, softmax_scale # softmax_scale could have been updated
865
+
866
+
867
+ def _flash_attn_backward(do,
868
+ q,
869
+ k,
870
+ v,
871
+ o,
872
+ lse,
873
+ dq,
874
+ dk,
875
+ dv,
876
+ bias=None,
877
+ causal=False,
878
+ softmax_scale=None):
879
+ # Make sure that the last dimension is contiguous
880
+ if do.stride(-1) != 1:
881
+ do = do.contiguous()
882
+ batch, seqlen_q, nheads, d = q.shape
883
+ _, seqlen_k, _, _ = k.shape
884
+ # assert d in {16, 32, 64, 128}
885
+ assert d <= 128
886
+ seqlen_q_rounded = math.ceil(seqlen_q / 128) * 128
887
+ assert lse.shape == (batch, nheads, seqlen_q_rounded)
888
+ assert q.stride(-1) == k.stride(-1) == v.stride(-1) == o.stride(-1) == 1
889
+ assert dq.stride(-1) == dk.stride(-1) == dv.stride(-1) == 1
890
+ softmax_scale = softmax_scale or 1.0 / math.sqrt(d)
891
+ # dq_accum = torch.zeros_like(q, dtype=torch.float32)
892
+ dq_accum = torch.empty_like(q, dtype=torch.float32)
893
+ delta = torch.empty_like(lse)
894
+ # delta = torch.zeros_like(lse)
895
+
896
+ BLOCK_HEADDIM = max(triton.next_power_of_2(d), 16)
897
+ grid = lambda META: (triton.cdiv(seqlen_q, META['BLOCK_M']), batch * nheads)
898
+ _bwd_preprocess_do_o_dot[grid]( # type: ignore
899
+ o,
900
+ do,
901
+ delta,
902
+ o.stride(0),
903
+ o.stride(2),
904
+ o.stride(1),
905
+ do.stride(0),
906
+ do.stride(2),
907
+ do.stride(1),
908
+ nheads,
909
+ seqlen_q,
910
+ seqlen_q_rounded,
911
+ d,
912
+ BLOCK_M=128,
913
+ BLOCK_HEADDIM=BLOCK_HEADDIM,
914
+ )
915
+
916
+ has_bias = bias is not None
917
+ bias_type = 'none'
918
+ if has_bias:
919
+ assert bias.dtype in [q.dtype, torch.float]
920
+ assert bias.is_cuda
921
+ assert bias.dim() == 4
922
+ assert bias.stride(-1) == 1
923
+ if bias.shape[2:] == (1, seqlen_k):
924
+ bias_type = 'vector'
925
+ elif bias.shape[2:] == (seqlen_q, seqlen_k):
926
+ bias_type = 'matrix'
927
+ else:
928
+ raise RuntimeError('Last 2 dimensions of bias must be (1, seqlen_k)'
929
+ ' or (seqlen_q, seqlen_k)')
930
+ if bias.shape[:2] == (1, nheads):
931
+ bias = repeat(bias, '1 h ... -> b h ...', b=batch)
932
+ elif bias.shape[:2] == (batch, 1):
933
+ bias = repeat(bias, 'b 1 ... -> b h ...', h=nheads)
934
+ elif bias.shape[:2] == (1, 1):
935
+ bias = repeat(bias, '1 h ... -> b h ...', b=batch)
936
+ bias = repeat(bias, 'b 1 ... -> b h ...', h=nheads)
937
+ assert bias.shape[:2] == (
938
+ batch, nheads
939
+ ), f'First 2 dimensions of bias must be broadcastible to (batch, nheads) = ({batch, nheads}). Bias has shape: {bias.shape}'
940
+ assert bias is not None # type checking
941
+ bias_strides = (bias.stride(0), bias.stride(1),
942
+ bias.stride(2)) if has_bias else (0, 0, 0)
943
+
944
+ # BLOCK_M = 128
945
+ # BLOCK_N = 64
946
+ # num_warps = 4
947
+ grid = lambda META: (triton.cdiv(seqlen_k, META['BLOCK_N'])
948
+ if META['SEQUENCE_PARALLEL'] else 1, batch * nheads)
949
+ _bwd_kernel[grid]( # type: ignore
950
+ q,
951
+ k,
952
+ v,
953
+ bias,
954
+ do,
955
+ dq_accum,
956
+ dk,
957
+ dv,
958
+ lse,
959
+ delta,
960
+ softmax_scale,
961
+ q.stride(0),
962
+ q.stride(2),
963
+ q.stride(1),
964
+ k.stride(0),
965
+ k.stride(2),
966
+ k.stride(1),
967
+ v.stride(0),
968
+ v.stride(2),
969
+ v.stride(1),
970
+ *bias_strides,
971
+ do.stride(0),
972
+ do.stride(2),
973
+ do.stride(1),
974
+ dq_accum.stride(0),
975
+ dq_accum.stride(2),
976
+ dq_accum.stride(1),
977
+ dk.stride(0),
978
+ dk.stride(2),
979
+ dk.stride(1),
980
+ dv.stride(0),
981
+ dv.stride(2),
982
+ dv.stride(1),
983
+ nheads,
984
+ seqlen_q,
985
+ seqlen_k,
986
+ seqlen_q_rounded,
987
+ d,
988
+ seqlen_q // 32,
989
+ seqlen_k // 32, # key for triton cache (limit number of compilations)
990
+ # Can't use kwargs here because triton autotune expects key to be args, not kwargs
991
+ # IS_CAUSAL=causal, BLOCK_HEADDIM=d,
992
+ bias_type,
993
+ causal,
994
+ BLOCK_HEADDIM,
995
+ # SEQUENCE_PARALLEL=False,
996
+ # BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N,
997
+ # num_warps=num_warps,
998
+ # num_stages=1,
999
+ )
1000
+ dq.copy_(dq_accum)
1001
+
1002
+
1003
+ class _FlashAttnQKVPackedFunc(torch.autograd.Function):
1004
+
1005
+ @staticmethod
1006
+ def forward(ctx, qkv, bias=None, causal=False, softmax_scale=None):
1007
+ """Forward pass for packed FlashAttention.
1008
+
1009
+ Args:
1010
+ ctx: autograd context
1011
+ qkv: (batch, seqlen, 3, nheads, headdim)
1012
+ bias: optional, shape broadcastible to (batch, nheads, seqlen, seqlen).
1013
+ For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen).
1014
+ ALiBi mask for non-causal would have shape (1, nheads, seqlen, seqlen)
1015
+ causal (bool): whether to incorporate causal attention masking
1016
+ softmax_scale (float, optional): scale factor for softmax
1017
+ """
1018
+ # Make sure that the last dimension is contiguous
1019
+ if qkv.stride(-1) != 1:
1020
+ qkv = qkv.contiguous()
1021
+ o, lse, ctx.softmax_scale = _flash_attn_forward(
1022
+ qkv[:, :, 0],
1023
+ qkv[:, :, 1],
1024
+ qkv[:, :, 2],
1025
+ bias=bias,
1026
+ causal=causal,
1027
+ softmax_scale=softmax_scale)
1028
+ ctx.save_for_backward(qkv, o, lse, bias)
1029
+ ctx.causal = causal
1030
+ return o
1031
+
1032
+ @staticmethod
1033
+ def backward(ctx, do):
1034
+ qkv, o, lse, bias = ctx.saved_tensors
1035
+ assert not ctx.needs_input_grad[
1036
+ 1], 'FlashAttention does not support bias gradient yet'
1037
+ # Triton's autotune causes the Tensor._version to change, and so Pytorch autograd
1038
+ # does a memcpy. To avoid this we run in inference_mode, which doesn't track the version.
1039
+ with torch.inference_mode():
1040
+ dqkv = torch.empty_like(qkv)
1041
+ _flash_attn_backward(do,
1042
+ qkv[:, :, 0],
1043
+ qkv[:, :, 1],
1044
+ qkv[:, :, 2],
1045
+ o,
1046
+ lse,
1047
+ dqkv[:, :, 0],
1048
+ dqkv[:, :, 1],
1049
+ dqkv[:, :, 2],
1050
+ bias=bias,
1051
+ causal=ctx.causal,
1052
+ softmax_scale=ctx.softmax_scale)
1053
+ return dqkv, None, None, None
1054
+
1055
+
1056
+ flash_attn_qkvpacked_func = _FlashAttnQKVPackedFunc.apply
1057
+
1058
+
1059
+ class _FlashAttnFunc(torch.autograd.Function):
1060
+
1061
+ @staticmethod
1062
+ def forward(ctx, q, k, v, bias=None, causal=False, softmax_scale=None):
1063
+ """Forward pass for FlashAttention.
1064
+
1065
+ Args:
1066
+ ctx: autograd context
1067
+ q: (batch_size, seqlen_q, nheads, headdim)
1068
+ k: (batch_size, seqlen_k, nheads, headdim)
1069
+ v: (batch_size, seqlen_k, nheads, headdim)
1070
+ bias: optional, shape broadcastible to (batch, nheads, seqlen_q, seqlen_k).
1071
+ For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen_k).
1072
+ ALiBi mask for non-causal would have shape (1, nheads, seqlen_q, seqlen_k)
1073
+ causal (bool): whether to incorporate causal attention masking
1074
+ softmax_scale (float, optional): scale factor for softmax
1075
+ """
1076
+ # Make sure that the last dimension is contiguous
1077
+ q, k, v = [
1078
+ x if x.stride(-1) == 1 else x.contiguous() for x in [q, k, v]
1079
+ ]
1080
+ o, lse, ctx.softmax_scale = _flash_attn_forward(
1081
+ q, k, v, bias=bias, causal=causal, softmax_scale=softmax_scale)
1082
+ ctx.save_for_backward(q, k, v, o, lse, bias)
1083
+ ctx.causal = causal
1084
+ return o
1085
+
1086
+ @staticmethod
1087
+ def backward(ctx, do):
1088
+ q, k, v, o, lse, bias = ctx.saved_tensors
1089
+ assert not ctx.needs_input_grad[
1090
+ 3], 'FlashAttention does not support bias gradient yet'
1091
+ # Triton's autotune causes the Tensor._version to change, and so Pytorch autograd
1092
+ # does a memcpy. To avoid this we run in inference_mode, which doesn't track the version.
1093
+ with torch.inference_mode():
1094
+ dq = torch.empty_like(q)
1095
+ dk = torch.empty_like(k)
1096
+ dv = torch.empty_like(v)
1097
+ _flash_attn_backward(do,
1098
+ q,
1099
+ k,
1100
+ v,
1101
+ o,
1102
+ lse,
1103
+ dq,
1104
+ dk,
1105
+ dv,
1106
+ bias=bias,
1107
+ causal=ctx.causal,
1108
+ softmax_scale=ctx.softmax_scale)
1109
+ return dq, dk, dv, None, None, None
1110
+
1111
+
1112
+ flash_attn_func = _FlashAttnFunc.apply