ArmNN
 22.05
PermuteDepthwiseConv2dWeightsTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "../armnnTestUtils/GraphUtils.hpp"
7 #include "../armnnTestUtils/TestUtils.hpp"
8 
9 #include <armnn/INetwork.hpp>
10 
11 #include <doctest/doctest.h>
12 
13 using namespace armnn;
14 
15 namespace
16 {
17 #if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
18 armnn::INetworkPtr CreateSimpleDepthwiseConv2dNetwork(const armnn::TensorInfo& inputTensorInfo,
19  const armnn::TensorInfo& outputTensorInfo,
20  const armnn::TensorInfo& weightsTensorInfo,
22 {
24 
25  armnn::IConnectableLayer* inputLayer = network->AddInputLayer(0, "input");
26  armnn::IConnectableLayer* weightsInputLayer = network->AddInputLayer(1, "weights_input");
27  armnn::IConnectableLayer* depthwiseLayer = network->AddDepthwiseConvolution2dLayer(descriptor, "depthwise_conv2d");
28  armnn::IConnectableLayer* outputLayer = network->AddOutputLayer(0, "output");
29 
30  inputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
31  weightsInputLayer->GetOutputSlot(0).SetTensorInfo(weightsTensorInfo);
32  depthwiseLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
33 
34  inputLayer->GetOutputSlot(0).Connect(depthwiseLayer->GetInputSlot(0));
35  weightsInputLayer->GetOutputSlot(0).Connect(depthwiseLayer->GetInputSlot(1));
36  depthwiseLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
37 
38  return network;
39 }
40 
41 void PermuteDepthwiseConv2dWeightsTestRunner(INetworkPtr& network,
42  const TensorShape& outputShape,
43  Compute backendId)
44 {
45  // Create ArmNN runtime
47 
48  // Optimise ArmNN network
49  IOptimizedNetworkPtr optNet = Optimize(*network, {backendId}, run->GetDeviceSpec());
50 
51  Graph& graph = GetGraphForTesting(optNet.get());
52 
53  CHECK(graph.GetNumLayers() == 5);
54  CHECK(CheckSequence(graph.cbegin(),
55  graph.cend(),
56  &IsLayerOfType<InputLayer>,
57  &IsLayerOfType<InputLayer>,
58  &IsLayerOfType<PermuteLayer>,
59  &IsLayerOfType<DepthwiseConvolution2dLayer>,
60  &IsLayerOfType<OutputLayer>));
61 
62  armnn::Layer* const permuteLayer = GetFirstLayerWithName(graph, "permute_layer");
63  CHECK(permuteLayer);
64 
65  // Swap original shape to compare with new shape.
66  unsigned int weightsShape[] = {outputShape[0], outputShape[1], outputShape[2], outputShape[3]};
67 
68  // Tensorshape and the data type are correct
69  // [ 1, H, W, I*M] --> [ 1, I * M, H, W ]
70  TensorShape newShape = permuteLayer->GetOutputSlot().GetTensorInfo().GetShape();
71  CHECK((newShape[0] == weightsShape[0]));
72  CHECK((newShape[1] == weightsShape[3]));
73  CHECK((newShape[2] == weightsShape[1]));
74  CHECK((newShape[3] == weightsShape[2]));
75 }
76 
77 void PermuteDepthwiseConv2dWeightsTest(Compute backendId)
78 {
79  armnn::TensorInfo inputTensorInfo({ 1, 1, 2, 3 }, armnn::DataType::Float32);
80  armnn::TensorInfo outputTensorInfo({ 1, 2 }, armnn::DataType::Float32);
81  armnn::TensorInfo weightsTensorInfo({ 2, 6 }, armnn::DataType::Float32);
82 
84  descriptor.m_BiasEnabled = false;
85 
86  armnn::INetworkPtr network = CreateSimpleDepthwiseConv2dNetwork(inputTensorInfo,
87  outputTensorInfo,
88  weightsTensorInfo,
89  descriptor);
90 
91  PermuteDepthwiseConv2dWeightsTestRunner(network,
92  weightsTensorInfo.GetShape(),
93  backendId);
94 }
95 #endif
96 }
97 
98 #if defined(ARMCOMPUTECL_ENABLED)
99 TEST_SUITE("Optimizer_PermuteDepthwiseConv2dWeightsGpuAcc")
100 {
101 TEST_CASE("PermuteDepthwiseConv2dWeightsGpuAccTest")
102 {
103  PermuteDepthwiseConv2dWeightsTest(Compute::GpuAcc);
104 }
105 }
106 #endif
107 
108 #if defined(ARMCOMPUTENEON_ENABLED)
109 TEST_SUITE("Optimizer_PermuteDepthwiseConv2dWeightsCpuAcc")
110 {
111 TEST_CASE("PermuteDepthwiseConv2dWeightsCpuAccTest")
112 {
113  PermuteDepthwiseConv2dWeightsTest(Compute::CpuAcc);
114 }
115 }
116 #endif
TEST_SUITE("TestConstTensorLayerVisitor")
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:49
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:66
bool m_BiasEnabled
Enable/disable bias.
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
bool CheckSequence(const armnn::Graph::ConstIterator first, const armnn::Graph::ConstIterator last)
Definition: TestUtils.hpp:21
armnn::Layer * GetFirstLayerWithName(armnn::Graph &graph, const std::string &name)
Definition: GraphUtils.cpp:22
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:33
Copyright (c) 2021 ARM Limited and Contributors.
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
Compute
The Compute enum is now deprecated and it is now being replaced by BackendId.
Definition: BackendId.hpp:21
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1847
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:242
GPU Execution: OpenCL: ArmCompute.
Graph & GetGraphForTesting(IOptimizedNetwork *optNet)
Definition: TestUtils.cpp:49
CPU Execution: NEON: ArmCompute.
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:324
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:241
virtual int Connect(IInputSlot &destination)=0
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
static INetworkPtr Create(NetworkOptions networkOptions={})
Definition: Network.cpp:476
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.