(l-onnx-doc-CumSum)= # CumSum (l-onnx-op-cumsum-14)= ## CumSum - 14 ### Version - **name**: [CumSum (GitHub)](https://github.com/onnx/onnx/blob/main/docs/Operators.md#CumSum) - **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 - INT** (default is `0`): 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. * **reverse - INT** (default is `0`): If set to 1 will perform the sums in reverse direction. ### 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 numeric tensors. * **T2** in ( `tensor(int32)`, `tensor(int64)` ): axis tensor can be int32 or int64 only ### Examples #### _cumsum_1d ```python import numpy as np import onnx 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 ```python import numpy as np import onnx 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 ```python import numpy as np import onnx 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 ```python import numpy as np import onnx 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 ```python import numpy as np import onnx 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 ```python import numpy as np import onnx 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 ```python import numpy as np import onnx 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") ``` #### _cumsum_2d_int32 ```python import numpy as np import onnx node = onnx.helper.make_node( "CumSum", inputs=["x", "axis"], outputs=["y"], ) x = np.array([1, 2, 3, 4, 5, 6]).astype(np.int32).reshape((2, 3)) axis = np.int32(0) y = np.array([1, 2, 3, 5, 7, 9]).astype(np.int32).reshape((2, 3)) expect(node, inputs=[x, axis], outputs=[y], name="test_cumsum_2d_int32") ``` #### _cumsum_1d_int32_exclusive ```python import numpy as np import onnx node = onnx.helper.make_node( "CumSum", inputs=["x", "axis"], outputs=["y"], exclusive=1 ) x = np.array([1, 2, 3, 4, 5]).astype(np.int32) axis = np.int32(0) y = np.array([0, 1, 3, 6, 10]).astype(np.int32) expect( node, inputs=[x, axis], outputs=[y], name="test_cumsum_1d_int32_exclusive" ) ``` ```{toctree} text_diff_CumSum_11_14 ``` (l-onnx-op-cumsum-11)= ## CumSum - 11 ### Version - **name**: [CumSum (GitHub)](https://github.com/onnx/onnx/blob/main/docs/Operators.md#CumSum) - **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 - INT** (default is `0`): 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. * **reverse - INT** (default is `0`): If set to 1 will perform the sums in reverse direction. ### 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