(l-onnx-doc-Clip)= # Clip (l-onnx-op-clip-13)= ## Clip - 13 ### Version - **name**: [Clip (GitHub)](https://github.com/onnx/onnx/blob/main/docs/Operators.md#Clip) - **domain**: `main` - **since_version**: `13` - **function**: `True` - **support_level**: `SupportType.COMMON` - **shape inference**: `True` This version of the operator has been available **since version 13**. ### Summary Clip operator limits the given input within an interval. The interval is specified by the inputs 'min' and 'max'. They default to numeric_limits::lowest() and numeric_limits::max(), respectively. When 'min' is greater than 'max', the clip operator sets all the 'input' values to the value of 'max'. Thus, this is equivalent to 'Min(max, Max(input, min))'. ### Inputs Between 1 and 3 inputs. - **input** (heterogeneous) - **T**: Input tensor whose elements to be clipped - **min** (optional, heterogeneous) - **T**: Minimum value, under which element is replaced by min. It must be a scalar(tensor of empty shape). - **max** (optional, heterogeneous) - **T**: Maximum value, above which element is replaced by max. It must be a scalar(tensor of empty shape). ### Outputs - **output** (heterogeneous) - **T**: Output tensor with clipped input elements ### 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 ```python import numpy as np import onnx node = onnx.helper.make_node( "Clip", inputs=["x", "min", "max"], outputs=["y"], ) x = np.array([-2, 0, 2]).astype(np.float32) min_val = np.float32(-1) max_val = np.float32(1) y = np.clip(x, min_val, max_val) # expected output [-1., 0., 1.] expect( node, inputs=[x, min_val, max_val], outputs=[y], name="test_clip_example" ) x = np.random.randn(3, 4, 5).astype(np.float32) y = np.clip(x, min_val, max_val) expect(node, inputs=[x, min_val, max_val], outputs=[y], name="test_clip") node = onnx.helper.make_node( "Clip", inputs=["x", "min", "max"], outputs=["y"], ) min_val = np.float32(-5) max_val = np.float32(5) x = np.array([-1, 0, 1]).astype(np.float32) y = np.array([-1, 0, 1]).astype(np.float32) expect( node, inputs=[x, min_val, max_val], outputs=[y], name="test_clip_inbounds" ) x = np.array([-6, 0, 6]).astype(np.float32) y = np.array([-5, 0, 5]).astype(np.float32) expect( node, inputs=[x, min_val, max_val], outputs=[y], name="test_clip_outbounds" ) x = np.array([-1, 0, 6]).astype(np.float32) y = np.array([-1, 0, 5]).astype(np.float32) expect( node, inputs=[x, min_val, max_val], outputs=[y], name="test_clip_splitbounds", ) x = np.array([-2, 0, 6]).astype(np.float32) y = np.array([1, 1, 1]).astype(np.float32) min_val = np.float32(2) max_val = np.float32(1) expect( node, inputs=[x, min_val, max_val], outputs=[y], name="test_clip_min_greater_than_max", ) ``` #### _clip_default ```python import numpy as np import onnx node = onnx.helper.make_node( "Clip", inputs=["x", "min"], outputs=["y"], ) min_val = np.float32(0) x = np.random.randn(3, 4, 5).astype(np.float32) y = np.clip(x, min_val, np.inf) expect(node, inputs=[x, min_val], outputs=[y], name="test_clip_default_min") no_min = "" # optional input, not supplied node = onnx.helper.make_node( "Clip", inputs=["x", no_min, "max"], outputs=["y"], ) max_val = np.float32(0) x = np.random.randn(3, 4, 5).astype(np.float32) y = np.clip(x, -np.inf, max_val) expect(node, inputs=[x, max_val], outputs=[y], name="test_clip_default_max") no_max = "" # optional input, not supplied node = onnx.helper.make_node( "Clip", inputs=["x", no_min, no_max], outputs=["y"], ) x = np.array([-1, 0, 1]).astype(np.float32) y = np.array([-1, 0, 1]).astype(np.float32) expect(node, inputs=[x], outputs=[y], name="test_clip_default_inbounds") ``` #### _clip_default_int8 ```python import numpy as np import onnx node = onnx.helper.make_node( "Clip", inputs=["x", "min"], outputs=["y"], ) min_val = np.int8(0) x = np.random.randn(3, 4, 5).astype(np.int8) y = np.clip(x, min_val, np.iinfo(np.int8).max) expect( node, inputs=[x, min_val], outputs=[y], name="test_clip_default_int8_min" ) no_min = "" # optional input, not supplied node = onnx.helper.make_node( "Clip", inputs=["x", no_min, "max"], outputs=["y"], ) max_val = np.int8(0) x = np.random.randn(3, 4, 5).astype(np.int8) y = np.clip(x, np.iinfo(np.int8).min, max_val) expect( node, inputs=[x, max_val], outputs=[y], name="test_clip_default_int8_max" ) no_max = "" # optional input, not supplied node = onnx.helper.make_node( "Clip", inputs=["x", no_min, no_max], outputs=["y"], ) x = np.array([-1, 0, 1]).astype(np.int8) y = np.array([-1, 0, 1]).astype(np.int8) expect(node, inputs=[x], outputs=[y], name="test_clip_default_int8_inbounds") ``` ```{toctree} text_diff_Clip_12_13 ``` (l-onnx-op-clip-12)= ## Clip - 12 ### Version - **name**: [Clip (GitHub)](https://github.com/onnx/onnx/blob/main/docs/Operators.md#Clip) - **domain**: `main` - **since_version**: `12` - **function**: `False` - **support_level**: `SupportType.COMMON` - **shape inference**: `True` This version of the operator has been available **since version 12**. ### Summary Clip operator limits the given input within an interval. The interval is specified by the inputs 'min' and 'max'. They default to numeric_limits::lowest() and numeric_limits::max(), respectively. ### Inputs Between 1 and 3 inputs. - **input** (heterogeneous) - **T**: Input tensor whose elements to be clipped - **min** (optional, heterogeneous) - **T**: Minimum value, under which element is replaced by min. It must be a scalar(tensor of empty shape). - **max** (optional, heterogeneous) - **T**: Maximum value, above which element is replaced by max. It must be a scalar(tensor of empty shape). ### Outputs - **output** (heterogeneous) - **T**: Output tensor with clipped input elements ### Type Constraints * **T** in ( `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. ```{toctree} text_diff_Clip_11_13 text_diff_Clip_11_12 ``` (l-onnx-op-clip-11)= ## Clip - 11 ### Version - **name**: [Clip (GitHub)](https://github.com/onnx/onnx/blob/main/docs/Operators.md#Clip) - **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 Clip operator limits the given input within an interval. The interval is specified by the inputs 'min' and 'max'. They default to numeric_limits::lowest() and numeric_limits::max(), respectively. ### Inputs Between 1 and 3 inputs. - **input** (heterogeneous) - **T**: Input tensor whose elements to be clipped - **min** (optional, heterogeneous) - **T**: Minimum value, under which element is replaced by min. It must be a scalar(tensor of empty shape). - **max** (optional, heterogeneous) - **T**: Maximum value, above which element is replaced by max. It must be a scalar(tensor of empty shape). ### Outputs - **output** (heterogeneous) - **T**: Output tensor with clipped input elements ### Type Constraints * **T** in ( `tensor(double)`, `tensor(float)`, `tensor(float16)` ): Constrain input and output types to float tensors. ```{toctree} text_diff_Clip_6_13 text_diff_Clip_6_12 text_diff_Clip_6_11 ``` (l-onnx-op-clip-6)= ## Clip - 6 ### Version - **name**: [Clip (GitHub)](https://github.com/onnx/onnx/blob/main/docs/Operators.md#Clip) - **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 Clip operator limits the given input within an interval. The interval is specified with arguments 'min' and 'max'. They default to numeric_limits::lowest() and numeric_limits::max() respectively. ### Attributes * **max - FLOAT** (default is `(3.402823e+38)`): Maximum value, above which element is replaced by max * **min - FLOAT** (default is `(-3.402823e+38)`): Minimum value, under which element is replaced by min ### Inputs - **input** (heterogeneous) - **T**: Input tensor whose elements to be clipped ### Outputs - **output** (heterogeneous) - **T**: Output tensor with clipped input elements ### Type Constraints * **T** in ( `tensor(double)`, `tensor(float)`, `tensor(float16)` ): Constrain input and output types to float tensors. ```{toctree} text_diff_Clip_1_13 text_diff_Clip_1_12 text_diff_Clip_1_11 text_diff_Clip_1_6 ``` (l-onnx-op-clip-1)= ## Clip - 1 ### Version - **name**: [Clip (GitHub)](https://github.com/onnx/onnx/blob/main/docs/Operators.md#Clip) - **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 Clip operator limits the given input within an interval. The interval is specified with arguments 'min' and 'max'. They default to numeric_limits::lowest() and numeric_limits::max() respectively. ### Attributes * **consumed_inputs - INTS** : legacy optimization attribute. * **max - FLOAT** : Maximum value, above which element is replaced by max * **min - FLOAT** : Minimum value, under which element is replaced by min ### Inputs - **input** (heterogeneous) - **T**: Input tensor whose elements to be clipped ### Outputs - **output** (heterogeneous) - **T**: Output tensor with clipped input elements ### Type Constraints * **T** in ( `tensor(double)`, `tensor(float)`, `tensor(float16)` ): Constrain input and output types to float tensors.