Generate a registered OpTestCase subclass for a binary math op.
(
op_name: str,
op_fn: Callable,
shapes: List[Tuple[int, ...]] = None,
dtypes: List[torch.dtype] = None,
input_fn_a: Callable = None,
input_fn_b: Callable = None,
)
| 4282 | |
| 4283 | |
| 4284 | def _make_binary_op_test( |
| 4285 | op_name: str, |
| 4286 | op_fn: Callable, |
| 4287 | shapes: List[Tuple[int, ...]] = None, |
| 4288 | dtypes: List[torch.dtype] = None, |
| 4289 | input_fn_a: Callable = None, |
| 4290 | input_fn_b: Callable = None, |
| 4291 | ) -> type: |
| 4292 | """Generate a registered OpTestCase subclass for a binary math op.""" |
| 4293 | if shapes is None: |
| 4294 | shapes = _SHAPES_3 |
| 4295 | if dtypes is None: |
| 4296 | dtypes = _BINARY_DTYPES |
| 4297 | if input_fn_a is None: |
| 4298 | input_fn_a = _input_fn() |
| 4299 | if input_fn_b is None: |
| 4300 | input_fn_b = _input_fn() |
| 4301 | |
| 4302 | class _Test(OpTestCase): |
| 4303 | name = op_name |
| 4304 | |
| 4305 | def __init__( |
| 4306 | self, |
| 4307 | shape: Tuple[int, ...], |
| 4308 | dtype: torch.dtype, |
| 4309 | ): |
| 4310 | self.shape = shape |
| 4311 | self.dtype = dtype |
| 4312 | shape_str = "x".join(str(s) for s in shape) |
| 4313 | dtype_str = str(dtype).replace("torch.", "") |
| 4314 | self.name = f"{op_name}_{shape_str}_{dtype_str}" |
| 4315 | |
| 4316 | @classmethod |
| 4317 | def get_test_configs(cls) -> List["_Test"]: |
| 4318 | return [cls(shape=s, dtype=d) for s in shapes for d in dtypes] |
| 4319 | |
| 4320 | def create_inputs(self) -> Tuple[torch.Tensor, ...]: |
| 4321 | return input_fn_a(self.shape, self.dtype) + input_fn_b( |
| 4322 | self.shape, self.dtype |
| 4323 | ) |
| 4324 | |
| 4325 | def create_model(self) -> nn.Module: |
| 4326 | return BinaryOpModel(op_fn) |
| 4327 | |
| 4328 | _Test.__name__ = f"{op_name.title().replace('_', '')}Test" |
| 4329 | _Test.__qualname__ = _Test.__name__ |
| 4330 | return _Test |
| 4331 | |
| 4332 | |
| 4333 | # fmt: off |