Data Definition
Tensor
Description
Defines a Tensor variable.
Prototype
Tensor(dtype, shape, scope, name, enable_buffer_reuse=False, no_reuse_list=None, reuse_list=None, is_workspace=False, is_atomic_add=False)
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 |
shape |
Input |
A list or tuple of ints, specifying the shape of the Tensor object. NOTICE:
In the current version, only a list or tuple of ints is allowed. |
scope |
Input |
Buffer scope of the Tensor object, that is, buffer space where the Tensor object is located:
|
name |
Input |
A string specifying the name of the Tensor object. Only digits, letters, and underscores (_) are allowed. However, the name cannot start with a digit. If set to None, the name auto_tensor_$(COUNT) is automatically used, with COUNT starting at zero. NOTE:
When scope is set to scope_gm, the name must not be __fake_tensor. |
enable_buffer_reuse |
Input |
Reserved and not recommended |
no_reuse_list |
Input |
Reserved and not recommended |
reuse_list |
Input |
Reserved and not recommended |
is_workspace |
Input |
A bool. Defaults to False. If set to True, the current tensor is used for storing intermediate data only. If set to True, scope must be scope_gm and the tensor must not be included in the input and output tensors (that is, the names of the input and output tensors do not contain the workspace tensor). |
is_atomic_add |
Input |
A bool. Defaults to False. This argument does not take effect. |
Restrictions
- When the total size of the tensors exceeds the total size of the corresponding buffer type, a build error is reported.
In the following example, the size of data_a is 1025 x 1024 bytes, which is greater than the total size of L1 Buffer by 1 MB.
import numpy as np import sys from te import tik import tvm def buffer_allocate_test6(): tik_instance = tik.Tik() data_a = tik_instance.Tensor("int8", (1025 * 1024,), name="data_a", scope=tik.scope_cbuf) tik_instance.BuildCCE(kernel_name="buffer_allocate_test",inputs=[],outputs=[]) return tik_instance if __name__ == "__main__": tik_instance = buffer_allocate_test6()
Build error:
RuntimeError: Appiled buffer size(1049600B) more than avaiable buffer size(1048576B).
- If a tensor is access beyond its defined scope, a build error will be reported.In the following example, data_a_l1 is defined only in new_stmt_scope. Beyond the defined scope, an error will be reported when the data_move API is called to access data_a_l1 again.
import numpy as np import sys from te import tik import tvm def tensor_outrange_examine_test6(): tik_instance = tik.Tik() data_a = tik_instance.Tensor("float16", (128,), name="data_a", scope=tik.scope_gm) data_b = tik_instance.Tensor("float16", (128,), name="data_b", scope=tik.scope_gm) with tik_instance.new_stmt_scope(): data_a_ub = tik_instance.Tensor("float16", (128,), name="data_a_ub", scope=tik.scope_ubuf) data_a_l1 = tik_instance.Tensor("float16", (128,), name="data_a_l1", scope=tik.scope_cbuf) tik_instance.data_move(data_a_l1, data_a, 0, 1, 128 // 16, 0, 0) tik_instance.data_move(data_a_ub, data_a_l1, 0, 1, 128 // 16, 0, 0) tik_instance.data_move(data_b, data_a_ub, 0, 1, 128 // 16, 0, 0) tik_instance.BuildCCE(kernel_name="tensor_outrange_examine", inputs=[data_a], outputs=[data_b]) return tik_instance
Build error:
RuntimeError: This tensor is not defined in this scope.
- If a tensor is beyond its defined scope, the buffer can be reused.In the following example, as data_a_ub1 and data_a_ub2 are beyond the defined scopes, the occupied buffer of size 126,976 bytes (62 x 2 x 1024 bytes) can be reused by data_b_ub.
import numpy as np import sys from te import tik import tvm def double_buffer_test6(): tik_instance = tik.Tik() data_a = tik_instance.Tensor("int8", (124 *1024,), name="data_a", scope=tik.scope_ubuf) with tik_instance.for_range(0, 2): data_a_ub1 = tik_instance.Tensor("int8", (62 * 1024,), name="data_a_ub1", scope=tik.scope_ubuf) data_a_ub2 = tik_instance.Tensor("int8", (62 * 1024,), name="data_a_ub2", scope=tik.scope_ubuf) data_b_ub = tik_instance.Tensor("int8", (125 * 1024,), name="data_b_ub", scope=tik.scope_ubuf) tik_instance.BuildCCE(kernel_name="tbe_double_buffer_no_loop", inputs=[ ], outputs=[ ]) return tik_instance if __name__ == "__main__": tik_instance = double_buffer_test6()
If data_b_ub exceeds the Unified Buffer size, the following error is reported during the build:
RuntimeError: Tensor data_b_ub appiles buffer size(128000B) more than avaiable buffer size(126976B).
- shape does not support scalar variables. It supports only immediates or Python variables.
- For user-defined tensors, the starting address of the allocated buffer scope will be aligned according to General Restrictions.
If the total size of a buffer type is exceeded due to address alignment, a build error is reported.
Returns
Tensor instance
Example
from te import tik tik_instance = tik.Tik() data_A = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_gm)
Scalar
Description
Defines a Scalar variable.
Prototype
Scalar(dtype="int64", name="reg_buf", init_value=None)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
dtype |
Input |
Data type of the Scalar object. Must be one of the following data types: int8, uint8, int16, uint16, float16, int32, uint32, float32, int64, uint64 Defaults to int64. |
name |
Input |
A string specifying the name of the Scalar object. Only digits, letters, and underscores (_) are allowed. Defaults to reg_buf$(COUNT), with COUNT starting at zero. |
init_value |
Input |
Initial value: An immediate of type int or float A Scalar variable A Tensor value An Expr consisting of a Scalar variable, an immediate, and a Tensor value NOTICE:
If the argument is an Expr, its immediate cannot be of type float. |
Restrictions
When the initial value is an Expr, its immediate can only be of integer type instead of float, for example:
from te import tik tik_instance = tik.Tik() index_reg = tik_instance.Scalar(dtype="float32") # Immediate: float index_reg.set_as(10.2) # Assign an initial value to the scalar using init_value. index_reg1 = tik_instance.Scalar(dtype="float32", init_value=10.2) index_reg2 = tik_instance.Scalar(dtype="float32") # Expr. The immediate is of float type. An error occurs with the CCE compiler (CCEC), because the hardware does not support this data type. #index_reg2.set_as(index_reg+2.2);
Returns
Scalar instance
Example
from te import tik tik_instance = tik.Tik() # Immediate: int index_reg = tik_instance.Scalar(dtype = "int32") index_reg.set_as(10) # Immediate: float index_reg2 = tik_instance.Scalar(dtype = "float16") index_reg2.set_as(10.2) # A Scalar variable index_reg3 = tik_instance.Scalar(dtype = "float16") index_reg3.set_as(index_reg2) # Tensor value data_A = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_gm) index_reg3.set_as(data_A[0])// tensor value #An Expr index_reg4 = tik_instance.Scalar(dtype = "int32") index_reg4.set_as(index_reg+20)
InputScalar
Description
Defines an InputScalar variable. An InputScalar serves as an inputs argument passed to the BuildCCE call. It supports a range of basic data types including int, uint, and float.
Prototype
InputScalar(dtype="int64", name="input_scalar")
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
dtype |
Input |
Data type of the InputScalar object. Must be one of the following data types: int8, uint8, int16, uint16, int32, uint32, int64, uint64, float16, float32 Defaults to int64. |
name |
Input |
A string specifying the name of the InputScalar object. Only digits, letters, and underscores (_) are allowed. Defaults to input_scalar. Ensure that each InputScalar variable has a unique name. |
Restrictions
- Currently, InputScalar can be used in scenarios where a variable is an Expr.
For example, if repeat_times in vec_abs can be an Expr, the code can be as follows: from te import tik tik_instance = tik.Tik() data_A = tik_instance.Tensor("float16", (128,), name="data_A", scope=tik.scope_gm) data_B = tik_instance.Tensor("float16", (128,), name="data_B", scope=tik.scope_gm) src_ub = tik_instance.Tensor("float16", (128,), name="src_ub", scope=tik.scope_ubuf) dst_ub = tik_instance.Tensor("float16", (128,), name="dst_ub", scope=tik.scope_ubuf) inputscalar = tik_instance.InputScalar(dtype="int16", name="inputscalar") tik_instance.vec_abs(128, dst_ub, src_ub, inputscalar, 8, 8) tik_instance.BuildCCE(kernel_name="simple_add",inputs=[data_A,data_B,inputscalar],outputs=[])
- Ensure that each InputScalar object has a unique name.
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 = tik_instance.Tensor("float16", (128,), name="data_B", scope=tik.scope_gm) abc = tik_instance.InputScalar(dtype="int16", name="abc") src_ub = tik_instance.Tensor("float16", (128,), name="src_ub", scope=tik.scope_ubuf) dst_ub = tik_instance.Tensor("float16", (128,), name="dst_ub", scope=tik.scope_ubuf) tik_instance.vec_abs(128, dst_ub, src_ub, abc, 8, 8) tik_instance.BuildCCE(kernel_name="simple_add",inputs=[data_A,data_B,abc],outputs=[])