Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "TestUtils.hpp" |
| 9 | |
| 10 | #include <Graph.hpp> |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 11 | #include <ResolveType.hpp> |
Francis Murtagh | a49ff08 | 2022-01-17 17:08:01 +0000 | [diff] [blame] | 12 | #include <SubgraphViewSelector.hpp> |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 13 | |
| 14 | #include <armnn/BackendRegistry.hpp> |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 15 | #include <armnn/Types.hpp> |
Francis Murtagh | a49ff08 | 2022-01-17 17:08:01 +0000 | [diff] [blame] | 16 | #include <armnn/backends/SubgraphView.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 17 | #include <armnn/backends/TensorHandle.hpp> |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <random> |
| 21 | #include <vector> |
| 22 | |
| 23 | // Checks that two collections have the exact same contents (in any order) |
| 24 | // The given collections do not have to contain duplicates |
| 25 | // Cannot use std::sort here because std lists have their own std::list::sort method |
| 26 | template <typename CollectionType> |
| 27 | bool AreEqual(const CollectionType& lhs, const CollectionType& rhs) |
| 28 | { |
| 29 | if (lhs.size() != rhs.size()) |
| 30 | { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | auto lhs_it = std::find_if(lhs.begin(), lhs.end(), [&rhs](auto& item) |
| 35 | { |
| 36 | return std::find(rhs.begin(), rhs.end(), item) == rhs.end(); |
| 37 | }); |
| 38 | |
| 39 | return lhs_it == lhs.end(); |
| 40 | } |
| 41 | |
| 42 | // Checks that the given collection contains the specified item |
| 43 | template <typename CollectionType> |
| 44 | bool Contains(const CollectionType& collection, const typename CollectionType::value_type& item) |
| 45 | { |
| 46 | return std::find(collection.begin(), collection.end(), item) != collection.end(); |
| 47 | } |
| 48 | |
| 49 | // Checks that the given map contains the specified key |
| 50 | template <typename MapType> |
| 51 | bool Contains(const MapType& map, const typename MapType::key_type& key) |
| 52 | { |
| 53 | return map.find(key) != map.end(); |
| 54 | } |
| 55 | |
| 56 | // Utility template for comparing tensor elements |
| 57 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 58 | inline bool Compare(T a, T b, float tolerance = 0.000001f) |
| 59 | { |
| 60 | if (ArmnnType == armnn::DataType::Boolean) |
| 61 | { |
| 62 | // NOTE: Boolean is represented as uint8_t (with zero equals |
| 63 | // false and everything else equals true), therefore values |
| 64 | // need to be casted to bool before comparing them |
| 65 | return static_cast<bool>(a) == static_cast<bool>(b); |
| 66 | } |
| 67 | |
| 68 | // NOTE: All other types can be cast to float and compared with |
| 69 | // a certain level of tolerance |
| 70 | return std::fabs(static_cast<float>(a) - static_cast<float>(b)) <= tolerance; |
| 71 | } |
| 72 | |
| 73 | template <typename ConvolutionLayer> |
| 74 | void SetWeightAndBias(ConvolutionLayer* layer, const armnn::TensorInfo& weightInfo, const armnn::TensorInfo& biasInfo) |
| 75 | { |
| 76 | layer->m_Weight = std::make_unique<armnn::ScopedTensorHandle>(weightInfo); |
| 77 | layer->m_Bias = std::make_unique<armnn::ScopedTensorHandle>(biasInfo); |
| 78 | |
| 79 | layer->m_Weight->Allocate(); |
| 80 | layer->m_Bias->Allocate(); |
| 81 | } |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 82 | armnn::SubgraphView::InputSlots CreateInputsFrom(armnn::Layer* layer, |
| 83 | std::vector<unsigned int> ignoreSlots = {}); |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 84 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 85 | armnn::SubgraphView::InputSlots CreateInputsFrom(const std::vector<armnn::Layer*>& layers, |
| 86 | std::vector<unsigned int> ignoreSlots = {}); |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 87 | |
| 88 | armnn::SubgraphView::OutputSlots CreateOutputsFrom(const std::vector<armnn::Layer*>& layers); |
| 89 | |
| 90 | armnn::SubgraphView::SubgraphViewPtr CreateSubgraphViewFrom(armnn::SubgraphView::InputSlots&& inputs, |
| 91 | armnn::SubgraphView::OutputSlots&& outputs, |
| 92 | armnn::SubgraphView::Layers&& layers); |
| 93 | |
| 94 | armnn::IBackendInternalUniquePtr CreateBackendObject(const armnn::BackendId& backendId); |
| 95 | |
| 96 | armnn::TensorShape MakeTensorShape(unsigned int batches, |
| 97 | unsigned int channels, |
| 98 | unsigned int height, |
| 99 | unsigned int width, |
| 100 | armnn::DataLayout layout); |
| 101 | |
| 102 | template<typename DataType> |
| 103 | static std::vector<DataType> GenerateRandomData(size_t size) |
| 104 | { |
| 105 | constexpr bool isIntegerType = std::is_integral<DataType>::value; |
| 106 | using Distribution = |
| 107 | typename std::conditional<isIntegerType, |
| 108 | std::uniform_int_distribution<DataType>, |
| 109 | std::uniform_real_distribution<DataType>>::type; |
| 110 | |
| 111 | static constexpr DataType lowerLimit = std::numeric_limits<DataType>::min(); |
| 112 | static constexpr DataType upperLimit = std::numeric_limits<DataType>::max(); |
| 113 | |
| 114 | static Distribution distribution(lowerLimit, upperLimit); |
| 115 | static std::default_random_engine generator; |
| 116 | |
| 117 | std::vector<DataType> randomData(size); |
| 118 | generate(randomData.begin(), randomData.end(), []() { return distribution(generator); }); |
| 119 | |
| 120 | return randomData; |
| 121 | } |