-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding support to isinf, isnan and nonzero (#196)
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
__all__ = [ | ||
'OnnxIsInf', | ||
] | ||
|
||
import torch | ||
from torch import nn | ||
|
||
from onnx2torch.node_converters.registry import add_converter | ||
from onnx2torch.onnx_graph import OnnxGraph | ||
from onnx2torch.onnx_node import OnnxNode | ||
from onnx2torch.utils.common import OnnxMapping | ||
from onnx2torch.utils.common import OnnxToTorchModule | ||
from onnx2torch.utils.common import OperationConverterResult | ||
|
||
|
||
class OnnxIsInf(nn.Module, OnnxToTorchModule): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: | ||
return torch.isinf(input_tensor) | ||
|
||
|
||
@add_converter(operation_type='IsInf', version=10) | ||
def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: | ||
torch_module = OnnxIsInf() | ||
|
||
return OperationConverterResult( | ||
torch_module=torch_module, | ||
onnx_mapping=OnnxMapping( | ||
inputs=(node.input_values[0],), | ||
outputs=node.output_values, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
__all__ = [ | ||
'OnnxIsNaN', | ||
] | ||
|
||
import torch | ||
from torch import nn | ||
|
||
from onnx2torch.node_converters.registry import add_converter | ||
from onnx2torch.onnx_graph import OnnxGraph | ||
from onnx2torch.onnx_node import OnnxNode | ||
from onnx2torch.utils.common import OnnxMapping | ||
from onnx2torch.utils.common import OnnxToTorchModule | ||
from onnx2torch.utils.common import OperationConverterResult | ||
|
||
|
||
class OnnxIsNaN(nn.Module, OnnxToTorchModule): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: | ||
return torch.isnan(input_tensor) | ||
|
||
|
||
@add_converter(operation_type='IsNaN', version=13) | ||
def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: | ||
torch_module = OnnxIsNaN() | ||
|
||
return OperationConverterResult( | ||
torch_module=torch_module, | ||
onnx_mapping=OnnxMapping( | ||
inputs=(node.input_values[0],), | ||
outputs=node.output_values, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
__all__ = [ | ||
'OnnxNonZero', | ||
] | ||
|
||
import torch | ||
from torch import nn | ||
|
||
from onnx2torch.node_converters.registry import add_converter | ||
from onnx2torch.onnx_graph import OnnxGraph | ||
from onnx2torch.onnx_node import OnnxNode | ||
from onnx2torch.utils.common import OnnxMapping | ||
from onnx2torch.utils.common import OnnxToTorchModule | ||
from onnx2torch.utils.common import OperationConverterResult | ||
|
||
|
||
class OnnxNonZero(nn.Module, OnnxToTorchModule): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: | ||
return torch.nonzero(input_tensor) | ||
|
||
|
||
@add_converter(operation_type='NonZero', version=13) | ||
def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: | ||
torch_module = OnnxNonZero() | ||
|
||
return OperationConverterResult( | ||
torch_module=torch_module, | ||
onnx_mapping=OnnxMapping( | ||
inputs=(node.input_values[0],), | ||
outputs=node.output_values, | ||
), | ||
) |