ArmNN
 21.02
LogSoftmaxEndToEndTestImpl.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "EndToEndTestImpl.hpp"
8 
9 #include <armnn/INetwork.hpp>
10 
11 #include <test/TestUtils.hpp>
12 
13 #include <boost/test/unit_test.hpp>
14 
15 namespace {
16 
17 template <typename armnn::DataType DataType>
18 armnn::INetworkPtr CreateLogSoftmaxNetwork(const armnn::TensorShape& inputShape,
19  const armnn::TensorShape& outputShape,
20  const float beta,
21  const int axis,
22  const float qScale = 1.0f,
23  const int32_t qOffset = 0)
24 {
25  using namespace armnn;
26 
27  // Builds up the structure of the network.
29 
30  TensorInfo inputTensorInfo(inputShape, DataType, qScale, qOffset);
31 
32  LogSoftmaxDescriptor logSoftmaxDesc;
33  logSoftmaxDesc.m_Beta = beta;
34  logSoftmaxDesc.m_Axis = axis;
35 
36  IConnectableLayer* logSoftmax = net->AddLogSoftmaxLayer(logSoftmaxDesc, "Log_Softmax");
37  IConnectableLayer* input = net->AddInputLayer(0, "input");
38  Connect(input, logSoftmax, inputTensorInfo, 0, 0);
39 
40  TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);
41  IConnectableLayer* output = net->AddOutputLayer(0, "output");
42  Connect(logSoftmax, output, outputTensorInfo, 0, 0);
43 
44  return net;
45 }
46 
47 void LogSoftmaxEndToEnd(const std::vector<armnn::BackendId>& backends,
48  armnn::TensorInfo& inputTensorInfo,
49  armnn::TensorInfo& outputTensorInfo,
50  std::vector<float>& inputData,
51  std::vector<float>& expectedOutputData,
52  const float beta,
53  const int axis)
54 {
55  using namespace armnn;
56 
57  // Builds up the structure of the network
58  INetworkPtr net = CreateLogSoftmaxNetwork<DataType::Float32>(inputTensorInfo.GetShape(),
59  outputTensorInfo.GetShape(),
60  beta,
61  axis);
62 
63  BOOST_TEST_CHECKPOINT("Create a network");
64 
65  std::map<int, std::vector<float>> inputTensorData = { {0, inputData} };
66  std::map<int, std::vector<float>> expectedOutputTensorData = { {0, expectedOutputData} };
67 
68  EndToEndLayerTestImpl<DataType::Float32, DataType::Float32>(move(net),
69  inputTensorData,
70  expectedOutputTensorData,
71  backends);
72 }
73 
74 } // anonymous namespace
75 
76 void LogSoftmaxEndToEndTest(const std::vector<armnn::BackendId>& defaultBackends)
77 {
78  using namespace armnn;
79 
80  const float beta = 10.0f; // non-default beta
81  const int axis = 3; // positive axis
82 
83  const TensorShape inputShape{1, 1, 2, 4};
84  TensorInfo inputTensorInfo(inputShape, DataType::Float32);
85 
86  const TensorShape outputShape{1, 1, 2, 4};
87  TensorInfo outputTensorInfo(outputShape, DataType::Float32);
88 
89  std::vector<float> inputData = std::vector<float>({
90  0.0f, -0.6f, 0.2f, 0.4f,
91  0.3f, -0.2f, 1.0f, 0.1f
92  });
93 
94  std::vector<float> expectedOutputData = std::vector<float>({
95  -4.14297f, -10.14297f, -2.14297f, -0.14297f,
96  -7.00104f, -12.00104f, -0.00104087f, -9.00104f
97  });
98 
99  LogSoftmaxEndToEnd(defaultBackends,
100  inputTensorInfo,
101  outputTensorInfo,
102  inputData,
103  expectedOutputData,
104  beta,
105  axis);
106 }
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:62
int m_Axis
Scalar, defaulted to the last index (-1), specifying the dimension the activation will be performed o...
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
void LogSoftmaxEndToEndTest(const std::vector< armnn::BackendId > &defaultBackends)
float m_Beta
Exponentiation value.
Copyright (c) 2021 ARM Limited and Contributors.
DataType
Definition: Types.hpp:32
std::vector< armnn::BackendId > defaultBackends
void Connect(armnn::IConnectableLayer *from, armnn::IConnectableLayer *to, const armnn::TensorInfo &tensorInfo, unsigned int fromIndex, unsigned int toIndex)
Definition: TestUtils.cpp:12
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:173
static INetworkPtr Create(NetworkOptions networkOptions={})
Definition: Network.cpp:510
A SoftmaxDescriptor for the SoftmaxLayer.