From 49b9e100bfbb3b8da01472a0ff48b2bd92944e01 Mon Sep 17 00:00:00 2001 From: surmeh01 Date: Thu, 17 May 2018 14:11:25 +0100 Subject: Release 18.05 --- test/FullyConnected.cpp | 254 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 test/FullyConnected.cpp (limited to 'test/FullyConnected.cpp') diff --git a/test/FullyConnected.cpp b/test/FullyConnected.cpp new file mode 100644 index 00000000..ea6c8715 --- /dev/null +++ b/test/FullyConnected.cpp @@ -0,0 +1,254 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// See LICENSE file in the project root for full license information. +// +#include "DriverTestHelpers.hpp" +#include +#include + +BOOST_AUTO_TEST_SUITE(FullyConnectedTests) + +using ArmnnDriver = armnn_driver::ArmnnDriver; +using DriverOptions = armnn_driver::DriverOptions; +using namespace driverTestHelpers; + +// Add our own test here since we fail the fc tests which Google supplies (because of non-const weights) +BOOST_AUTO_TEST_CASE(FullyConnected) +{ + // this should ideally replicate fully_connected_float.model.cpp + // but that uses slightly weird dimensions which I don't think we need to support for now + + auto driver = std::make_unique(DriverOptions(armnn::Compute::CpuRef)); + Model model = {}; + + // add operands + int32_t actValue = 0; + float weightValue[] = {2, 4, 1}; + float biasValue[] = {4}; + + AddInputOperand(model, hidl_vec{1, 3}); + AddTensorOperand(model, hidl_vec{1, 3}, weightValue); + AddTensorOperand(model, hidl_vec{1}, biasValue); + AddIntOperand(model, actValue); + AddOutputOperand(model, hidl_vec{1, 1}); + + // make the fully connected operation + model.operations.resize(1); + model.operations[0].type = OperationType::FULLY_CONNECTED; + model.operations[0].inputs = hidl_vec{0, 1, 2, 3}; + model.operations[0].outputs = hidl_vec{4}; + + // make the prepared model + android::sp preparedModel = PrepareModel(model, *driver); + + // construct the request + DataLocation inloc = {}; + inloc.poolIndex = 0; + inloc.offset = 0; + inloc.length = 3 * sizeof(float); + RequestArgument input = {}; + input.location = inloc; + input.dimensions = hidl_vec{}; + + DataLocation outloc = {}; + outloc.poolIndex = 1; + outloc.offset = 0; + outloc.length = 1 * sizeof(float); + RequestArgument output = {}; + output.location = outloc; + output.dimensions = hidl_vec{}; + + Request request = {}; + request.inputs = hidl_vec{input}; + request.outputs = hidl_vec{output}; + + // set the input data (matching source test) + float indata[] = {2, 32, 16}; + AddPoolAndSetData(3, request, indata); + + // add memory for the output + android::sp outMemory = AddPoolAndGetData(1, request); + float* outdata = static_cast(static_cast(outMemory->getPointer())); + + // run the execution + Execute(preparedModel, request); + + // check the result + BOOST_TEST(outdata[0] == 152); +} + +BOOST_AUTO_TEST_CASE(TestFullyConnected4dInput) +{ + auto driver = std::make_unique(DriverOptions(armnn::Compute::CpuRef)); + + ErrorStatus error; + std::vector sup; + + ArmnnDriver::getSupportedOperations_cb cb = [&](ErrorStatus status, const std::vector& supported) + { + error = status; + sup = supported; + }; + + Model model = {}; + + // operands + int32_t actValue = 0; + float weightValue[] = {1, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 1}; //identity + float biasValue[] = {0, 0, 0, 0, 0, 0, 0, 0}; + + // fully connected operation + AddInputOperand(model, hidl_vec{1, 1, 1, 8}); + AddTensorOperand(model, hidl_vec{8, 8}, weightValue); + AddTensorOperand(model, hidl_vec{8}, biasValue); + AddIntOperand(model, actValue); + AddOutputOperand(model, hidl_vec{1, 8}); + + model.operations.resize(1); + + model.operations[0].type = OperationType::FULLY_CONNECTED; + model.operations[0].inputs = hidl_vec{0,1,2,3}; + model.operations[0].outputs = hidl_vec{4}; + + // make the prepared model + android::sp preparedModel = PrepareModel(model, *driver); + + + // construct the request + DataLocation inloc = {}; + inloc.poolIndex = 0; + inloc.offset = 0; + inloc.length = 8 * sizeof(float); + RequestArgument input = {}; + input.location = inloc; + input.dimensions = hidl_vec{}; + + DataLocation outloc = {}; + outloc.poolIndex = 1; + outloc.offset = 0; + outloc.length = 8 * sizeof(float); + RequestArgument output = {}; + output.location = outloc; + output.dimensions = hidl_vec{}; + + Request request = {}; + request.inputs = hidl_vec{input}; + request.outputs = hidl_vec{output}; + + // set the input data + float indata[] = {1,2,3,4,5,6,7,8}; + AddPoolAndSetData(8, request, indata); + + // add memory for the output + android::sp outMemory = AddPoolAndGetData(8, request); + float* outdata = static_cast(static_cast(outMemory->getPointer())); + + // run the execution + Execute(preparedModel, request); + + // check the result + BOOST_TEST(outdata[0] == 1); + BOOST_TEST(outdata[1] == 2); + BOOST_TEST(outdata[2] == 3); + BOOST_TEST(outdata[3] == 4); + BOOST_TEST(outdata[4] == 5); + BOOST_TEST(outdata[5] == 6); + BOOST_TEST(outdata[6] == 7); + BOOST_TEST(outdata[7] == 8); +} + +BOOST_AUTO_TEST_CASE(TestFullyConnected4dInputReshape) +{ + auto driver = std::make_unique(DriverOptions(armnn::Compute::CpuRef)); + + ErrorStatus error; + std::vector sup; + + ArmnnDriver::getSupportedOperations_cb cb = [&](ErrorStatus status, const std::vector& supported) + { + error = status; + sup = supported; + }; + + Model model = {}; + + // operands + int32_t actValue = 0; + float weightValue[] = {1, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 1}; //identity + float biasValue[] = {0, 0, 0, 0, 0, 0, 0, 0}; + + // fully connected operation + AddInputOperand(model, hidl_vec{1, 2, 2, 2}); + AddTensorOperand(model, hidl_vec{8, 8}, weightValue); + AddTensorOperand(model, hidl_vec{8}, biasValue); + AddIntOperand(model, actValue); + AddOutputOperand(model, hidl_vec{1, 8}); + + model.operations.resize(1); + + model.operations[0].type = OperationType::FULLY_CONNECTED; + model.operations[0].inputs = hidl_vec{0,1,2,3}; + model.operations[0].outputs = hidl_vec{4}; + + // make the prepared model + android::sp preparedModel = PrepareModel(model, *driver); + + + // construct the request + DataLocation inloc = {}; + inloc.poolIndex = 0; + inloc.offset = 0; + inloc.length = 8 * sizeof(float); + RequestArgument input = {}; + input.location = inloc; + input.dimensions = hidl_vec{}; + + DataLocation outloc = {}; + outloc.poolIndex = 1; + outloc.offset = 0; + outloc.length = 8 * sizeof(float); + RequestArgument output = {}; + output.location = outloc; + output.dimensions = hidl_vec{}; + + Request request = {}; + request.inputs = hidl_vec{input}; + request.outputs = hidl_vec{output}; + + // set the input data + float indata[] = {1,2,3,4,5,6,7,8}; + AddPoolAndSetData(8, request, indata); + + // add memory for the output + android::sp outMemory = AddPoolAndGetData(8, request); + float* outdata = static_cast(static_cast(outMemory->getPointer())); + + // run the execution + Execute(preparedModel, request); + + // check the result + BOOST_TEST(outdata[0] == 1); + BOOST_TEST(outdata[1] == 2); + BOOST_TEST(outdata[2] == 3); + BOOST_TEST(outdata[3] == 4); + BOOST_TEST(outdata[4] == 5); + BOOST_TEST(outdata[5] == 6); + BOOST_TEST(outdata[6] == 7); + BOOST_TEST(outdata[7] == 8); +} + +BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.1