-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathtest_prod.py
More file actions
111 lines (89 loc) · 3.67 KB
/
Copy pathtest_prod.py
File metadata and controls
111 lines (89 loc) · 3.67 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
# Copyright 2026 FlagOS Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
import torch
import flag_gems
from . import accuracy_utils as utils
from . import conftest as cfg
if cfg.QUICK_MODE:
FLOAT_DTYPES = [torch.float32]
DIM_LIST = [0]
KEEPDIM = [True]
else:
FLOAT_DTYPES = utils.FLOAT_DTYPES
DIM_LIST = [0, 1]
KEEPDIM = [True, False]
@pytest.mark.prod
@pytest.mark.parametrize("shape", utils.REDUCTION_SHAPES)
@pytest.mark.parametrize("dtype", FLOAT_DTYPES)
def test_prod(shape, dtype):
if flag_gems.vendor_name == "kunlunxin":
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
inp = torch.randn(shape, dtype=dtype, device=flag_gems.device)
ref_inp = utils.to_reference(inp, True)
ref_out = torch.prod(ref_inp)
with flag_gems.use_gems():
res_out = torch.prod(inp)
utils.gems_assert_close(res_out, ref_out, dtype)
# TODO: failed at (200, 40999, 3), while successed at this shape in mean_dim
@pytest.mark.prod_dim_int
@pytest.mark.parametrize("shape", utils.REDUCTION_SMALL_SHAPES)
@pytest.mark.parametrize("keepdim", KEEPDIM)
@pytest.mark.parametrize("dim", DIM_LIST)
@pytest.mark.parametrize("dtype", FLOAT_DTYPES)
def test_prod_dim_int(shape, dim, keepdim, dtype):
if flag_gems.vendor_name == "kunlunxin":
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
inp = torch.randn(shape, dtype=dtype, device=flag_gems.device)
ref_inp = utils.to_reference(inp, True)
ref_out = torch.prod(ref_inp, dim=dim, keepdim=keepdim)
with flag_gems.use_gems():
res_out = torch.prod(inp, dim=dim, keepdim=keepdim)
utils.gems_assert_close(res_out, ref_out, dtype)
@pytest.mark.prod
@pytest.mark.parametrize(
"shape, dim",
[
((4, 8, 4096), 1), # non-inner: K = 4096 spans multiple K tiles
((4, 4096, 8), 1), # non-inner: N = 4096 exercises the reduction loop
((8, 4096), 1), # inner: N = 4096 exercises the reduction loop
((4096, 8), 0), # non-inner via the outer dim
],
)
@pytest.mark.parametrize("keepdim", [False, True])
def test_prod_dim_multi_tile(shape, dim, keepdim):
# Values near 1 keep the product finite over large reduction sizes.
inp = torch.rand(shape, dtype=torch.float32, device=flag_gems.device) * 0.4 + 0.8
ref_inp = utils.to_reference(inp, True)
ref_out = torch.prod(ref_inp, dim=dim, keepdim=keepdim)
with flag_gems.use_gems():
res_out = torch.prod(inp, dim=dim, keepdim=keepdim)
utils.gems_assert_close(res_out, ref_out, torch.float32)
@pytest.mark.prod
@pytest.mark.parametrize("dtype", FLOAT_DTYPES)
def test_prod_full_reduction_noncontiguous(dtype):
# The flattened reduction kernel addresses the input linearly, so for a view
# whose storage still holds the discarded columns it read those elements
# instead. The zeros outside the view would drive the product to 0.
base = torch.zeros((4, 6), dtype=dtype, device=flag_gems.device)
base[:, :3] = 1.0
base[0, 0] = 2.0
inp = base[:, :3]
ref_inp = utils.to_reference(inp, True)
ref_out = torch.prod(ref_inp)
with flag_gems.use_gems():
res_out = torch.prod(inp)
utils.gems_assert_close(res_out, ref_out, dtype)