2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include <armnn/Exceptions.hpp>
6 #include <armnn/utility/IgnoreUnused.hpp>
8 #include <backendsCommon/CpuTensorHandle.hpp>
15 TensorShape GetUnpaddedTensorStrides(const TensorInfo& tensorInfo)
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++)
24 strides[lastIdx-i] = runningSize;
25 runningSize *= shape[lastIdx-i];
27 strides[0] = runningSize;
28 return TensorShape(shape.GetNumDimensions(), strides.data());
31 ConstCpuTensorHandle::ConstCpuTensorHandle(const TensorInfo& tensorInfo)
32 : m_TensorInfo(tensorInfo)
38 const void* ConstCpuTensorHandle::GetConstTensor<void>() const
43 CpuTensorHandle::CpuTensorHandle(const TensorInfo& tensorInfo)
44 : ConstCpuTensorHandle(tensorInfo)
45 , m_MutableMemory(nullptr)
50 void* CpuTensorHandle::GetTensor<void>() const
52 return m_MutableMemory;
55 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const TensorInfo& tensorInfo)
56 : CpuTensorHandle(tensorInfo)
60 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstTensor& tensor)
61 : ScopedCpuTensorHandle(tensor.GetInfo())
63 CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes());
66 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstCpuTensorHandle& tensorHandle)
67 : ScopedCpuTensorHandle(tensorHandle.GetTensorInfo())
69 CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes());
72 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ScopedCpuTensorHandle& other)
73 : CpuTensorHandle(other.GetTensorInfo())
78 ScopedCpuTensorHandle& ScopedCpuTensorHandle::operator=(const ScopedCpuTensorHandle& other)
80 ::operator delete(GetTensor<void>());
86 ScopedCpuTensorHandle::~ScopedCpuTensorHandle()
88 ::operator delete(GetTensor<void>());
91 void ScopedCpuTensorHandle::Allocate()
93 if (GetTensor<void>() == nullptr)
95 SetMemory(::operator new(GetTensorInfo().GetNumBytes()));
99 throw InvalidArgumentException("CpuTensorHandle::Allocate Trying to allocate a CpuTensorHandle"
100 "that already has allocated memory.");
104 void ScopedCpuTensorHandle::CopyOutTo(void* memory) const
106 memcpy(memory, GetTensor<void>(), GetTensorInfo().GetNumBytes());
109 void ScopedCpuTensorHandle::CopyInFrom(const void* memory)
111 memcpy(GetTensor<void>(), memory, GetTensorInfo().GetNumBytes());
114 void ScopedCpuTensorHandle::CopyFrom(const ScopedCpuTensorHandle& other)
116 CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes());
119 void ScopedCpuTensorHandle::CopyFrom(const void* srcMemory, unsigned int numBytes)
121 ARMNN_ASSERT(GetTensor<void>() == nullptr);
122 ARMNN_ASSERT(GetTensorInfo().GetNumBytes() == numBytes);
127 memcpy(GetTensor<void>(), srcMemory, numBytes);
131 void PassthroughCpuTensorHandle::Allocate()
133 throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called");
136 void ConstPassthroughCpuTensorHandle::Allocate()
138 throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called");