-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtest_softmax.py
More file actions
71 lines (58 loc) · 2.55 KB
/
test_softmax.py
File metadata and controls
71 lines (58 loc) · 2.55 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
# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import pytest
import torch
from math import ceil
from util import assert_close
from conftest import float_dtypes, dtype_id
from torch.testing import make_tensor
# example-begin imports
import cuda.tile as ct
# example-end imports
# example-begin softmax
@ct.kernel
def softmax(input, output, B: ct.Constant[int], N: ct.Constant[int]):
rows = ct.load(input, index=(ct.bid(0), 0), shape=(B, N))
numerator = ct.exp(rows - ct.max(rows, axis=1, keepdims=True))
denominator = ct.sum(numerator, axis=1, keepdims=True)
ct.store(output, index=(ct.bid(0), 0), tile=numerator / denominator)
# example-end softmax
@ct.kernel
def softmax_per_row(input, output,
num_rows: ct.Constant[int],
num_cols: ct.Constant[int]):
bidx = ct.bid(0)
num_blocks = ct.num_blocks(0)
for i in range(bidx, num_rows, num_blocks):
row = ct.load(input, index=(i, 0), shape=(1, num_cols))
numerator = ct.exp(row - ct.max(row, axis=1, keepdims=True))
denominator = ct.sum(numerator, axis=1, keepdims=True)
ct.store(output, index=(i, 0), tile=numerator / denominator)
def softmax_gold(input):
max_input = torch.max(input, dim=1, keepdim=True).values
exps = torch.exp(input - max_input)
sum_exps = torch.sum(exps, dim=1, keepdim=True)
return exps / sum_exps
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", float_dtypes, ids=dtype_id)
def test_softmax(shape, tile, dtype):
x = make_tensor(shape, dtype=dtype, device="cuda")
y = torch.zeros_like(x)
grid = (ceil(shape[0] / tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, softmax, (x, y, tile, shape[1]))
ref_result = softmax_gold(x)
atol, rtol = (1e-4, 1e-5) if dtype == torch.float32 else (1e-2, 1e-1)
assert_close(y, ref_result, atol=atol, rtol=rtol)
@pytest.mark.parametrize("shape", [(512, 128)])
@pytest.mark.parametrize("tile", [16])
@pytest.mark.parametrize("dtype", float_dtypes, ids=dtype_id)
def test_softmax_per_row(shape, tile, dtype):
x = make_tensor(shape, dtype=dtype, device="cuda")
y = torch.zeros_like(x)
grid = (ceil(shape[0] / tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, softmax_per_row, (x, y, shape[0], shape[1]))
ref_result = softmax_gold(x)
atol, rtol = (1e-4, 1e-5) if dtype == torch.float32 else (1e-2, 1e-1)
assert_close(y, ref_result, atol=atol, rtol=rtol)