Obtaining Partial Tensor Data
Description
Obtains partial data of a Tensor by the index.
Prototype
__getitem__(index_in)
Parameters
Parameter |
Input/Output |
Description |
|---|---|---|
index_in |
Input |
Tensor index. Must be one of the following data types:
|
Applicability
Restrictions
- When index_in is an immediate, a Scalar, an Expr, or a Slice, if the original Tensor is multi-dimensional, it will be reshaped into a 1D tensor for return.
- dataA_UB[100] and dataA_UB[100:101:1] are equivalent.
- If index_in is an immediate, it must be in the range of [–dim, +dim), where dim indicates the number of elements of the Tensor. A negative index_in is interpreted as abs(index_in) indexing from the end.
- If index_in is a Scalar or an Expr, it must be in the range of [0, dim), where dim indicates the number of elements of the Tensor.
- If index_in is a Slice and start and stop are immediates, index_in must be in the range of [–dim, +dim). If index_in is a Slice and start and stop are Scalars or Exprs, index_in must be in the range of [0, dim).
dim indicates the number of elements of the Tensor. The element pointed by stop must be after the element pointed by start.
- If index_in is a tuple, the dimensions of index_in follow the restriction of the particular data type, immediate, Scalar, Expr, or Slice.
dim indicates the number of Tensor elements in a dimension.
- The alignment requirements for slice vary with the scope. start must meet the following requirements:
- Unified Buffer: 32-byte aligned
- Global Memory: no alignment requirement
- When the shape of a tensor contains a variable, if the value of the variable is changed before the tensor is sliced, an error may be reported because the space required by the sliced tensor is greater than max_mem_size. For example, if the shape of data_A is [i]=[64], then max_mem_size is 128. data_B is sliced from data_A and they point to the same memory address. However, the shape of data_B changes to [i]=[128], and the required space exceeds max_mem_size. As a result, the error message "The space required exceeds max_mem_size" is displayed at debug time.
from tbe import tik tik_instance = tik.Tik() i = tik_instance.Scalar(dtype="int32", name="i",init_value=64) data_A = tik_instance.Tensor("int16", (i, ), name="data_A", scope=tik.scope_gm, max_mem_size=128) i.set_as(128) data_B = data_A[:]
Returns
The new Tensor.
Example
from tbe import tik
tik_instance = tik.Tik()
A = tik_instance.Tensor("int64", (2, 4), name="A", scope=tik.scope_gm)
tik_instance.tikdb.debug_print("A")
data_1 = A[0]
tik_instance.tikdb.debug_print("data_1")
data_2 = A[0:]
tik_instance.tikdb.debug_print("data_2")
data_3 = A[0, 1]
tik_instance.tikdb.debug_print("data_3")
data_4 = A[0:, 1]
tik_instance.tikdb.debug_print("data_4")
# data_5 = A[0][1]
# tik_instance.tikdb.debug_print("data_5")
data_6 = A[0:][1]
tik_instance.tikdb.debug_print("data_6")
tik_instance.BuildCCE(kernel_name="getitem_sample", inputs=[A], outputs=[])
# Input A:
[[0, 1, 2, 3], [4, 5, 6, 7]]
# Print result of A
[[0, 1, 2, 3], [4, 5, 6, 7]]
# data_1 indicates the element 0 after A is unfolded.
[0]
# data_2 indicates the elements starting from element 0 after A is unfolded.
[0, 1, 2, 3, 4, 5, 6, 7]
# data_3 indicates the element in row 0, column 1 of A.
[[1]]
# data_4 indicates the elements in column 1 of A.
[[1], [5]]
# data_5 indicates element 1 of A[0]. Because A[0] has only one element, an error is reported.
# data_6 indicates element 1 of A[0:].
[1]
Parent topic: Tensor Management