|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 5 | + |
| 6 | +import torch |
| 7 | +from framework.base import BaseOperatorTest, TensorSpec, TestCase |
| 8 | +from framework.runner import GenericTestRunner |
| 9 | +from framework.utils import is_broadcast |
| 10 | + |
| 11 | +import infinicore |
| 12 | + |
| 13 | +# ============================================================================== |
| 14 | +# Operator-specific configuration |
| 15 | +# ============================================================================== |
| 16 | +_TEST_CASES_DATA = [ |
| 17 | + # bs, n, in_features, out_features, bias |
| 18 | + (1, 5, 2048, 5632, True, None, None, None), |
| 19 | + (1, 1, 2048, 32000, False, None, None, None), |
| 20 | + (2, 5, 2048, 5632, True, None, None, None), |
| 21 | + (2, 5, 256, 2048, False, None, None, None), |
| 22 | + (None, 5, 256, 2048, False, None, None, None), |
| 23 | + (None, 1, 2048, 5632, True, None, None, None), |
| 24 | +] |
| 25 | + |
| 26 | +# Tolerance configuration |
| 27 | +_TOLERANCE_MAP = { |
| 28 | + infinicore.float16: {"atol": 0, "rtol": 1e-2}, |
| 29 | + infinicore.float32: {"atol": 0, "rtol": 1e-3}, |
| 30 | + infinicore.bfloat16: {"atol": 0, "rtol": 5e-2}, |
| 31 | +} |
| 32 | + |
| 33 | +# Data types to test |
| 34 | +_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32] |
| 35 | + |
| 36 | + |
| 37 | +def parse_test_cases(): |
| 38 | + """ |
| 39 | + Parse test case data and return list of TestCase objects for linear operation. |
| 40 | + Each test case contains all necessary information for execution and validation. |
| 41 | + """ |
| 42 | + test_cases = [] |
| 43 | + |
| 44 | + for data in _TEST_CASES_DATA: |
| 45 | + bs = data[0] |
| 46 | + n, in_features, out_features = data[1], data[2], data[3] |
| 47 | + bias = data[4] |
| 48 | + input_strides = data[5] if len(data) > 5 else None |
| 49 | + weight_strides = data[6] if len(data) > 6 else None |
| 50 | + out_strides = data[7] if len(data) > 7 else None |
| 51 | + |
| 52 | + # Determine shapes based on batch dimension |
| 53 | + if bs is None: |
| 54 | + input_shape = (n, in_features) |
| 55 | + weight_shape = (out_features, in_features) |
| 56 | + out_shape = (n, out_features) |
| 57 | + else: |
| 58 | + input_shape = (bs, n, in_features) |
| 59 | + weight_shape = (out_features, in_features) |
| 60 | + out_shape = (bs, n, out_features) |
| 61 | + |
| 62 | + if bias is True: |
| 63 | + bias_shape = (out_features,) |
| 64 | + else: |
| 65 | + bias_shape = None |
| 66 | + |
| 67 | + # Check if tensors support in-place operations |
| 68 | + c_supports_inplace = not is_broadcast(out_shape) |
| 69 | + |
| 70 | + # Generate test cases for all data types |
| 71 | + for dtype in _TENSOR_DTYPES: |
| 72 | + tolerance = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 1e-3}) |
| 73 | + |
| 74 | + # Create typed tensor specs |
| 75 | + input_spec = TensorSpec.from_tensor(input_shape, input_strides, dtype) |
| 76 | + weight_spec = TensorSpec.from_tensor(weight_shape, weight_strides, dtype) |
| 77 | + out_spec = TensorSpec.from_tensor(out_shape, out_strides, dtype) |
| 78 | + |
| 79 | + if bias_shape is not None: |
| 80 | + bias_spec = TensorSpec.from_tensor(bias_shape, None, dtype) |
| 81 | + else: |
| 82 | + bias_spec = None |
| 83 | + |
| 84 | + # Test Case 1: Out-of-place (return value) |
| 85 | + test_cases.append( |
| 86 | + TestCase( |
| 87 | + inputs=[input_spec, weight_spec, bias_spec], |
| 88 | + kwargs={}, |
| 89 | + output_spec=None, |
| 90 | + comparison_target=None, |
| 91 | + tolerance=tolerance, |
| 92 | + description=f"Linear - OUT_OF_PLACE", |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | + # Test Case 2: In-place with explicit output tensor (Linear(a, b, out=c)) |
| 97 | + if c_supports_inplace: |
| 98 | + test_cases.append( |
| 99 | + TestCase( |
| 100 | + inputs=[input_spec, weight_spec, bias_spec], |
| 101 | + kwargs=None, |
| 102 | + output_spec=out_spec, # Specify the output tensor spec |
| 103 | + comparison_target="out", |
| 104 | + tolerance=tolerance, |
| 105 | + description=f"Linear - INPLACE(out)", |
| 106 | + ) |
| 107 | + ) |
| 108 | + |
| 109 | + return test_cases |
| 110 | + |
| 111 | + |
| 112 | +class OpTest(BaseOperatorTest): |
| 113 | + """Linear operator test with simplified implementation""" |
| 114 | + |
| 115 | + def __init__(self): |
| 116 | + super().__init__("Linear") |
| 117 | + |
| 118 | + def get_test_cases(self): |
| 119 | + return parse_test_cases() |
| 120 | + |
| 121 | + def torch_operator(self, *args, **kwargs): |
| 122 | + """PyTorch linear implementation""" |
| 123 | + return torch.nn.functional.linear(*args, **kwargs) |
| 124 | + |
| 125 | + def infinicore_operator(self, *args, **kwargs): |
| 126 | + """InfiniCore linear implementation""" |
| 127 | + return infinicore.nn.functional.linear(*args, **kwargs) |
| 128 | + |
| 129 | + |
| 130 | +def main(): |
| 131 | + """Main entry point""" |
| 132 | + runner = GenericTestRunner(OpTest) |
| 133 | + runner.run_and_exit() |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main() |
0 commit comments