ArmNN
 24.02
DataTypeUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <ResolveType.hpp>
9 
10 
12 
13 #include <vector>
14 
15 // Utility tenmplate to convert a collection of values to the correct type
16 template <armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
17 std::vector<T> ConvertToDataType(const std::vector<float>& input,
18  const armnn::TensorInfo& inputTensorInfo)
19 {
20  std::vector<T> output(input.size());
21  auto outputTensorInfo = inputTensorInfo;
22  outputTensorInfo.SetDataType(ArmnnType);
23 
24  std::unique_ptr<armnn::Encoder<float>> pOutputEncoder = armnn::MakeEncoder<float>(outputTensorInfo, output.data());
25  armnn::Encoder<float>& rOutputEncoder = *pOutputEncoder;
26 
27  for (auto it = input.begin(); it != input.end(); ++it)
28  {
29  rOutputEncoder.Set(*it);
30  ++rOutputEncoder;
31  }
32  return output;
33 }
34 
35 // Utility tenmplate to convert a single value to the correct type
36 template <typename T>
37 T ConvertToDataType(const float& value,
38  const armnn::TensorInfo& tensorInfo)
39 {
40  std::vector<T> output(1);
41  std::unique_ptr<armnn::Encoder<float>> pEncoder = armnn::MakeEncoder<float>(tensorInfo, output.data());
42  armnn::Encoder<float>& rEncoder = *pEncoder;
43  rEncoder.Set(value);
44  return output[0];
45 }
ConvertToDataType
std::vector< T > ConvertToDataType(const std::vector< float > &input, const armnn::TensorInfo &inputTensorInfo)
Definition: DataTypeUtils.hpp:17
armnn::Encoder::Set
virtual void Set(IType right)=0
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::TensorInfo::SetDataType
void SetDataType(DataType type)
Definition: Tensor.hpp:201
ResolveType.hpp
armnn::Encoder< float >
Encoders.hpp