diff --git a/src/tim/vx/ops/transpose_test.cc b/src/tim/vx/ops/transpose_test.cc new file mode 100644 index 0000000..d63ee29 --- /dev/null +++ b/src/tim/vx/ops/transpose_test.cc @@ -0,0 +1,77 @@ +#include "tim/vx/context.h" +#include "tim/vx/graph.h" +#include "tim/vx/ops/transpose.h" + +#include "gtest/gtest.h" + +TEST(Transpose, shape_1_3_float_3_1) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType input_shape({1,3}); + tim::vx::ShapeType output_shape({3,1}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, + input_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, + output_shape, tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor = graph->CreateTensor(output_spec); + + std::vector in_data = { + 1,2,3 + }; + std::vector golden = { + 1,2,3 + }; + + EXPECT_TRUE(input_tensor->CopyDataToTensor( + in_data.data(), in_data.size() * sizeof(int32_t))); + + std::vector perm = {1, 0}; + auto op = graph->CreateOperation(perm); + (*op).BindInputs({input_tensor}).BindOutputs({output_tensor}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} + +TEST(Transpose, shape_1_3_int32_3_1) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType input_shape({1,3}); + tim::vx::ShapeType output_shape({3,1}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::INT32, + input_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::INT32, + output_shape, tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor = graph->CreateTensor(output_spec); + + std::vector in_data = { + 1,2,3 + }; + std::vector golden = { + 1,2,3 + }; + + EXPECT_TRUE(input_tensor->CopyDataToTensor( + in_data.data(), in_data.size() * sizeof(float))); + + std::vector perm = {1, 0}; + auto op = graph->CreateOperation(perm); + (*op).BindInputs({input_tensor}).BindOutputs({output_tensor}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} \ No newline at end of file