ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Tensor.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Tensor.h"
18
20
21#include <cassert>
22
23namespace
24{
25
26// Return number of elements of the node.
27uint32_t numElements(const luci::CircleNode *node)
28{
29 uint32_t num_elem = 1;
30 for (uint32_t i = 0; i < node->rank(); ++i)
31 num_elem *= node->dim(i).value();
32 return num_elem;
33}
34
35} // namespace
36
37namespace circle_eval_diff
38{
39
40#define THROW_UNLESS(COND, MSG) \
41 if (not(COND)) \
42 throw std::runtime_error(MSG);
43
44template <loco::DataType DT> uint32_t Tensor::size(void) const
45{
46 assert(dtype() == DT);
47 assert(_data.size() % sizeof(typename loco::DataTypeImpl<DT>::Type) == 0);
48 return _data.size() / sizeof(typename loco::DataTypeImpl<DT>::Type);
49}
50
51template <loco::DataType DT> void Tensor::size(uint32_t l)
52{
53 assert(dtype() == DT);
54 _data.resize(l * sizeof(typename loco::DataTypeImpl<DT>::Type));
55}
56
57template <loco::DataType DT>
58const typename loco::DataTypeImpl<DT>::Type &Tensor::at(uint32_t n) const
59{
60 assert(dtype() == DT);
61 THROW_UNLESS(n < size<DT>(), "Access to out of buffer boundary.");
62 return *(reinterpret_cast<const typename loco::DataTypeImpl<DT>::Type *>(_data.data()) + n);
63}
64
65template <loco::DataType DT> typename loco::DataTypeImpl<DT>::Type &Tensor::at(uint32_t n)
66{
67 assert(dtype() == DT);
68 THROW_UNLESS(n < size<DT>(), "Access to out of buffer boundary.");
69 return *(reinterpret_cast<typename loco::DataTypeImpl<DT>::Type *>(_data.data()) + n);
70}
71
72#undef THROW_UNLESS
73
74#define INSTANTIATE(DT) \
75 template uint32_t Tensor::size<DT>(void) const; \
76 template void Tensor::size<DT>(uint32_t); \
77 template const typename loco::DataTypeImpl<DT>::Type &Tensor::at<DT>(uint32_t) const; \
78 template typename loco::DataTypeImpl<DT>::Type &Tensor::at<DT>(uint32_t);
79
80INSTANTIATE(loco::DataType::S64);
81INSTANTIATE(loco::DataType::S32);
82INSTANTIATE(loco::DataType::S16);
83INSTANTIATE(loco::DataType::U8);
84INSTANTIATE(loco::DataType::FLOAT32);
85
86#undef INSTANTIATE
87
88// Return Tensor which has the same dtype and shape with node.
89// Buffer does not have any data yet.
90std::shared_ptr<Tensor> createEmptyTensor(const luci::CircleNode *node)
91{
92 auto tensor = std::make_shared<Tensor>();
93 {
94 tensor->dtype(node->dtype());
95 tensor->rank(node->rank());
96 for (uint32_t i = 0; i < node->rank(); i++)
97 tensor->dim(i) = node->dim(i);
98
99 switch (node->dtype())
100 {
101 case loco::DataType::FLOAT32:
102 tensor->size<loco::DataType::FLOAT32>(numElements(node));
103 break;
104 case loco::DataType::U8:
105 tensor->size<loco::DataType::U8>(numElements(node));
106 break;
107 case loco::DataType::S16:
108 tensor->size<loco::DataType::S16>(numElements(node));
109 break;
110 case loco::DataType::S32:
111 tensor->size<loco::DataType::S32>(numElements(node));
112 break;
113 case loco::DataType::S64:
114 tensor->size<loco::DataType::S64>(numElements(node));
115 break;
116 default:
117 throw std::runtime_error("Unsupported input tensor dtype for " + node->name());
118 }
119 }
120
121 return tensor;
122}
123
124} // namespace circle_eval_diff
#define THROW_UNLESS(COND, MSG)
#define INSTANTIATE(DT)
Definition Tensor.cpp:74
std::shared_ptr< Tensor > createEmptyTensor(const luci::CircleNode *node)
Definition Tensor.cpp:90
uint32_t numElements(const luci::CircleNode *node)
Definition Utils.cpp:41
const loco::DataType & dtype(void) const
Definition Tensor.h:31
const loco::DataTypeImpl< DT >::Type & at(uint32_t n) const
Definition Tensor.cpp:58
uint32_t size(void) const
Definition Tensor.cpp:44
C++ scalar type corresponding to each DataType.
NodeName name(void) const