From e80aa7f77ed8dfa492c6ed3a779aa5997afe789d Mon Sep 17 00:00:00 2001 From: "jing.tang" Date: Tue, 2 Apr 2024 10:23:33 +0000 Subject: [PATCH] add some Broadcast unit tests Type: Unit Test --- src/tim/vx/ops/broadcast_test.cc | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/tim/vx/ops/broadcast_test.cc b/src/tim/vx/ops/broadcast_test.cc index 6668edd1d..ae5ea31f9 100644 --- a/src/tim/vx/ops/broadcast_test.cc +++ b/src/tim/vx/ops/broadcast_test.cc @@ -73,6 +73,80 @@ TEST(Broadcast, ScalarTo2D_2x3) { CheckResult(graph, golden, output_tensor); } +TEST(Broadcast, ScalarTo2D_INT32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType input_shape({1}); + tim::vx::ShapeType output_shape({6, 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 = { + 5, + }; + std::vector golden = { + 5, 5, 5, 5, 5, 5, + }; + std::vector shape = {6, 1}; + std::vector dimensions = {1}; + + EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(), + in_data.size() * sizeof(int32_t))); + + auto op = graph->CreateOperation(shape, dimensions); + (*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(Broadcast, ScalarTo2D_INT8) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType input_shape({1}); + tim::vx::ShapeType output_shape({6, 1}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::INT8, input_shape, + tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::INT8, output_shape, + tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor = graph->CreateTensor(output_spec); + + std::vector in_data = { + 5, + }; + std::vector golden = { + 5, 5, 5, 5, 5, 5, + }; + std::vector shape = {6, 1}; + std::vector dimensions = {1}; + + EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(), + in_data.size() * sizeof(int8_t))); + + auto op = graph->CreateOperation(shape, dimensions); + (*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(Broadcast, 1DTo2D) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph();