Skip to content

Commit cf8cf71

Browse files
committed
issue/566: 添加 Python 算子测试
1 parent 1a618ff commit cf8cf71

File tree

255 files changed

+23641
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+23641
-29
lines changed

test/infinicore/ops/abs.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import sys
2+
import os
3+
4+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
5+
6+
import torch
7+
import infinicore
8+
from framework.base import BaseOperatorTest, TensorSpec, TestCase
9+
from framework.runner import GenericTestRunner
10+
11+
# Test cases format: (in_shape, in_strides_or_None)
12+
_TEST_CASES_DATA = [
13+
((2, 3), None),
14+
((1, 4, 8), (32, 8, 1)),
15+
((3, 2, 5, 7), None),
16+
((2, 1, 16), None),
17+
((1, 8, 9, 11), (792, 99, 11, 1)),
18+
((2, 6, 10), None),
19+
]
20+
21+
_TOLERANCE_MAP = {
22+
infinicore.float16: {"atol": 0.0, "rtol": 0.0},
23+
infinicore.float32: {"atol": 0.0, "rtol": 0.0},
24+
}
25+
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
26+
27+
28+
def parse_test_cases():
29+
cases = []
30+
for shape, strides in _TEST_CASES_DATA:
31+
for dtype in _TENSOR_DTYPES:
32+
tol = _TOLERANCE_MAP[dtype]
33+
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
34+
35+
# Out-of-place
36+
cases.append(
37+
TestCase(
38+
inputs=[in_spec],
39+
kwargs={},
40+
output_spec=None,
41+
comparison_target=None,
42+
tolerance=tol,
43+
description="Abs - OUT_OF_PLACE",
44+
)
45+
)
46+
47+
# Explicit out
48+
out_spec = TensorSpec.from_tensor(shape, None, dtype)
49+
cases.append(
50+
TestCase(
51+
inputs=[in_spec],
52+
kwargs={},
53+
output_spec=out_spec,
54+
comparison_target="out",
55+
tolerance=tol,
56+
description="Abs - INPLACE(out)",
57+
)
58+
)
59+
60+
# In-place on input (out=0)
61+
cases.append(
62+
TestCase(
63+
inputs=[in_spec],
64+
kwargs={"out": 0},
65+
output_spec=None,
66+
comparison_target=0,
67+
tolerance=tol,
68+
description="Abs - INPLACE(a)",
69+
)
70+
)
71+
72+
return cases
73+
74+
75+
class OpTest(BaseOperatorTest):
76+
"""Abs operator test with simplified implementation"""
77+
78+
def __init__(self):
79+
super().__init__("Abs")
80+
81+
def get_test_cases(self):
82+
return parse_test_cases()
83+
84+
def torch_operator(self, *args, **kwargs):
85+
return torch.abs(*args, **kwargs)
86+
87+
# def infinicore_operator(self, *args, **kwargs):
88+
# """InfiniCore implementation (operator not yet available)."""
89+
# return infinicore.abs(*args, **kwargs)
90+
91+
92+
def main():
93+
"""Main entry point"""
94+
runner = GenericTestRunner(OpTest)
95+
runner.run_and_exit()
96+
97+
98+
if __name__ == "__main__":
99+
main()

test/infinicore/ops/acos.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import sys
2+
import os
3+
4+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
5+
6+
import torch
7+
import infinicore
8+
from framework.base import BaseOperatorTest, TensorSpec, TestCase
9+
from framework.runner import GenericTestRunner
10+
from framework.utils import is_broadcast
11+
12+
# =======================================================================
13+
# Test cases format: (shape, input_strides_or_None)
14+
# =======================================================================
15+
16+
_TEST_CASES_DATA = [
17+
((13, 4), None),
18+
((13, 4), (10, 1)),
19+
((8, 16), None),
20+
((8, 16), (40, 1)),
21+
((2, 3, 4), None),
22+
((16, 5632), None),
23+
]
24+
25+
_TOLERANCE_MAP = {
26+
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
27+
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
28+
infinicore.bfloat16: {"atol": 1e-2, "rtol": 5e-2},
29+
}
30+
31+
_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32]
32+
33+
34+
def parse_test_cases():
35+
test_cases = []
36+
for data in _TEST_CASES_DATA:
37+
shape = data[0]
38+
in_strides = data[1] if len(data) > 1 else None
39+
40+
supports_inplace = not is_broadcast(in_strides)
41+
42+
for dtype in _TENSOR_DTYPES:
43+
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
44+
input_spec = TensorSpec.from_tensor(shape, in_strides, dtype)
45+
out_spec = TensorSpec.from_tensor(shape, None, dtype)
46+
47+
test_cases.append(
48+
TestCase(
49+
inputs=[input_spec],
50+
kwargs={},
51+
output_spec=None,
52+
comparison_target=None,
53+
tolerance=tol,
54+
description="acos - OUT_OF_PLACE",
55+
)
56+
)
57+
58+
test_cases.append(
59+
TestCase(
60+
inputs=[input_spec],
61+
kwargs={},
62+
output_spec=out_spec,
63+
comparison_target="out",
64+
tolerance=tol,
65+
description="acos - INPLACE(out)",
66+
)
67+
)
68+
69+
if supports_inplace:
70+
test_cases.append(
71+
TestCase(
72+
inputs=[input_spec],
73+
kwargs={"out": 0},
74+
output_spec=None,
75+
comparison_target=0,
76+
tolerance=tol,
77+
description="acos - INPLACE(input)",
78+
)
79+
)
80+
81+
return test_cases
82+
83+
84+
class OpTest(BaseOperatorTest):
85+
"""Acos operator test with simplified implementation"""
86+
87+
def __init__(self):
88+
super().__init__("Acos")
89+
90+
def get_test_cases(self):
91+
return parse_test_cases()
92+
93+
def torch_operator(self, *args, **kwargs):
94+
return torch.acos(*args, **kwargs)
95+
96+
# def infinicore_operator(self, *args, **kwargs):
97+
# """InfiniCore implementation (operator not yet available)."""
98+
# return infinicore.acos(*args, **kwargs)
99+
100+
101+
def main():
102+
"""Main entry point"""
103+
runner = GenericTestRunner(OpTest)
104+
runner.run_and_exit()
105+
106+
107+
if __name__ == "__main__":
108+
main()

test/infinicore/ops/acosh.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import sys
2+
import os
3+
4+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
5+
6+
import torch
7+
import infinicore
8+
from framework.base import BaseOperatorTest, TensorSpec, TestCase
9+
from framework.runner import GenericTestRunner
10+
from framework.utils import is_broadcast
11+
12+
# =======================================================================
13+
# Test cases format: (shape, input_strides_or_None)
14+
# Note: acosh domain is [1, inf); tests should use valid ranges when generating tensors.
15+
# =======================================================================
16+
17+
_TEST_CASES_DATA = [
18+
((13, 4), None),
19+
((13, 4), (10, 1)),
20+
((8, 16), None),
21+
((8, 16), (40, 1)),
22+
((2, 3, 4), None),
23+
((16, 5632), None),
24+
]
25+
26+
_TOLERANCE_MAP = {
27+
infinicore.float16: {"atol": 1e-2, "rtol": 1e-2},
28+
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
29+
infinicore.bfloat16: {"atol": 1e-2, "rtol": 5e-2},
30+
}
31+
32+
_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32]
33+
34+
35+
def parse_test_cases():
36+
test_cases = []
37+
for data in _TEST_CASES_DATA:
38+
shape = data[0]
39+
in_strides = data[1] if len(data) > 1 else None
40+
41+
supports_inplace = not is_broadcast(in_strides)
42+
43+
for dtype in _TENSOR_DTYPES:
44+
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
45+
input_spec = TensorSpec.from_tensor(shape, in_strides, dtype)
46+
out_spec = TensorSpec.from_tensor(shape, None, dtype)
47+
48+
test_cases.append(
49+
TestCase(
50+
inputs=[input_spec],
51+
kwargs={},
52+
output_spec=None,
53+
comparison_target=None,
54+
tolerance=tol,
55+
description="acosh - OUT_OF_PLACE",
56+
)
57+
)
58+
59+
test_cases.append(
60+
TestCase(
61+
inputs=[input_spec],
62+
kwargs={},
63+
output_spec=out_spec,
64+
comparison_target="out",
65+
tolerance=tol,
66+
description="acosh - INPLACE(out)",
67+
)
68+
)
69+
70+
if supports_inplace:
71+
test_cases.append(
72+
TestCase(
73+
inputs=[input_spec],
74+
kwargs={"out": 0},
75+
output_spec=None,
76+
comparison_target=0,
77+
tolerance=tol,
78+
description="acosh - INPLACE(input)",
79+
)
80+
)
81+
82+
return test_cases
83+
84+
85+
class OpTest(BaseOperatorTest):
86+
"""Acosh operator test with simplified implementation"""
87+
88+
def __init__(self):
89+
super().__init__("Acosh")
90+
91+
def get_test_cases(self):
92+
return parse_test_cases()
93+
94+
def torch_operator(self, *args, **kwargs):
95+
return torch.acosh(*args, **kwargs)
96+
97+
# def infinicore_operator(self, *args, **kwargs):
98+
# """InfiniCore implementation (operator not yet available)."""
99+
# return infinicore.acosh(*args, **kwargs)
100+
101+
102+
def main():
103+
"""Main entry point"""
104+
runner = GenericTestRunner(OpTest)
105+
runner.run_and_exit()
106+
107+
108+
if __name__ == "__main__":
109+
main()

0 commit comments

Comments
 (0)