Generate a registered OpTestCase subclass for tril or triu.
(mode: str)
| 4745 | |
| 4746 | |
| 4747 | def _make_triangular_test(mode: str) -> type: |
| 4748 | """Generate a registered OpTestCase subclass for tril or triu.""" |
| 4749 | |
| 4750 | class _Test(OpTestCase): |
| 4751 | name = mode |
| 4752 | |
| 4753 | def __init__( |
| 4754 | self, |
| 4755 | shape: Tuple[int, ...] = (4, 4), |
| 4756 | diagonal: int = 0, |
| 4757 | dtype: torch.dtype = torch.float32, |
| 4758 | ): |
| 4759 | self.shape = shape |
| 4760 | self.diagonal = diagonal |
| 4761 | self.dtype = dtype |
| 4762 | shape_str = "x".join(str(s) for s in shape) |
| 4763 | dtype_str = str(dtype).replace("torch.", "") |
| 4764 | diag_str = f"d{diagonal}" if diagonal != 0 else "" |
| 4765 | self.name = f"{mode}_{shape_str}_{dtype_str}{diag_str}" |
| 4766 | |
| 4767 | @classmethod |
| 4768 | def get_test_configs(cls) -> List["_Test"]: |
| 4769 | return [cls(**c) for c in _TRIANGULAR_CONFIGS] |
| 4770 | |
| 4771 | def create_inputs(self) -> Tuple[torch.Tensor, ...]: |
| 4772 | return (torch.randn(self.shape, dtype=self.dtype),) |
| 4773 | |
| 4774 | def create_model(self) -> nn.Module: |
| 4775 | return TriangularModel(mode=mode, diagonal=self.diagonal) |
| 4776 | |
| 4777 | _Test.__name__ = f"{mode.title()}Test" |
| 4778 | _Test.__qualname__ = _Test.__name__ |
| 4779 | return _Test |
| 4780 | |
| 4781 | |
| 4782 | TrilTest = _make_triangular_test("tril") |