Commit 4deec279 authored by thatPepe's avatar thatPepe Committed by MaYuhang
Browse files

Merge pull request #631 from InfiniTensor/issue/630

issue/630 - slightly improved unimplemented messages
parents f3a25b70 2a343a3a
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, a_strides_or_None, b_strides_or_None, out_strides_or_None)
# logical_and performs element-wise boolean AND; inputs may be bool or integer
_TEST_CASES_DATA = [
((8, 8), None, None, None),
((8, 8), (16, 1), (16, 1), None),
((8, 8), None, (0, 1), None),
((1, 8), None, None, (8, 1)),
((2, 3, 4), None, None, None),
((16, 128), (256, 1), (256, 1), None),
]
_TOLERANCE_MAP = {
infinicore.bool: {"atol": 0, "rtol": 0},
}
_TENSOR_DTYPES = [infinicore.bool, infinicore.int32, infinicore.uint8]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, a_strides, b_strides, out_strides = data[0], data[1], data[2], data[3]
a_supports_inplace = not is_broadcast(a_strides)
b_supports_inplace = not is_broadcast(b_strides)
out_supports_inplace = not is_broadcast(out_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 0})
a_spec = TensorSpec.from_tensor(shape, a_strides, dtype)
b_spec = TensorSpec.from_tensor(shape, b_strides, dtype)
out_spec = TensorSpec.from_tensor(shape, out_strides, infinicore.bool)
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tol,
description="Logical AND - OUT_OF_PLACE",
)
)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs=None,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="Logical AND - INPLACE(out)",
)
)
if a_supports_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={"out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="Logical AND - INPLACE(a)",
)
)
if b_supports_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={"out": 1},
output_spec=None,
comparison_target=1,
tolerance=tol,
description="Logical AND - INPLACE(b)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""LogicalAnd operator test with simplified implementation"""
def __init__(self):
super().__init__("LogicalAnd")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.logical_and(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.logical_and(*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, out_strides_or_None)
# logical_not negates boolean values
_TEST_CASES_DATA = [
((8, 8), None, None),
((8, 8), (16, 1), None),
((8, 8), None, (0, 1)),
((2, 3, 4), None, None),
((1, 8), None, None),
((16, 128), (256, 1), (256, 1)),
]
_TOLERANCE_MAP = {infinicore.bool: {"atol": 0, "rtol": 0}}
_TENSOR_DTYPES = [infinicore.bool, infinicore.int32]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, in_strides, out_strides = data[0], data[1], data[2]
input_supports_inplace = not is_broadcast(in_strides)
out_supports_inplace = not is_broadcast(out_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 0})
input_spec = TensorSpec.from_tensor(shape, in_strides, dtype)
out_spec = TensorSpec.from_tensor(shape, out_strides, infinicore.bool)
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tol,
description="Logical NOT - OUT_OF_PLACE",
)
)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs=None,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="Logical NOT - INPLACE(out)",
)
)
if input_supports_inplace:
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs={"out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="Logical NOT - INPLACE(input)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""LogicalNot operator test with simplified implementation"""
def __init__(self):
super().__init__("LogicalNot")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.logical_not(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.logical_not(*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, a_strides_or_None, b_strides_or_None, out_strides_or_None)
# logical_or performs element-wise boolean OR
_TEST_CASES_DATA = [
((8, 8), None, None, None),
((8, 8), (16, 1), (16, 1), None),
((8, 8), None, (0, 1), None),
((1, 8), None, None, (8, 1)),
((2, 3, 4), None, None, None),
((16, 128), (256, 1), (256, 1), None),
]
_TOLERANCE_MAP = {infinicore.bool: {"atol": 0, "rtol": 0}}
_TENSOR_DTYPES = [infinicore.bool, infinicore.int32, infinicore.uint8]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, a_strides, b_strides, out_strides = data[0], data[1], data[2], data[3]
a_supports_inplace = not is_broadcast(a_strides)
b_supports_inplace = not is_broadcast(b_strides)
out_supports_inplace = not is_broadcast(out_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 0})
a_spec = TensorSpec.from_tensor(shape, a_strides, dtype)
b_spec = TensorSpec.from_tensor(shape, b_strides, dtype)
out_spec = TensorSpec.from_tensor(shape, out_strides, infinicore.bool)
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tol,
description="Logical OR - OUT_OF_PLACE",
)
)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs=None,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="Logical OR - INPLACE(out)",
)
)
if a_supports_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={"out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="Logical OR - INPLACE(a)",
)
)
if b_supports_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={"out": 1},
output_spec=None,
comparison_target=1,
tolerance=tol,
description="Logical OR - INPLACE(b)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""LogicalOr operator test with simplified implementation"""
def __init__(self):
super().__init__("LogicalOr")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.logical_or(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.logical_or(*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, a_strides_or_None, b_strides_or_None, out_strides_or_None)
# logical_xor performs element-wise boolean XOR
_TEST_CASES_DATA = [
((8, 8), None, None, None),
((8, 8), (16, 1), (16, 1), None),
((8, 8), None, (0, 1), None),
((1, 8), None, None, (8, 1)),
((2, 3, 4), None, None, None),
((16, 128), (256, 1), (256, 1), None),
]
_TOLERANCE_MAP = {infinicore.bool: {"atol": 0, "rtol": 0}}
_TENSOR_DTYPES = [infinicore.bool, infinicore.int32, infinicore.uint8]
def parse_test_cases():
test_cases = []
for data in _TEST_CASES_DATA:
shape, a_strides, b_strides, out_strides = data[0], data[1], data[2], data[3]
a_supports_inplace = not is_broadcast(a_strides)
b_supports_inplace = not is_broadcast(b_strides)
out_supports_inplace = not is_broadcast(out_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 0})
a_spec = TensorSpec.from_tensor(shape, a_strides, dtype)
b_spec = TensorSpec.from_tensor(shape, b_strides, dtype)
out_spec = TensorSpec.from_tensor(shape, out_strides, infinicore.bool)
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tol,
description="Logical XOR - OUT_OF_PLACE",
)
)
if out_supports_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs=None,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="Logical XOR - INPLACE(out)",
)
)
if a_supports_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={"out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="Logical XOR - INPLACE(a)",
)
)
if b_supports_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={"out": 1},
output_spec=None,
comparison_target=1,
tolerance=tol,
description="Logical XOR - INPLACE(b)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""LogicalXor operator test with simplified implementation"""
def __init__(self):
super().__init__("LogicalXor")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.logical_xor(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.logical_xor(*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)
_TEST_CASES_DATA = [
((13, 4), None),
((13, 4), (10, 1)),
((8, 8, 8), 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():
"""logsigmoid(input)"""
test_cases = []
for data in _TEST_CASES_DATA:
shape = data[0]
in_strides = data[1] if len(data) > 1 else None
for dtype in _TENSOR_DTYPES:
tolerance = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
input_spec = TensorSpec.from_tensor(shape, in_strides, dtype)
test_cases.append(
TestCase(
inputs=[input_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tolerance,
description=f"LogSigmoid - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""LogSigmoid operator test with simplified implementation"""
def __init__(self):
super().__init__("LogSigmoid")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.logsigmoid(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.logsigmoid(*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, keepdim_or_None, out_strides_or_None)
# logsumexp computes log(sum(exp(input), dim=dim)) with numerical stability
_TEST_CASES_DATA = [
((8, 8), None, 1, None, None),
((8, 8), (16, 1), 0, False, None),
((2, 3, 4), None, 2, True, (0, 1, 1)),
((1, 8), None, 0, False, None),
((16, 64), (128, 1), 1, True, 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 isinstance(dim, tuple):
dims = sorted([(d if d >= 0 else len(shape) + d) for d in dim])
else:
dims = [dim]
if dim is None:
return ()
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)
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 = {"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="LogSumExp - 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="LogSumExp - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""LogSumExp operator test with simplified implementation"""
def __init__(self):
super().__init__("LogSumExp")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.logsumexp(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.logsumexp(*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
# Test cases format: (in_shape, in_strides_or_None, norm_type, kernel_size, stride_or_None, ceil_mode)
_TEST_CASES_DATA = [
((2, 3, 16), None, 2.0, 3, None, False),
((1, 4, 15), (60, 15, 1), 1.0, 5, 1, True),
((2, 1, 32), None, 3.0, 2, 2, False),
((3, 2, 7), None, 2.0, 3, None, True),
((4, 6, 31), None, 1.5, 4, 2, False),
((2, 8, 9), (72, 9, 1), 2.0, 3, 1, False),
]
_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():
tests = []
for in_shape, in_strides, p, k, s, ceil_mode in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP[dtype]
in_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
kwargs = {"norm_type": p, "kernel_size": k, "ceil_mode": ceil_mode}
if s is not None:
kwargs["stride"] = s
tests.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="LpPool1d - OUT_OF_PLACE",
)
)
return tests
class OpTest(BaseOperatorTest):
"""LpPool1d operator test with simplified implementation"""
def __init__(self):
super().__init__("LpPool1d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.lp_pool1d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.lp_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
# Test cases format: (in_shape, in_strides_or_None, norm_type, kernel_size, stride_or_None, ceil_mode)
_TEST_CASES_DATA = [
((2, 3, 16, 16), None, 2.0, (3, 3), None, False),
((1, 4, 15, 17), (1020, 255, 17, 1), 1.0, (5, 4), (2, 2), True),
((2, 1, 32, 32), None, 3.0, (2, 2), (2, 2), False),
((3, 2, 7, 9), None, 2.0, (3, 3), None, True),
((4, 6, 31, 29), None, 1.5, (4, 4), (2, 2), False),
((2, 8, 9, 11), (1584, 198, 11, 1), 2.0, (3, 2), 1, False),
]
_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():
tests = []
for in_shape, in_strides, p, k, s, ceil_mode in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP[dtype]
in_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
kwargs = {"norm_type": p, "kernel_size": k, "ceil_mode": ceil_mode}
if s is not None:
kwargs["stride"] = s
tests.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="LpPool2d - OUT_OF_PLACE",
)
)
return tests
class OpTest(BaseOperatorTest):
"""LpPool2d operator test with simplified implementation"""
def __init__(self):
super().__init__("LpPool2d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.lp_pool2d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.lp_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
# Test cases format: (in_shape, in_strides_or_None, norm_type, kernel_size, stride_or_None, ceil_mode)
_TEST_CASES_DATA = [
((1, 2, 8, 8, 8), None, 2.0, (2, 2, 2), None, False),
((2, 3, 7, 9, 5), None, 1.0, (3, 3, 2), (2, 2, 1), True),
((1, 4, 16, 16, 6), None, 3.0, (4, 4, 2), (2, 2, 1), False),
((2, 1, 9, 11, 7), None, 2.0, (3, 2, 3), None, True),
((3, 2, 5, 6, 4), None, 1.5, (2, 2, 2), (1, 1, 1), False),
((2, 6, 10, 9, 8), None, 2.0, (3, 3, 2), (2, 1, 2), False),
]
_TOLERANCE_MAP = {infinicore.float32: {"atol": 1e-5, "rtol": 1e-4}}
_TENSOR_DTYPES = [infinicore.float32]
def parse_test_cases():
tests = []
for in_shape, in_strides, p, k, s, ceil_mode in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP[dtype]
in_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
kwargs = {"norm_type": p, "kernel_size": k, "ceil_mode": ceil_mode}
if s is not None:
kwargs["stride"] = s
tests.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="LpPool3d - OUT_OF_PLACE",
)
)
return tests
class OpTest(BaseOperatorTest):
"""LpPool3d operator test with simplified implementation"""
def __init__(self):
super().__init__("LpPool3d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.lp_pool3d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.lp_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
# Test cases format: (input1_shape, input2_shape, target_shape, input1_strides_or_None, input2_strides_or_None, target_strides_or_None, margin_or_None, p_or_None)
# infinicore.nn.functional.margin_ranking_loss(input1, input2, target, margin=0, p=1, reduction='mean')
_TEST_CASES_DATA = [
((4,), (4,), (4,), None, None, None, None),
((8,), (8,), (8,), None, None, None, 1),
((1,), (1,), (1,), None, None, None, None),
((16,), (16,), (16,), (2,), None, None, 2),
((3,), (3,), (3,), None, (1,), None, None),
((2,), (2,), (2,), None, None, None, 1),
]
_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 s1, s2, st, st1, st2, stt, margin in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
a = TensorSpec.from_tensor(s1, st1, dtype)
b = TensorSpec.from_tensor(s2, st2, dtype)
y = TensorSpec.from_tensor(st, stt, dtype)
kwargs = {}
if margin is not None:
kwargs["margin"] = margin
test_cases.append(
TestCase(
inputs=[a, b, y],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="margin_ranking_loss - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""margin_ranking_loss operator test with simplified implementation"""
def __init__(self):
super().__init__("margin_ranking_loss")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.margin_ranking_loss(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.margin_ranking_loss(*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
# Test cases format: (input_shape, input_strides_or_None, mask_shape)
_TEST_CASES_DATA = [
((3, 4), None, (3, 4)),
((5,), None, (5,)),
((2, 2, 3), (12, 6, 2), (2, 2, 3)),
((1, 6), None, (1, 6)),
((4, 4), None, (4, 4)),
((2, 3, 2), None, (2, 3, 2)),
]
_TOLERANCE_MAP = {infinicore.float32: {"atol": 1e-5, "rtol": 1e-4}}
_TENSOR_DTYPES = [infinicore.float32]
def parse_test_cases():
test_cases = []
for shape, strides, mask_shape in _TEST_CASES_DATA:
input_spec = TensorSpec.from_tensor(shape, strides, infinicore.float32)
mask_spec = TensorSpec.from_tensor(mask_shape, None, infinicore.bool)
test_cases.append(
TestCase(
inputs=[input_spec, mask_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=_TOLERANCE_MAP[infinicore.float32],
description=f"masked_select - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""MaskedSelect operator test with simplified implementation"""
def __init__(self):
super().__init__("MaskedSelect")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.masked_select(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.masked_select(*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
# Test cases format: (matrix_shape, strides_or_None)
# infinicore.matrix_exp(input)
_TEST_CASES_DATA = [
((1, 1), None),
((2, 2), None),
((3, 3), (18, 6)),
((4, 4), None),
((6, 6), None),
((8, 8), (512, 1)),
]
_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 shape, strides in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
spec = TensorSpec.from_tensor(shape, strides, dtype)
test_cases.append(
TestCase(
inputs=[spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tol,
description="matrix_exp - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""matrix_exp operator test with simplified implementation"""
def __init__(self):
super().__init__("matrix_exp")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.matrix_exp(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.matrix_exp(*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
# Test cases format: (matrix_shape, strides_or_None, n_or_None)
# infinicore.matrix_power(input, n)
_TEST_CASES_DATA = [
((2, 2), None, 1),
((3, 3), None, 2),
((4, 4), (256, 64), 3),
((1, 1), None, 5),
((6, 6), None, 0),
((8, 8), (512, 1), 2),
]
_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 shape, strides, n in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
spec = TensorSpec.from_tensor(shape, strides, dtype)
kwargs = {"n": n} if n is not None else {}
test_cases.append(
TestCase(
inputs=[spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="matrix_power - OUT_OF_PLACE",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""matrix_power operator test with simplified implementation"""
def __init__(self):
super().__init__("matrix_power")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.matrix_power(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.matrix_power(*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)
# max reduces by taking maximum along dim(s) or overall
_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="Max - 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 and dim is None:
test_cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=out_spec,
comparison_target="out",
tolerance=tol,
description="Max - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""Max operator test with simplified implementation"""
def __init__(self):
super().__init__("Max")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.max(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.max(*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
# Test cases format: (in_shape, in_strides_or_None, kernel_size, stride_or_None, padding, dilation, ceil_mode)
_TEST_CASES_DATA = [
((2, 3, 16), None, 3, None, 0, 1, False),
((1, 4, 15), (60, 15, 1), 5, 1, 2, 1, True),
((2, 1, 32), None, 2, 2, 0, 1, False),
((3, 2, 7), (14, 7, 1), 3, None, 1, 1, True),
((4, 6, 31), None, 4, 2, 1, 1, False),
((2, 8, 9), (72, 9, 1), 3, 1, 0, 1, False),
]
_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 in_shape, in_strides, k, s, p, d, ceil_mode in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP[dtype]
in_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
kwargs = {"kernel_size": k, "dilation": d, "ceil_mode": ceil_mode}
if s is not None:
kwargs["stride"] = s
if p is not None:
kwargs["padding"] = p
cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="MaxPool1d - OUT_OF_PLACE",
)
)
return cases
class OpTest(BaseOperatorTest):
"""MaxPool1d operator test with simplified implementation"""
def __init__(self):
super().__init__("MaxPool1d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.max_pool1d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.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
# Test cases format: (in_shape, in_strides_or_None, kernel_size, stride_or_None, padding, dilation, ceil_mode)
_TEST_CASES_DATA = [
((2, 3, 16, 16), None, (3, 3), None, (1, 1), (1, 1), False),
((1, 4, 15, 17), (1020, 255, 17, 1), (5, 4), (2, 2), (2, 1), (1, 1), True),
((2, 1, 32, 32), None, (2, 2), (2, 2), (0, 0), (1, 1), False),
((3, 2, 7, 9), None, (3, 3), None, (1, 1), (1, 1), True),
((4, 6, 31, 29), None, (4, 4), (2, 2), (1, 1), (1, 1), False),
((2, 8, 9, 11), (1584, 198, 11, 1), (3, 2), 1, 0, (1, 1), False),
]
_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 in_shape, in_strides, k, s, p, d, ceil_mode in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP[dtype]
in_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
kwargs = {"kernel_size": k, "dilation": d, "ceil_mode": ceil_mode}
if s is not None:
kwargs["stride"] = s
if p is not None:
kwargs["padding"] = p
cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="MaxPool2d - OUT_OF_PLACE",
)
)
return cases
class OpTest(BaseOperatorTest):
"""MaxPool2d operator test with simplified implementation"""
def __init__(self):
super().__init__("MaxPool2d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.max_pool2d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.max_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
# Test cases format: (in_shape, in_strides_or_None, kernel_size, stride_or_None, padding, dilation, ceil_mode)
_TEST_CASES_DATA = [
((1, 2, 8, 8, 8), None, (2, 2, 2), None, (0, 0, 0), (1, 1, 1), False),
((2, 3, 7, 9, 5), None, (3, 2, 3), (2, 2, 1), (1, 1, 0), (1, 1, 1), True),
((1, 4, 16, 16, 6), None, (4, 4, 2), (2, 2, 1), (0, 1, 0), (1, 1, 1), False),
((2, 1, 9, 11, 7), None, (3, 3, 3), None, (1, 0, 1), (1, 1, 1), True),
((3, 2, 5, 6, 4), None, (2, 2, 2), (1, 1, 1), 0, (1, 1, 1), False),
((2, 6, 10, 9, 8), None, (3, 3, 2), (2, 1, 2), (1, 0, 1), (1, 1, 1), False),
]
_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 in_shape, in_strides, k, s, p, d, ceil_mode in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP[dtype]
in_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
kwargs = {"kernel_size": k, "dilation": d, "ceil_mode": ceil_mode}
if s is not None:
kwargs["stride"] = s
if p is not None:
kwargs["padding"] = p
cases.append(
TestCase(
inputs=[in_spec],
kwargs=kwargs,
output_spec=None,
comparison_target=None,
tolerance=tol,
description="MaxPool3d - OUT_OF_PLACE",
)
)
return cases
class OpTest(BaseOperatorTest):
"""MaxPool3d operator test with simplified implementation"""
def __init__(self):
super().__init__("MaxPool3d")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.nn.functional.max_pool3d(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.nn.functional.max_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: (shape, a_strides, b_strides)
_TEST_CASES_DATA = [
((6, 8), None, None),
((8, 4), (16, 1), None),
((5, 5), None, (10, 1)),
((3, 7), (14, 1), (14, 1)),
((10, 3), None, None),
((2, 16), (32, 1), (32, 1)),
]
_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:
shape, a_strides, b_strides = data
a_inplace = not is_broadcast(a_strides)
b_inplace = not is_broadcast(b_strides)
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 1e-3})
a_spec = TensorSpec.from_tensor(shape, a_strides, dtype)
b_spec = TensorSpec.from_tensor(shape, b_strides, dtype)
# Out-of-place
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={},
output_spec=None,
comparison_target=None,
tolerance=tol,
description="maximum - OUT",
)
)
# In-place variations
if a_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={"out": 0},
output_spec=None,
comparison_target=0,
tolerance=tol,
description="maximum - INPLACE(a)",
)
)
if b_inplace:
test_cases.append(
TestCase(
inputs=[a_spec, b_spec],
kwargs={"out": 1},
output_spec=None,
comparison_target=1,
tolerance=tol,
description="maximum - INPLACE(b)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""Maximum operator test with simplified implementation"""
def __init__(self):
super().__init__("Maximum")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.maximum(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.maximum(*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)
# mean computes average along dimension(s) or overall
_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},
infinicore.bfloat16: {"atol": 1e-2, "rtol": 5e-2},
}
_TENSOR_DTYPES = [infinicore.float16, infinicore.float32, infinicore.bfloat16]
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="Mean - 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="Mean - INPLACE(out)",
)
)
return test_cases
class OpTest(BaseOperatorTest):
"""Mean operator test with simplified implementation"""
def __init__(self):
super().__init__("Mean")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.mean(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.mean(*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)
# median can return median values (and indices when dim is provided)
_TEST_CASES_DATA = [
((9,), None, None, None, None),
((8, 8), (16, 1), 1, False, None),
((2, 3, 5), None, 2, True, (0, 1, 1)),
((1, 8), None, 0, False, None),
((16, 63), (128, 1), None, None, None),
((5, 6, 7), (210, 35, 5), 2, True, (35, 5, 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="Median - OUT_OF_PLACE",
)
)
# Note: infinicore.median returns (values, indices) when dim is provided. explicit out param for both outputs is not available in PyTorch.
return test_cases
class OpTest(BaseOperatorTest):
"""Median operator test with simplified implementation"""
def __init__(self):
super().__init__("Median")
def get_test_cases(self):
return parse_test_cases()
def torch_operator(self, *args, **kwargs):
return torch.median(*args, **kwargs)
# def infinicore_operator(self, *args, **kwargs):
# """InfiniCore implementation (operator not yet available)."""
# return infinicore.median(*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