reinterpret_cast_to
Description
Reinterprets a Tensor into a specified data type.
Although this API reinterprets the memory bytes of a Tensor, it does not perform precision conversion, which is provided by the vec_conv API instead.
Assuming that there are 128 elements of type float16, the reinterpret_cast_to call can read the data in float32, resulting in 64 elements of type float32. Therefore, reinterpret_cast_to does not actually convert the precision.
Prototype
reinterpret_cast_to(dtype)
Parameters
Parameter |
Input/Output |
Description |
|---|---|---|
dtype |
Input |
Data type of the Tensor object, such as: uint8, int8, uint16, int16, float16, uint32, int32, float32, uint64, int64 |
Applicability
Restrictions
- To make it easier to describe the restrictions in using reinterpret_cast_to, we define a factor yielded by dividing the number of bits of the original data type by those of the specified data type. Assume the original tensor is declared to have 128 float16 data entries in the buffer. To read the entries in float32 mode, the factor should be 0.5 (16/32). The call to reinterpret_cast_to() must meet the following restrictions:
- The factor must be greater than 0.
- If the factor is greater than 1, it must be an integer.
- If the factor is less than 1, pay attention to the tensor shape. The last dimension size (shape[-1]) multiplied by the factor must be an integer. Assume the original data is 128 float16 elements and the shape is set to (128, 1). To read the 128 float16 elements in float32 mode, shape[-1] * factor = 1 * 0.5 = 0.5, which is not an integer and therefore the preceding restriction is not met. In this case, the error message "Error: Last dimension can't be divided" will be reported. Setting the tensor shape to 128 can avoid this error.
Returns
A new Tensor.
Example
Example 1:
from tbe import tik
tik_instance = tik.Tik()
data_A = tik_instance.Tensor("float16", (16,), name="data_A", scope=tik.scope_gm)
data_B = data_A.reinterpret_cast_to("uint32")
data_C = data_B.reinterpret_cast_to("float16")
"""Example:
Input:
data_A:
[ 4.812e+00 1.870e-04 -5.692e-02 2.528e-02 -9.225e+02 -1.431e+02
-1.541e+01 -2.018e-03 1.653e-03 -4.090e+00 2.016e+01 -5.846e+04
-8.072e-03 2.627e+00 -3.174e-02 -3.088e-01]
Output:
data_B:
[ 169952464 645507913 3631866677 2552417204 3289847493 4213394698
1094819874 3035736080]
data_C:
[ 4.812e+00 1.870e-04 -5.692e-02 2.528e-02 -9.225e+02 -1.431e+02
-1.541e+01 -2.018e-03 1.653e-03 -4.090e+00 2.016e+01 -5.846e+04
-8.072e-03 2.627e+00 -3.174e-02 -3.088e-01]
"""
Example 2
from tbe import tik
tik_instance = tik.Tik()
data_A = tik_instance.Tensor("float16", (16,), name="data_A", scope=tik.scope_gm)
data_B = data_A.reinterpret_cast_to("uint16")
data_C = data_B.reinterpret_cast_to("float16")
"""Example:
Input:
data_A:
[ 4.566e+01 -7.880e+02 1.414e-04 -1.300e-02 -1.893e+03 -1.622e-01
-1.289e+00 2.478e+02 -3.107e+00 -2.072e+01 7.192e-01 -1.805e+00
3.259e+01 -3.181e-03 -3.248e-05 4.086e+04]
Output:
data_B:
[20917 57896 2210 41640 59237 45361 48424 23486 49719 52526 14785 48952
20499 39556 33313 30973]
data_C:
[ 4.566e+01 -7.880e+02 1.414e-04 -1.300e-02 -1.893e+03 -1.622e-01
-1.289e+00 2.478e+02 -3.107e+00 -2.072e+01 7.192e-01 -1.805e+00
3.259e+01 -3.181e-03 -3.248e-05 4.086e+04]
"""
Parent topic: Tensor Management