Unverified Commit 1e43a28e authored by thatPepe's avatar thatPepe Committed by GitHub
Browse files

Merge pull request #589 from InfiniTensor/issue/566

issue/566: 添加 Python 算子测试
parents 798dc76e 4deec279
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
# Test cases format: (in_shape, in_strides_or_None)
_TEST_CASES_DATA = [
((2, 3), None),
((1, 4, 8), (32, 8, 1)),
((3, 2, 5, 7), None),
((2, 1, 16), None),
((1, 8, 9, 11), (792, 99, 11, 1)),
((2, 6, 10), None),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 0.0, "rtol": 0.0},
infinicore.float32: {"atol": 0.0, "rtol": 0.0},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def parse_test_cases():
cases = []
for shape, strides in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP[dtype]
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
# Out-of-place
cases.append(
TestCase(
inputs=[in_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tol,
description="Abs - OUT_OF_PLACE",
)
)
# Explicit out
out_spec = TensorSpec.from_tensor(shape, None, dtype)
cases.append(
TestCase(
inputs=[in_spec],
kwargs={},
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="Abs - INPLACE(out)",
)
)
# In-place on input (out=0)
cases.append(
TestCase(
inputs=[in_spec],
kwargs={"out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="Abs - INPLACE(a)",
)
)
return cases
class OpTest(BaseOperatorTest):
"""Abs operator test with simplified implementation"""
def __init__(self):
super().__init__("Abs")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.abs(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.abs(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# =======================================================================
# Test cases format: (shape, input_strides_or_None)
# =======================================================================
_TEST_CASES_DATA = [
((13, 4), None),
((13, 4), (10, 1)),
((8, 16), None),
((8, 16), (40, 1)),
((2, 3, 4), None),
((16, 5632), None),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
infinicore.bfloat16: {"atol": 1e-2, "rtol": 5e-2},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape = data[0]
in_strides = data[1] if len(data) > 1 else None
supports_inplace = not is_broadcast(in_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
input_spec = TensorSpec.from_tensor(shape, in_strides, dtype)
out_spec = TensorSpec.from_tensor(shape, None, dtype)
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tol,
description="acos - OUT_OF_PLACE",
)
)
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs={},
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="acos - INPLACE(out)",
)
)
if supports_inplace:
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs={"out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="acos - INPLACE(input)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""Acos operator test with simplified implementation"""
def __init__(self):
super().__init__("Acos")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.acos(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.acos(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# =======================================================================
# Test cases format: (shape, input_strides_or_None)
# Note: acosh domain is [1, inf); tests should use valid ranges when generating tensors.
# =======================================================================
_TEST_CASES_DATA = [
((13, 4), None),
((13, 4), (10, 1)),
((8, 16), None),
((8, 16), (40, 1)),
((2, 3, 4), None),
((16, 5632), None),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-2, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
infinicore.bfloat16: {"atol": 1e-2, "rtol": 5e-2},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape = data[0]
in_strides = data[1] if len(data) > 1 else None
supports_inplace = not is_broadcast(in_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
input_spec = TensorSpec.from_tensor(shape, in_strides, dtype)
out_spec = TensorSpec.from_tensor(shape, None, dtype)
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tol,
description="acosh - OUT_OF_PLACE",
)
)
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs={},
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="acosh - INPLACE(out)",
)
)
if supports_inplace:
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs={"out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="acosh - INPLACE(input)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""Acosh operator test with simplified implementation"""
def __init__(self):
super().__init__("Acosh")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.acosh(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.acosh(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, output_size_or_None)
_TEST_CASES_DATA = [
((2, 3, 16), None, 1),
((2, 3, 15), (45, 15, 1), 5),
((1, 4, 64), None, 8),
((4, 2, 7), (14, 7, 1), 3),
((3, 3, 32), None, 16),
((2, 8, 9), (72, 9, 1), 4),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, strides, out_size = data
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
kwargs = {"output_size": out_size}
# Out-of-place
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="AdaptiveAvgPool1d - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""AdaptiveAvgPool1d operator test with simplified implementation"""
def __init__(self):
super().__init__("AdaptiveAvgPool1d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.adaptive_avg_pool1d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.adaptive_avg_pool1d(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, output_size_or_None)
# adaptive_avg_pool2d maps input HxW to target output size (h, w)
_TEST_CASES_DATA = [
((2, 3, 16, 16), None, (1, 1)),
((2, 4, 15, 17), (204, 51, 17, 1), (5, 6)),
((1, 8, 32, 32), None, (8, 8)),
((4, 2, 7, 9), (126, 63, 9, 1), (3, 4)),
((3, 3, 31, 29), None, (16, 15)),
((2, 8, 9, 11), (792, 99, 11, 1), (4, 5)),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, strides, out_size = data
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
kwargs = {"output_size": out_size}
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="AdaptiveAvgPool2d - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""AdaptiveAvgPool2d operator test with simplified implementation"""
def __init__(self):
super().__init__("AdaptiveAvgPool2d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.adaptive_avg_pool2d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.adaptive_avg_pool2d(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, output_size_or_None)
# adaptive_avg_pool3d maps input D x H x W to target output size (d, h, w)
_TEST_CASES_DATA = [
((2, 3, 8, 8, 8), None, (1, 1, 1)),
((2, 4, 7, 9, 6), (2016, 504, 72, 8, 1), (3, 3, 2)),
((1, 8, 16, 16, 16), None, (4, 4, 4)),
((2, 2, 5, 7, 9), (1260, 630, 126, 18, 2), (2, 3, 4)),
((3, 3, 10, 9, 8), None, (5, 5, 4)),
((2, 6, 9, 11, 13), (5148, 858, 13, 1, 1), (3, 4, 5)),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, strides, out_size = data
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
kwargs = {"output_size": out_size}
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="AdaptiveAvgPool3d - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""AdaptiveAvgPool3d operator test with simplified implementation"""
def __init__(self):
super().__init__("AdaptiveAvgPool3d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.adaptive_avg_pool3d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.adaptive_avg_pool3d(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, output_size_or_None)
_TEST_CASES_DATA = [
((2, 3, 16), None, 1),
((2, 3, 15), (45, 15, 1), 5),
((1, 4, 64), None, 8),
((4, 2, 7), (14, 7, 1), 3),
((3, 3, 32), None, 16),
((2, 8, 9), (72, 9, 1), 4),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, strides, out_size = data
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
kwargs = {"output_size": out_size}
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="AdaptiveMaxPool1d - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""AdaptiveMaxPool1d operator test with simplified implementation"""
def __init__(self):
super().__init__("AdaptiveMaxPool1d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.adaptive_max_pool1d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.adaptive_max_pool1d(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, output_size_or_None)
_TEST_CASES_DATA = [
((2, 3, 16, 16), None, (1, 1)),
((2, 4, 15, 17), (204, 51, 17, 1), (5, 6)),
((1, 8, 32, 32), None, (8, 8)),
((4, 2, 7, 9), (126, 63, 9, 1), (3, 4)),
((3, 3, 31, 29), None, (16, 15)),
((2, 8, 9, 11), (792, 99, 11, 1), (4, 5)),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, strides, out_size = data
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
kwargs = {"output_size": out_size}
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="AdaptiveMaxPool2d - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""AdaptiveMaxPool2d operator test with simplified implementation"""
def __init__(self):
super().__init__("AdaptiveMaxPool2d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.adaptive_max_pool2d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.adaptive_max_pool2d(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
...@@ -86,7 +86,7 @@ def parse_test_cases(): ...@@ -86,7 +86,7 @@ def parse_test_cases():
test_cases.append( test_cases.append(
TestCase( TestCase(
inputs=[a_spec, b_spec], inputs=[a_spec, b_spec],
kwargs=None, kwargs={},
output_spec=c_spec, # Specify the output tensor spec output_spec=c_spec, # Specify the output tensor spec
comparison_target="out", comparison_target="out",
tolerance=tolerance, tolerance=tolerance,
......
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (input_shape, batch1_shape, batch2_shape, input_strides_or_None, batch1_strides_or_None, batch2_strides_or_None, beta_or_None, alpha_or_None)
# addbmm(input, batch1, batch2, beta=1, alpha=1, out=None)
_TEST_CASES_DATA = [
# small basic (shapes must satisfy: input (M,N), batch1 (B,M,K), batch2 (B,K,N))
((3, 5), (2, 3, 4), (2, 4, 5), None, None, None, None, None),
# larger
((8, 8), (4, 8, 8), (4, 8, 8), None, None, None, 0.5, 2.0),
# strided input
((5, 7), (2, 5, 6), (2, 6, 7), (30, 1), (0, 5, 1), None, None, None),
# batched different strides
((2, 2), (4, 2, 3), (4, 3, 2), None, (24, 6, 1), (0, 3, 1), 1.0, None),
# square
((16, 16), (2, 16, 16), (2, 16, 16), None, None, (512, 1, 1), None, 0.1),
# edge small
((1, 1), (1, 1, 1), (1, 1, 1), None, None, None, None, None),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 0, "rtol": 1e-2},
infinicore.float32: {"atol": 0, "rtol": 1e-3},
infinicore.bfloat16: {"atol": 0, "rtol": 5e-2},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
in_shape, b1_shape, b2_shape = data[0], data[1], data[2]
in_strides = data[3] if len(data) > 3 else None
b1_strides = data[4] if len(data) > 4 else None
b2_strides = data[5] if len(data) > 5 else None
beta = data[6] if len(data) > 6 else None
alpha = data[7] if len(data) > 7 else None
out_supports_inplace = not is_broadcast(in_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
b1_spec = TensorSpec.from_tensor(b1_shape, b1_strides, dtype)
b2_spec = TensorSpec.from_tensor(b2_shape, b2_strides, dtype)
kwargs = {}
if beta is not None:
kwargs["beta"] = beta
if alpha is not None:
kwargs["alpha"] = alpha
# Out-of-place
test_cases.append(
TestCase(
inputs=[in_spec, b1_spec, b2_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="addbmm - OUT_OF_PLACE",
)
)
# In-place out= provided
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[in_spec, b1_spec, b2_spec],
kwargs=kwargs,
output_spec=in_spec,
comparison_target="out",
tolerance=tol,
description="addbmm - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""addbmm operator test with simplified implementation"""
def __init__(self):
super().__init__("addbmm")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.addbmm(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.addbmm(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, t1_shape_or_None, t2_shape_or_None, value)
_TEST_CASES_DATA = [
((2, 3, 4), None, None, None, 1.0),
((1, 4, 8), (32, 8, 1), None, None, 0.5),
((3, 2, 5, 7), None, None, None, 2.0),
((2, 1, 16), None, None, None, 1.0),
((1, 8, 9, 11), (792, 99, 11, 1), None, None, 1.5),
((2, 6, 10), None, None, None, 0.25),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def parse_test_cases():
cases = []
for in_shape, in_strides, t1_shape, t2_shape, value in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP[dtype]
input_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
t1_spec = TensorSpec.from_tensor(
in_shape if t1_shape is None else t1_shape, None, dtype
)
t2_spec = TensorSpec.from_tensor(
in_shape if t2_shape is None else t2_shape, None, dtype
)
# Out-of-place
kwargs = {"value": value}
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="addcdiv - OUT_OF_PLACE",
)
)
# Explicit out
out_spec = TensorSpec.from_tensor(in_shape, None, dtype)
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs=kwargs,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="addcdiv - INPLACE(out)",
)
)
# In-place on input (out=0)
if not is_broadcast(input_spec.strides):
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs={"value": value, "out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="addcdiv - INPLACE(a)",
)
)
# In-place on tensor1 (out=1)
if not is_broadcast(t1_spec.strides):
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs={"value": value, "out": 1},
output_spec=None,
comparison_target=1,
tolerance=tol,
description="addcdiv - INPLACE(b)",
)
)
# In-place on tensor2 (out=2)
if not is_broadcast(t2_spec.strides):
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs={"value": value, "out": 2},
output_spec=None,
comparison_target=2,
tolerance=tol,
description="addcdiv - INPLACE(c)",
)
)
return cases
class OpTest(BaseOperatorTest):
"""AddCdiv operator test with simplified implementation"""
def __init__(self):
super().__init__("AddCdiv")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.addcdiv(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.addcdiv(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, t1_shape_or_None, t2_shape_or_None, value)
_TEST_CASES_DATA = [
((2, 3, 4), None, None, None, 1.0),
((1, 4, 8), (32, 8, 1), None, None, 0.5),
((3, 2, 5, 7), None, None, None, 2.0),
((2, 1, 16), None, None, None, 1.0),
((1, 8, 9, 11), (792, 99, 11, 1), None, None, 1.5),
((2, 6, 10), None, None, None, 0.25),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def parse_test_cases():
cases = []
for in_shape, in_strides, t1_shape, t2_shape, value in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP[dtype]
input_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
t1_spec = TensorSpec.from_tensor(
in_shape if t1_shape is None else t1_shape, None, dtype
)
t2_spec = TensorSpec.from_tensor(
in_shape if t2_shape is None else t2_shape, None, dtype
)
kwargs = {"value": value}
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="addcmul - OUT_OF_PLACE",
)
)
out_spec = TensorSpec.from_tensor(in_shape, None, dtype)
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs=kwargs,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="addcmul - INPLACE(out)",
)
)
if not is_broadcast(input_spec.strides):
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs={"value": value, "out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="addcmul - INPLACE(a)",
)
)
if not is_broadcast(t1_spec.strides):
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs={"value": value, "out": 1},
output_spec=None,
comparison_target=1,
tolerance=tol,
description="addcmul - INPLACE(b)",
)
)
if not is_broadcast(t2_spec.strides):
cases.append(
TestCase(
inputs=[input_spec, t1_spec, t2_spec],
kwargs={"value": value, "out": 2},
output_spec=None,
comparison_target=2,
tolerance=tol,
description="addcmul - INPLACE(c)",
)
)
return cases
class OpTest(BaseOperatorTest):
"""AddCmul operator test with simplified implementation"""
def __init__(self):
super().__init__("AddCmul")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.addcmul(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.addcmul(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (input_shape, mat_shape, vec_shape, input_strides_or_None, mat_strides_or_None, vec_strides_or_None, beta_or_None, alpha_or_None)
_TEST_CASES_DATA = [
((4,), (4, 6), (6,), None, None, None, None, None),
((8,), (8, 8), (8,), None, None, None, 0.0, 1.0),
((3,), (3, 5), (5,), None, (15, 1), None, None, 0.5),
((16,), (16, 32), (32,), None, (512, 1), None, 1.0, None),
((1,), (1, 1), (1,), None, None, None, None, None),
((12,), (12, 12), (12,), None, None, None, None, None),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 0, "rtol": 1e-2},
infinicore.float32: {"atol": 0, "rtol": 1e-3},
infinicore.bfloat16: {"atol": 0, "rtol": 5e-2},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32]
def parse_test_cases():
test_cases = []
for d in _TEST_CASES_DATA:
in_shape, mat_shape, vec_shape = d[0], d[1], d[2]
in_strides, mat_strides, vec_strides = d[3], d[4], d[5]
beta, alpha = d[6], d[7]
out_supports_inplace = not is_broadcast(in_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
mat_spec = TensorSpec.from_tensor(mat_shape, mat_strides, dtype)
vec_spec = TensorSpec.from_tensor(vec_shape, vec_strides, dtype)
kwargs = {}
if beta is not None:
kwargs["beta"] = beta
if alpha is not None:
kwargs["alpha"] = alpha
test_cases.append(
TestCase(
inputs=[in_spec, mat_spec, vec_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="addmv - OUT_OF_PLACE",
)
)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[in_spec, mat_spec, vec_spec],
kwargs=kwargs,
output_spec=in_spec,
comparison_target="out",
tolerance=tol,
description="addmv - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""addmv operator test with simplified implementation"""
def __init__(self):
super().__init__("addmv")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.addmv(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.addmv(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (input_shape, vec1_shape, vec2_shape, input_strides_or_None, vec1_strides_or_None, vec2_strides_or_None, beta_or_None, alpha_or_None)
_TEST_CASES_DATA = [
((3, 4), (3,), (4,), None, None, None, None, None),
((8, 8), (8,), (8,), None, None, None, 0.5, 2.0),
((5, 6), (5,), (6,), (30, 1), None, None, None, None),
((1, 1), (1,), (1,), None, None, None, None, None),
((16, 4), (16,), (4,), None, None, None, 1.0, None),
((2, 7), (2,), (7,), None, None, None, None, None),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 0, "rtol": 1e-2},
infinicore.float32: {"atol": 0, "rtol": 1e-3},
infinicore.bfloat16: {"atol": 0, "rtol": 5e-2},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32]
def parse_test_cases():
test_cases = []
for d in _TEST_CASES_DATA:
in_shape, v1_shape, v2_shape = d[0], d[1], d[2]
in_strides, v1_strides, v2_strides = d[3], d[4], d[5]
beta, alpha = d[6], d[7]
out_supports_inplace = not is_broadcast(in_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
v1_spec = TensorSpec.from_tensor(v1_shape, v1_strides, dtype)
v2_spec = TensorSpec.from_tensor(v2_shape, v2_strides, dtype)
kwargs = {}
if beta is not None:
kwargs["beta"] = beta
if alpha is not None:
kwargs["alpha"] = alpha
test_cases.append(
TestCase(
inputs=[in_spec, v1_spec, v2_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="addr - OUT_OF_PLACE",
)
)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[in_spec, v1_spec, v2_spec],
kwargs=kwargs,
output_spec=in_spec,
comparison_target="out",
tolerance=tol,
description="addr - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""addr operator test with simplified implementation"""
def __init__(self):
super().__init__("addr")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.addr(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.addr(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (theta_shape, out_shape, theta_strides_or_None)
_TEST_CASES_DATA = [
((1, 2, 3), (1, 3, 4, 4), None),
((2, 2, 3), (2, 3, 8, 8), None),
((1, 2, 3), (1, 4, 6, 6), (6, 2, 1)),
((4, 2, 3), (4, 3, 5, 5), None),
((2, 2, 3), (2, 1, 7, 7), None),
((3, 2, 3), (3, 3, 2, 2), None),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-2, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
infinicore.bfloat16: {"atol": 1e-2, "rtol": 5e-2},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
theta_shape, out_shape = data[0], data[1]
theta_strides = data[2] if len(data) > 2 else None
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
theta_spec = TensorSpec.from_tensor(theta_shape, theta_strides, dtype)
# Out-of-place with align_corners variations
for align in (True, False):
kwargs = {"size": out_shape}
if align is not None:
kwargs["align_corners"] = align
test_cases.append(
TestCase(
inputs=[theta_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="affine_grid - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""AffineGrid operator test with simplified implementation"""
def __init__(self):
super().__init__("AffineGrid")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.affine_grid(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.affine_grid(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, dim_or_None, keepdim_or_None, out_strides_or_None)
_TEST_CASES_DATA = [
((8, 8), None, None, None, None),
((8, 8), (16, 1), 1, False, None),
((2, 3, 4), None, 0, True, (0, 1, 1)),
((1, 8), None, (0, 1), False, None),
((16, 64), (128, 1), None, None, None),
((4, 5, 6), (60, 12, 2), 2, True, (12, 4, 1)),
]
_TOLERANCE_MAP = {infinicore.bool: {"atol": 0, "rtol": 0}}
_TENSOR_DTYPES = [infinicore.bool, infinicore.uint8]
def _compute_out_shape(shape, dim, keepdim):
if dim is None:
return ()
if isinstance(dim, tuple):
dims = sorted([(d if d >= 0 else len(shape) + d) for d in dim])
if keepdim:
out = list(shape)
for d in dims:
out[d] = 1
return tuple(out)
else:
return tuple(s for i, s in enumerate(shape) if i not in dims)
else:
d = dim if dim >= 0 else len(shape) + dim
if keepdim:
out = list(shape)
out[d] = 1
return tuple(out)
else:
return tuple(s for i, s in enumerate(shape) if i != d)
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, strides, dim, keepdim, out_strides = data
input_supports_inplace = not is_broadcast(strides)
out_supports_inplace = not is_broadcast(out_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 0})
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
# Out-of-place
kwargs = {}
if dim is not None:
kwargs["dim"] = dim
if keepdim is not None:
kwargs["keepdim"] = keepdim
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="All - OUT_OF_PLACE",
)
)
# explicit out when supported (create out tensor with computed shape)
out_shape = _compute_out_shape(shape, dim, keepdim)
out_spec = TensorSpec.from_tensor(out_shape, out_strides, infinicore.bool)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="All - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""All operator test with simplified implementation"""
def __init__(self):
super().__init__("All")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.all(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.all(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (shape, p, training, in_strides_or_None)
_TEST_CASES_DATA = [
((8, 16), 0.1, True, None),
((8, 16), 0.2, False, (128, 1)),
((2, 3, 4), 0.5, True, None),
((16, 64), 0.3, True, None),
((4, 5, 6), 0.5, False, None),
((3, 4, 5), 0.4, True, (60, 20, 4)),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-2, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
infinicore.bfloat16: {"atol": 1e-2, "rtol": 5e-2},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, p, training = data[0], data[1], data[2]
in_strides = data[3] if len(data) > 3 else None
supports_inplace = not is_broadcast(in_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-2, "rtol": 1e-2})
input_spec = TensorSpec.from_tensor(shape, in_strides, dtype)
kwargs = {"p": p, "training": training}
# Out-of-place
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="alpha_dropout - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""AlphaDropout operator test with simplified implementation"""
def __init__(self):
super().__init__("AlphaDropout")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.alpha_dropout(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.alpha_dropout(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, dim_or_None, keepdim_or_None, out_strides_or_None)
_TEST_CASES_DATA = [
((8, 8), None, None, None, None),
((8, 8), (16, 1), 1, False, None),
((2, 3, 4), None, 0, True, (0, 1, 1)),
((1, 8), None, (0,), False, None),
((16, 64), (128, 1), None, None, None),
((4, 5, 6), (60, 12, 2), 2, True, (12, 4, 1)),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def _compute_out_shape(shape, dim, keepdim):
if dim is None:
return ()
if isinstance(dim, tuple):
dims = sorted([(d if d >= 0 else len(shape) + d) for d in dim])
if keepdim:
out = list(shape)
for d in dims:
out[d] = 1
return tuple(out)
else:
return tuple(s for i, s in enumerate(shape) if i not in dims)
else:
d = dim if dim >= 0 else len(shape) + dim
if keepdim:
out = list(shape)
out[d] = 1
return tuple(out)
else:
return tuple(s for i, s in enumerate(shape) if i != d)
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, strides, dim, keepdim, out_strides = data
out_supports_inplace = not is_broadcast(out_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
kwargs = {}
if dim is not None:
kwargs["dim"] = dim
if keepdim is not None:
kwargs["keepdim"] = keepdim
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="Amax - OUT_OF_PLACE",
)
)
out_shape = _compute_out_shape(shape, dim, keepdim)
out_spec = TensorSpec.from_tensor(out_shape, out_strides, dtype)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="Amax - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""Amax operator test with simplified implementation"""
def __init__(self):
super().__init__("Amax")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.amax(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.amax(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, dim_or_None, keepdim_or_None, out_strides_or_None)
_TEST_CASES_DATA = [
((8, 8), None, None, None, None),
((8, 8), (16, 1), 1, False, None),
((2, 3, 4), None, 0, True, (0, 1, 1)),
((1, 8), None, (0,), False, None),
((16, 64), (128, 1), None, None, None),
((4, 5, 6), (60, 12, 2), 2, True, (12, 4, 1)),
]
_TOLERANCE_MAP = {
infinicore.float16: {"atol": 1e-3, "rtol": 1e-2},
infinicore.float32: {"atol": 1e-5, "rtol": 1e-4},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32]
def _compute_out_shape(shape, dim, keepdim):
if dim is None:
return ()
if isinstance(dim, tuple):
dims = sorted([(d if d >= 0 else len(shape) + d) for d in dim])
if keepdim:
out = list(shape)
for d in dims:
out[d] = 1
return tuple(out)
else:
return tuple(s for i, s in enumerate(shape) if i not in dims)
else:
d = dim if dim >= 0 else len(shape) + dim
if keepdim:
out = list(shape)
out[d] = 1
return tuple(out)
else:
return tuple(s for i, s in enumerate(shape) if i != d)
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, strides, dim, keepdim, out_strides = data
out_supports_inplace = not is_broadcast(out_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-3})
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
kwargs = {}
if dim is not None:
kwargs["dim"] = dim
if keepdim is not None:
kwargs["keepdim"] = keepdim
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="Amin - OUT_OF_PLACE",
)
)
out_shape = _compute_out_shape(shape, dim, keepdim)
out_spec = TensorSpec.from_tensor(out_shape, out_strides, dtype)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="Amin - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""Amin operator test with simplified implementation"""
def __init__(self):
super().__init__("Amin")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.amin(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.amin(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import torch
import infinicore
from framework.base import BaseOperatorTest, TensorSpec, TestCase
from framework.runner import GenericTestRunner
from framework.utils import is_broadcast
# Test cases format: (in_shape, in_strides_or_None, dim_or_None, keepdim_or_None, out_strides_or_None)
_TEST_CASES_DATA = [
((8, 8), None, None, None, None),
((8, 8), (16, 1), 1, False, None),
((2, 3, 4), None, 0, True, (0, 1, 1)),
((1, 8), None, (0, 1), False, None),
((16, 64), (128, 1), None, None, None),
((4, 5, 6), (60, 12, 2), 2, True, (12, 4, 1)),
]
_TOLERANCE_MAP = {infinicore.bool: {"atol": 0, "rtol": 0}}
_TENSOR_DTYPES = [infinicore.bool, infinicore.uint8]
def _compute_out_shape(shape, dim, keepdim):
if dim is None:
return ()
if isinstance(dim, tuple):
dims = sorted([(d if d >= 0 else len(shape) + d) for d in dim])
if keepdim:
out = list(shape)
for d in dims:
out[d] = 1
return tuple(out)
else:
return tuple(s for i, s in enumerate(shape) if i not in dims)
else:
d = dim if dim >= 0 else len(shape) + dim
if keepdim:
out = list(shape)
out[d] = 1
return tuple(out)
else:
return tuple(s for i, s in enumerate(shape) if i != d)
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, strides, dim, keepdim, out_strides = data
out_supports_inplace = not is_broadcast(out_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 0})
in_spec = TensorSpec.from_tensor(shape, strides, dtype)
kwargs = {}
if dim is not None:
kwargs["dim"] = dim
if keepdim is not None:
kwargs["keepdim"] = keepdim
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="Any - OUT_OF_PLACE",
)
)
out_shape = _compute_out_shape(shape, dim, keepdim)
out_spec = TensorSpec.from_tensor(out_shape, out_strides, infinicore.bool)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="Any - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""Any operator test with simplified implementation"""
def __init__(self):
super().__init__("Any")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.any(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.any(*args, **kwargs)
def main():
"""Main entry point"""
runner = GenericTestRunner(OpTest)
runner.run_and_exit()
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment