Exemple #1
0
    def test_clip_aten_fallback_due_exception(self):
        def bad_clamp(g, self, min, max):
            return _onnx_unsupported("Bad boy!")

        class MyClip(torch.nn.Module):
            def forward(self, x):
                return torch.clamp(x, min=-0.5, max=0.5)

        onnx_model = export_to_onnx(
            MyClip(),
            torch.randn(3, 4, requires_grad=True),
            custom_ops=[custom_op("aten::clamp", bad_clamp, 9)],
            operator_export_type=OperatorExportTypes.ONNX_ATEN_FALLBACK,
        )
        self.assertAtenOp(onnx_model, "clamp", "Tensor")
    def test_clip_aten_fallback_due_exception(self):
        x = torch.randn(3, 4, requires_grad=True)

        def bad_clamp(g, self, min, max):
            return _onnx_unsupported("Bad boy!")

        class MyClip(torch.nn.Module):
            def forward(self, x):
                return torch.clamp(x, min=-0.5, max=0.5)

        f = io.BytesIO()
        with custom_op("aten::clamp", bad_clamp, 9):
            torch.onnx.export(MyClip(), x, f,
                              operator_export_type=OperatorExportTypes.ONNX_ATEN_FALLBACK)
        onnx_model = onnx.load_from_string(f.getvalue())
        self.assertAtenOp(onnx_model, "clamp", "Tensor")