# CumSum#

## CumSum - 14#

Version

• name: CumSum (GitHub)

• domain: main

• since_version: 14

• function: False

• support_level: SupportType.COMMON

• shape inference: True

This version of the operator has been available since version 14.

Summary

Performs cumulative sum of the input elements along the given axis. By default, it will do the sum inclusively meaning the first element is copied as is. Through an exclusive attribute, this behavior can change to exclude the first element. It can also perform summation in the opposite direction of the axis. For that, set reverse attribute to 1.

Example:

```input_x = [1, 2, 3]
axis=0
output = [1, 3, 6]
exclusive=1
output = [0, 1, 3]
exclusive=0
reverse=1
output = [6, 5, 3]
exclusive=1
reverse=1
output = [5, 3, 0]
```

Attributes

• exclusive: If set to 1 will return exclusive sum in which the top element is not included. In other terms, if set to 1, the j-th output element would be the sum of the first (j-1) elements. Otherwise, it would be the sum of the first j elements. Default value is `0`.

• reverse: If set to 1 will perform the sums in reverse direction. Default value is `0`.

Inputs

• x (heterogeneous) - T: An input tensor that is to be processed.

• axis (heterogeneous) - T2: A 0-D tensor. Must be in the range [-rank(x), rank(x)-1]. Negative value means counting dimensions from the back.

Outputs

• y (heterogeneous) - T: Output tensor of the same type as ‘x’ with cumulative sums of the x’s elements

Type Constraints

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

• T2 in ( tensor(int32), tensor(int64) ): axis tensor can be int32 or int64 only

Examples

_cumsum_1d

```node = onnx.helper.make_node("CumSum", inputs=["x", "axis"], outputs=["y"])
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)
axis = np.int32(0)
y = np.array([1.0, 3.0, 6.0, 10.0, 15.0]).astype(np.float64)
expect(node, inputs=[x, axis], outputs=[y], name="test_cumsum_1d")
```

_cumsum_1d_exclusive

```node = onnx.helper.make_node(
"CumSum", inputs=["x", "axis"], outputs=["y"], exclusive=1
)
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)
axis = np.int32(0)
y = np.array([0.0, 1.0, 3.0, 6.0, 10.0]).astype(np.float64)
expect(node, inputs=[x, axis], outputs=[y], name="test_cumsum_1d_exclusive")
```

_cumsum_1d_reverse

```node = onnx.helper.make_node(
"CumSum", inputs=["x", "axis"], outputs=["y"], reverse=1
)
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)
axis = np.int32(0)
y = np.array([15.0, 14.0, 12.0, 9.0, 5.0]).astype(np.float64)
expect(node, inputs=[x, axis], outputs=[y], name="test_cumsum_1d_reverse")
```

_cumsum_1d_reverse_exclusive

```node = onnx.helper.make_node(
"CumSum", inputs=["x", "axis"], outputs=["y"], reverse=1, exclusive=1
)
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)
axis = np.int32(0)
y = np.array([14.0, 12.0, 9.0, 5.0, 0.0]).astype(np.float64)
expect(
node, inputs=[x, axis], outputs=[y], name="test_cumsum_1d_reverse_exclusive"
)
```

_cumsum_2d_axis_0

```node = onnx.helper.make_node(
"CumSum",
inputs=["x", "axis"],
outputs=["y"],
)
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))
axis = np.int32(0)
y = np.array([1.0, 2.0, 3.0, 5.0, 7.0, 9.0]).astype(np.float64).reshape((2, 3))
expect(node, inputs=[x, axis], outputs=[y], name="test_cumsum_2d_axis_0")
```

_cumsum_2d_axis_1

```node = onnx.helper.make_node(
"CumSum",
inputs=["x", "axis"],
outputs=["y"],
)
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))
axis = np.int32(1)
y = np.array([1.0, 3.0, 6.0, 4.0, 9.0, 15.0]).astype(np.float64).reshape((2, 3))
expect(node, inputs=[x, axis], outputs=[y], name="test_cumsum_2d_axis_1")
```

_cumsum_2d_negative_axis

```node = onnx.helper.make_node(
"CumSum",
inputs=["x", "axis"],
outputs=["y"],
)
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))
axis = np.int32(-1)
y = np.array([1.0, 3.0, 6.0, 4.0, 9.0, 15.0]).astype(np.float64).reshape((2, 3))
expect(node, inputs=[x, axis], outputs=[y], name="test_cumsum_2d_negative_axis")
```

Differences

 `0` `0` `Performs cumulative sum of the input elements along the given axis.` `Performs cumulative sum of the input elements along the given axis.` `1` `1` `By default, it will do the sum inclusively meaning the first element is copied as is.` `By default, it will do the sum inclusively meaning the first element is copied as is.` `2` `2` `Through an exclusive attribute, this behavior can change to exclude the first element.` `Through an exclusive attribute, this behavior can change to exclude the first element.` `3` `3` `It can also perform summation in the opposite direction of the axis. For that, set reverse attribute to 1.` `It can also perform summation in the opposite direction of the axis. For that, set reverse attribute to 1.` `4` `4` `5` `5` `Example:` `Example:` `6` `6` `::` `::` `7` `7` `8` `8` ` input_x = [1, 2, 3]` ` input_x = [1, 2, 3]` `9` `9` ` axis=0` ` axis=0` `10` `10` ` output = [1, 3, 6]` ` output = [1, 3, 6]` `11` `11` ` exclusive=1` ` exclusive=1` `12` `12` ` output = [0, 1, 3]` ` output = [0, 1, 3]` `13` `13` ` exclusive=0` ` exclusive=0` `14` `14` ` reverse=1` ` reverse=1` `15` `15` ` output = [6, 5, 3]` ` output = [6, 5, 3]` `16` `16` ` exclusive=1` ` exclusive=1` `17` `17` ` reverse=1` ` reverse=1` `18` `18` ` output = [5, 3, 0]` ` output = [5, 3, 0]` `19` `19` `20` `20` `**Attributes**` `**Attributes**` `21` `21` `22` `22` `* **exclusive**:` `* **exclusive**:` `23` `23` ` If set to 1 will return exclusive sum in which the top element is` ` If set to 1 will return exclusive sum in which the top element is` `24` `24` ` not included. In other terms, if set to 1, the j-th output element` ` not included. In other terms, if set to 1, the j-th output element` `25` `25` ` would be the sum of the first (j-1) elements. Otherwise, it would be` ` would be the sum of the first (j-1) elements. Otherwise, it would be` `26` `26` ` the sum of the first j elements. Default value is 0.` ` the sum of the first j elements. Default value is 0.` `27` `27` `* **reverse**:` `* **reverse**:` `28` `28` ` If set to 1 will perform the sums in reverse direction. Default value is 0.` ` If set to 1 will perform the sums in reverse direction. Default value is 0.` `29` `29` `30` `30` `**Inputs**` `**Inputs**` `31` `31` `32` `32` `* **x** (heterogeneous) - **T**:` `* **x** (heterogeneous) - **T**:` `33` `33` ` An input tensor that is to be processed.` ` An input tensor that is to be processed.` `34` `34` `* **axis** (heterogeneous) - **T2**:` `* **axis** (heterogeneous) - **T2**:` `35` `35` ` A 0-D tensor. Must be in the range [-rank(x), rank(x)-1]. Negative` ` A 0-D tensor. Must be in the range [-rank(x), rank(x)-1]. Negative` `36` `36` ` value means counting dimensions from the back.` ` value means counting dimensions from the back.` `37` `37` `38` `38` `**Outputs**` `**Outputs**` `39` `39` `40` `40` `* **y** (heterogeneous) - **T**:` `* **y** (heterogeneous) - **T**:` `41` `41` ` Output tensor of the same type as 'x' with cumulative sums of the` ` Output tensor of the same type as 'x' with cumulative sums of the` `42` `42` ` x's elements` ` x's elements` `43` `43` `44` `44` `**Type Constraints**` `**Type Constraints**` `45` `45` `46` `46` `* **T** in (` `* **T** in (` `47` ` tensor(bfloat16),` `47` `48` ` tensor(double),` ` tensor(double),` `48` `49` ` tensor(float),` ` tensor(float),` `50` ` tensor(float16),` `49` `51` ` tensor(int32),` ` tensor(int32),` `50` `52` ` tensor(int64),` ` tensor(int64),` `51` `53` ` tensor(uint32),` ` tensor(uint32),` `52` `54` ` tensor(uint64)` ` tensor(uint64)` `53` `55` ` ):` ` ):` `54` `56` ` Input can be of any tensor type.` ` Constrain input and output types to high-precision numeric tensors.` `55` `57` `* **T2** in (` `* **T2** in (` `56` `58` ` tensor(int32),` ` tensor(int32),` `57` `59` ` tensor(int64)` ` tensor(int64)` `58` `60` ` ):` ` ):` `59` `61` ` axis tensor can be int32 or int64 only` ` axis tensor can be int32 or int64 only`

## CumSum - 11#

Version

• name: CumSum (GitHub)

• domain: main

• since_version: 11

• function: False

• support_level: SupportType.COMMON

• shape inference: True

This version of the operator has been available since version 11.

Summary

Performs cumulative sum of the input elements along the given axis. By default, it will do the sum inclusively meaning the first element is copied as is. Through an exclusive attribute, this behavior can change to exclude the first element. It can also perform summation in the opposite direction of the axis. For that, set reverse attribute to 1.

Example:

```input_x = [1, 2, 3]
axis=0
output = [1, 3, 6]
exclusive=1
output = [0, 1, 3]
exclusive=0
reverse=1
output = [6, 5, 3]
exclusive=1
reverse=1
output = [5, 3, 0]
```

Attributes

• exclusive: If set to 1 will return exclusive sum in which the top element is not included. In other terms, if set to 1, the j-th output element would be the sum of the first (j-1) elements. Otherwise, it would be the sum of the first j elements. Default value is `0`.

• reverse: If set to 1 will perform the sums in reverse direction. Default value is `0`.

Inputs

• x (heterogeneous) - T: An input tensor that is to be processed.

• axis (heterogeneous) - T2: A 0-D tensor. Must be in the range [-rank(x), rank(x)-1]. Negative value means counting dimensions from the back.

Outputs

• y (heterogeneous) - T: Output tensor of the same type as ‘x’ with cumulative sums of the x’s elements

Type Constraints

• T in ( tensor(double), tensor(float), tensor(int32), tensor(int64), tensor(uint32), tensor(uint64) ): Input can be of any tensor type.

• T2 in ( tensor(int32), tensor(int64) ): axis tensor can be int32 or int64 only