-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathsoftmax.py
More file actions
310 lines (245 loc) · 8.26 KB
/
Copy pathsoftmax.py
File metadata and controls
310 lines (245 loc) · 8.26 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: MIT
"""
CUDA Tile C++ Softmax
Computes softmax along the last dimension using CUDA C++ tile kernels.
"""
import logging
from pathlib import Path
from typing import Optional
import numpy as np
import torch
from tilegym.backend import register_impl
from tilegym.ops.tilecpp.utils._cuda_utils import TileCppKernel
from tilegym.ops.tilecpp.utils._dump_types import dump_kernel_types
logger = logging.getLogger(__name__)
# =============================================================================
# Constants
# =============================================================================
# Threshold for using online algorithm (large column counts)
COL_THRESHOLD = 16384 * 2
# Block size for online algorithm
ONLINE_BLOCK_SIZE = 8192
# =============================================================================
# Kernel Definitions
# =============================================================================
_softmax_kernel = TileCppKernel(
source_path=Path(__file__).parent / "softmax.cuh",
kernel_name="softmax_kernel",
)
_online_softmax_kernel = TileCppKernel(
source_path=Path(__file__).parent / "softmax.cuh",
kernel_name="online_softmax_kernel",
)
_softmax_kernel_backward = TileCppKernel(
source_path=Path(__file__).parent / "softmax.cuh",
kernel_name="softmax_kernel_backward",
)
_online_softmax_kernel_backward = TileCppKernel(
source_path=Path(__file__).parent / "softmax.cuh",
kernel_name="online_softmax_kernel_backward",
)
# =============================================================================
# Helper Functions
# =============================================================================
def _next_power_of_2(n):
"""Returns the next power of 2 for n."""
if n == 0:
return 1
n -= 1
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
return n + 1
def _get_num_sm():
"""Get number of SMs on current device."""
props = torch.cuda.get_device_properties(torch.cuda.current_device())
return props.multi_processor_count
# =============================================================================
# Kernel Launch Functions
# =============================================================================
def _launch_softmax_forward(
input_tensor: torch.Tensor,
output_tensor: torch.Tensor,
n_rows: int,
n_cols: int,
block_size: int,
):
"""Launch the softmax forward kernel."""
dump_kernel_types("softmax_kernel", input_tensor, output_tensor)
dtype = input_tensor.dtype
kernel, _, _ = _softmax_kernel.get_kernel(
dtype=dtype,
template_params=[block_size],
signature="{T}*, const {T}*, int, int, int, int, int",
)
num_sm = _get_num_sm()
occupancy = 4
num_programs = min(num_sm * occupancy, n_rows)
grid = (num_programs,)
_softmax_kernel.launch(
grid=grid,
kernel=kernel,
args=[
np.uint64(output_tensor.data_ptr()),
np.uint64(input_tensor.data_ptr()),
np.int32(input_tensor.stride(0)),
np.int32(output_tensor.stride(0)),
np.int32(n_rows),
np.int32(n_cols),
np.int32(num_programs),
],
)
def _launch_online_softmax_forward(
input_tensor: torch.Tensor,
output_tensor: torch.Tensor,
n_rows: int,
n_cols: int,
block_size: int,
):
"""Launch the online softmax forward kernel."""
dump_kernel_types("online_softmax_kernel", input_tensor, output_tensor)
dtype = input_tensor.dtype
kernel, _, _ = _online_softmax_kernel.get_kernel(
dtype=dtype,
template_params=[block_size],
signature="{T}*, const {T}*, int, int, int",
)
grid = (n_rows,)
_online_softmax_kernel.launch(
grid=grid,
kernel=kernel,
args=[
np.uint64(output_tensor.data_ptr()),
np.uint64(input_tensor.data_ptr()),
np.int32(input_tensor.stride(0)),
np.int32(output_tensor.stride(0)),
np.int32(n_cols),
],
)
def _launch_softmax_backward(
dx_tensor: torch.Tensor,
y_tensor: torch.Tensor,
dy_tensor: torch.Tensor,
n_rows: int,
n_cols: int,
block_size: int,
):
"""Launch the softmax backward kernel."""
dump_kernel_types("softmax_kernel_backward", y_tensor, dy_tensor, dx_tensor)
dtype = y_tensor.dtype
kernel, _, _ = _softmax_kernel_backward.get_kernel(
dtype=dtype,
template_params=[block_size],
signature="{T}*, const {T}*, const {T}*, int, int, int, int",
)
grid = (n_rows,)
_softmax_kernel_backward.launch(
grid=grid,
kernel=kernel,
args=[
np.uint64(dx_tensor.data_ptr()),
np.uint64(y_tensor.data_ptr()),
np.uint64(dy_tensor.data_ptr()),
np.int32(dy_tensor.stride(0)),
np.int32(y_tensor.stride(0)),
np.int32(dx_tensor.stride(0)),
np.int32(n_cols),
],
)
def _launch_online_softmax_backward(
dx_tensor: torch.Tensor,
y_tensor: torch.Tensor,
dy_tensor: torch.Tensor,
n_rows: int,
n_cols: int,
block_size: int,
):
"""Launch the online softmax backward kernel."""
dump_kernel_types("online_softmax_kernel_backward", y_tensor, dy_tensor, dx_tensor)
dtype = y_tensor.dtype
kernel, _, _ = _online_softmax_kernel_backward.get_kernel(
dtype=dtype,
template_params=[block_size],
signature="{T}*, const {T}*, const {T}*, int, int, int, int",
)
grid = (n_rows,)
_online_softmax_kernel_backward.launch(
grid=grid,
kernel=kernel,
args=[
np.uint64(dx_tensor.data_ptr()),
np.uint64(y_tensor.data_ptr()),
np.uint64(dy_tensor.data_ptr()),
np.int32(dy_tensor.stride(0)),
np.int32(y_tensor.stride(0)),
np.int32(dx_tensor.stride(0)),
np.int32(n_cols),
],
)
# =============================================================================
# Autograd Functions
# =============================================================================
class Softmax(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
n_rows, n_cols = x.shape
BLOCK_SIZE = _next_power_of_2(n_cols)
y = torch.empty_like(x)
_launch_softmax_forward(x, y, n_rows, n_cols, BLOCK_SIZE)
ctx.save_for_backward(y)
return y
@staticmethod
def backward(ctx, dy):
(y,) = ctx.saved_tensors
n_rows, n_cols = dy.shape
BLOCK_SIZE = _next_power_of_2(n_cols)
dx = torch.empty_like(dy)
_launch_softmax_backward(dx, y, dy, n_rows, n_cols, BLOCK_SIZE)
return dx
class OnlineSoftmax(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
n_rows, n_cols = x.shape
BLOCK_SIZE = ONLINE_BLOCK_SIZE
y = torch.empty_like(x)
_launch_online_softmax_forward(x, y, n_rows, n_cols, BLOCK_SIZE)
ctx.save_for_backward(y)
return y
@staticmethod
def backward(ctx, dy):
(y,) = ctx.saved_tensors
n_rows, n_cols = dy.shape
BLOCK_SIZE = ONLINE_BLOCK_SIZE
dx = torch.empty_like(dy)
_launch_online_softmax_backward(dx, y, dy, n_rows, n_cols, BLOCK_SIZE)
return dx
# =============================================================================
# Public Interface
# =============================================================================
@register_impl("softmax", backend="tilecpp")
def softmax(
x: torch.Tensor,
use_tma: bool = False,
use_online: bool = None,
**kwargs,
):
r"""
Performs Softmax on a Tensor of shape (M, N) along the N axis.
Args:
x: Tensor of shape (M, N)
use_tma: Whether to use TMA.
use_online: Whether to use online softmax implementation.
If None, automatically chooses based on tensor size.
"""
n_rows, n_cols = x.shape
# Automatically choose between regular and online algorithms based on tensor size
if use_online is None:
use_online = n_cols >= COL_THRESHOLD
if use_online:
return OnlineSoftmax.apply(x)
else:
return Softmax.apply(x)