Overview
The TensorTrait data structure is a basic template class that describes tensor-related information, including the tensor data type, logical position, and layout memory layout. With the template metaprogramming technology, this class completes computation and code generation during compilation, thereby reducing runtime overhead.
Header Files to Be Included
1 | #include "kernel_operator_tensor_trait.h" |
Prototype
template <typename T, TPosition pos = TPosition::GM, typename LayoutType = Layout<Shape<>, Stride<>>>
struct TensorTrait {
using LiteType = T;
using LiteLayoutType = LayoutType;
static constexpr const TPosition tPos = pos; // This constant member is reserved for future function extension.
public:
__aicore__ inline TensorTrait(const LayoutType& t = {});
__aicore__ inline LayoutType& GetLayout();
__aicore__ inline const LayoutType& GetLayout() const;
__aicore__ inline void SetLayout(const LayoutType& t);
};
Template Parameters
Parameter |
Description |
|---|---|
T |
Only the following basic data types are supported: int4b_t, uint8_t, int8_t, int16_t, uint16_t, bfloat16_t, int32_t, uint32_t, int64_t, uint64_t, float, and half. In the TensorTrait structure, the using keyword is used to define a type alias LiteType, which is the same as the template parameter T. The LocalTensor/GlobalTensor defined by TensorTrait does not contain ShapeInfo. For example, the tensor corresponding to LocalTensor<float> that does not contain ShapeInfo is LocalTensor<TensorTrait<float>>. |
pos |
Logical position for storing data. The value is of the Tposition type. The default value is TPosition::GM. |
LayoutType |
Layout data type. The default value is empty, that is, Layout<Shape<>, Stride<>>. This parameter must comply with Restrictions. |
Member Function
__aicore__ inline TensorTrait(const LayoutType& t = {})
__aicore__ inline LayoutType& GetLayout()
__aicore__ inline const LayoutType& GetLayout() const
__aicore__ inline void SetLayout(const LayoutType& t)
Related APIs
// TensorTrait construction method template <typename T, TPosition pos, typename LayoutType> __aicore__ inline constexpr auto MakeTensorTrait(const LayoutType& t) // is_tensorTrait prototype definition template <typename T> struct is_tensorTrait
Restrictions
- The same API does not support the input of both the GlobalTensor/LocalTensor of the TensorTrait type and GlobalTensor/LocalTensor of the non-TensorTrait type.
- Copy constructors and assignment operators are not supported between non-TensorTrait and TensorTrait GlobalTensor/LocalTensor types.
- Currently, the TensorTrait feature supports only the following APIs:
- When used with APIs, the TensorTrait structure does not support the configuration of the pos and LayoutType template parameters. You need to use the constructor to construct the TensorTrait, and retain the default values for pos and LayoutType.
- The DataCopy slice data transfer API requires ShapeInfo and does not support the GlobalTensor/LocalTensor of the TensorTrait type.
Table 2 APIs supported by TensorTrait API Category
API
Basic APIs > Resource management > TQue/TQueBind
AllocTensor, FreeTensor, EnQue, DeQue
Basic APIs > Vector computation > Basic arithmetic
Exp, Ln, Abs, Reciprocal, Sqrt, Rsqrt, Relu, Add, Sub, Mul, Div, Max, Min, Adds, Muls, Maxs, Mins, VectorPadding, BilinearInterpolation, LeakyRelu
Basic APIs > Vector computation > Logical computation
And, Or
Basic APIs > Vector computation > Compound computation
CastDeq, AddRelu, AddDeqRelu, SubRelu, MulAddDst, FusedMulAdd, FusedMulAddRelu, AddReluCast, SubReluCast, MulCast
Basic APIs > Data movement
DataCopy, Copy
Basic APIs > Matrix computation
InitConstValue, LoadData, LoadDataWithTranspose, SetAippFunctions, LoadImageToLocal, LoadUnzipIndex, LoadDataUnzip, LoadDataWithSparse, Mmad, MmadWithSparse, BroadCastVecToMM, Gemm, Fixpipe
Basic APIs > Vector computation > Comparison and selection
Compare, GetCmpMask, SetCmpMask, Select, GatherMask
Basic APIs > Vector computation > Type conversion
Cast
Basic APIs > Vector computation > Reduction computation
ReduceMax, BlockReduceMax, WholeReduceMax, ReduceMin, BlockReduceMin, WholeReduceMin, ReduceSum, BlockReduceSum, WholeReduceSum, RepeatReduceSum, PairReduceSum
Basic APIs > Vector computation > Data conversion
Transpose, TransDataTo5HD
Basic APIs > Vector computation > Data padding
Brcb
Basic APIs > Vector computation > Discretization and aggregation
Gather, Gatherb, Scatter
Basic APIs > Vector computation > Sorting and combination (ISASI)
ProposalConcat, ProposalExtract, RpSort16, MrgSort4, Sort32