ReduceSum#

  • Domain: ai.onnx

  • Since version: 13

Computes the sum of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. The axes are provided as an optional second input tensor. Negative axes are supported. If axes is not provided or is empty and noop_with_empty_axes is set to 1, the input tensor is returned unchanged. If axes is not provided or is empty and noop_with_empty_axes is not set, all dimensions are reduced.

Inputs

  • data (T): An input tensor.

  • axes (tensor(int64)): Optional input list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor if noop_with_empty_axes is false, else act as an Identity op when noop_with_empty_axes is true. Accepted range is [-r, r-1] where r = rank(data).

Outputs

  • reduced (T): Reduced output tensor.

Attributes

  • keepdims (int): Keep the reduced dimension or not, default 1 means keep reduced dimension.

  • noop_with_empty_axes (int): Defines behavior if ‘axes’ is empty. Default behavior with ‘false’ is to reduce all axes. When axes is empty and this attribute is set to true, input tensor will not be reduced, and the output tensor would be equivalent to input tensor.

Type Constraints

  • T: Constrain input and output types to high-precision numeric tensors. Allowed types: tensor(bfloat16), tensor(double), tensor(float), tensor(float16), tensor(int32), tensor(int64), tensor(uint32), tensor(uint64).

Examples#

test_cc_reducesum_default_axes_keepdims

Node:
  ReduceSum(data) -> (reduced)
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]

Outputs:
  reduced: shape=(1, 1, 1), dtype=float32
    [[[78.]]]

test_cc_reducesum_do_not_keepdims

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 0
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]
  axes: shape=(1,), dtype=int64
    [1]

Outputs:
  reduced: shape=(3, 2), dtype=float32
    [[ 4.,  6.],
     [12., 14.],
     [20., 22.]]

test_cc_reducesum_empty_axes_input_noop

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    noop_with_empty_axes = 1
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]
  axes: shape=(0,), dtype=int64
    []

Outputs:
  reduced: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]

test_cc_reducesum_empty_set

Node:
  ReduceSum(data, axes) -> (reduced)
Inputs:
  data: shape=(2, 0, 4), dtype=float32
    []
  axes: shape=(1,), dtype=int64
    [1]

Outputs:
  reduced: shape=(2, 1, 4), dtype=float32
    [[[0., 0., 0., 0.]],

     [[0., 0., 0., 0.]]]

test_cc_reducesum_empty_set_non_reduced_axis_zero

Node:
  ReduceSum(data, axes) -> (reduced)
Inputs:
  data: shape=(2, 0, 4), dtype=float32
    []
  axes: shape=(1,), dtype=int64
    [2]

Outputs:
  reduced: shape=(2, 0, 1), dtype=float32
    []

test_cc_reducesum_keepdims

Node:
  ReduceSum(data, axes) -> (reduced)
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]
  axes: shape=(1,), dtype=int64
    [1]

Outputs:
  reduced: shape=(3, 1, 2), dtype=float32
    [[[ 4.,  6.]],

     [[12., 14.]],

     [[20., 22.]]]

test_cc_reducesum_negative_axes_keepdims

Node:
  ReduceSum(data, axes) -> (reduced)
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]
  axes: shape=(1,), dtype=int64
    [-2]

Outputs:
  reduced: shape=(3, 1, 2), dtype=float32
    [[[ 4.,  6.]],

     [[12., 14.]],

     [[20., 22.]]]

test_reduce_sum_default_axes_keepdims_example

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 1
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]
  axes: shape=(0,), dtype=int64
    []

Outputs:
  reduced: shape=(1, 1, 1), dtype=float32
    [[[78.]]]

test_reduce_sum_default_axes_keepdims_random

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 1
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 0.9762701 ,  4.303787  ],
      [ 2.0552676 ,  0.89766365]],

     [[-1.526904  ,  2.9178822 ],
      [-1.2482557 ,  7.83546   ]],

     [[ 9.273255  , -2.3311696 ],
      [ 5.834501  ,  0.5778984 ]]]
  axes: shape=(0,), dtype=int64
    []

Outputs:
  reduced: shape=(1, 1, 1), dtype=float32
    [[[29.565659]]]

test_reduce_sum_do_not_keepdims_example

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 0
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]
  axes: shape=(1,), dtype=int64
    [1]

Outputs:
  reduced: shape=(3, 2), dtype=float32
    [[ 4.,  6.],
     [12., 14.],
     [20., 22.]]

test_reduce_sum_do_not_keepdims_random

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 0
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 0.9762701 ,  4.303787  ],
      [ 2.0552676 ,  0.89766365]],

     [[-1.526904  ,  2.9178822 ],
      [-1.2482557 ,  7.83546   ]],

     [[ 9.273255  , -2.3311696 ],
      [ 5.834501  ,  0.5778984 ]]]
  axes: shape=(1,), dtype=int64
    [1]

Outputs:
  reduced: shape=(3, 2), dtype=float32
    [[ 3.0315375,  5.201451 ],
     [-2.7751598, 10.753343 ],
     [15.107756 , -1.7532712]]

test_reduce_sum_empty_axes_input_noop_example

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 1
    noop_with_empty_axes = 1
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]
  axes: shape=(0,), dtype=int64
    []

Outputs:
  reduced: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]

test_reduce_sum_empty_set

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 1
Inputs:
  data: shape=(2, 0, 4), dtype=float32
    []
  axes: shape=(1,), dtype=int64
    [1]

Outputs:
  reduced: shape=(2, 1, 4), dtype=float32
    [[[0., 0., 0., 0.]],

     [[0., 0., 0., 0.]]]

test_reduce_sum_empty_set_non_reduced_axis_zero

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 1
Inputs:
  data: shape=(2, 0, 4), dtype=float32
    []
  axes: shape=(1,), dtype=int64
    [2]

Outputs:
  reduced: shape=(2, 0, 1), dtype=float32
    []

test_reduce_sum_keepdims_example

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 1
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]
  axes: shape=(1,), dtype=int64
    [1]

Outputs:
  reduced: shape=(3, 1, 2), dtype=float32
    [[[ 4.,  6.]],

     [[12., 14.]],

     [[20., 22.]]]

test_reduce_sum_keepdims_random

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 1
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 0.9762701 ,  4.303787  ],
      [ 2.0552676 ,  0.89766365]],

     [[-1.526904  ,  2.9178822 ],
      [-1.2482557 ,  7.83546   ]],

     [[ 9.273255  , -2.3311696 ],
      [ 5.834501  ,  0.5778984 ]]]
  axes: shape=(1,), dtype=int64
    [1]

Outputs:
  reduced: shape=(3, 1, 2), dtype=float32
    [[[ 3.0315375,  5.201451 ]],

     [[-2.7751598, 10.753343 ]],

     [[15.107756 , -1.7532712]]]

test_reduce_sum_negative_axes_keepdims_example

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 1
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 1.,  2.],
      [ 3.,  4.]],

     [[ 5.,  6.],
      [ 7.,  8.]],

     [[ 9., 10.],
      [11., 12.]]]
  axes: shape=(1,), dtype=int64
    [-2]

Outputs:
  reduced: shape=(3, 1, 2), dtype=float32
    [[[ 4.,  6.]],

     [[12., 14.]],

     [[20., 22.]]]

test_reduce_sum_negative_axes_keepdims_random

Node:
  ReduceSum(data, axes) -> (reduced)
  Attributes:
    keepdims = 1
Inputs:
  data: shape=(3, 2, 2), dtype=float32
    [[[ 0.9762701 ,  4.303787  ],
      [ 2.0552676 ,  0.89766365]],

     [[-1.526904  ,  2.9178822 ],
      [-1.2482557 ,  7.83546   ]],

     [[ 9.273255  , -2.3311696 ],
      [ 5.834501  ,  0.5778984 ]]]
  axes: shape=(1,), dtype=int64
    [-2]

Outputs:
  reduced: shape=(3, 1, 2), dtype=float32
    [[[ 3.0315375,  5.201451 ]],

     [[-2.7751598, 10.753343 ]],

     [[15.107756 , -1.7532712]]]

Differences with previous version (11)#

SchemaDiff: ReduceSum (domain 'ai.onnx')

  • old version: 11

  • new version: 13

  • breaking: yes

Breaking reasons:

  • input ‘axes’ (added): at position 1; option=Single; type_str=’tensor(int64)’

  • attribute ‘axes’ (removed): type=INTS; required=False

Inputs:

  • [BREAKING] added ‘axes’: at position 1; option=Single; type_str=’tensor(int64)’

Attributes:

  • [BREAKING] removed ‘axes’: type=INTS; required=False

  • added ‘noop_with_empty_axes’: type=INT; required=False; default=0

Type constraints:

  • changed ‘T’: added types: [‘tensor(bfloat16)’]

Documentation:

  • line similarity: 0.50 (+3/-3 lines)

--- ReduceSum v11
+++ ReduceSum v13
@@ -1,6 +1,6 @@
 Computes the sum of the input tensor's elements along the provided axes.
 The resulting tensor has the same rank as the input if keepdims equals 1.
 If keepdims equals 0, then the resulting tensor has the reduced dimension pruned.
-The axes attribute specifies which dimensions to reduce. Negative axes are supported.
-If axes is not provided, all dimensions are reduced.
-If noop_with_empty_axes is set and axes is empty, the input tensor is returned unchanged.
+The axes are provided as an optional second input tensor. Negative axes are supported.
+If axes is not provided or is empty and noop_with_empty_axes is set to 1, the input tensor is returned unchanged.
+If axes is not provided or is empty and noop_with_empty_axes is not set, all dimensions are reduced.

Version History#