ArmNN
 20.02
OpenClTimerTest.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #if (defined(__aarch64__)) || (defined(__x86_64__)) // disable test failing on FireFly/Armv7
7 
9 
10 #include <test/TensorHelpers.hpp>
11 
14 
15 #include <cl/ClContextControl.hpp>
16 #include <cl/ClWorkloadFactory.hpp>
17 #include <cl/OpenClTimer.hpp>
18 
21 
22 #include <arm_compute/runtime/CL/CLScheduler.h>
23 
24 #include <boost/format.hpp>
25 #include <boost/test/unit_test.hpp>
26 
27 #include <iostream>
28 
29 using namespace armnn;
30 
31 struct OpenClFixture
32 {
33  // Initialising ClContextControl to ensure OpenCL is loaded correctly for each test case.
34  // NOTE: Profiling needs to be enabled in ClContextControl to be able to obtain execution
35  // times from OpenClTimer.
36  OpenClFixture() : m_ClContextControl(nullptr, true) {}
37  ~OpenClFixture() {}
38 
39  ClContextControl m_ClContextControl;
40 };
41 
42 BOOST_FIXTURE_TEST_SUITE(OpenClTimerBatchNorm, OpenClFixture)
44 
45 BOOST_AUTO_TEST_CASE(OpenClTimerBatchNorm)
46 {
47  auto memoryManager = ClWorkloadFactoryHelper::GetMemoryManager();
48  ClWorkloadFactory workloadFactory = ClWorkloadFactoryHelper::GetFactory(memoryManager);
49 
50  const unsigned int width = 2;
51  const unsigned int height = 3;
52  const unsigned int channels = 2;
53  const unsigned int num = 1;
54 
55  TensorInfo inputTensorInfo( {num, channels, height, width}, DataType::Float32);
56  TensorInfo outputTensorInfo({num, channels, height, width}, DataType::Float32);
57  TensorInfo tensorInfo({channels}, DataType::Float32);
58 
59  auto input = MakeTensor<float, 4>(inputTensorInfo,
60  {
61  1.f, 4.f,
62  4.f, 2.f,
63  1.f, 6.f,
64 
65  1.f, 1.f,
66  4.f, 1.f,
67  -2.f, 4.f
68  });
69 
70  // these values are per-channel of the input
71  auto mean = MakeTensor<float, 1>(tensorInfo, { 3.f, -2.f });
72  auto variance = MakeTensor<float, 1>(tensorInfo, { 4.f, 9.f });
73  auto beta = MakeTensor<float, 1>(tensorInfo, { 3.f, 2.f });
74  auto gamma = MakeTensor<float, 1>(tensorInfo, { 2.f, 1.f });
75 
76  std::unique_ptr<ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
77  std::unique_ptr<ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
78 
81  ScopedCpuTensorHandle meanTensor(tensorInfo);
82  ScopedCpuTensorHandle varianceTensor(tensorInfo);
83  ScopedCpuTensorHandle betaTensor(tensorInfo);
84  ScopedCpuTensorHandle gammaTensor(tensorInfo);
85 
86  AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]);
87  AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]);
88  AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]);
89  AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]);
90 
91  AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get());
92  AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get());
93  data.m_Mean = &meanTensor;
94  data.m_Variance = &varianceTensor;
95  data.m_Beta = &betaTensor;
96  data.m_Gamma = &gammaTensor;
97  data.m_Parameters.m_Eps = 0.0f;
98 
99  // for each channel:
100  // substract mean, divide by standard deviation (with an epsilon to avoid div by 0)
101  // multiply by gamma and add beta
102  std::unique_ptr<IWorkload> workload = workloadFactory.CreateBatchNormalization(data, info);
103 
104  inputHandle->Allocate();
105  outputHandle->Allocate();
106 
107  CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]);
108 
109  OpenClTimer openClTimer;
110 
111  BOOST_CHECK_EQUAL(openClTimer.GetName(), "OpenClKernelTimer");
112 
113  //Start the timer
114  openClTimer.Start();
115 
116  //Execute the workload
117  workload->Execute();
118 
119  //Stop the timer
120  openClTimer.Stop();
121 
122  BOOST_CHECK_EQUAL(openClTimer.GetMeasurements().size(), 1);
123 
124  BOOST_CHECK_EQUAL(openClTimer.GetMeasurements().front().m_Name,
125  "OpenClKernelTimer/0: batchnormalization_layer_nchw GWS[1,3,2]");
126 
127  BOOST_CHECK(openClTimer.GetMeasurements().front().m_Value > 0);
128 
129 }
130 
132 
133 #endif //aarch64 or x86_64
const ConstCpuTensorHandle * m_Gamma
const ConstCpuTensorHandle * m_Beta
void Start() override
Start the OpenCl timer.
Definition: OpenClTimer.cpp:21
const ConstCpuTensorHandle * m_Mean
float m_Eps
Value to add to the variance. Used to avoid dividing by zero.
std::unique_ptr< ITensorHandle > CreateTensorHandle(const TensorInfo &tensorInfo, const bool IsMemoryManaged=true) const override
const ConstCpuTensorHandle * m_Variance
Copyright (c) 2020 ARM Limited.
DataLayout::NCHW DataLayout::NCHW DataLayout::NHWC DataLayout::NHWC true
BOOST_CHECK(profilingService.GetCurrentState()==ProfilingState::WaitingForAck)
void AllocateAndCopyDataToITensorHandle(armnn::ITensorHandle *tensorHandle, const void *memory)
BOOST_AUTO_TEST_CASE(CheckConvolution2dLayer)
OpenClTimer instrument that times all OpenCl kernels executed between calls to Start() and Stop()...
Definition: OpenClTimer.hpp:20
BOOST_AUTO_TEST_SUITE_END()
Contains information about inputs and outputs to a layer.
std::unique_ptr< IWorkload > CreateBatchNormalization(const BatchNormalizationQueueDescriptor &descriptor, const WorkloadInfo &info) const override
void CopyDataToITensorHandle(armnn::ITensorHandle *tensorHandle, const void *memory)