John Mcloughlin | 3375390 | 2024-02-07 15:00:57 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2024 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <armnn/INetwork.hpp> |
| 9 | |
| 10 | #include <CommonTestUtils.hpp> |
| 11 | #include <ResolveType.hpp> |
| 12 | |
| 13 | #include <doctest/doctest.h> |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | template <typename armnn::DataType DataType> |
| 18 | armnn::INetworkPtr CreateSoftmaxNetwork(const armnn::TensorShape& inputShape, |
| 19 | const armnn::TensorShape& outputShape, |
| 20 | const armnn::SoftmaxDescriptor& descriptor, |
| 21 | const float qScale = 1.0f, |
| 22 | const int32_t qOffset = 0) |
| 23 | { |
| 24 | using namespace armnn; |
| 25 | |
| 26 | // Builds up the structure of the network. |
| 27 | INetworkPtr net(INetwork::Create()); |
| 28 | |
| 29 | TensorInfo inputTensorInfo(inputShape, DataType, qScale, qOffset, true); |
| 30 | |
| 31 | IConnectableLayer* Softmax = net->AddSoftmaxLayer(descriptor, "Softmax"); |
| 32 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 33 | Connect(input, Softmax, inputTensorInfo, 0, 0); |
| 34 | |
| 35 | TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset); |
| 36 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 37 | Connect(Softmax, output, outputTensorInfo, 0, 0); |
| 38 | |
| 39 | return net; |
| 40 | } |
| 41 | |
| 42 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 43 | void SoftmaxEndToEnd(const std::vector<armnn::BackendId>& backends) |
| 44 | { |
| 45 | using namespace armnn; |
| 46 | |
| 47 | const TensorShape& inputShape = { 2, 2 }; |
| 48 | const TensorShape& outputShape = { 2, 2 }; |
| 49 | |
| 50 | SoftmaxDescriptor softmaxDesc; |
| 51 | softmaxDesc.m_Beta = 1.0f; |
| 52 | softmaxDesc.m_Axis = 1; |
| 53 | |
| 54 | // Builds up the structure of the network |
| 55 | INetworkPtr net = CreateSoftmaxNetwork<ArmnnType>(inputShape, |
| 56 | outputShape, |
| 57 | softmaxDesc); |
| 58 | |
| 59 | CHECK(net); |
| 60 | |
| 61 | std::vector<T> inputData |
| 62 | { |
| 63 | 17.0f, 16.0f, 5.0f, 14.0f |
| 64 | }; |
| 65 | |
| 66 | std::vector<T> expectedOutputData |
| 67 | { |
| 68 | 0.731059f, 0.268941f, 0.000123f, 0.999877f |
| 69 | }; |
| 70 | |
| 71 | std::map<int, std::vector<T>> inputTensorData = { {0, inputData} }; |
| 72 | std::map<int, std::vector<T>> expectedOutputTensorData = { {0, expectedOutputData} }; |
| 73 | |
| 74 | EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(net), |
| 75 | inputTensorData, |
| 76 | expectedOutputTensorData, |
| 77 | backends); |
| 78 | } |
| 79 | |
| 80 | } // anonymous namespace |