|
| 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 torch.nn.functional as F |
| 8 | +import infinicore |
| 9 | +from framework.base import BaseOperatorTest, TensorSpec, TestCase |
| 10 | +from framework.runner import GenericTestRunner |
| 11 | +from framework.tensor import TensorInitializer |
| 12 | + |
| 13 | +# ============================================================================== |
| 14 | +# Operator-specific configuration |
| 15 | +# ============================================================================== |
| 16 | + |
| 17 | +# Test cases format: (input_shape, num_classes, has_weight, p, margin, reduction) |
| 18 | +_TEST_CASES_DATA = [ |
| 19 | + # Basic cases without weight - 2D inputs only |
| 20 | + ((10, 5), 5, False, 1, 1.0, "mean"), |
| 21 | + ((10, 5), 5, False, 1, 1.0, "sum"), |
| 22 | + ((10, 5), 5, False, 1, 1.0, "none"), |
| 23 | + ((8, 3), 3, False, 2, 1.0, "mean"), |
| 24 | + ((8, 3), 3, False, 2, 0.5, "sum"), |
| 25 | + # Cases with weight tensor |
| 26 | + ((10, 5), 5, True, 1, 1.0, "mean"), |
| 27 | + ((10, 5), 5, True, 1, 1.0, "sum"), |
| 28 | + ((8, 3), 3, True, 2, 1.0, "mean"), |
| 29 | + ((8, 3), 3, True, 2, 0.5, "sum"), |
| 30 | + # Edge cases - only 2D inputs |
| 31 | + ((1, 3), 3, False, 1, 1.0, "mean"), # Single sample |
| 32 | + ((5, 1), 1, False, 1, 1.0, "mean"), # Single class |
| 33 | + ((100, 10), 10, True, 1, 2.0, "mean"), # Larger tensors |
| 34 | +] |
| 35 | + |
| 36 | +# Tolerance configuration |
| 37 | +_TOLERANCE_MAP = { |
| 38 | + infinicore.float16: {"atol": 1e-3, "rtol": 1e-2}, |
| 39 | + infinicore.float32: {"atol": 1e-5, "rtol": 1e-4}, |
| 40 | + infinicore.bfloat16: {"atol": 1e-2, "rtol": 5e-2}, |
| 41 | +} |
| 42 | + |
| 43 | +# Data types to test |
| 44 | +_TENSOR_DTYPES = [infinicore.float16, infinicore.bfloat16, infinicore.float32] |
| 45 | + |
| 46 | + |
| 47 | +def parse_test_cases(): |
| 48 | + """ |
| 49 | + Parse test case data for multi_margin_loss operation. |
| 50 | + All tensors will be created on the same device. |
| 51 | + """ |
| 52 | + test_cases = [] |
| 53 | + |
| 54 | + for data in _TEST_CASES_DATA: |
| 55 | + input_shape = data[0] |
| 56 | + num_classes = data[1] |
| 57 | + has_weight = data[2] |
| 58 | + p_value = data[3] |
| 59 | + margin_value = data[4] |
| 60 | + reduction = data[5] |
| 61 | + |
| 62 | + # Generate test cases for all data types |
| 63 | + for dtype in _TENSOR_DTYPES: |
| 64 | + tolerance = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4}) |
| 65 | + |
| 66 | + # Create input tensor spec |
| 67 | + input_spec = TensorSpec.from_tensor(input_shape, dtype=dtype) |
| 68 | + |
| 69 | + # FIX: Create target as a tensor, not a scalar |
| 70 | + # For 2D input (batch_size, num_classes), target should be (batch_size,) tensor |
| 71 | + target_shape = (input_shape[0],) |
| 72 | + target_spec = TensorSpec.from_tensor( |
| 73 | + target_shape, |
| 74 | + dtype=infinicore.int64, # target must be int64 for classification |
| 75 | + init_mode=TensorInitializer.RANDINT, |
| 76 | + low=0, |
| 77 | + high=num_classes, # class indices from 0 to num_classes-1 |
| 78 | + ) |
| 79 | + |
| 80 | + base_description = "MultiMarginLoss" |
| 81 | + |
| 82 | + # Build kwargs |
| 83 | + kwargs = {"p": p_value, "margin": margin_value, "reduction": reduction} |
| 84 | + |
| 85 | + # Add weight tensor if specified |
| 86 | + if has_weight: |
| 87 | + weight_spec = TensorSpec.from_tensor( |
| 88 | + (num_classes,), dtype=dtype, init_mode=TensorInitializer.RANDOM |
| 89 | + ) |
| 90 | + kwargs["weight"] = weight_spec |
| 91 | + |
| 92 | + test_cases.append( |
| 93 | + TestCase( |
| 94 | + inputs=[input_spec, target_spec], |
| 95 | + kwargs=kwargs, |
| 96 | + output_spec=None, |
| 97 | + comparison_target=None, |
| 98 | + tolerance=tolerance, |
| 99 | + description=base_description, |
| 100 | + ) |
| 101 | + ) |
| 102 | + |
| 103 | + return test_cases |
| 104 | + |
| 105 | + |
| 106 | +class MultiMarginLossOpTest(BaseOperatorTest): |
| 107 | + """MultiMarginLoss operator test with device handling""" |
| 108 | + |
| 109 | + def __init__(self): |
| 110 | + super().__init__("MultiMarginLoss") |
| 111 | + |
| 112 | + def get_test_cases(self): |
| 113 | + return parse_test_cases() |
| 114 | + |
| 115 | + def torch_operator(self, *args, **kwargs): |
| 116 | + """PyTorch multi_margin_loss implementation with device handling""" |
| 117 | + return F.multi_margin_loss(*args, **kwargs) |
| 118 | + |
| 119 | + def infinicore_operator(self, *args, **kwargs): |
| 120 | + """InfiniCore multi_margin_loss implementation""" |
| 121 | + return None |
| 122 | + |
| 123 | + |
| 124 | +def main(): |
| 125 | + """Main entry point""" |
| 126 | + runner = GenericTestRunner(MultiMarginLossOpTest) |
| 127 | + runner.run_and_exit() |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
0 commit comments