-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathfused_indices_converter.py
270 lines (234 loc) · 9.9 KB
/
fused_indices_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
import math
import torch
import triton
import triton.language as tl
from megatron.core.utils import experimental_fn
# Assign a block to a row([1,topk]), generate a local routing map([1,num_of_local_experts])
@triton.jit
def _indices_to_multihot_kernel(
indices_ptr,
probs_in_indices_ptr,
multihot_indices_ptr, # bool
probs_in_multihot_ptr,
position_map_ptr,
num_of_local_experts: tl.constexpr,
num_of_local_experts_next_power_of_2: tl.constexpr,
topk: tl.constexpr,
topk_next_power_of_2: tl.constexpr,
BLOCK_SIZE: tl.constexpr,
):
'''
Triton kernel for converting indices to multihot representation.
Input:
indices: [num_of_tokens, topk]
probs_in_indices: [num_of_tokens, topk]
Output:
multihot_indices: [num_of_tokens, num_of_local_experts]
probs_in_multihot: [num_of_tokens, num_of_local_experts]
Assume that topk = 2 , num_of_local_experts = 4, num_of_tokens = 2,
then the kernel can process the following conversion:
Input Example:
indices = [
[0, 1],
[1, 2]
]
probs_in_indices = [
[0.1, 0.2],
[0.3, 0.4]
]
Output Example:
multihot_indices = [
[1, 1, -1, -1],
[-1, 1, 1, -1]
]
probs_in_multihot = [
[0.1, 0.2, 0.0, 0.0],
[0.0, 0.3, 0.4, 0.0]
]
'''
# Prepare the [0, topk) row
topk_row = tl.arange(0, topk_next_power_of_2)
topk_row = tl.where(topk_row < topk, topk_row, -1)
topk_row_mask = topk_row != -1
# Prepare the [0, num_of_local_experts) row
num_exp_row = tl.arange(0, num_of_local_experts_next_power_of_2)
num_exp_row = tl.where(num_exp_row < num_of_local_experts, num_exp_row, -1)
num_exp_row_mask = num_exp_row != -1
# Load a [1, topk] row from the indices buffer
row_idx = tl.program_id(0)
indices_row = tl.load(indices_ptr + row_idx * topk + topk_row, mask=topk_row_mask)
indices_row = tl.where(topk_row_mask, indices_row, -1)
probs_row = tl.load(probs_in_indices_ptr + row_idx * topk + topk_row, mask=topk_row_mask)
# Get the position of the each index in the indices_row, which is saved for backwards
position_row = tl.where(indices_row != -1, topk_row, -1)
# Mask of the valid indices
mask = (indices_row != -1) & (indices_row < num_of_local_experts)
row_idx_offset = row_idx * num_of_local_experts
# Store to initialize
tl.store(multihot_indices_ptr + row_idx_offset + num_exp_row, 0, mask=num_exp_row_mask)
tl.store(probs_in_multihot_ptr + row_idx_offset + num_exp_row, 0, mask=num_exp_row_mask)
tl.store(position_map_ptr + row_idx_offset + num_exp_row, -1, mask=num_exp_row_mask)
# Use barrier to make sure the initialization is done
tl.debug_barrier()
# Store the indices and probs_in_indices
tl.store(multihot_indices_ptr + row_idx_offset + indices_row, 1, mask)
tl.store(probs_in_multihot_ptr + row_idx_offset + indices_row, probs_row, mask)
# Store the position of the position_row for backwards
tl.store(position_map_ptr + row_idx_offset + indices_row, position_row, mask)
# Assign a block to a row([1,topk]), generate a probs_indices([1,topk])
@triton.jit
def _multihot_to_indices_kernel(
probs_in_multihot_ptr,
position_map_ptr,
probs_indices_ptr,
num_of_local_experts: tl.constexpr,
num_of_local_experts_next_power_of_2: tl.constexpr,
topk: tl.constexpr,
topk_next_power_of_2: tl.constexpr,
BLOCK_SIZE: tl.constexpr,
):
'''
Triton kernel for converting multihot representation to indices.
Input:
probs_in_multihot: [num_of_tokens, num_of_local_experts]
position_map: [num_of_tokens, num_of_local_experts]
Output:
probs_indices: [num_of_tokens, topk]
Assume that topk = 2 , num_of_local_experts = 4, num_of_tokens = 2,
then the kernel can process the following conversion:
Input Example:
probs_in_multihot = [
[0.7, 0.8, 0.0, 0.0],
[0.0, 0.1, 0.9, 0.0]
]
position_map = [
[1, 1, -1, -1],
[-1, 1, 1, -1]
]
Output Example:
probs_indices = [
[0.7, 0.8],
[0.1, 0.9]
]
'''
# Prepare the [0, topk) row
topk_row = tl.arange(0, topk_next_power_of_2)
topk_row = tl.where(topk_row < topk, topk_row, -1)
topk_row_mask = topk_row != -1
# Prepare the [0, num_of_local_experts) row
num_exp_row = tl.arange(0, num_of_local_experts_next_power_of_2)
num_exp_row = tl.where(num_exp_row < num_of_local_experts, num_exp_row, -1)
num_exp_row_mask = num_exp_row != -1
# Load a [1, num_of_local_experts] row from the local routing map
row_idx = tl.program_id(0)
ptr_offset = row_idx * num_of_local_experts + num_exp_row
probs_in_multihot_row = tl.load(probs_in_multihot_ptr + ptr_offset, mask=num_exp_row_mask)
# Get the original position of the valid value in the the indices
position_map_row = tl.load(position_map_ptr + ptr_offset, mask=num_exp_row_mask)
position_map_row = tl.where(num_exp_row_mask, position_map_row, -1)
mask = position_map_row != -1
# Store to initialize
tl.store(probs_indices_ptr + row_idx * topk + topk_row, 0, mask=topk_row_mask)
# Use barrier to make sure the initialization is done
tl.debug_barrier()
# Restore the indices and probs_indices
tl.store(probs_indices_ptr + row_idx * topk + position_map_row, probs_in_multihot_row, mask)
class IndicesToMultihot(torch.autograd.Function):
"""Convert moe topk indices to multihot representation.
This class implements a custom forward and backward propagation
operation for efficiently converting indices to multihot
representation.
It is an experimental feature and may change in future versions.
"""
@staticmethod
def forward(ctx, indices, probs_indices, num_of_local_experts):
'''Forward function for IndicesToMultihot
Convert indices to multihot representation.
Args:
indices: [num_of_tokens, topk]
probs_indices: [num_of_tokens, topk]
num_of_local_experts: int
Returns:
multihot_indices: [num_of_tokens, num_of_local_experts]
probs_in_multihot: [num_of_tokens, num_of_local_experts]
'''
num_of_tokens = indices.shape[0]
assert (
indices.shape == probs_indices.shape
), "indices and probs_indices must have the same shape"
topk = indices.shape[1]
multihot_indices = torch.empty(
(num_of_tokens, num_of_local_experts), dtype=torch.bool, device="cuda"
)
probs_in_multihot = torch.empty(
(num_of_tokens, num_of_local_experts), dtype=probs_indices.dtype, device="cuda"
)
position_map = torch.empty(
(num_of_tokens, num_of_local_experts), dtype=torch.int32, device="cuda"
)
# Compute the next power of 2 for the topk and num_of_local_experts
topk_next_power_of_2 = 2 ** int(math.ceil(math.log2(topk)))
num_of_local_experts_next_power_of_2 = 2 ** int(math.ceil(math.log2(num_of_local_experts)))
grid = (num_of_tokens,)
_indices_to_multihot_kernel[grid](
indices,
probs_indices,
multihot_indices,
probs_in_multihot,
position_map,
num_of_local_experts,
num_of_local_experts_next_power_of_2,
topk,
topk_next_power_of_2,
BLOCK_SIZE=32, # use only 1 warp per block
num_warps=1,
)
ctx.save_for_backward(position_map)
ctx.num_of_tokens = num_of_tokens
ctx.num_of_local_experts = num_of_local_experts
ctx.topk = topk
return multihot_indices, probs_in_multihot
@staticmethod
def backward(ctx, grad_multihot_indices, grad_probs_in_multihot):
'''Backward function for IndicesToMultihot
Convert multihot probs representation to indices.
indices is ignored in the backward function.
Args:
grad_multihot_indices: [num_of_tokens, num_of_local_experts]
grad_probs_in_multihot: [num_of_tokens, num_of_local_experts]
Returns:
grad_probs_indices: [num_of_tokens, topk]
'''
position_map = ctx.saved_tensors[0]
num_of_tokens = ctx.num_of_tokens
num_of_local_experts = ctx.num_of_local_experts
topk = ctx.topk
# Initialize the gradient of the indices and probs_indices
grad_probs_indices = torch.empty(
(num_of_tokens, topk), dtype=grad_probs_in_multihot.dtype, device="cuda"
)
# Compute the next power of 2 for the topk and num_of_local_experts
topk_next_power_of_2 = 2 ** int(math.ceil(math.log2(topk)))
num_of_local_experts_next_power_of_2 = 2 ** int(math.ceil(math.log2(num_of_local_experts)))
grid = (num_of_tokens,)
_multihot_to_indices_kernel[grid](
# if the grad_probs_in_multihot is all-one/all-zero,
# overlapping stride will cause error without contiguous()
grad_probs_in_multihot.contiguous(),
position_map,
grad_probs_indices,
num_of_local_experts,
num_of_local_experts_next_power_of_2,
topk,
topk_next_power_of_2,
BLOCK_SIZE=32, # use only 1 warp per block
num_warps=1,
)
return None, grad_probs_indices, None, None
@experimental_fn(introduced_with_version='0.11.0rc0')
def fused_indices_to_multihot(indices, probs_indices, num_of_local_experts):
"""Convert moe topk indices to multihot representation.
This function is an experimental feature and may change in future versions.
"""
return IndicesToMultihot.apply(indices, probs_indices, num_of_local_experts)