|
| 1 | +import sys |
| 2 | +import os |
| 3 | + |
| 4 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 5 | + |
| 6 | +import torch |
| 7 | +import infinicore |
| 8 | +from framework.base import BaseOperatorTest, TensorSpec, TestCase |
| 9 | +from framework.runner import GenericTestRunner |
| 10 | +from framework.utils import is_broadcast |
| 11 | + |
| 12 | +# ============================================================================== |
| 13 | +# Operator-specific configuration |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +# Test cases format: (shape, dim, start, length) |
| 17 | +_TEST_CASES_DATA = [ |
| 18 | + # Basic cases |
| 19 | + ((2, 4), 0, 0, 1), |
| 20 | + ((2, 4), 1, 1, 1), |
| 21 | + ((5, 3, 2), 1, 0, 3), |
| 22 | + ((5, 3, 2), 0, 1, 3), |
| 23 | + ((4, 4, 1024, 32), 2, 1023, 1), |
| 24 | +] |
| 25 | + |
| 26 | +# Tolerance configuration |
| 27 | +_TOLERANCE_MAP = { |
| 28 | + infinicore.float16: {"atol": 0, "rtol": 0}, |
| 29 | + infinicore.float32: {"atol": 0, "rtol": 0}, |
| 30 | + infinicore.bfloat16: {"atol": 0, "rtol": 0}, |
| 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 all operation types. |
| 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 | + shape = data[0] |
| 46 | + dim = data[1] |
| 47 | + start = data[2] |
| 48 | + length = data[3] |
| 49 | + |
| 50 | + # Generate test cases for all data types |
| 51 | + for dtype in _TENSOR_DTYPES: |
| 52 | + tolerance = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 0}) |
| 53 | + |
| 54 | + # Create typed tensor specs |
| 55 | + a_spec = TensorSpec.from_tensor(shape, None, dtype) |
| 56 | + test_cases.append( |
| 57 | + TestCase( |
| 58 | + inputs=[a_spec, dim, start, length], |
| 59 | + kwargs={}, |
| 60 | + output_spec=None, |
| 61 | + comparison_target=None, # Compare output |
| 62 | + tolerance=tolerance, |
| 63 | + description=f"Narrow", |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + return test_cases |
| 68 | + |
| 69 | + |
| 70 | +class OpTest(BaseOperatorTest): |
| 71 | + """Narrow operator test with simplified implementation""" |
| 72 | + |
| 73 | + def __init__(self): |
| 74 | + super().__init__("Narrow") |
| 75 | + |
| 76 | + def get_test_cases(self): |
| 77 | + return parse_test_cases() |
| 78 | + |
| 79 | + def torch_operator(self, *args, **kwargs): |
| 80 | + """PyTorch narrow implementation""" |
| 81 | + return torch.narrow(*args, **kwargs) |
| 82 | + |
| 83 | + def infinicore_operator(self, *args, **kwargs): |
| 84 | + """InfiniCore narrow implementation""" |
| 85 | + return infinicore.narrow(*args, **kwargs) |
| 86 | + |
| 87 | + |
| 88 | +def main(): |
| 89 | + """Main entry point""" |
| 90 | + runner = GenericTestRunner(OpTest) |
| 91 | + runner.run_and_exit() |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments