Skip to content

Commit

Permalink
Clarify reduction behavior for an empty set of values (#5568)
Browse files Browse the repository at this point in the history
Clarify the behavior of the reduction-ops when reducing an empty set of
values, by updating the test-cases and documentation.

It is useful in various edge-cases. For example, ReduceProd should
return 1 for an empty tensor, and ReduceSum should return 0 for an empty
tensor.

(See #3651 (comment))

### Summary

ReduceSum ({}) = 0
ReduceProd ({}) = 1
ReduceMin ({}) = Max. value of datatype
ReduceMax ({}) = Min. value of datatype
ReduceLogSum ({}) = minus infinity or undefined for datatypes without
minus infinity
ReduceLogSumExp ({}) = minus infinity or undefined for datatypes without
minus infinity
ReduceMean ({}) = Undefined

---------

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: gramalingam <gramalingam@users.noreply.github.com>
  • Loading branch information
gramalingam and gramalingam committed Sep 22, 2023
1 parent d811848 commit 2b770ab
Show file tree
Hide file tree
Showing 22 changed files with 1,027 additions and 94 deletions.
101 changes: 68 additions & 33 deletions docs/Changelog.md

Large diffs are not rendered by default.

306 changes: 296 additions & 10 deletions docs/Operators.md

Large diffs are not rendered by default.

274 changes: 266 additions & 8 deletions docs/TestCoverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13837,7 +13837,7 @@ expect(node, inputs=[x], outputs=[y], name="test_reciprocal")


### ReduceL1
There are 4 test cases, listed as following:
There are 5 test cases, listed as following:
<details>
<summary>default_axes_keepdims</summary>

Expand Down Expand Up @@ -13920,6 +13920,34 @@ expect(
)
```

</details>
<details>
<summary>empty_set</summary>

```python
shape = [2, 0, 4]
keepdims = 1
reduced_shape = [2, 1, 4]

node = onnx.helper.make_node(
"ReduceL1",
inputs=["data", "axes"],
outputs=["reduced"],
keepdims=keepdims,
)

data = np.array([], dtype=np.float32).reshape(shape)
axes = np.array([1], dtype=np.int64)
reduced = np.array(np.zeros(reduced_shape, dtype=np.float32))

expect(
node,
inputs=[data, axes],
outputs=[reduced],
name="test_reduce_l1_empty_set",
)
```

</details>
<details>
<summary>keepdims</summary>
Expand Down Expand Up @@ -14010,7 +14038,7 @@ expect(


### ReduceL2
There are 4 test cases, listed as following:
There are 5 test cases, listed as following:
<details>
<summary>default_axes_keepdims</summary>

Expand Down Expand Up @@ -14099,6 +14127,34 @@ expect(
)
```

</details>
<details>
<summary>empty_set</summary>

```python
shape = [2, 0, 4]
keepdims = 1
reduced_shape = [2, 1, 4]

node = onnx.helper.make_node(
"ReduceL2",
inputs=["data", "axes"],
outputs=["reduced"],
keepdims=keepdims,
)

data = np.array([], dtype=np.float32).reshape(shape)
axes = np.array([1], dtype=np.int64)
reduced = np.array(np.zeros(reduced_shape, dtype=np.float32))

expect(
node,
inputs=[data, axes],
outputs=[reduced],
name="test_reduce_l2_empty_set",
)
```

</details>
<details>
<summary>keepdims</summary>
Expand Down Expand Up @@ -14201,7 +14257,36 @@ expect(


### ReduceLogSum
There are 3 test cases, listed as following:
There are 4 test cases, listed as following:
<details>
<summary>empty_set</summary>

```python
shape = [2, 0, 4]
keepdims = 1
reduced_shape = [2, 1, 4]

node = onnx.helper.make_node(
"ReduceLogSum",
inputs=["data", "axes"],
outputs=["reduced"],
keepdims=keepdims,
)

data = np.array([], dtype=np.float32).reshape(shape)
axes = np.array([1], dtype=np.int64)
zero = np.array(np.zeros(reduced_shape, dtype=np.float32))
reduced = np.log(zero) # -inf

expect(
node,
inputs=[data, axes],
outputs=[reduced],
name="test_reduce_log_sum_empty_set",
)
```

</details>
<details>
<summary>keepdims</summary>

Expand Down Expand Up @@ -14284,7 +14369,7 @@ expect(


### ReduceLogSumExp
There are 4 test cases, listed as following:
There are 5 test cases, listed as following:
<details>
<summary>default_axes_keepdims</summary>

Expand Down Expand Up @@ -14368,6 +14453,35 @@ expect(
)
```

</details>
<details>
<summary>empty_set</summary>

```python
shape = [2, 0, 4]
keepdims = 1
reduced_shape = [2, 1, 4]

node = onnx.helper.make_node(
"ReduceLogSumExp",
inputs=["data", "axes"],
outputs=["reduced"],
keepdims=keepdims,
)

data = np.array([], dtype=np.float32).reshape(shape)
axes = np.array([1], dtype=np.int64)
zero = np.array(np.zeros(reduced_shape, dtype=np.float32))
reduced = np.log(zero) # -inf

expect(
node,
inputs=[data, axes],
outputs=[reduced],
name="test_reduce_log_sum_exp_empty_set",
)
```

</details>
<details>
<summary>keepdims</summary>
Expand Down Expand Up @@ -14859,7 +14973,7 @@ expect(


### ReduceMin
There are 5 test cases, listed as following:
There are 6 test cases, listed as following:
<details>
<summary>bool_inputs</summary>

Expand Down Expand Up @@ -14981,6 +15095,36 @@ expect(
)
```

</details>
<details>
<summary>empty_set</summary>

```python
shape = [2, 0, 4]
keepdims = 1
reduced_shape = [2, 1, 4]

node = onnx.helper.make_node(
"ReduceMin",
inputs=["data", "axes"],
outputs=["reduced"],
keepdims=keepdims,
)

data = np.array([], dtype=np.float32).reshape(shape)
axes = np.array([1], dtype=np.int64)
one = np.array(np.ones(reduced_shape, dtype=np.float32))
zero = np.array(np.zeros(reduced_shape, dtype=np.float32))
reduced = one / zero # inf

expect(
node,
inputs=[data, axes],
outputs=[reduced],
name="test_reduce_min_empty_set",
)
```

</details>
<details>
<summary>keepdims</summary>
Expand Down Expand Up @@ -15079,7 +15223,7 @@ expect(


### ReduceProd
There are 4 test cases, listed as following:
There are 5 test cases, listed as following:
<details>
<summary>default_axes_keepdims</summary>

Expand Down Expand Up @@ -15160,6 +15304,34 @@ expect(
)
```

</details>
<details>
<summary>empty_set</summary>

```python
shape = [2, 0, 4]
keepdims = 1
reduced_shape = [2, 1, 4]

node = onnx.helper.make_node(
"ReduceProd",
inputs=["data", "axes"],
outputs=["reduced"],
keepdims=keepdims,
)

data = np.array([], dtype=np.float32).reshape(shape)
axes = np.array([1], dtype=np.int64)
reduced = np.array(np.ones(reduced_shape, dtype=np.float32))

expect(
node,
inputs=[data, axes],
outputs=[reduced],
name="test_reduce_prod_empty_set",
)
```

</details>
<details>
<summary>keepdims</summary>
Expand Down Expand Up @@ -15250,7 +15422,7 @@ expect(


### ReduceSum
There are 5 test cases, listed as following:
There are 7 test cases, listed as following:
<details>
<summary>default_axes_keepdims</summary>

Expand Down Expand Up @@ -15373,6 +15545,35 @@ expect(
)
```

</details>
<details>
<summary>empty_set</summary>

```python
"""Test case with the reduced-axis of size zero."""
shape = [2, 0, 4]
keepdims = 1
reduced_shape = [2, 1, 4]

node = onnx.helper.make_node(
"ReduceSum",
inputs=["data", "axes"],
outputs=["reduced"],
keepdims=keepdims,
)

data = np.array([], dtype=np.float32).reshape(shape)
axes = np.array([1], dtype=np.int64)
reduced = np.array(np.zeros(reduced_shape, dtype=np.float32))

expect(
node,
inputs=[data, axes],
outputs=[reduced],
name="test_reduce_sum_empty_set",
)
```

</details>
<details>
<summary>keepdims</summary>
Expand Down Expand Up @@ -15455,11 +15656,40 @@ expect(
)
```

</details>
<details>
<summary>non_reduced_axis_zero</summary>

```python
"""Test case with the non-reduced-axis of size zero."""
shape = [2, 0, 4]
keepdims = 1
reduced_shape = [2, 0, 1]

node = onnx.helper.make_node(
"ReduceSum",
inputs=["data", "axes"],
outputs=["reduced"],
keepdims=keepdims,
)

data = np.array([], dtype=np.float32).reshape(shape)
axes = np.array([2], dtype=np.int64)
reduced = np.array([], dtype=np.float32).reshape(reduced_shape)

expect(
node,
inputs=[data, axes],
outputs=[reduced],
name="test_reduce_sum_empty_set",
)
```

</details>


### ReduceSumSquare
There are 4 test cases, listed as following:
There are 5 test cases, listed as following:
<details>
<summary>default_axes_keepdims</summary>

Expand Down Expand Up @@ -15545,6 +15775,34 @@ expect(
)
```

</details>
<details>
<summary>empty_set</summary>

```python
shape = [2, 0, 4]
keepdims = 1
reduced_shape = [2, 1, 4]

node = onnx.helper.make_node(
"ReduceSumSquare",
inputs=["data", "axes"],
outputs=["reduced"],
keepdims=keepdims,
)

data = np.array([], dtype=np.float32).reshape(shape)
axes = np.array([1], dtype=np.int64)
reduced = np.array(np.zeros(reduced_shape, dtype=np.float32))

expect(
node,
inputs=[data, axes],
outputs=[reduced],
name="test_reduce_sum_square_empty_set",
)
```

</details>
<details>
<summary>keepdims</summary>
Expand Down

0 comments on commit 2b770ab

Please sign in to comment.