TensorTrait Constructor
Product Support
Product |
Supported |
|---|---|
√ |
|
√ |
|
x |
|
x |
|
x |
|
x |
Function
Instantiates a TensorTrait object based on the input Layout object.
Prototype
1 | __aicore__ inline TensorTrait(const LayoutType& t = {}) |
Parameters
Parameter |
Input/Output |
Description |
|---|---|---|
t |
Input |
Input Layout object. The input data type LayoutType must meet the requirements described in Restrictions. |
Returns
None
Restrictions
None
Example
- TensorTrait usage example
// The following is a unit test example of using TensorTrait based on GoogleTest. // Use the MakeTensorTrait method to create a TensorTrait object. AscendC::Shape<int,int,int> shape = AscendC::MakeShape(10, 20, 30); AscendC::Stride<int,int,int> stride = AscendC::MakeStride(1, 100, 200); auto layoutMake = AscendC::MakeLayout(shape, stride); auto tensorTraitMake = AscendC::MakeTensorTrait<float, AscendC::TPosition::VECIN>(layoutMake); EXPECT_EQ(AscendC::Std::get<0>(tensorTraitMake.GetLayout().GetShape()), 10); EXPECT_EQ(AscendC::Std::get<1>(tensorTraitMake.GetLayout().GetShape()), 20); EXPECT_EQ(AscendC::Std::get<2>(tensorTraitMake.GetLayout().GetShape()), 30); EXPECT_EQ(AscendC::Std::get<0>(tensorTraitMake.GetLayout().GetStride()), 1); EXPECT_EQ(AscendC::Std::get<1>(tensorTraitMake.GetLayout().GetStride()), 100); EXPECT_EQ(AscendC::Std::get<2>(tensorTraitMake.GetLayout().GetStride()), 200); // Use the constructor to create a TensorTrait object. using TensorTraitType = AscendC::TensorTrait<half, AscendC::TPosition::VECCALC, AscendC::Layout<AscendC::Shape<int, int, int>, AscendC::Stride<int, int, int>>>; TensorTraitType tensorTraitInit(layoutMake); EXPECT_EQ(AscendC::Std::get<0>(tensorTraitInit.GetLayout().GetShape()), 10); EXPECT_EQ(AscendC::Std::get<1>(tensorTraitInit.GetLayout().GetShape()), 20); EXPECT_EQ(AscendC::Std::get<2>(tensorTraitInit.GetLayout().GetShape()), 30); EXPECT_EQ(AscendC::Std::get<0>(tensorTraitInit.GetLayout().GetStride()), 1); EXPECT_EQ(AscendC::Std::get<1>(tensorTraitInit.GetLayout().GetStride()), 100); EXPECT_EQ(AscendC::Std::get<2>(tensorTraitInit.GetLayout().GetStride()), 200); EXPECT_EQ(AscendC::Std::get<0>(tensorTraitInit.GetShape()), 10); EXPECT_EQ(AscendC::Std::get<1>(tensorTraitInit.GetShape()), 20); EXPECT_EQ(AscendC::Std::get<2>(tensorTraitInit.GetShape()), 30); EXPECT_EQ(AscendC::Std::get<0>(tensorTraitInit.GetStride()), 1); EXPECT_EQ(AscendC::Std::get<1>(tensorTraitInit.GetStride()), 100); EXPECT_EQ(AscendC::Std::get<2>(tensorTraitInit.GetStride()), 200); // Use the SetLayout method to set TensorTrait. TensorTraitType tensorTraitSet; tensorTraitSet.SetLayout(layoutMake); EXPECT_EQ(AscendC::Std::get<0>(tensorTraitSet.GetLayout().GetShape()), 10); EXPECT_EQ(AscendC::Std::get<1>(tensorTraitSet.GetLayout().GetShape()), 20); EXPECT_EQ(AscendC::Std::get<2>(tensorTraitSet.GetLayout().GetShape()), 30); EXPECT_EQ(AscendC::Std::get<0>(tensorTraitSet.GetLayout().GetStride()), 1); EXPECT_EQ(AscendC::Std::get<1>(tensorTraitSet.GetLayout().GetStride()), 100); EXPECT_EQ(AscendC::Std::get<2>(tensorTraitSet.GetLayout().GetStride()), 200);
- TensorTrait and API usage example
1 2 3 4
AscendC::LocalTensor<AscendC::TensorTrait<half>> tensor1 = que1.DeQue<AscendC::TensorTrait<half>>(); AscendC::LocalTensor<AscendC::TensorTrait<half>> tensor2 = que2.DeQue<AscendC::TensorTrait<half>>(); AscendC::LocalTensor<AscendC::TensorTrait<half>> tensor3 = que3.AllocTensor<AscendC::TensorTrait<half>>(); Add(tensor3, tensor1, tensor2, tensor3.GetSize());
Parent topic: TensorTrait