ArmNN
 23.02
TensorHandle.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <armnn/Exceptions.hpp>
7 
9 
10 #include <cstring>
11 
12 namespace armnn
13 {
14 
16 {
17  TensorShape shape(tensorInfo.GetShape());
18  auto size = GetDataTypeSize(tensorInfo.GetDataType());
19  auto runningSize = size;
20  std::vector<unsigned int> strides(shape.GetNumDimensions());
21  auto lastIdx = shape.GetNumDimensions()-1;
22  for (unsigned int i=0; i < lastIdx ; i++)
23  {
24  strides[lastIdx-i] = runningSize;
25  runningSize *= shape[lastIdx-i];
26  }
27  strides[0] = runningSize;
28  return TensorShape(shape.GetNumDimensions(), strides.data());
29 }
30 
32 : m_TensorInfo(tensorInfo)
33 , m_Memory(nullptr)
34 {
35 }
36 
37 template <>
38 const void* ConstTensorHandle::GetConstTensor<void>() const
39 {
40  return m_Memory;
41 }
42 
44 : ConstTensorHandle(tensorInfo)
45 , m_MutableMemory(nullptr)
46 {
47 }
48 
49 template <>
50 void* TensorHandle::GetTensor<void>() const
51 {
52  return m_MutableMemory;
53 }
54 
56 : TensorHandle(tensorInfo)
57 {
58 }
59 
61 : ScopedTensorHandle(tensor.GetInfo())
62 {
63  CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes());
64 }
65 
67 : ScopedTensorHandle(tensorHandle.GetTensorInfo())
68 {
69  CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes());
70 }
71 
73 : TensorHandle(other.GetTensorInfo())
74 {
75  CopyFrom(other);
76 }
77 
79 {
80  ::operator delete(GetTensor<void>());
81  SetMemory(nullptr);
82  CopyFrom(other);
83  return *this;
84 }
85 
87 {
88  ::operator delete(GetTensor<void>());
89 }
90 
92 {
93  if (GetTensor<void>() == nullptr)
94  {
95  SetMemory(::operator new(GetTensorInfo().GetNumBytes()));
96  }
97  else
98  {
99  throw InvalidArgumentException("TensorHandle::Allocate Trying to allocate a TensorHandle"
100  "that already has allocated memory.");
101  }
102 }
103 
104 void ScopedTensorHandle::CopyOutTo(void* memory) const
105 {
106  memcpy(memory, GetTensor<void>(), GetTensorInfo().GetNumBytes());
107 }
108 
109 void ScopedTensorHandle::CopyInFrom(const void* memory)
110 {
111  memcpy(GetTensor<void>(), memory, GetTensorInfo().GetNumBytes());
112 }
113 
114 void ScopedTensorHandle::CopyFrom(const ScopedTensorHandle& other)
115 {
116  CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes());
117 }
118 
119 void ScopedTensorHandle::CopyFrom(const void* srcMemory, unsigned int numBytes)
120 {
121  ARMNN_ASSERT(GetTensor<void>() == nullptr);
122  ARMNN_ASSERT(GetTensorInfo().GetNumBytes() == numBytes);
123 
124  if (srcMemory)
125  {
126  Allocate();
127  memcpy(GetTensor<void>(), srcMemory, numBytes);
128  }
129 }
130 
132 {
133  throw InvalidArgumentException("PassthroughTensorHandle::Allocate() should never be called");
134 }
135 
137 {
138  throw InvalidArgumentException("ConstPassthroughTensorHandle::Allocate() should never be called");
139 }
140 
141 } // namespace armnn
armnn::GetTensorInfo
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
Definition: RefWorkloadUtils.hpp:27
armnn::ScopedTensorHandle
Definition: TensorHandle.hpp:115
armnn::TensorHandle::SetMemory
void SetMemory(void *mem)
Definition: TensorHandle.hpp:98
armnn::TensorHandle::TensorHandle
TensorHandle(const TensorInfo &tensorInfo)
Definition: TensorHandle.cpp:43
armnn::ConstTensor
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
armnn::ConstTensorHandle
Definition: TensorHandle.hpp:24
TensorHandle.hpp
armnn::ConstPassthroughTensorHandle::Allocate
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
Definition: TensorHandle.cpp:136
armnn::GetDataTypeSize
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:155
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::ScopedTensorHandle::Allocate
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
Definition: TensorHandle.cpp:91
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::ConstTensorHandle::GetConstTensor
const T * GetConstTensor() const
Definition: TensorHandle.hpp:28
armnn::TensorHandle
Definition: TensorHandle.hpp:79
armnn::ScopedTensorHandle::ScopedTensorHandle
ScopedTensorHandle(const TensorInfo &tensorInfo)
Definition: TensorHandle.cpp:55
armnn::PassthroughTensorHandle::Allocate
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
Definition: TensorHandle.cpp:131
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::ConstTensorHandle::GetTensorInfo
const TensorInfo & GetTensorInfo() const
Definition: TensorHandle.hpp:40
armnn::BaseTensor::GetNumBytes
unsigned int GetNumBytes() const
Definition: Tensor.hpp:302
armnn::ConstTensorHandle::ConstTensorHandle
ConstTensorHandle(const TensorInfo &tensorInfo)
Definition: TensorHandle.cpp:31
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
armnn::TensorShape::GetNumDimensions
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
armnn::ScopedTensorHandle::operator=
ScopedTensorHandle & operator=(const ScopedTensorHandle &other)
Definition: TensorHandle.cpp:78
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::TensorInfo::GetNumBytes
unsigned int GetNumBytes() const
Definition: Tensor.cpp:427
Exceptions.hpp
armnn::ScopedTensorHandle::~ScopedTensorHandle
~ScopedTensorHandle()
Definition: TensorHandle.cpp:86
armnn::GetUnpaddedTensorStrides
TensorShape GetUnpaddedTensorStrides(const TensorInfo &tensorInfo)
Definition: TensorHandle.cpp:15
IgnoreUnused.hpp
armnn::BaseTensor::GetMemoryArea
MemoryType GetMemoryArea() const
Definition: Tensor.hpp:305
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:198