.. _l-onnx-doc-Sub: === Sub === .. contents:: :local: .. _l-onnx-op-sub-14: Sub - 14 ======== **Version** * **name**: `Sub (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 element-wise binary subtraction (with Numpy-style broadcasting support). This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check `Broadcasting in ONNX `_. (Opset 14 change): Extend supported types to include uint8, int8, uint16, and int16. **Inputs** * **A** (heterogeneous) - **T**: First operand. * **B** (heterogeneous) - **T**: Second operand. **Outputs** * **C** (heterogeneous) - **T**: Result, has same element type as two inputs **Type Constraints** * **T** in ( tensor(bfloat16), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output types to all numeric tensors. **Examples** **default** :: node = onnx.helper.make_node( "Sub", inputs=["x", "y"], outputs=["z"], ) x = np.array([1, 2, 3]).astype(np.float32) y = np.array([3, 2, 1]).astype(np.float32) z = x - y # expected output [-2., 0., 2.] expect(node, inputs=[x, y], outputs=[z], name="test_sub_example") x = np.random.randn(3, 4, 5).astype(np.float32) y = np.random.randn(3, 4, 5).astype(np.float32) z = x - y expect(node, inputs=[x, y], outputs=[z], name="test_sub") x = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint8) y = np.random.randint(12, size=(3, 4, 5), dtype=np.uint8) z = x - y expect(node, inputs=[x, y], outputs=[z], name="test_sub_uint8") **_sub_broadcast** :: node = onnx.helper.make_node( "Sub", inputs=["x", "y"], outputs=["z"], ) x = np.random.randn(3, 4, 5).astype(np.float32) y = np.random.randn(5).astype(np.float32) z = x - y expect(node, inputs=[x, y], outputs=[z], name="test_sub_bcast") **Differences** .. raw:: html
00Performs element-wise binary subtraction (with Numpy-style broadcasting support).Performs element-wise binary subtraction (with Numpy-style broadcasting support).
11
22This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check Broadcasting in ONNX _.This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check Broadcasting in ONNX _.
33
4(Opset 14 change): Extend supported types to include uint8, int8, uint16, and int16.
5
46**Inputs****Inputs**
57
68* **A** (heterogeneous) - **T**:* **A** (heterogeneous) - **T**:
79 First operand. First operand.
810* **B** (heterogeneous) - **T**:* **B** (heterogeneous) - **T**:
911 Second operand. Second operand.
1012
1113**Outputs****Outputs**
1214
1315* **C** (heterogeneous) - **T**:* **C** (heterogeneous) - **T**:
1416 Result, has same element type as two inputs Result, has same element type as two inputs
1517
1618**Type Constraints****Type Constraints**
1719
1820* **T** in (* **T** in (
1921 tensor(bfloat16), tensor(bfloat16),
2022 tensor(double), tensor(double),
2123 tensor(float), tensor(float),
2224 tensor(float16), tensor(float16),
25 tensor(int16),
2326 tensor(int32), tensor(int32),
2427 tensor(int64), tensor(int64),
28 tensor(int8),
29 tensor(uint16),
2530 tensor(uint32), tensor(uint32),
2631 tensor(uint64) tensor(uint64),
32 tensor(uint8)
2733 ): ):
2834 Constrain input and output types to high-precision numeric tensors. Constrain input and output types to all numeric tensors.
.. _l-onnx-op-sub-13: Sub - 13 ======== **Version** * **name**: `Sub (GitHub) `_ * **domain**: **main** * **since_version**: **13** * **function**: False * **support_level**: SupportType.COMMON * **shape inference**: True This version of the operator has been available **since version 13**. **Summary** Performs element-wise binary subtraction (with Numpy-style broadcasting support). This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check `Broadcasting in ONNX `_. **Inputs** * **A** (heterogeneous) - **T**: First operand. * **B** (heterogeneous) - **T**: Second operand. **Outputs** * **C** (heterogeneous) - **T**: Result, has same element type as two inputs **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. **Differences** .. raw:: html
00Performs element-wise binary subtraction (with Numpy-style broadcasting support).Performs element-wise binary subtraction (with Numpy-style broadcasting support).
11
22This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check Broadcasting in ONNX _.This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check Broadcasting in ONNX _.
33
44**Inputs****Inputs**
55
66* **A** (heterogeneous) - **T**:* **A** (heterogeneous) - **T**:
77 First operand. First operand.
88* **B** (heterogeneous) - **T**:* **B** (heterogeneous) - **T**:
99 Second operand. Second operand.
1010
1111**Outputs****Outputs**
1212
1313* **C** (heterogeneous) - **T**:* **C** (heterogeneous) - **T**:
1414 Result, has same element type as two inputs Result, has same element type as two inputs
1515
1616**Type Constraints****Type Constraints**
1717
1818* **T** in (* **T** in (
19 tensor(bfloat16),
1920 tensor(double), tensor(double),
2021 tensor(float), tensor(float),
2122 tensor(float16), tensor(float16),
2223 tensor(int32), tensor(int32),
2324 tensor(int64), tensor(int64),
2425 tensor(uint32), tensor(uint32),
2526 tensor(uint64) tensor(uint64)
2627 ): ):
2728 Constrain input and output types to high-precision numeric tensors. Constrain input and output types to high-precision numeric tensors.
.. _l-onnx-op-sub-7: Sub - 7 ======= **Version** * **name**: `Sub (GitHub) `_ * **domain**: **main** * **since_version**: **7** * **function**: False * **support_level**: SupportType.COMMON * **shape inference**: True This version of the operator has been available **since version 7**. **Summary** Performs element-wise binary subtraction (with Numpy-style broadcasting support). This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check `Broadcasting in ONNX `_. **Inputs** * **A** (heterogeneous) - **T**: First operand. * **B** (heterogeneous) - **T**: Second operand. **Outputs** * **C** (heterogeneous) - **T**: Result, has same element type as two inputs **Type Constraints** * **T** in ( tensor(double), tensor(float), tensor(float16), tensor(int32), tensor(int64), tensor(uint32), tensor(uint64) ): Constrain input and output types to high-precision numeric tensors. **Differences** .. raw:: html
00Performs element-wise binary subtraction (with limited broadcast support).Performs element-wise binary subtraction (with Numpy-style broadcasting support).
11
2If necessary the right-hand-side argument will be broadcasted to match the
3shape of left-hand-side argument. When broadcasting is specified, the second
4tensor can either be of element size 1 (including a scalar tensor and any
5tensor with rank equal to or smaller than the first tensor), or having its
6shape as a contiguous subset of the first tensor's shape. The starting of the
72mutually equal shape is specified by the argument "axis", and if it is not set,This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check Broadcasting in ONNX <https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md>_.
8suffix matching is assumed. 1-dim expansion doesn't work yet.
93
10For example, the following tensor shapes are supported (with broadcast=1):
11
12 shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor
13 shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor
14 shape(A) = (2, 3, 4, 5), shape(B) = (5,)
15 shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)
16 shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1
17 shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0
18
19Attribute broadcast=1 needs to be passed to enable broadcasting.
20
21**Attributes**
22
23* **axis**:
24 If set, defines the broadcast dimensions. See doc for details.
25* **broadcast**:
26 Pass 1 to enable broadcasting Default value is 0.
27
284**Inputs****Inputs**
295
306* **A** (heterogeneous) - **T**:* **A** (heterogeneous) - **T**:
317 First operand, should share the type with the second operand. First operand.
328* **B** (heterogeneous) - **T**:* **B** (heterogeneous) - **T**:
339 Second operand. With broadcasting can be of smaller size than A. If Second operand.
34 broadcasting is disabled it should be of the same size.
3510
3611**Outputs****Outputs**
3712
3813* **C** (heterogeneous) - **T**:* **C** (heterogeneous) - **T**:
3914 Result, has same dimensions and type as A Result, has same element type as two inputs
4015
4116**Type Constraints****Type Constraints**
4217
4318* **T** in (* **T** in (
4419 tensor(double), tensor(double),
4520 tensor(float), tensor(float),
4621 tensor(float16), tensor(float16),
4722 tensor(int32), tensor(int32),
4823 tensor(int64), tensor(int64),
4924 tensor(uint32), tensor(uint32),
5025 tensor(uint64) tensor(uint64)
5126 ): ):
5227 Constrain input and output types to high-precision numeric tensors. Constrain input and output types to high-precision numeric tensors.
.. _l-onnx-op-sub-6: Sub - 6 ======= **Version** * **name**: `Sub (GitHub) `_ * **domain**: **main** * **since_version**: **6** * **function**: False * **support_level**: SupportType.COMMON * **shape inference**: True This version of the operator has been available **since version 6**. **Summary** Performs element-wise binary subtraction (with limited broadcast support). If necessary the right-hand-side argument will be broadcasted to match the shape of left-hand-side argument. When broadcasting is specified, the second tensor can either be of element size 1 (including a scalar tensor and any tensor with rank equal to or smaller than the first tensor), or having its shape as a contiguous subset of the first tensor's shape. The starting of the mutually equal shape is specified by the argument "axis", and if it is not set, suffix matching is assumed. 1-dim expansion doesn't work yet. For example, the following tensor shapes are supported (with broadcast=1): shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor shape(A) = (2, 3, 4, 5), shape(B) = (5,) shape(A) = (2, 3, 4, 5), shape(B) = (4, 5) shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1 shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0 Attribute `broadcast=1` needs to be passed to enable broadcasting. **Attributes** * **axis**: If set, defines the broadcast dimensions. See doc for details. * **broadcast**: Pass 1 to enable broadcasting Default value is ``0``. **Inputs** * **A** (heterogeneous) - **T**: First operand, should share the type with the second operand. * **B** (heterogeneous) - **T**: Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size. **Outputs** * **C** (heterogeneous) - **T**: Result, has same dimensions and type as A **Type Constraints** * **T** in ( tensor(double), tensor(float), tensor(float16), tensor(int32), tensor(int64), tensor(uint32), tensor(uint64) ): Constrain input and output types to high-precision numeric tensors. **Differences** .. raw:: html
00Performs element-wise binary subtraction (with limited broadcast support).Performs element-wise binary subtraction (with limited broadcast support).
11
22If necessary the right-hand-side argument will be broadcasted to match theIf necessary the right-hand-side argument will be broadcasted to match the
33shape of left-hand-side argument. When broadcasting is specified, the secondshape of left-hand-side argument. When broadcasting is specified, the second
44tensor can either be of element size 1 (including a scalar tensor and anytensor can either be of element size 1 (including a scalar tensor and any
55tensor with rank equal to or smaller than the first tensor), or having itstensor with rank equal to or smaller than the first tensor), or having its
66shape as a contiguous subset of the first tensor's shape. The starting of theshape as a contiguous subset of the first tensor's shape. The starting of the
77mutually equal shape is specified by the argument "axis", and if it is not set,mutually equal shape is specified by the argument "axis", and if it is not set,
88suffix matching is assumed. 1-dim expansion doesn't work yet.suffix matching is assumed. 1-dim expansion doesn't work yet.
99
1010For example, the following tensor shapes are supported (with broadcast=1):For example, the following tensor shapes are supported (with broadcast=1):
1111
1212 shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor
1313 shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor
1414 shape(A) = (2, 3, 4, 5), shape(B) = (5,) shape(A) = (2, 3, 4, 5), shape(B) = (5,)
1515 shape(A) = (2, 3, 4, 5), shape(B) = (4, 5) shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)
1616 shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1 shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1
1717 shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0 shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0
1818
1919Attribute broadcast=1 needs to be passed to enable broadcasting.Attribute broadcast=1 needs to be passed to enable broadcasting.
2020
2121**Attributes****Attributes**
2222
2323* **axis**:* **axis**:
2424 If set, defines the broadcast dimensions. See doc for details. If set, defines the broadcast dimensions. See doc for details.
2525* **broadcast**:* **broadcast**:
2626 Pass 1 to enable broadcasting Default value is 0. Pass 1 to enable broadcasting Default value is 0.
27* **consumed_inputs**:
28 legacy optimization attribute.
2927
3028**Inputs****Inputs**
3129
3230* **A** (heterogeneous) - **T**:* **A** (heterogeneous) - **T**:
3331 First operand, should share the type with the second operand. First operand, should share the type with the second operand.
3432* **B** (heterogeneous) - **T**:* **B** (heterogeneous) - **T**:
3533 Second operand. With broadcasting can be of smaller size than A. If Second operand. With broadcasting can be of smaller size than A. If
3634 broadcasting is disabled it should be of the same size. broadcasting is disabled it should be of the same size.
3735
3836**Outputs****Outputs**
3937
4038* **C** (heterogeneous) - **T**:* **C** (heterogeneous) - **T**:
4139 Result, has same dimensions and type as A Result, has same dimensions and type as A
4240
4341**Type Constraints****Type Constraints**
4442
4543* **T** in (* **T** in (
4644 tensor(double), tensor(double),
4745 tensor(float), tensor(float),
4846 tensor(float16) tensor(float16),
47 tensor(int32),
48 tensor(int64),
49 tensor(uint32),
50 tensor(uint64)
4951 ): ):
5052 Constrain input and output types to float tensors. Constrain input and output types to high-precision numeric tensors.
.. _l-onnx-op-sub-1: Sub - 1 ======= **Version** * **name**: `Sub (GitHub) `_ * **domain**: **main** * **since_version**: **1** * **function**: False * **support_level**: SupportType.COMMON * **shape inference**: False This version of the operator has been available **since version 1**. **Summary** Performs element-wise binary subtraction (with limited broadcast support). If necessary the right-hand-side argument will be broadcasted to match the shape of left-hand-side argument. When broadcasting is specified, the second tensor can either be of element size 1 (including a scalar tensor and any tensor with rank equal to or smaller than the first tensor), or having its shape as a contiguous subset of the first tensor's shape. The starting of the mutually equal shape is specified by the argument "axis", and if it is not set, suffix matching is assumed. 1-dim expansion doesn't work yet. For example, the following tensor shapes are supported (with broadcast=1): shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor shape(A) = (2, 3, 4, 5), shape(B) = (5,) shape(A) = (2, 3, 4, 5), shape(B) = (4, 5) shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1 shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0 Attribute `broadcast=1` needs to be passed to enable broadcasting. **Attributes** * **axis**: If set, defines the broadcast dimensions. See doc for details. * **broadcast**: Pass 1 to enable broadcasting Default value is ``0``. * **consumed_inputs**: legacy optimization attribute. **Inputs** * **A** (heterogeneous) - **T**: First operand, should share the type with the second operand. * **B** (heterogeneous) - **T**: Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size. **Outputs** * **C** (heterogeneous) - **T**: Result, has same dimensions and type as A **Type Constraints** * **T** in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.