|
| 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, input_strides, output_strides) |
| 16 | +# Causal softmax is a single-input function that applies causal masking before softmax |
| 17 | +_TEST_CASES_DATA = [ |
| 18 | + # Basic 2D causal softmax |
| 19 | + (TestCase.BOTH, (3, 3), None, None), |
| 20 | + (TestCase.BOTH, (32, 512), None, None), |
| 21 | + # Strided tensors |
| 22 | + (TestCase.BOTH, (32, 512), (1024, 1), (1024, 1)), |
| 23 | + # 3D causal softmax |
| 24 | + (TestCase.BOTH, (32, 5, 5), None, None), |
| 25 | + (TestCase.BOTH, (32, 20, 512), None, None), |
| 26 | + (TestCase.BOTH, (32, 20, 512), (20480, 512, 1), None), |
| 27 | + (TestCase.BOTH, (28, 15, 15), None, None), |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +def parse_test_cases(data): |
| 32 | + """ |
| 33 | + Parse causal_softmax test case data according to format: |
| 34 | + (operation_mode, shape, input_strides, output_strides) |
| 35 | + """ |
| 36 | + operation_mode = data[0] |
| 37 | + shape = data[1] |
| 38 | + input_strides = data[2] if len(data) > 2 else None |
| 39 | + output_strides = data[3] if len(data) > 3 else None |
| 40 | + |
| 41 | + # Create input specifications |
| 42 | + inputs = [] |
| 43 | + |
| 44 | + # Tensor input |
| 45 | + if input_strides is not None: |
| 46 | + inputs.append(TensorSpec.from_strided_tensor(shape, input_strides)) |
| 47 | + else: |
| 48 | + inputs.append(TensorSpec.from_tensor(shape)) |
| 49 | + |
| 50 | + # Output tensor |
| 51 | + if output_strides is not None: |
| 52 | + output = TensorSpec.from_strided_tensor(shape, output_strides) |
| 53 | + else: |
| 54 | + output = TensorSpec.from_tensor(shape) |
| 55 | + |
| 56 | + return TestCase(operation_mode, inputs, output) |
| 57 | + |
| 58 | + |
| 59 | +# Parse test cases |
| 60 | +_TEST_CASES = [parse_test_cases(data) for data in _TEST_CASES_DATA] |
| 61 | + |
| 62 | +# Data types |
| 63 | +_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32] |
| 64 | + |
| 65 | +# Tolerance |
| 66 | +_TOLERANCE_MAP = { |
| 67 | + infinicore.float16: {"atol": 1e-3, "rtol": 1e-2}, |
| 68 | + infinicore.float32: {"atol": 3e-5, "rtol": 1e-5}, |
| 69 | + infinicore.bfloat16: {"atol": 5e-3, "rtol": 5e-2}, |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +class OpTest(BaseOperatorTest): |
| 74 | + """CausalSoftmax test with simplified test case parsing""" |
| 75 | + |
| 76 | + def __init__(self): |
| 77 | + super().__init__("CausalSoftmax") |
| 78 | + |
| 79 | + def get_test_cases(self): |
| 80 | + return _TEST_CASES |
| 81 | + |
| 82 | + def get_tensor_dtypes(self): |
| 83 | + return _TENSOR_DTYPES |
| 84 | + |
| 85 | + def get_tolerance_map(self): |
| 86 | + return _TOLERANCE_MAP |
| 87 | + |
| 88 | + def torch_operator(self, input, out=None, **kwargs): |
| 89 | + # Causal softmax implementation: apply causal mask then softmax |
| 90 | + dtype = input.dtype |
| 91 | + |
| 92 | + # Create causal mask |
| 93 | + mask = torch.tril(torch.ones_like(input), diagonal=-1).flip(dims=[-2, -1]) |
| 94 | + masked = torch.where(mask == 1, -torch.inf, input.to(torch.float32)) |
| 95 | + |
| 96 | + result = torch.nn.functional.softmax(masked, dim=-1, dtype=dtype) |
| 97 | + |
| 98 | + if out is not None: |
| 99 | + out.copy_(result) |
| 100 | + return out |
| 101 | + return result |
| 102 | + |
| 103 | + def infinicore_operator(self, input, out=None, **kwargs): |
| 104 | + return infinicore.causal_softmax(input, out=out) |
| 105 | + |
| 106 | + |
| 107 | +def main(): |
| 108 | + """Main entry point""" |
| 109 | + runner = GenericTestRunner(OpTest) |
| 110 | + runner.run_and_exit() |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments