blksprs 1.11__py3-none-any.whl → 2.0rc1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- blksprs/__init__.py +2 -5
- blksprs/layouting/distribution_layout.py +32 -25
- blksprs/layouting/sparsity_layout.py +65 -52
- blksprs/ops/conversion.py +421 -399
- blksprs/ops/distribution.py +404 -366
- blksprs/ops/flow.py +125 -106
- blksprs/ops/matmul.py +220 -204
- blksprs/ops/misc/broadcast_ops.py +53 -35
- blksprs/ops/misc/row_wise.py +151 -91
- blksprs/ops/partitioning.py +136 -132
- blksprs/ops/repeat.py +115 -120
- blksprs/ops/softmax.py +274 -246
- blksprs/ops/transpose.py +52 -51
- blksprs/utils/benchmarking.py +3 -3
- blksprs/utils/tools.py +31 -4
- blksprs/utils/validation.py +0 -14
- {blksprs-1.11.dist-info → blksprs-2.0rc1.dist-info}/METADATA +42 -36
- blksprs-2.0rc1.dist-info/RECORD +22 -0
- {blksprs-1.11.dist-info → blksprs-2.0rc1.dist-info}/WHEEL +1 -1
- blksprs/utils/layout_utils.py +0 -17
- blksprs-1.11.dist-info/RECORD +0 -23
- {blksprs-1.11.dist-info → blksprs-2.0rc1.dist-info}/top_level.txt +0 -0
blksprs/ops/softmax.py
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import torch
|
|
2
2
|
import triton
|
|
3
3
|
from torch import Tensor
|
|
4
|
+
from torch._library import triton_op
|
|
5
|
+
from torch._library.triton import wrap_triton
|
|
4
6
|
from triton import language as tl
|
|
5
7
|
|
|
6
8
|
from blksprs.ops.misc.row_wise import row_wise_sum, row_wise_max, row_wise_sub
|
|
7
9
|
from blksprs.utils.blksprs_tensor import BlksprsTensor
|
|
8
|
-
from blksprs.utils.tools import
|
|
10
|
+
from blksprs.utils.tools import stride, get_autotune_configs
|
|
9
11
|
from blksprs.utils.validation import validate_contiguous, validate_dimensions, validate_device, \
|
|
10
|
-
validate_sparsity, validate_sparsity_block_size
|
|
12
|
+
validate_sparsity, validate_sparsity_block_size
|
|
11
13
|
|
|
12
14
|
|
|
13
|
-
def softmax(x: BlksprsTensor, sparsity_layout: Tensor, sparsity_block_size: int,
|
|
14
|
-
triton_block_size: int = None, lut: dict = None) -> BlksprsTensor:
|
|
15
|
+
def softmax(x: BlksprsTensor, sparsity_layout: Tensor, sparsity_block_size: int, lut: dict = None) -> BlksprsTensor:
|
|
15
16
|
"""Computes the softmax of a block-sparse tensor in compressed form.
|
|
16
17
|
|
|
17
18
|
Note:
|
|
@@ -21,7 +22,6 @@ def softmax(x: BlksprsTensor, sparsity_layout: Tensor, sparsity_block_size: int,
|
|
|
21
22
|
x (BlksprsTensor): A block-sparse tensor in compressed form.
|
|
22
23
|
sparsity_layout (Tensor): The sparsity layout of the block-sparse tensor.
|
|
23
24
|
sparsity_block_size (int): The size of the sparsity blocks.
|
|
24
|
-
triton_block_size (int): The block size to use for the triton kernel (default ``None``).
|
|
25
25
|
lut (dict, optional): A dictionary containing the look-up tables for the operation (default ``None``).
|
|
26
26
|
|
|
27
27
|
Returns:
|
|
@@ -35,244 +35,272 @@ def softmax(x: BlksprsTensor, sparsity_layout: Tensor, sparsity_block_size: int,
|
|
|
35
35
|
validate_device(x)
|
|
36
36
|
validate_sparsity(sparsity_block_size, (x, sparsity_layout))
|
|
37
37
|
validate_sparsity_block_size(sparsity_block_size, x)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
#
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
38
|
+
|
|
39
|
+
lut = softmax_build_lut(lut, sparsity_layout)
|
|
40
|
+
|
|
41
|
+
return BlksprsTensor(softmax_forward(x, sparsity_layout,
|
|
42
|
+
lut["sparsity_lut"],
|
|
43
|
+
lut["sparsity_reverse_lut_rws"],
|
|
44
|
+
sparsity_block_size))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@triton_op("blksprs::softmax", mutates_args={})
|
|
48
|
+
def softmax_forward(x: Tensor, sparsity_layout: Tensor,
|
|
49
|
+
sparsity_lut: Tensor,
|
|
50
|
+
sparsity_reverse_lut_rws: Tensor,
|
|
51
|
+
sparsity_block_size: int) -> Tensor:
|
|
52
|
+
output = torch.empty_like(x)
|
|
53
|
+
|
|
54
|
+
x_b, x_r, x_c = x.size()
|
|
55
|
+
x_b_s, x_r_s, x_c_s = stride(x)
|
|
56
|
+
s_lut_r, s_lut_c = sparsity_lut.size()
|
|
57
|
+
s_lut_r_s, s_lut_c_s = stride(sparsity_lut)
|
|
58
|
+
o_b, o_r, o_c = output.size()
|
|
59
|
+
|
|
60
|
+
x_row_wise_max, sparsity_layout_rwm = row_wise_max(x, sparsity_layout, sparsity_block_size,
|
|
61
|
+
flag_slice_only=True)
|
|
62
|
+
x_scaled = row_wise_sub(x, sparsity_layout, x_row_wise_max, sparsity_block_size)
|
|
63
|
+
x_exp = torch.exp(x_scaled)
|
|
64
|
+
x_exp_row_wise_sum, sparsity_layout_rws = row_wise_sum(x_exp, sparsity_layout, sparsity_block_size,
|
|
65
|
+
flag_slice_only=True)
|
|
66
|
+
|
|
67
|
+
s_b, s_r, s_c = x_exp_row_wise_sum.shape
|
|
68
|
+
s_b_s, s_r_s, s_c_s = stride(x_exp_row_wise_sum)
|
|
69
|
+
s_l_s_b, s_l_s_r, s_l_s_c = sparsity_layout_rws.shape
|
|
70
|
+
s_l_s_b_s, s_l_s_r_s, s_l_s_c_s = stride(sparsity_layout_rws)
|
|
71
|
+
|
|
72
|
+
triton_grid = lambda meta: [o_b,
|
|
73
|
+
triton.cdiv(o_r, meta["TRITON_BLOCK_SIZE"]),
|
|
74
|
+
triton.cdiv(o_c, meta["TRITON_BLOCK_SIZE"])]
|
|
75
|
+
|
|
76
|
+
(wrap_triton(softmax_kernel)[triton_grid]
|
|
77
|
+
(x_exp,
|
|
78
|
+
x_b, x_b_s, x_r_s, x_c_s,
|
|
79
|
+
sparsity_lut, s_lut_r, s_lut_r_s, s_lut_c_s,
|
|
80
|
+
x_exp_row_wise_sum, s_b, s_b_s, s_r_s, s_c_s,
|
|
81
|
+
s_l_s_b, s_l_s_b_s, s_l_s_r_s,
|
|
82
|
+
sparsity_reverse_lut_rws,
|
|
83
|
+
output,
|
|
84
|
+
sparsity_block_size))
|
|
85
|
+
|
|
86
|
+
return output
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def softmax_backward(ctx, grad_output):
|
|
90
|
+
o, sparsity_layout, sparsity_lut = ctx.saved_tensors
|
|
91
|
+
sparsity_block_size = ctx.sparsity_block_size
|
|
92
|
+
|
|
93
|
+
s, sparsity_layout_s = row_wise_sum(grad_output * o, sparsity_layout, sparsity_block_size, flag_slice_only=True)
|
|
94
|
+
|
|
95
|
+
sparsity_layout_s_flat = sparsity_layout_s.reshape(-1)
|
|
96
|
+
sparsity_reverse_lut_s = ((torch.cumsum(sparsity_layout_s_flat, dim=-1) - 1) *
|
|
97
|
+
(sparsity_layout_s_flat == 1) -
|
|
98
|
+
(1 * (sparsity_layout_s_flat == 0)))
|
|
99
|
+
|
|
100
|
+
o_b, o_r, o_c = o.size()
|
|
101
|
+
o_b_s, o_r_s, o_c_s = stride(o)
|
|
102
|
+
s_lut_r, s_lut_c = sparsity_lut.size()
|
|
103
|
+
s_lut_r_s, s_lut_c_s = stride(sparsity_lut)
|
|
104
|
+
s_b, s_r, s_c = s.size()
|
|
105
|
+
s_b_s, s_r_s, s_c_s = stride(s)
|
|
106
|
+
s_l_s_b, s_l_s_r, s_l_s_c = sparsity_layout_s.size()
|
|
107
|
+
s_l_s_b_s, s_l_s_r_s, s_l_s_c_s = stride(sparsity_layout_s)
|
|
108
|
+
|
|
109
|
+
grad_x = torch.empty_like(o, dtype=torch.float)
|
|
110
|
+
|
|
111
|
+
triton_grid = lambda meta: [o_b,
|
|
112
|
+
triton.cdiv(o_r, meta["TRITON_BLOCK_SIZE"]),
|
|
113
|
+
triton.cdiv(o_c, meta["TRITON_BLOCK_SIZE"])]
|
|
114
|
+
|
|
115
|
+
(wrap_triton(softmax_kernel_grad)[triton_grid]
|
|
116
|
+
(grad_output,
|
|
117
|
+
o_b, o_b_s, o_r_s, o_c_s,
|
|
118
|
+
o,
|
|
119
|
+
o_b, o_b_s, o_r_s, o_c_s,
|
|
120
|
+
sparsity_lut, s_lut_r, s_lut_r_s, s_lut_c_s,
|
|
121
|
+
s,
|
|
122
|
+
s_b, s_b_s, s_r_s, s_c_s,
|
|
123
|
+
s_l_s_b, s_l_s_b_s, s_l_s_r_s,
|
|
124
|
+
sparsity_reverse_lut_s,
|
|
125
|
+
grad_x,
|
|
126
|
+
o_b, o_b_s, o_r_s, o_c_s,
|
|
127
|
+
sparsity_block_size))
|
|
128
|
+
|
|
129
|
+
return grad_x, None, None, None, None, None
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@triton.autotune(
|
|
133
|
+
configs=get_autotune_configs(),
|
|
134
|
+
key=[]
|
|
135
|
+
)
|
|
136
|
+
@triton.jit
|
|
137
|
+
def softmax_kernel(x,
|
|
138
|
+
x_b, x_b_s, x_r_s, x_c_s,
|
|
139
|
+
s_lut, s_lut_r, s_lut_r_s, s_lut_c_s,
|
|
140
|
+
s, s_b, s_b_s, s_r_s, s_c_s,
|
|
141
|
+
s_l_s_b, s_l_s_b_s, s_l_s_r_s,
|
|
142
|
+
r_lut_s,
|
|
143
|
+
o,
|
|
144
|
+
sparsity_block_size,
|
|
145
|
+
TRITON_BLOCK_SIZE: tl.constexpr) -> None:
|
|
146
|
+
# Get triton block indices
|
|
147
|
+
pid_blk = tl.program_id(axis=0)
|
|
148
|
+
pid_row = tl.program_id(axis=1)
|
|
149
|
+
pid_col = tl.program_id(axis=2)
|
|
150
|
+
|
|
151
|
+
# Get valid triton block size
|
|
152
|
+
val_tbs = min(sparsity_block_size, TRITON_BLOCK_SIZE)
|
|
153
|
+
|
|
154
|
+
# Get position of current sparsity block consisting of its batch and row index
|
|
155
|
+
spa_bat_idx = (pid_blk * s_lut_r_s + 0 * s_lut_c_s)
|
|
156
|
+
spa_bat_msk = (spa_bat_idx >= 0 and spa_bat_idx < s_lut_r * s_lut_r_s)
|
|
157
|
+
spa_bat = tl.load(s_lut + spa_bat_idx, mask=spa_bat_msk)
|
|
158
|
+
|
|
159
|
+
spa_row_idx = (pid_blk * s_lut_r_s + 1 * s_lut_c_s)
|
|
160
|
+
spa_row_msk = (spa_row_idx >= 0 and spa_row_idx < s_lut_r * s_lut_r_s)
|
|
161
|
+
spa_row = tl.load(s_lut + spa_row_idx, mask=spa_row_msk)
|
|
162
|
+
|
|
163
|
+
# Get reverse sparsity indices for s
|
|
164
|
+
rev_idx_spa_s_idx = (spa_bat * s_l_s_b_s +
|
|
165
|
+
spa_row * s_l_s_r_s)
|
|
166
|
+
rev_idx_spa_s_msk = (rev_idx_spa_s_idx >= 0 and rev_idx_spa_s_idx < s_l_s_b * s_l_s_b_s)
|
|
167
|
+
rev_idx_spa_s = tl.load(r_lut_s + rev_idx_spa_s_idx, mask=rev_idx_spa_s_msk).to(tl.int32)
|
|
168
|
+
|
|
169
|
+
if rev_idx_spa_s >= 0:
|
|
170
|
+
# Load x block
|
|
171
|
+
blk_x_idx = ((pid_blk * x_b_s) +
|
|
172
|
+
((pid_row * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * x_r_s)[:, None] +
|
|
173
|
+
((pid_col * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * x_c_s)[None, :])
|
|
174
|
+
blk_x_msk = ((blk_x_idx >= 0 and
|
|
175
|
+
blk_x_idx < x_b * x_b_s) and
|
|
176
|
+
(tl.arange(0, TRITON_BLOCK_SIZE)[:, None] < val_tbs and
|
|
177
|
+
tl.arange(0, TRITON_BLOCK_SIZE)[None, :] < val_tbs))
|
|
178
|
+
blk_x = tl.load(x + blk_x_idx, mask=blk_x_msk)
|
|
179
|
+
|
|
180
|
+
# Load sum block
|
|
181
|
+
blk_s_idx = (rev_idx_spa_s * s_b_s +
|
|
182
|
+
((pid_row * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * s_r_s)[:, None] +
|
|
183
|
+
(tl.arange(0, 1) * s_c_s)[None, :])
|
|
184
|
+
blk_s_msk = ((blk_s_idx >= 0 and
|
|
185
|
+
blk_s_idx < s_b * s_b_s) and
|
|
186
|
+
(tl.arange(0, TRITON_BLOCK_SIZE)[:, None] < val_tbs and
|
|
187
|
+
tl.arange(0, 1)[None, :] < val_tbs))
|
|
188
|
+
blk_s = tl.load(s + blk_s_idx, mask=blk_s_msk)
|
|
189
|
+
|
|
190
|
+
# Compute softmax
|
|
191
|
+
buf = tl.div_rn(blk_x, blk_s)
|
|
192
|
+
|
|
193
|
+
# Store output
|
|
194
|
+
tl.store(o + blk_x_idx, buf, mask=blk_x_msk)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
@triton.autotune(
|
|
198
|
+
configs=get_autotune_configs(),
|
|
199
|
+
key=[]
|
|
200
|
+
)
|
|
201
|
+
@triton.jit
|
|
202
|
+
def softmax_kernel_grad(g,
|
|
203
|
+
g_b, g_b_s, g_r_s, g_c_s,
|
|
204
|
+
x,
|
|
205
|
+
x_b, x_b_s, x_r_s, x_c_s,
|
|
206
|
+
s_lut, s_lut_r, s_lut_r_s, s_lut_c_s,
|
|
207
|
+
s,
|
|
208
|
+
s_b, s_b_s, s_r_s, s_c_s,
|
|
209
|
+
s_l_s_b, s_l_s_b_s, s_l_s_r_s,
|
|
210
|
+
r_lut_s,
|
|
211
|
+
o,
|
|
212
|
+
o_b, o_b_s, o_r_s, o_c_s,
|
|
213
|
+
sparsity_block_size,
|
|
214
|
+
TRITON_BLOCK_SIZE: tl.constexpr) -> None:
|
|
215
|
+
# Get triton block indices
|
|
216
|
+
pid_blk = tl.program_id(axis=0)
|
|
217
|
+
pid_row = tl.program_id(axis=1)
|
|
218
|
+
pid_col = tl.program_id(axis=2)
|
|
219
|
+
|
|
220
|
+
# Get valid triton block size
|
|
221
|
+
val_tbs = min(sparsity_block_size, TRITON_BLOCK_SIZE)
|
|
222
|
+
|
|
223
|
+
# Get position of current sparsity block consisting of its batch and row index
|
|
224
|
+
spa_bat_idx = (pid_blk * s_lut_r_s + 0 * s_lut_c_s)
|
|
225
|
+
spa_bat_msk = (spa_bat_idx >= 0 and spa_bat_idx < s_lut_r * s_lut_r_s)
|
|
226
|
+
spa_bat = tl.load(s_lut + spa_bat_idx, mask=spa_bat_msk)
|
|
227
|
+
|
|
228
|
+
spa_row_idx = (pid_blk * s_lut_r_s + 1 * s_lut_c_s)
|
|
229
|
+
spa_row_msk = (spa_row_idx >= 0 and spa_row_idx < s_lut_r * s_lut_r_s)
|
|
230
|
+
spa_row = tl.load(s_lut + spa_row_idx, mask=spa_row_msk)
|
|
231
|
+
|
|
232
|
+
rev_idx_spa_s_idx = (spa_bat * s_l_s_b_s +
|
|
233
|
+
spa_row * s_l_s_r_s)
|
|
234
|
+
rev_idx_spa_s_msk = (rev_idx_spa_s_idx >= 0 and rev_idx_spa_s_idx < s_l_s_b * s_l_s_b_s)
|
|
235
|
+
rev_idx_spa_s = tl.load(r_lut_s + rev_idx_spa_s_idx, mask=rev_idx_spa_s_msk).to(tl.int32)
|
|
236
|
+
|
|
237
|
+
if rev_idx_spa_s >= 0:
|
|
238
|
+
blk_s_idx = (rev_idx_spa_s * s_b_s +
|
|
239
|
+
((pid_row * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * s_r_s)[:, None] +
|
|
240
|
+
(tl.arange(0, 1) * s_c_s)[None, :])
|
|
241
|
+
blk_s_msk = ((blk_s_idx >= 0 and
|
|
242
|
+
blk_s_idx < s_b * s_b_s) and
|
|
243
|
+
(tl.arange(0, TRITON_BLOCK_SIZE)[:, None] < val_tbs and
|
|
244
|
+
tl.arange(0, 1)[None, :] < val_tbs))
|
|
245
|
+
blk_s = tl.load(s + blk_s_idx, mask=blk_s_msk)
|
|
246
|
+
|
|
247
|
+
blk_g_idx = ((pid_blk * g_b_s) +
|
|
248
|
+
((pid_row * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * g_r_s)[:, None] +
|
|
249
|
+
((pid_col * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * g_c_s)[None, :])
|
|
250
|
+
blk_g_msk = ((blk_g_idx >= 0 and
|
|
251
|
+
blk_g_idx < g_b * g_b_s) and
|
|
252
|
+
(tl.arange(0, TRITON_BLOCK_SIZE)[:, None] < val_tbs and
|
|
253
|
+
tl.arange(0, TRITON_BLOCK_SIZE)[None, :] < val_tbs))
|
|
254
|
+
blk_g = tl.load(g + blk_g_idx, mask=blk_g_msk)
|
|
255
|
+
|
|
256
|
+
blk_x_idx = ((pid_blk * x_b_s) +
|
|
257
|
+
((pid_row * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * x_r_s)[:, None] +
|
|
258
|
+
((pid_col * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * x_c_s)[None, :])
|
|
259
|
+
blk_x_msk = ((blk_x_idx >= 0 and
|
|
260
|
+
blk_x_idx < x_b * x_b_s) and
|
|
261
|
+
(tl.arange(0, TRITON_BLOCK_SIZE)[:, None] < val_tbs and
|
|
262
|
+
tl.arange(0, TRITON_BLOCK_SIZE)[None, :] < val_tbs))
|
|
263
|
+
blk_x = tl.load(x + blk_x_idx, mask=blk_x_msk)
|
|
264
|
+
|
|
265
|
+
buf = blk_x * (blk_g - blk_s)
|
|
266
|
+
|
|
267
|
+
blk_o_idx = ((pid_blk * o_b_s) +
|
|
268
|
+
((pid_row * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * o_r_s)[:, None] +
|
|
269
|
+
((pid_col * val_tbs + tl.arange(0, TRITON_BLOCK_SIZE)) * o_c_s)[None, :])
|
|
270
|
+
blk_o_msk = ((blk_o_idx >= 0 and
|
|
271
|
+
blk_o_idx < o_b * o_b_s) and
|
|
272
|
+
(tl.arange(0, TRITON_BLOCK_SIZE)[:, None] < val_tbs and
|
|
273
|
+
tl.arange(0, TRITON_BLOCK_SIZE)[None, :] < val_tbs))
|
|
274
|
+
tl.store(o + blk_o_idx, buf, mask=blk_o_msk)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def softmax_build_lut(lut: dict, sparsity_layout: Tensor):
|
|
278
|
+
if lut is None:
|
|
279
|
+
lut = dict()
|
|
280
|
+
|
|
281
|
+
if "sparsity_lut" not in lut:
|
|
282
|
+
sparsity_lut = torch.nonzero(sparsity_layout).contiguous()
|
|
283
|
+
lut["sparsity_lut"] = sparsity_lut
|
|
284
|
+
|
|
285
|
+
if "sparsity_reverse_lut_rws" not in lut:
|
|
286
|
+
sparsity_layout_rws, _ = torch.max(sparsity_layout, dim=-1, keepdim=True)
|
|
287
|
+
sparsity_layout_rws_flat = sparsity_layout_rws.reshape(-1)
|
|
288
|
+
sparsity_reverse_lut_rws = ((torch.cumsum(sparsity_layout_rws_flat, dim=-1) - 1) *
|
|
289
|
+
(sparsity_layout_rws_flat == 1) -
|
|
290
|
+
(1 * (sparsity_layout_rws_flat == 0)))
|
|
291
|
+
lut["sparsity_reverse_lut_rws"] = sparsity_reverse_lut_rws
|
|
292
|
+
|
|
293
|
+
validate_contiguous(sparsity_layout, lut["sparsity_lut"], lut["sparsity_reverse_lut_rws"])
|
|
294
|
+
|
|
295
|
+
return lut
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
# noinspection PyUnusedLocal
|
|
299
|
+
def softmax_setup_context(ctx, inputs, output):
|
|
300
|
+
(_, sparsity_layout, sparsity_lut, _, sparsity_block_size) = inputs
|
|
301
|
+
|
|
302
|
+
ctx.save_for_backward(output, sparsity_layout, sparsity_lut)
|
|
303
|
+
ctx.sparsity_block_size = sparsity_block_size
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
softmax_forward.register_autograd(softmax_backward, setup_context=softmax_setup_context)
|