Skip to content

Commit ad03640

Browse files
wschinlinkerzhang
authored andcommitted
Add IsInf to detect infinity values (#1884)
* Add IsInf * Add test files * Fix attribute names * Format code * Update doc * Add coverage change
1 parent ad73134 commit ad03640

15 files changed

Lines changed: 296 additions & 1 deletion

File tree

docs/Changelog.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9473,6 +9473,46 @@ This version of the operator has been available since version 10 of the default
94739473
<dd>Constrain output mask types to boolean tensors.</dd>
94749474
</dl>
94759475

9476+
### <a name="IsInf-10"></a>**IsInf-10**</a>
9477+
9478+
Map infinity to true and other values to false.
9479+
9480+
#### Version
9481+
9482+
This version of the operator has been available since version 10 of the default ONNX operator set.
9483+
9484+
#### Attributes
9485+
9486+
<dl>
9487+
<dt><tt>detect_negative</tt> : int (default is 1)</dt>
9488+
<dd>(Optional) Whether map negative infinity to true. Default to 1 so that negative infinity induces true. Set this attribute to 0 if negative infinity should be mapped to false.</dd>
9489+
<dt><tt>detect_positive</tt> : int (default is 1)</dt>
9490+
<dd>(Optional) Whether map positive infinity to true. Default to 1 so that positive infinity induces true. Set this attribute to 0 if positive infinity should be mapped to false.</dd>
9491+
</dl>
9492+
9493+
#### Inputs
9494+
9495+
<dl>
9496+
<dt><tt>X</tt> : T1</dt>
9497+
<dd>input</dd>
9498+
</dl>
9499+
9500+
#### Outputs
9501+
9502+
<dl>
9503+
<dt><tt>Y</tt> : T2</dt>
9504+
<dd>output</dd>
9505+
</dl>
9506+
9507+
#### Type Constraints
9508+
9509+
<dl>
9510+
<dt><tt>T1</tt> : tensor(float), tensor(double)</dt>
9511+
<dd>Constrain input types to float tensors.</dd>
9512+
<dt><tt>T2</tt> : tensor(bool)</dt>
9513+
<dd>Constrain output types to boolean tensors.</dd>
9514+
</dl>
9515+
94769516
### <a name="MatMulInteger-10"></a>**MatMulInteger-10**</a>
94779517

94789518
Matrix product that behaves like numpy.matmul: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matmul.html.

docs/Operators.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* <a href="#Identity">Identity</a>
5454
* <a href="#If">If</a>
5555
* <a href="#InstanceNormalization">InstanceNormalization</a>
56+
* <a href="#IsInf">IsInf</a>
5657
* <a href="#IsNaN">IsNaN</a>
5758
* <a href="#LRN">LRN</a>
5859
* <a href="#LSTM">LSTM</a>
@@ -5074,6 +5075,105 @@ expect(node, inputs=[x, s, bias], outputs=[y],
50745075
</details>
50755076

50765077

5078+
### <a name="IsInf"></a><a name="isinf">**IsInf**</a>
5079+
5080+
Map infinity to true and other values to false.
5081+
5082+
#### Version
5083+
5084+
This version of the operator has been available since version 10 of the default ONNX operator set.
5085+
5086+
#### Attributes
5087+
5088+
<dl>
5089+
<dt><tt>detect_negative</tt> : int (default is 1)</dt>
5090+
<dd>(Optional) Whether map negative infinity to true. Default to 1 so that negative infinity induces true. Set this attribute to 0 if negative infinity should be mapped to false.</dd>
5091+
<dt><tt>detect_positive</tt> : int (default is 1)</dt>
5092+
<dd>(Optional) Whether map positive infinity to true. Default to 1 so that positive infinity induces true. Set this attribute to 0 if positive infinity should be mapped to false.</dd>
5093+
</dl>
5094+
5095+
#### Inputs
5096+
5097+
<dl>
5098+
<dt><tt>X</tt> : T1</dt>
5099+
<dd>input</dd>
5100+
</dl>
5101+
5102+
#### Outputs
5103+
5104+
<dl>
5105+
<dt><tt>Y</tt> : T2</dt>
5106+
<dd>output</dd>
5107+
</dl>
5108+
5109+
#### Type Constraints
5110+
5111+
<dl>
5112+
<dt><tt>T1</tt> : tensor(float), tensor(double)</dt>
5113+
<dd>Constrain input types to float tensors.</dd>
5114+
<dt><tt>T2</tt> : tensor(bool)</dt>
5115+
<dd>Constrain output types to boolean tensors.</dd>
5116+
</dl>
5117+
5118+
5119+
#### Examples
5120+
5121+
<details>
5122+
<summary>infinity</summary>
5123+
5124+
```python
5125+
node = onnx.helper.make_node('IsInf',
5126+
inputs=['x'],
5127+
outputs=['y'],
5128+
)
5129+
5130+
x = np.array([-1.2, np.nan, np.inf, 2.8, np.NINF, np.inf],
5131+
dtype=np.float32)
5132+
y = np.isinf(x)
5133+
expect(node, inputs=[x], outputs=[y], name='test_isinf')
5134+
```
5135+
5136+
</details>
5137+
5138+
5139+
<details>
5140+
<summary>negative_infinity_only</summary>
5141+
5142+
```python
5143+
node = onnx.helper.make_node('IsInf',
5144+
inputs=['x'],
5145+
outputs=['y'],
5146+
detect_positive=0
5147+
)
5148+
5149+
x = np.array([-1.7, np.nan, np.inf, -3.6, np.NINF, np.inf],
5150+
dtype=np.float32)
5151+
y = np.isneginf(x)
5152+
expect(node, inputs=[x], outputs=[y], name='test_isinf_negative')
5153+
```
5154+
5155+
</details>
5156+
5157+
5158+
<details>
5159+
<summary>positive_infinity_only</summary>
5160+
5161+
```python
5162+
node = onnx.helper.make_node('IsInf',
5163+
inputs=['x'],
5164+
outputs=['y'],
5165+
detect_negative=0
5166+
)
5167+
5168+
x = np.array([-1.7, np.nan, np.inf, 3.6, np.NINF, np.inf],
5169+
dtype=np.float32)
5170+
y = np.isposinf(x)
5171+
expect(node, inputs=[x], outputs=[y], name='test_isinf_positive')
5172+
```
5173+
5174+
</details>
5175+
5176+
50775177
### <a name="IsNaN"></a><a name="isnan">**IsNaN**</a>
50785178

50795179
Returns which elements of the input are NaN.

docs/TestCoverage.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* [Overall Test Coverage](#overall-test-coverage)
66
# Node Test Coverage
77
## Summary
8-
Node tests have covered 114/127 (89.76%, 5 generators excluded) common operators.
8+
Node tests have covered 115/128 (89.84%, 5 generators excluded) common operators.
99

1010
Node tests have covered 0/0 (N/A) experimental operators.
1111

@@ -2689,6 +2689,60 @@ expect(node, inputs=[x, s, bias], outputs=[y],
26892689
</details>
26902690

26912691

2692+
### IsInf
2693+
There are 3 test cases, listed as following:
2694+
<details>
2695+
<summary>infinity</summary>
2696+
2697+
```python
2698+
node = onnx.helper.make_node('IsInf',
2699+
inputs=['x'],
2700+
outputs=['y'],
2701+
)
2702+
2703+
x = np.array([-1.2, np.nan, np.inf, 2.8, np.NINF, np.inf],
2704+
dtype=np.float32)
2705+
y = np.isinf(x)
2706+
expect(node, inputs=[x], outputs=[y], name='test_isinf')
2707+
```
2708+
2709+
</details>
2710+
<details>
2711+
<summary>negative_infinity_only</summary>
2712+
2713+
```python
2714+
node = onnx.helper.make_node('IsInf',
2715+
inputs=['x'],
2716+
outputs=['y'],
2717+
detect_positive=0
2718+
)
2719+
2720+
x = np.array([-1.7, np.nan, np.inf, -3.6, np.NINF, np.inf],
2721+
dtype=np.float32)
2722+
y = np.isneginf(x)
2723+
expect(node, inputs=[x], outputs=[y], name='test_isinf_negative')
2724+
```
2725+
2726+
</details>
2727+
<details>
2728+
<summary>positive_infinity_only</summary>
2729+
2730+
```python
2731+
node = onnx.helper.make_node('IsInf',
2732+
inputs=['x'],
2733+
outputs=['y'],
2734+
detect_negative=0
2735+
)
2736+
2737+
x = np.array([-1.7, np.nan, np.inf, 3.6, np.NINF, np.inf],
2738+
dtype=np.float32)
2739+
y = np.isposinf(x)
2740+
expect(node, inputs=[x], outputs=[y], name='test_isinf_positive')
2741+
```
2742+
2743+
</details>
2744+
2745+
26922746
### IsNaN
26932747
There are 1 test cases, listed as following:
26942748
<details>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
from __future__ import unicode_literals
5+
6+
import numpy as np # type: ignore
7+
8+
import onnx
9+
from ..base import Base
10+
from . import expect
11+
12+
13+
class IsInf(Base):
14+
15+
@staticmethod
16+
def export_infinity(): # type: () -> None
17+
node = onnx.helper.make_node('IsInf',
18+
inputs=['x'],
19+
outputs=['y'],
20+
)
21+
22+
x = np.array([-1.2, np.nan, np.inf, 2.8, np.NINF, np.inf],
23+
dtype=np.float32)
24+
y = np.isinf(x)
25+
expect(node, inputs=[x], outputs=[y], name='test_isinf')
26+
27+
@staticmethod
28+
def export_positive_infinity_only(): # type: () -> None
29+
node = onnx.helper.make_node('IsInf',
30+
inputs=['x'],
31+
outputs=['y'],
32+
detect_negative=0
33+
)
34+
35+
x = np.array([-1.7, np.nan, np.inf, 3.6, np.NINF, np.inf],
36+
dtype=np.float32)
37+
y = np.isposinf(x)
38+
expect(node, inputs=[x], outputs=[y], name='test_isinf_positive')
39+
40+
@staticmethod
41+
def export_negative_infinity_only(): # type: () -> None
42+
node = onnx.helper.make_node('IsInf',
43+
inputs=['x'],
44+
outputs=['y'],
45+
detect_positive=0
46+
)
47+
48+
x = np.array([-1.7, np.nan, np.inf, -3.6, np.NINF, np.inf],
49+
dtype=np.float32)
50+
y = np.isneginf(x)
51+
expect(node, inputs=[x], outputs=[y], name='test_isinf_negative')
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
 backend-test:=
2+
3+
xy"IsInf
4+
test_isinfZ
5+
x
6+
7+

8+
b
9+
y
10+
11+
 
12+
B
33 Bytes
Binary file not shown.
15 Bytes
Binary file not shown.
116 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)