From aa0b474c19e96b4c27b28c92f1b0b027eca6653c Mon Sep 17 00:00:00 2001 From: Chen Xin Date: Tue, 27 Dec 2022 16:59:57 +0800 Subject: [PATCH] Added a case for fc Signed-off-by: Chen Xin --- src/tim/vx/ops/fullyconnected_test.cc | 76 +++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/tim/vx/ops/fullyconnected_test.cc diff --git a/src/tim/vx/ops/fullyconnected_test.cc b/src/tim/vx/ops/fullyconnected_test.cc new file mode 100644 index 0000000..c3a9c01 --- /dev/null +++ b/src/tim/vx/ops/fullyconnected_test.cc @@ -0,0 +1,76 @@ +/**************************************************************************** +* +* Copyright (c) 2022 Vivante Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +* DEALINGS IN THE SOFTWARE. +* +*****************************************************************************/ +#include "tim/vx/context.h" +#include "tim/vx/graph.h" +#include "tim/vx/ops/fullyconnected.h" +#include +#include "gtest/gtest.h" +#include "test_utils.h" + +TEST(FullyConnected, unit_2_float_axis_0) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({2, 2}); + tim::vx::ShapeType weight_shape({2, 3}); + tim::vx::ShapeType bias_shape({3}); + tim::vx::ShapeType out_shape({3, 2}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, in_shape, + tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec weight_spec(tim::vx::DataType::FLOAT32, weight_shape, + tim::vx::TensorAttribute::CONSTANT); + tim::vx::TensorSpec bias_spec(tim::vx::DataType::FLOAT32, bias_shape, + tim::vx::TensorAttribute::CONSTANT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, out_shape, + tim::vx::TensorAttribute::OUTPUT); + std::vector in_data = { + 1,4,2,6, + }; + std::vector weight = { + -3,3,2,1,0,4, + }; + std::vector bias = { + 0.1, 0.4, 0.6, + }; + std::vector golden = { + 9.1, 6.4, 16.6, 12.1, 10.4, 24.6, + }; + + auto input_tensor = graph->CreateTensor(input_spec); + auto weight_tensor = graph->CreateTensor(weight_spec, weight.data()); + auto bias_tensor = graph->CreateTensor(bias_spec, bias.data()); + auto output_tensor = graph->CreateTensor(output_spec); + + EXPECT_TRUE( + input_tensor->CopyDataToTensor(in_data.data(), in_data.size() * 4)); + auto op = graph->CreateOperation(0, 3); + (*op).BindInputs({input_tensor, weight_tensor, bias_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); +}