|
| 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 | +# ============================================================================== |
| 12 | +# Operator-specific configuration |
| 13 | +# ============================================================================== |
| 14 | + |
| 15 | +# Test cases format: (operation_mode, shape, a_strides, b_strides, c_strides) |
| 16 | +_TEST_CASES_DATA = [ |
| 17 | + (TestCase.BOTH, (13, 4), None, None, None), |
| 18 | + (TestCase.BOTH, (13, 4), (10, 1), (10, 1), (10, 1)), |
| 19 | + (TestCase.BOTH, (13, 4), (0, 1), None, None), |
| 20 | + (TestCase.BOTH, (13, 4, 4), None, None, None), |
| 21 | + (TestCase.BOTH, (13, 4, 4), (20, 4, 1), (20, 4, 1), (20, 4, 1)), |
| 22 | + (TestCase.BOTH, (13, 4, 4), (4, 0, 1), (0, 4, 1), None), |
| 23 | + (TestCase.BOTH, (16, 5632), None, None, None), |
| 24 | + (TestCase.BOTH, (16, 5632), (13312, 1), (13312, 1), (13312, 1)), |
| 25 | +] |
| 26 | + |
| 27 | + |
| 28 | +def parse_test_cases(data): |
| 29 | + """ |
| 30 | + Parse mul test case data according to format: |
| 31 | + (operation_mode, shape, a_strides, b_strides, c_strides) |
| 32 | + """ |
| 33 | + operation_mode = data[0] |
| 34 | + shape = data[1] |
| 35 | + a_strides = data[2] if len(data) > 2 else None |
| 36 | + b_strides = data[3] if len(data) > 3 else None |
| 37 | + c_strides = data[4] if len(data) > 4 else None |
| 38 | + |
| 39 | + # Create input specifications |
| 40 | + inputs = [] |
| 41 | + |
| 42 | + # Input tensor a |
| 43 | + if a_strides is not None: |
| 44 | + inputs.append(TensorSpec.from_strided_tensor(shape, a_strides)) |
| 45 | + else: |
| 46 | + inputs.append(TensorSpec.from_tensor(shape)) |
| 47 | + |
| 48 | + # Input tensor b (same shape as a) |
| 49 | + if b_strides is not None: |
| 50 | + inputs.append(TensorSpec.from_strided_tensor(shape, b_strides)) |
| 51 | + else: |
| 52 | + inputs.append(TensorSpec.from_tensor(shape)) |
| 53 | + |
| 54 | + # Output tensor |
| 55 | + if c_strides is not None: |
| 56 | + output = TensorSpec.from_strided_tensor(shape, c_strides) |
| 57 | + else: |
| 58 | + output = TensorSpec.from_tensor(shape) |
| 59 | + |
| 60 | + return TestCase(operation_mode, inputs, output) |
| 61 | + |
| 62 | + |
| 63 | +# Parse test cases |
| 64 | +_TEST_CASES = [parse_test_cases(data) for data in _TEST_CASES_DATA] |
| 65 | + |
| 66 | +# Data types |
| 67 | +_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32] |
| 68 | + |
| 69 | +# Tolerance |
| 70 | +_TOLERANCE_MAP = { |
| 71 | + infinicore.float16: {"atol": 0, "rtol": 1e-2}, |
| 72 | + infinicore.float32: {"atol": 0, "rtol": 1e-3}, |
| 73 | + infinicore.bfloat16: {"atol": 0, "rtol": 5e-2}, |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +class OpTest(BaseOperatorTest): |
| 78 | + """Mul test with simplified test case parsing""" |
| 79 | + |
| 80 | + def __init__(self): |
| 81 | + super().__init__("Mul") |
| 82 | + |
| 83 | + def get_test_cases(self): |
| 84 | + return _TEST_CASES |
| 85 | + |
| 86 | + def get_tensor_dtypes(self): |
| 87 | + return _TENSOR_DTYPES |
| 88 | + |
| 89 | + def get_tolerance_map(self): |
| 90 | + return _TOLERANCE_MAP |
| 91 | + |
| 92 | + def torch_operator(self, a, b, out=None, **kwargs): |
| 93 | + return torch.mul(a, b, out=out) |
| 94 | + |
| 95 | + def infinicore_operator(self, a, b, out=None, **kwargs): |
| 96 | + return infinicore.mul(a, b, out=out) |
| 97 | + |
| 98 | + |
| 99 | +def main(): |
| 100 | + """Main entry point""" |
| 101 | + runner = GenericTestRunner(OpTest) |
| 102 | + runner.run_and_exit() |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments