Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <armnn/Tensor.hpp> |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 9 | |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 10 | #include <fmt/format.h> |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 11 | #include <mapbox/variant.hpp> |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 12 | |
| 13 | namespace armnnUtils |
| 14 | { |
| 15 | |
| 16 | template<typename TContainer> |
Matteo Martincigh | 9a5f9f2 | 2019-10-31 11:02:47 +0000 | [diff] [blame] | 17 | inline armnn::InputTensors MakeInputTensors(const std::vector<armnn::BindingPointInfo>& inputBindings, |
| 18 | const std::vector<TContainer>& inputDataContainers) |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 19 | { |
| 20 | armnn::InputTensors inputTensors; |
| 21 | |
| 22 | const size_t numInputs = inputBindings.size(); |
| 23 | if (numInputs != inputDataContainers.size()) |
| 24 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 25 | throw armnn::Exception(fmt::format("The number of inputs does not match number of " |
| 26 | "tensor data containers: {0} != {1}", |
| 27 | numInputs, |
| 28 | inputDataContainers.size())); |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | for (size_t i = 0; i < numInputs; i++) |
| 32 | { |
| 33 | const armnn::BindingPointInfo& inputBinding = inputBindings[i]; |
| 34 | const TContainer& inputData = inputDataContainers[i]; |
| 35 | |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 36 | mapbox::util::apply_visitor([&](auto&& value) |
Matteo Martincigh | 9a5f9f2 | 2019-10-31 11:02:47 +0000 | [diff] [blame] | 37 | { |
| 38 | if (value.size() != inputBinding.second.GetNumElements()) |
| 39 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 40 | throw armnn::Exception(fmt::format("The input tensor has incorrect size (expected {0} got {1})", |
| 41 | inputBinding.second.GetNumElements(), |
| 42 | value.size())); |
Matteo Martincigh | 9a5f9f2 | 2019-10-31 11:02:47 +0000 | [diff] [blame] | 43 | } |
Cathal Corbett | 5b8093c | 2021-10-22 11:12:07 +0100 | [diff] [blame] | 44 | armnn::TensorInfo inputTensorInfo = inputBinding.second; |
| 45 | inputTensorInfo.SetConstant(true); |
| 46 | armnn::ConstTensor inputTensor(inputTensorInfo, value.data()); |
Matteo Martincigh | 9a5f9f2 | 2019-10-31 11:02:47 +0000 | [diff] [blame] | 47 | inputTensors.push_back(std::make_pair(inputBinding.first, inputTensor)); |
| 48 | }, |
| 49 | inputData); |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return inputTensors; |
| 53 | } |
| 54 | |
| 55 | template<typename TContainer> |
Matteo Martincigh | 9a5f9f2 | 2019-10-31 11:02:47 +0000 | [diff] [blame] | 56 | inline armnn::OutputTensors MakeOutputTensors(const std::vector<armnn::BindingPointInfo>& outputBindings, |
| 57 | std::vector<TContainer>& outputDataContainers) |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 58 | { |
| 59 | armnn::OutputTensors outputTensors; |
| 60 | |
| 61 | const size_t numOutputs = outputBindings.size(); |
| 62 | if (numOutputs != outputDataContainers.size()) |
| 63 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 64 | throw armnn::Exception(fmt::format("Number of outputs does not match number" |
| 65 | "of tensor data containers: {0} != {1}", |
| 66 | numOutputs, |
| 67 | outputDataContainers.size())); |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | for (size_t i = 0; i < numOutputs; i++) |
| 71 | { |
| 72 | const armnn::BindingPointInfo& outputBinding = outputBindings[i]; |
| 73 | TContainer& outputData = outputDataContainers[i]; |
| 74 | |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 75 | mapbox::util::apply_visitor([&](auto&& value) |
Matteo Martincigh | 9a5f9f2 | 2019-10-31 11:02:47 +0000 | [diff] [blame] | 76 | { |
| 77 | if (value.size() != outputBinding.second.GetNumElements()) |
| 78 | { |
| 79 | throw armnn::Exception("Output tensor has incorrect size"); |
| 80 | } |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 81 | |
Matteo Martincigh | 9a5f9f2 | 2019-10-31 11:02:47 +0000 | [diff] [blame] | 82 | armnn::Tensor outputTensor(outputBinding.second, value.data()); |
| 83 | outputTensors.push_back(std::make_pair(outputBinding.first, outputTensor)); |
| 84 | }, |
| 85 | outputData); |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return outputTensors; |
| 89 | } |
| 90 | |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 91 | } // namespace armnnUtils |