narpra01 | b9546cf | 2018-11-20 15:21:28 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 5 | |
narpra01 | b9546cf | 2018-11-20 15:21:28 +0000 | [diff] [blame] | 6 | #pragma once |
| 7 | |
| 8 | #include <Graph.hpp> |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 9 | #include <SubgraphView.hpp> |
| 10 | #include <SubgraphViewSelector.hpp> |
Keith Davis | 3674f14 | 2020-08-16 23:44:15 +0100 | [diff] [blame] | 11 | #include <ResolveType.hpp> |
narpra01 | b9546cf | 2018-11-20 15:21:28 +0000 | [diff] [blame] | 12 | |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 13 | #include <armnn/BackendRegistry.hpp> |
| 14 | |
Keith Davis | 3674f14 | 2020-08-16 23:44:15 +0100 | [diff] [blame] | 15 | #include <armnn/Types.hpp> |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 16 | #include <backendsCommon/CpuTensorHandle.hpp> |
narpra01 | b9546cf | 2018-11-20 15:21:28 +0000 | [diff] [blame] | 17 | |
Matteo Martincigh | bf0e722 | 2019-06-20 17:17:45 +0100 | [diff] [blame] | 18 | #include <test/TestUtils.hpp> |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 19 | |
Matteo Martincigh | bf0e722 | 2019-06-20 17:17:45 +0100 | [diff] [blame] | 20 | #include <algorithm> |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 21 | |
| 22 | // Checks that two collections have the exact same contents (in any order) |
| 23 | // The given collections do not have to contain duplicates |
| 24 | // Cannot use std::sort here because std lists have their own std::list::sort method |
| 25 | template <typename CollectionType> |
| 26 | bool AreEqual(const CollectionType& lhs, const CollectionType& rhs) |
narpra01 | b9546cf | 2018-11-20 15:21:28 +0000 | [diff] [blame] | 27 | { |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 28 | if (lhs.size() != rhs.size()) |
| 29 | { |
| 30 | return false; |
| 31 | } |
narpra01 | b9546cf | 2018-11-20 15:21:28 +0000 | [diff] [blame] | 32 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 33 | auto lhs_it = std::find_if(lhs.begin(), lhs.end(), [&rhs](auto& item) |
| 34 | { |
| 35 | return std::find(rhs.begin(), rhs.end(), item) == rhs.end(); |
| 36 | }); |
| 37 | |
| 38 | return lhs_it == lhs.end(); |
narpra01 | b9546cf | 2018-11-20 15:21:28 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 41 | // Checks that the given collection contains the specified item |
| 42 | template <typename CollectionType> |
| 43 | bool Contains(const CollectionType& collection, const typename CollectionType::value_type& item) |
| 44 | { |
| 45 | return std::find(collection.begin(), collection.end(), item) != collection.end(); |
narpra01 | b9546cf | 2018-11-20 15:21:28 +0000 | [diff] [blame] | 46 | } |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 47 | |
| 48 | // Checks that the given map contains the specified key |
| 49 | template <typename MapType> |
| 50 | bool Contains(const MapType& map, const typename MapType::key_type& key) |
| 51 | { |
| 52 | return map.find(key) != map.end(); |
| 53 | } |
| 54 | |
Keith Davis | 3674f14 | 2020-08-16 23:44:15 +0100 | [diff] [blame] | 55 | // Utility template for comparing tensor elements |
| 56 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 57 | bool Compare(T a, T b, float tolerance = 0.000001f) |
| 58 | { |
| 59 | if (ArmnnType == armnn::DataType::Boolean) |
| 60 | { |
| 61 | // NOTE: Boolean is represented as uint8_t (with zero equals |
| 62 | // false and everything else equals true), therefore values |
| 63 | // need to be casted to bool before comparing them |
| 64 | return static_cast<bool>(a) == static_cast<bool>(b); |
| 65 | } |
| 66 | |
| 67 | // NOTE: All other types can be cast to float and compared with |
| 68 | // a certain level of tolerance |
| 69 | return std::fabs(static_cast<float>(a) - static_cast<float>(b)) <= tolerance; |
| 70 | } |
| 71 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 72 | template <typename ConvolutionLayer> |
| 73 | void SetWeightAndBias(ConvolutionLayer* layer, const armnn::TensorInfo& weightInfo, const armnn::TensorInfo& biasInfo) |
| 74 | { |
| 75 | layer->m_Weight = std::make_unique<armnn::ScopedCpuTensorHandle>(weightInfo); |
| 76 | layer->m_Bias = std::make_unique<armnn::ScopedCpuTensorHandle>(biasInfo); |
| 77 | |
| 78 | layer->m_Weight->Allocate(); |
| 79 | layer->m_Bias->Allocate(); |
| 80 | } |
| 81 | |
| 82 | armnn::SubgraphView::InputSlots CreateInputsFrom(const std::vector<armnn::Layer*>& layers); |
| 83 | |
| 84 | armnn::SubgraphView::OutputSlots CreateOutputsFrom(const std::vector<armnn::Layer*>& layers); |
| 85 | |
| 86 | armnn::SubgraphView::SubgraphViewPtr CreateSubgraphViewFrom(armnn::SubgraphView::InputSlots&& inputs, |
| 87 | armnn::SubgraphView::OutputSlots&& outputs, |
| 88 | armnn::SubgraphView::Layers&& layers); |
| 89 | |
| 90 | armnn::IBackendInternalUniquePtr CreateBackendObject(const armnn::BackendId& backendId); |
Aron Virginas-Tar | 735a450 | 2019-06-26 15:02:47 +0100 | [diff] [blame] | 91 | |
| 92 | armnn::TensorShape MakeTensorShape(unsigned int batches, |
| 93 | unsigned int channels, |
| 94 | unsigned int height, |
| 95 | unsigned int width, |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 96 | armnn::DataLayout layout); |