Skip to content

Commit

Permalink
Use randint instead of randn to prevent undefined cast (float to uint) (
Browse files Browse the repository at this point in the history
onnx#4804)

* use randint instead of randn to prevent undefined cast float to uint

Signed-off-by: jcwchen <[email protected]>

* use unsigned integer to cover more cases

Signed-off-by: jcwchen <[email protected]>

* introduce create_random_int_input_for_cast

Signed-off-by: jcwchen <[email protected]>

* improve by reviews

Signed-off-by: jcwchen <[email protected]>

* set correct boundary

Signed-off-by: jcwchen <[email protected]>

* added for int8, int16, int32, int64

Signed-off-by: jcwchen <[email protected]>

* fix bug test case

Signed-off-by: jcwchen <[email protected]>

---------

Signed-off-by: jcwchen <[email protected]>
  • Loading branch information
jcwchen authored Feb 16, 2023
1 parent e313cc1 commit a90d702
Show file tree
Hide file tree
Showing 50 changed files with 121 additions and 82 deletions.
54 changes: 27 additions & 27 deletions docs/Operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -2651,14 +2651,14 @@ node = onnx.helper.make_node(
)

# 2d
x = np.random.randn(3, 4).astype(np.int32)
y = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = create_random_int((3, 4), np.int32)
z = np.bitwise_and(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_and_i32_2d")

# 3d
x = np.random.randn(3, 4, 5).astype(np.int16)
y = np.random.randn(3, 4, 5).astype(np.int16)
x = create_random_int((3, 4, 5), np.int16)
y = create_random_int((3, 4, 5), np.int16)
z = np.bitwise_and(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_and_i16_3d")
```
Expand All @@ -2677,16 +2677,16 @@ node = onnx.helper.make_node(
)

# 3d vs 1d
x = np.random.randn(3, 4, 5).astype(np.uint64)
y = np.random.randn(5).astype(np.uint64)
x = create_random_int((3, 4, 5), np.uint64)
y = create_random_int((5,), np.uint64)
z = np.bitwise_and(x, y)
expect(
node, inputs=[x, y], outputs=[z], name="test_bitwise_and_ui64_bcast_3v1d"
)

# 4d vs 3d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
y = np.random.randn(4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = create_random_int((4, 5, 6), np.uint8)
z = np.bitwise_and(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_and_ui8_bcast_4v3d")
```
Expand Down Expand Up @@ -2737,17 +2737,17 @@ node = onnx.helper.make_node(
)

# 2d
x = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = np.bitwise_not(x)
expect(node, inputs=[x], outputs=[y], name="test_bitwise_not_2d")

# 3d
x = np.random.randn(3, 4, 5).astype(np.uint16)
x = create_random_int((3, 4, 5), np.uint16)
y = np.bitwise_not(x)
expect(node, inputs=[x], outputs=[y], name="test_bitwise_not_3d")

# 4d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = np.bitwise_not(x)
expect(node, inputs=[x], outputs=[y], name="test_bitwise_not_4d")
```
Expand Down Expand Up @@ -2802,14 +2802,14 @@ node = onnx.helper.make_node(
outputs=["bitwiseor"],
)
# 2d
x = np.random.randn(3, 4).astype(np.int32)
y = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = create_random_int((3, 4), np.int32)
z = np.bitwise_or(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_or_i32_2d")

# 4d
x = np.random.randn(3, 4, 5, 6).astype(np.int8)
y = np.random.randn(3, 4, 5, 6).astype(np.int8)
x = create_random_int((3, 4, 5, 6), np.int8)
y = create_random_int((3, 4, 5, 6), np.int8)
z = np.bitwise_or(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_or_i16_4d")
```
Expand All @@ -2828,14 +2828,14 @@ node = onnx.helper.make_node(
)

# 3d vs 1d
x = np.random.randn(3, 4, 5).astype(np.uint64)
y = np.random.randn(5).astype(np.uint64)
x = create_random_int((3, 4, 5), np.uint64)
y = create_random_int((5,), np.uint64)
z = np.bitwise_or(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_or_ui64_bcast_3v1d")

# 4d vs 3d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
y = np.random.randn(4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = create_random_int((4, 5, 6), np.uint8)
z = np.bitwise_or(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_or_ui8_bcast_4v3d")
```
Expand Down Expand Up @@ -2891,16 +2891,16 @@ node = onnx.helper.make_node(
)

# 3d vs 1d
x = np.random.randn(3, 4, 5).astype(np.uint64)
y = np.random.randn(5).astype(np.uint64)
x = create_random_int((3, 4, 5), np.uint64)
y = create_random_int((5,), np.uint64)
z = np.bitwise_xor(x, y)
expect(
node, inputs=[x, y], outputs=[z], name="test_bitwise_xor_ui64_bcast_3v1d"
)

# 4d vs 3d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
y = np.random.randn(4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = create_random_int((4, 5, 6), np.uint8)
z = np.bitwise_xor(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_xor_ui8_bcast_4v3d")
```
Expand All @@ -2919,14 +2919,14 @@ node = onnx.helper.make_node(
)

# 2d
x = np.random.randn(3, 4).astype(np.int32)
y = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = create_random_int((3, 4), np.int32)
z = np.bitwise_xor(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_xor_i32_2d")

# 3d
x = np.random.randn(3, 4, 5).astype(np.int16)
y = np.random.randn(3, 4, 5).astype(np.int16)
x = create_random_int((3, 4, 5), np.int16)
y = create_random_int((3, 4, 5), np.int16)
z = np.bitwise_xor(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_xor_i16_3d")
```
Expand Down
54 changes: 27 additions & 27 deletions docs/TestCoverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -1875,14 +1875,14 @@ node = onnx.helper.make_node(
)

# 2d
x = np.random.randn(3, 4).astype(np.int32)
y = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = create_random_int((3, 4), np.int32)
z = np.bitwise_and(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_and_i32_2d")

# 3d
x = np.random.randn(3, 4, 5).astype(np.int16)
y = np.random.randn(3, 4, 5).astype(np.int16)
x = create_random_int((3, 4, 5), np.int16)
y = create_random_int((3, 4, 5), np.int16)
z = np.bitwise_and(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_and_i16_3d")
```
Expand All @@ -1899,16 +1899,16 @@ node = onnx.helper.make_node(
)

# 3d vs 1d
x = np.random.randn(3, 4, 5).astype(np.uint64)
y = np.random.randn(5).astype(np.uint64)
x = create_random_int((3, 4, 5), np.uint64)
y = create_random_int((5,), np.uint64)
z = np.bitwise_and(x, y)
expect(
node, inputs=[x, y], outputs=[z], name="test_bitwise_and_ui64_bcast_3v1d"
)

# 4d vs 3d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
y = np.random.randn(4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = create_random_int((4, 5, 6), np.uint8)
z = np.bitwise_and(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_and_ui8_bcast_4v3d")
```
Expand All @@ -1929,17 +1929,17 @@ node = onnx.helper.make_node(
)

# 2d
x = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = np.bitwise_not(x)
expect(node, inputs=[x], outputs=[y], name="test_bitwise_not_2d")

# 3d
x = np.random.randn(3, 4, 5).astype(np.uint16)
x = create_random_int((3, 4, 5), np.uint16)
y = np.bitwise_not(x)
expect(node, inputs=[x], outputs=[y], name="test_bitwise_not_3d")

# 4d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = np.bitwise_not(x)
expect(node, inputs=[x], outputs=[y], name="test_bitwise_not_4d")
```
Expand All @@ -1959,14 +1959,14 @@ node = onnx.helper.make_node(
outputs=["bitwiseor"],
)
# 2d
x = np.random.randn(3, 4).astype(np.int32)
y = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = create_random_int((3, 4), np.int32)
z = np.bitwise_or(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_or_i32_2d")

# 4d
x = np.random.randn(3, 4, 5, 6).astype(np.int8)
y = np.random.randn(3, 4, 5, 6).astype(np.int8)
x = create_random_int((3, 4, 5, 6), np.int8)
y = create_random_int((3, 4, 5, 6), np.int8)
z = np.bitwise_or(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_or_i16_4d")
```
Expand All @@ -1983,14 +1983,14 @@ node = onnx.helper.make_node(
)

# 3d vs 1d
x = np.random.randn(3, 4, 5).astype(np.uint64)
y = np.random.randn(5).astype(np.uint64)
x = create_random_int((3, 4, 5), np.uint64)
y = create_random_int((5,), np.uint64)
z = np.bitwise_or(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_or_ui64_bcast_3v1d")

# 4d vs 3d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
y = np.random.randn(4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = create_random_int((4, 5, 6), np.uint8)
z = np.bitwise_or(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_or_ui8_bcast_4v3d")
```
Expand All @@ -2011,16 +2011,16 @@ node = onnx.helper.make_node(
)

# 3d vs 1d
x = np.random.randn(3, 4, 5).astype(np.uint64)
y = np.random.randn(5).astype(np.uint64)
x = create_random_int((3, 4, 5), np.uint64)
y = create_random_int((5,), np.uint64)
z = np.bitwise_xor(x, y)
expect(
node, inputs=[x, y], outputs=[z], name="test_bitwise_xor_ui64_bcast_3v1d"
)

# 4d vs 3d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
y = np.random.randn(4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = create_random_int((4, 5, 6), np.uint8)
z = np.bitwise_xor(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_xor_ui8_bcast_4v3d")
```
Expand All @@ -2037,14 +2037,14 @@ node = onnx.helper.make_node(
)

# 2d
x = np.random.randn(3, 4).astype(np.int32)
y = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = create_random_int((3, 4), np.int32)
z = np.bitwise_xor(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_xor_i32_2d")

# 3d
x = np.random.randn(3, 4, 5).astype(np.int16)
y = np.random.randn(3, 4, 5).astype(np.int16)
x = create_random_int((3, 4, 5), np.int16)
y = create_random_int((3, 4, 5), np.int16)
z = np.bitwise_xor(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_xor_i16_3d")
```
Expand Down
2 changes: 2 additions & 0 deletions docs/docsgen/source/api/numpy_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ tools
.. autofunction:: onnx.numpy_helper.convert_endian

.. autofunction:: onnx.numpy_helper.combine_pairs_to_complex

.. autofunction:: onnx.numpy_helper.create_random_int
17 changes: 9 additions & 8 deletions onnx/backend/test/case/node/bitwiseand.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np # type: ignore

import onnx
from onnx.numpy_helper import create_random_int

from ..base import Base
from . import expect
Expand All @@ -18,14 +19,14 @@ def export() -> None:
)

# 2d
x = np.random.randn(3, 4).astype(np.int32)
y = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = create_random_int((3, 4), np.int32)
z = np.bitwise_and(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_and_i32_2d")

# 3d
x = np.random.randn(3, 4, 5).astype(np.int16)
y = np.random.randn(3, 4, 5).astype(np.int16)
x = create_random_int((3, 4, 5), np.int16)
y = create_random_int((3, 4, 5), np.int16)
z = np.bitwise_and(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_and_i16_3d")

Expand All @@ -38,15 +39,15 @@ def export_bitwiseand_broadcast() -> None:
)

# 3d vs 1d
x = np.random.randn(3, 4, 5).astype(np.uint64)
y = np.random.randn(5).astype(np.uint64)
x = create_random_int((3, 4, 5), np.uint64)
y = create_random_int((5,), np.uint64)
z = np.bitwise_and(x, y)
expect(
node, inputs=[x, y], outputs=[z], name="test_bitwise_and_ui64_bcast_3v1d"
)

# 4d vs 3d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
y = np.random.randn(4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = create_random_int((4, 5, 6), np.uint8)
z = np.bitwise_and(x, y)
expect(node, inputs=[x, y], outputs=[z], name="test_bitwise_and_ui8_bcast_4v3d")
7 changes: 4 additions & 3 deletions onnx/backend/test/case/node/bitwisenot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np # type: ignore

import onnx
from onnx.numpy_helper import create_random_int

from ..base import Base
from . import expect
Expand All @@ -18,16 +19,16 @@ def export() -> None:
)

# 2d
x = np.random.randn(3, 4).astype(np.int32)
x = create_random_int((3, 4), np.int32)
y = np.bitwise_not(x)
expect(node, inputs=[x], outputs=[y], name="test_bitwise_not_2d")

# 3d
x = np.random.randn(3, 4, 5).astype(np.uint16)
x = create_random_int((3, 4, 5), np.uint16)
y = np.bitwise_not(x)
expect(node, inputs=[x], outputs=[y], name="test_bitwise_not_3d")

# 4d
x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
x = create_random_int((3, 4, 5, 6), np.uint8)
y = np.bitwise_not(x)
expect(node, inputs=[x], outputs=[y], name="test_bitwise_not_4d")
Loading

0 comments on commit a90d702

Please sign in to comment.