Tensor Management
TIK tensor management class
reshape
Description
Sets the shape of a Tensor object.
Prototype
reshape(new_shape)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
new_shape |
Input |
A list or tuple of ints, specifying the new shape of the Tensor object. NOTICE:
In the current version, only a list or tuple of immediate integers is supported. |
Restrictions
- The total size of the new shape must be the same as that of the old shape.
- The new and old tensors point to the same memory address. If the value of the new tensor is changed, the value of the old tensor change accordingly.
Returns
New tensor
Example
from te import tik tik_instance = tik.Tik() data_A = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_gm) data_A.reshape((64,2))
reinterpret_cast_to
Description
Casts the data type of a tensor. Reads the same memory data based on the specified data type and re-interprets the bytes in the memory. Precision conversion is not supported. For details about how to convert the data precision, see vec_conv.
For example, to cast 128 elements from type float16 to float32, call reinterpret_cast_to to read the data in float32 mode, resulting 64 elements of type float32. Therefore, reinterpret_cast_to does not actually change the precision.
Prototype
reinterpret_cast_to(dtype)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
dtype |
Input |
Data type of the Tensor object. Must be one of the following data types: uint8, int8, uint16, int16, float16, uint32, int32, float32, uint64, int64 |
Restrictions
- To make it easier to describe the restrictions in calling reinterpret_cast_to(), we define a factor yielded by dividing the number of bits of the original data type by that 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 tensor is with shape (128, 1). To read its 128 float16 entries 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 in shape multiplies factor should be an integer" will be reported. Setting the tensor shape to 128 can avoid this error.
Returns
The new tensor
Example
Example 1:
from te 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] Returns: 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 te 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] Returns: 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] """
Obtaining Partial Tensor Data
Description
Obtains partial data of a tensor by an array of indexes.
Prototype
__getitem__(index_in)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
index_in |
Input |
An array of tensor indexes. Must be one of the following data types:
|
Restrictions
- The alignment requirements for slice vary with the scope. The start must meet the following requirements:
- Unified Buffer: 32-byte aligned
- Global Memory: no alignment requirement
Returns
The new tensor
Example
from te import tik tik_instance = tik.Tik() data_A = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_gm) data_b = data_A[1]
Setting Tensor Content
Description
Sets the content of a tensor by an array of indexes.
Prototype
__setitem__(index, value)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
index |
Input |
An array of tensor indexes. Must be one of the following data types:
|
value |
Input |
The value, which is related to the tensor data type. Currently, only scalars, Exprs, and tensor variables are supported. Immediates are not supported. |
Restrictions
- The alignment requirements for slice vary with the scope. The start must meet the following requirements:
- Unified Buffer: 32-byte aligned
- Global Memory: no alignment requirement
Returns
None
Example
from te import tik tik_instance = tik.Tik() data_A = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_ubuf) scalar_B = tik_instance.Scalar(dtype="float16", name="scalar_B", init_value=2.0) data_A[0].set_as(scalar_B)
shape
Description
Obtains the tensor shape.
Prototype
shape()
Parameters
None
Restrictions
None
Returns
A list specifying the tensor shape
Example
from te import tik tik_instance = tik.Tik() data_A = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_ubuf) data_A.shape();
[128] is returned.
set_as
Description
Sets a tensor.
Prototype
set_as(value, dst_offset=0, src_offset=None)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
value |
Input |
Value to set:
|
dst_offset |
Input |
Reserved and not recommended |
src_offset |
Input |
Reserved and not recommended |
Restrictions
None
Returns
None
Example
from te import tik tik_instance = tik.Tik() data_A = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_ubuf) data_B = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_ubuf) data_A[0].set_as(data_B[0])