.. _l-onnx-doc-Cast: ==== Cast ==== .. contents:: :local: .. _l-onnx-op-cast-13: Cast - 13 ========= **Version** * **name**: `Cast (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** The operator casts the elements of a given input tensor to a data type specified by the 'to' argument and returns an output tensor of the same size in the converted type. The 'to' argument must be one of the data types specified in the 'DataType' enum field in the TensorProto message. Casting from string tensor in plain (e.g., "3.14" and "1000") and scientific numeric representations (e.g., "1e-5" and "1E8") to float types is supported. For example, converting string "100.5" to an integer may result 100. There are some string literals reserved for special floating-point values; "+INF" (and "INF"), "-INF", and "NaN" are positive infinity, negative infinity, and not-a-number, respectively. Any string which can exactly match "+INF" in a case-insensitive way would be mapped to positive infinite. Similarly, this case-insensitive rule is applied to "INF" and "NaN". When casting from numeric tensors to string tensors, plain floating-point representation (such as "314.15926") would be used. Converting non-numerical-literal string such as "Hello World!" is an undefined behavior. Cases of converting string representing floating-point arithmetic value, such as "2.718", to INT is an undefined behavior. Conversion from a numerical type to any numerical type is always allowed. User must be aware of precision loss and value change caused by range difference between two types. For example, a 64-bit float 3.1415926459 may be round to a 32-bit float 3.141592. Similarly, converting an integer 36 to Boolean may produce 1 because we truncate bits which can't be stored in the targeted type. In more detail, the conversion among numerical types should follow these rules: * Casting from floating point to: * floating point: +/- infinity if OOR (out of range). * fixed point: undefined if OOR. * bool: +/- 0.0 to False; all else to True. * Casting from fixed point to: * floating point: +/- infinity if OOR. (+ infinity in the case of uint) * fixed point: when OOR, discard higher bits and reinterpret (with respect to two's complement representation for signed types). For example, 200 (int16) -> -56 (int8). * bool: zero to False; nonzero to True. * Casting from bool to: * floating point: `{1.0, 0.0}`. * fixed point: `{1, 0}`. * bool: no change. **Attributes** * **to** (required): The data type to which the elements of the input tensor are cast. Strictly must be one of the types from DataType enum in TensorProto **Inputs** * **input** (heterogeneous) - **T1**: Input tensor to be cast. **Outputs** * **output** (heterogeneous) - **T2**: Output tensor with the same shape as input with type specified by the 'to' argument **Type Constraints** * **T1** in ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input types. Casting from complex is not supported. * **T2** in ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain output types. Casting to complex is not supported. **Examples** **default** :: shape = (3, 4) test_cases = [ ("FLOAT", "FLOAT16"), ("FLOAT", "DOUBLE"), ("FLOAT16", "FLOAT"), ("FLOAT16", "DOUBLE"), ("DOUBLE", "FLOAT"), ("DOUBLE", "FLOAT16"), ("FLOAT", "STRING"), ("STRING", "FLOAT"), ("FLOAT", "BFLOAT16"), ("BFLOAT16", "FLOAT"), ] for from_type, to_type in test_cases: input_type_proto = None output_type_proto = None if "BFLOAT16" == from_type or "BFLOAT16" == to_type: np_fp32 = np.array( [ "0.47892547", "0.48033667", "0.49968487", "0.81910545", "0.47031248", "0.816468", "0.21087195", "0.7229038", "NaN", "INF", "+INF", "-INF", ], dtype=np.float32, ) little_endisan = sys.byteorder == "little" np_uint16_view = np_fp32.view(dtype=np.uint16) np_bfp16 = ( np_uint16_view[1::2] if little_endisan else np_uint16_view[0::2] ) if "BFLOAT16" == to_type: assert from_type == "FLOAT" input = np_fp32.reshape([3, 4]) output = np_bfp16.reshape([3, 4]) input_type_proto = onnx.helper.make_tensor_type_proto( int(TensorProto.FLOAT), input.shape ) output_type_proto = onnx.helper.make_tensor_type_proto( int(TensorProto.BFLOAT16), output.shape ) else: assert to_type == "FLOAT" input = np_bfp16.reshape([3, 4]) # convert bfloat to FLOAT np_fp32_zeros = np.zeros((len(np_bfp16) * 2,), dtype=np.uint16) if little_endisan: np_fp32_zeros[1::2] = np_bfp16 else: np_fp32_zeros[0::2] = np_bfp16 np_fp32_from_bfloat = np_fp32_zeros.view(dtype=np.float32) output = np_fp32_from_bfloat.reshape([3, 4]) input_type_proto = onnx.helper.make_tensor_type_proto( int(TensorProto.BFLOAT16), input.shape ) output_type_proto = onnx.helper.make_tensor_type_proto( int(TensorProto.FLOAT), output.shape ) elif "STRING" != from_type: input = np.random.random_sample(shape).astype( helper.tensor_dtype_to_np_dtype(getattr(TensorProto, from_type)) ) if "STRING" == to_type: # Converting input to str, then give it object dtype for generating script ss = [] for i in input.flatten(): s = str(i).encode("utf-8") su = s.decode("utf-8") ss.append(su) output = np.array(ss).astype(object).reshape([3, 4]) else: output = input.astype( helper.tensor_dtype_to_np_dtype(getattr(TensorProto, to_type)) ) else: input = np.array( [ "0.47892547", "0.48033667", "0.49968487", "0.81910545", "0.47031248", "0.816468", "0.21087195", "0.7229038", "NaN", "INF", "+INF", "-INF", ], dtype=np.dtype(object), ).reshape([3, 4]) output = input.astype( helper.tensor_dtype_to_np_dtype(getattr(TensorProto, to_type)) ) node = onnx.helper.make_node( "Cast", inputs=["input"], outputs=["output"], to=getattr(TensorProto, to_type), ) if input_type_proto and output_type_proto: expect( node, inputs=[input], outputs=[output], name="test_cast_" + from_type + "_to_" + to_type, input_type_protos=[input_type_proto], output_type_protos=[output_type_proto], ) else: expect( node, inputs=[input], outputs=[output], name="test_cast_" + from_type + "_to_" + to_type, ) **Differences** .. raw:: html
00The operator casts the elements of a given input tensor to a data typeThe operator casts the elements of a given input tensor to a data type
11specified by the 'to' argument and returns an output tensor of the same size inspecified by the 'to' argument and returns an output tensor of the same size in
22the converted type. The 'to' argument must be one of the data types specifiedthe converted type. The 'to' argument must be one of the data types specified
33in the 'DataType' enum field in the TensorProto message.in the 'DataType' enum field in the TensorProto message.
44
55Casting from string tensor in plain (e.g., "3.14" and "1000") and scientific numeric representationsCasting from string tensor in plain (e.g., "3.14" and "1000") and scientific numeric representations
66(e.g., "1e-5" and "1E8") to float types is supported. For example, converting string "100.5" to an integer may(e.g., "1e-5" and "1E8") to float types is supported. For example, converting string "100.5" to an integer may
77result 100. There are some string literals reserved for special floating-point values;result 100. There are some string literals reserved for special floating-point values;
88"+INF" (and "INF"), "-INF", and "NaN" are positive infinity, negative infinity, and not-a-number, respectively."+INF" (and "INF"), "-INF", and "NaN" are positive infinity, negative infinity, and not-a-number, respectively.
99Any string which can exactly match "+INF" in a case-insensitive way would be mapped to positive infinite. Similarly,Any string which can exactly match "+INF" in a case-insensitive way would be mapped to positive infinite. Similarly,
1010this case-insensitive rule is applied to "INF" and "NaN". When casting from numeric tensorsthis case-insensitive rule is applied to "INF" and "NaN". When casting from numeric tensors
1111to string tensors, plain floating-point representation (such as "314.15926") would be used.to string tensors, plain floating-point representation (such as "314.15926") would be used.
1212Converting non-numerical-literal string such as "Hello World!" is an undefined behavior. CasesConverting non-numerical-literal string such as "Hello World!" is an undefined behavior. Cases
1313of converting string representing floating-point arithmetic value, such as "2.718", to INT is an undefined behavior.of converting string representing floating-point arithmetic value, such as "2.718", to INT is an undefined behavior.
1414
1515Conversion from a numerical type to any numerical type is always allowed.Conversion from a numerical type to any numerical type is always allowed.
1616User must be aware of precision loss and value change caused by range difference between two types.User must be aware of precision loss and value change caused by range difference between two types.
1717For example, a 64-bit float 3.1415926459 may be round to a 32-bit float 3.141592. Similarly, convertingFor example, a 64-bit float 3.1415926459 may be round to a 32-bit float 3.141592. Similarly, converting
1818an integer 36 to Boolean may produce 1 because we truncate bits which can't be stored in the targeted type.an integer 36 to Boolean may produce 1 because we truncate bits which can't be stored in the targeted type.
1919
20In more detail, the conversion among numerical types should follow these rules:
21
2022**Attributes*** Casting from floating point to:
23 * floating point: +/- infinity if OOR (out of range).
24 * fixed point: undefined if OOR.
25 * bool: +/- 0.0 to False; all else to True.
26* Casting from fixed point to:
27 * floating point: +/- infinity if OOR. (+ infinity in the case of uint)
28 * fixed point: when OOR, discard higher bits and reinterpret (with respect to two's complement representation for
29signed types). For example, 200 (int16) -> -56 (int8).
30 * bool: zero to False; nonzero to True.
31* Casting from bool to:
32 * floating point: {1.0, 0.0}.
33 * fixed point: {1, 0}.
34 * bool: no change.
2135
36**Attributes**
37
2238* **to** (required):* **to** (required):
2339 The data type to which the elements of the input tensor are cast. The data type to which the elements of the input tensor are cast.
2440 Strictly must be one of the types from DataType enum in TensorProto Strictly must be one of the types from DataType enum in TensorProto
2541
2642**Inputs****Inputs**
2743
2844* **input** (heterogeneous) - **T1**:* **input** (heterogeneous) - **T1**:
2945 Input tensor to be cast. Input tensor to be cast.
3046
3147**Outputs****Outputs**
3248
3349* **output** (heterogeneous) - **T2**:* **output** (heterogeneous) - **T2**:
3450 Output tensor with the same shape as input with type specified by Output tensor with the same shape as input with type specified by
3551 the 'to' argument the 'to' argument
3652
3753**Type Constraints****Type Constraints**
3854
3955* **T1** in (* **T1** in (
56 tensor(bfloat16),
4057 tensor(bool), tensor(bool),
4158 tensor(double), tensor(double),
4259 tensor(float), tensor(float),
4360 tensor(float16), tensor(float16),
4461 tensor(int16), tensor(int16),
4562 tensor(int32), tensor(int32),
4663 tensor(int64), tensor(int64),
4764 tensor(int8), tensor(int8),
4865 tensor(string), tensor(string),
4966 tensor(uint16), tensor(uint16),
5067 tensor(uint32), tensor(uint32),
5168 tensor(uint64), tensor(uint64),
5269 tensor(uint8) tensor(uint8)
5370 ): ):
5471 Constrain input types. Casting from complex is not supported. Constrain input types. Casting from complex is not supported.
5572* **T2** in (* **T2** in (
73 tensor(bfloat16),
5674 tensor(bool), tensor(bool),
5775 tensor(double), tensor(double),
5876 tensor(float), tensor(float),
5977 tensor(float16), tensor(float16),
6078 tensor(int16), tensor(int16),
6179 tensor(int32), tensor(int32),
6280 tensor(int64), tensor(int64),
6381 tensor(int8), tensor(int8),
6482 tensor(string), tensor(string),
6583 tensor(uint16), tensor(uint16),
6684 tensor(uint32), tensor(uint32),
6785 tensor(uint64), tensor(uint64),
6886 tensor(uint8) tensor(uint8)
6987 ): ):
7088 Constrain output types. Casting to complex is not supported. Constrain output types. Casting to complex is not supported.
.. _l-onnx-op-cast-9: Cast - 9 ======== **Version** * **name**: `Cast (GitHub) `_ * **domain**: **main** * **since_version**: **9** * **function**: False * **support_level**: SupportType.COMMON * **shape inference**: True This version of the operator has been available **since version 9**. **Summary** The operator casts the elements of a given input tensor to a data type specified by the 'to' argument and returns an output tensor of the same size in the converted type. The 'to' argument must be one of the data types specified in the 'DataType' enum field in the TensorProto message. Casting from string tensor in plain (e.g., "3.14" and "1000") and scientific numeric representations (e.g., "1e-5" and "1E8") to float types is supported. For example, converting string "100.5" to an integer may result 100. There are some string literals reserved for special floating-point values; "+INF" (and "INF"), "-INF", and "NaN" are positive infinity, negative infinity, and not-a-number, respectively. Any string which can exactly match "+INF" in a case-insensitive way would be mapped to positive infinite. Similarly, this case-insensitive rule is applied to "INF" and "NaN". When casting from numeric tensors to string tensors, plain floating-point representation (such as "314.15926") would be used. Converting non-numerical-literal string such as "Hello World!" is an undefined behavior. Cases of converting string representing floating-point arithmetic value, such as "2.718", to INT is an undefined behavior. Conversion from a numerical type to any numerical type is always allowed. User must be aware of precision loss and value change caused by range difference between two types. For example, a 64-bit float 3.1415926459 may be round to a 32-bit float 3.141592. Similarly, converting an integer 36 to Boolean may produce 1 because we truncate bits which can't be stored in the targeted type. **Attributes** * **to** (required): The data type to which the elements of the input tensor are cast. Strictly must be one of the types from DataType enum in TensorProto **Inputs** * **input** (heterogeneous) - **T1**: Input tensor to be cast. **Outputs** * **output** (heterogeneous) - **T2**: Output tensor with the same shape as input with type specified by the 'to' argument **Type Constraints** * **T1** in ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input types. Casting from complex is not supported. * **T2** in ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain output types. Casting to complex is not supported. **Differences** .. raw:: html
00The operator casts the elements of a given input tensor to a data typeThe operator casts the elements of a given input tensor to a data type
11specified by the 'to' argument and returns an output tensor of the same size inspecified by the 'to' argument and returns an output tensor of the same size in
22the converted type. The 'to' argument must be one of the data types specifiedthe converted type. The 'to' argument must be one of the data types specified
33in the 'DataType' enum field in the TensorProto message.in the 'DataType' enum field in the TensorProto message.
4
5Casting from string tensor in plain (e.g., "3.14" and "1000") and scientific numeric representations
6(e.g., "1e-5" and "1E8") to float types is supported. For example, converting string "100.5" to an integer may
7result 100. There are some string literals reserved for special floating-point values;
8"+INF" (and "INF"), "-INF", and "NaN" are positive infinity, negative infinity, and not-a-number, respectively.
49NOTE: Casting to and from strings is not supported yet.Any string which can exactly match "+INF" in a case-insensitive way would be mapped to positive infinite. Similarly,
10this case-insensitive rule is applied to "INF" and "NaN". When casting from numeric tensors
11to string tensors, plain floating-point representation (such as "314.15926") would be used.
12Converting non-numerical-literal string such as "Hello World!" is an undefined behavior. Cases
13of converting string representing floating-point arithmetic value, such as "2.718", to INT is an undefined behavior.
514
15Conversion from a numerical type to any numerical type is always allowed.
16User must be aware of precision loss and value change caused by range difference between two types.
617**Attributes**For example, a 64-bit float 3.1415926459 may be round to a 32-bit float 3.141592. Similarly, converting
18an integer 36 to Boolean may produce 1 because we truncate bits which can't be stored in the targeted type.
719
20**Attributes**
21
822* **to** (required):* **to** (required):
923 The data type to which the elements of the input tensor are cast. The data type to which the elements of the input tensor are cast.
1024 Strictly must be one of the types from DataType enum in TensorProto Strictly must be one of the types from DataType enum in TensorProto
1125
1226**Inputs****Inputs**
1327
1428* **input** (heterogeneous) - **T1**:* **input** (heterogeneous) - **T1**:
1529 Input tensor to be cast. Input tensor to be cast.
1630
1731**Outputs****Outputs**
1832
1933* **output** (heterogeneous) - **T2**:* **output** (heterogeneous) - **T2**:
2034 Output tensor with the same shape as input with type specified by Output tensor with the same shape as input with type specified by
2135 the 'to' argument the 'to' argument
2236
2337**Type Constraints****Type Constraints**
2438
2539* **T1** in (* **T1** in (
2640 tensor(bool), tensor(bool),
2741 tensor(double), tensor(double),
2842 tensor(float), tensor(float),
2943 tensor(float16), tensor(float16),
3044 tensor(int16), tensor(int16),
3145 tensor(int32), tensor(int32),
3246 tensor(int64), tensor(int64),
3347 tensor(int8), tensor(int8),
48 tensor(string),
3449 tensor(uint16), tensor(uint16),
3550 tensor(uint32), tensor(uint32),
3651 tensor(uint64), tensor(uint64),
3752 tensor(uint8) tensor(uint8)
3853 ): ):
3954 Constrain input types. Casting from strings and complex are not Constrain input types. Casting from complex is not supported.
40 supported.
4155* **T2** in (* **T2** in (
4256 tensor(bool), tensor(bool),
4357 tensor(double), tensor(double),
4458 tensor(float), tensor(float),
4559 tensor(float16), tensor(float16),
4660 tensor(int16), tensor(int16),
4761 tensor(int32), tensor(int32),
4862 tensor(int64), tensor(int64),
4963 tensor(int8), tensor(int8),
64 tensor(string),
5065 tensor(uint16), tensor(uint16),
5166 tensor(uint32), tensor(uint32),
5267 tensor(uint64), tensor(uint64),
5368 tensor(uint8) tensor(uint8)
5469 ): ):
5570 Constrain output types. Casting to strings and complex are not Constrain output types. Casting to complex is not supported.
56 supported.
.. _l-onnx-op-cast-6: Cast - 6 ======== **Version** * **name**: `Cast (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** The operator casts the elements of a given input tensor to a data type specified by the 'to' argument and returns an output tensor of the same size in the converted type. The 'to' argument must be one of the data types specified in the 'DataType' enum field in the TensorProto message. NOTE: Casting to and from strings is not supported yet. **Attributes** * **to** (required): The data type to which the elements of the input tensor are cast. Strictly must be one of the types from DataType enum in TensorProto **Inputs** * **input** (heterogeneous) - **T1**: Input tensor to be cast. **Outputs** * **output** (heterogeneous) - **T2**: Output tensor with the same shape as input with type specified by the 'to' argument **Type Constraints** * **T1** in ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input types. Casting from strings and complex are not supported. * **T2** in ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain output types. Casting to strings and complex are not supported. **Differences** .. raw:: html
00The operator casts the elements of a given input tensor to a data typeThe operator casts the elements of a given input tensor to a data type
11specified by the 'to' argument and returns an output tensor of the same size inspecified by the 'to' argument and returns an output tensor of the same size in
22the converted type. The 'to' argument must be one of the data types specifiedthe converted type. The 'to' argument must be one of the data types specified
33in the 'DataType' enum field in the TensorProto message.in the 'DataType' enum field in the TensorProto message.
44NOTE: Casting to and from strings is not supported yet.NOTE: Casting to and from strings is not supported yet.
55
66**Attributes****Attributes**
77
88* **to** (required):* **to** (required):
99 The data type to which the elements of the input tensor are cast. The data type to which the elements of the input tensor are cast.
1010 Strictly must be one of the types from DataType enum in TensorProto Strictly must be one of the types from DataType enum in TensorProto
1111
1212**Inputs****Inputs**
1313
1414* **input** (heterogeneous) - **T1**:* **input** (heterogeneous) - **T1**:
1515 Input tensor to be cast. Input tensor to be cast.
1616
1717**Outputs****Outputs**
1818
1919* **output** (heterogeneous) - **T2**:* **output** (heterogeneous) - **T2**:
2020 Output tensor with the same shape as input with type specified by Output tensor with the same shape as input with type specified by
2121 the 'to' argument the 'to' argument
2222
2323**Type Constraints****Type Constraints**
2424
2525* **T1** in (* **T1** in (
2626 tensor(bool), tensor(bool),
2727 tensor(double), tensor(double),
2828 tensor(float), tensor(float),
2929 tensor(float16), tensor(float16),
3030 tensor(int16), tensor(int16),
3131 tensor(int32), tensor(int32),
3232 tensor(int64), tensor(int64),
3333 tensor(int8), tensor(int8),
3434 tensor(uint16), tensor(uint16),
3535 tensor(uint32), tensor(uint32),
3636 tensor(uint64), tensor(uint64),
3737 tensor(uint8) tensor(uint8)
3838 ): ):
3939 Constrain input types. Casting from strings and complex are not Constrain input types. Casting from strings and complex are not
4040 supported. supported.
4141* **T2** in (* **T2** in (
4242 tensor(bool), tensor(bool),
4343 tensor(double), tensor(double),
4444 tensor(float), tensor(float),
4545 tensor(float16), tensor(float16),
4646 tensor(int16), tensor(int16),
4747 tensor(int32), tensor(int32),
4848 tensor(int64), tensor(int64),
4949 tensor(int8), tensor(int8),
5050 tensor(uint16), tensor(uint16),
5151 tensor(uint32), tensor(uint32),
5252 tensor(uint64), tensor(uint64),
5353 tensor(uint8) tensor(uint8)
5454 ): ):
5555 Constrain output types. Casting to strings and complex are not Constrain output types. Casting to strings and complex are not
5656 supported. supported.
.. _l-onnx-op-cast-1: Cast - 1 ======== **Version** * **name**: `Cast (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** The operator casts the elements of a given input tensor to a data type specified by the 'to' argument and returns an output tensor of the same size in the converted type. The 'to' argument must be one of the data types specified in the 'DataType' enum field in the TensorProto message. NOTE: Casting to and from strings is not supported yet. **Attributes** * **to** (required): The data type to which the elements of the input tensor are cast. Strictly must be one of the types from DataType enum in TensorProto **Inputs** * **input** (heterogeneous) - **T1**: Input tensor to be cast. **Outputs** * **output** (heterogeneous) - **T2**: Output tensor with the same shape as input with type specified by the 'to' argument **Type Constraints** * **T1** in ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input types. Casting from strings and complex are not supported. * **T2** in ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain output types. Casting to strings and complex are not supported.