ONE - On-device Neural Engine
Loading...
Searching...
No Matches
CircleConst.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 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
18
19#include <cassert>
20
21namespace luci
22{
23
24template <loco::DataType DT> uint32_t CircleConst::size(void) const
25{
26 assert(dtype() == DT);
27 assert(_data.size() % sizeof(typename loco::DataTypeImpl<DT>::Type) == 0);
28 return _data.size() / sizeof(typename loco::DataTypeImpl<DT>::Type);
29}
30
31template <loco::DataType DT> void CircleConst::size(uint32_t l)
32{
33 assert(dtype() == DT);
34 _data.resize(l * sizeof(typename loco::DataTypeImpl<DT>::Type));
35}
36
37template <loco::DataType DT>
38const typename loco::DataTypeImpl<DT>::Type &CircleConst::at(uint32_t n) const
39{
40 assert(dtype() == DT);
41 assert(n < size<DT>());
42 return *(reinterpret_cast<const typename loco::DataTypeImpl<DT>::Type *>(_data.data()) + n);
43}
44
45template <loco::DataType DT> typename loco::DataTypeImpl<DT>::Type &CircleConst::at(uint32_t n)
46{
47 assert(dtype() == DT);
48 assert(n < size<DT>());
49 return *(reinterpret_cast<typename loco::DataTypeImpl<DT>::Type *>(_data.data()) + n);
50}
51
52template <loco::DataType DT>
54{
55 assert(dtype() == DT);
56 return *(reinterpret_cast<const typename loco::DataTypeImpl<DT>::Type *>(_data.data()));
57}
58
59template <loco::DataType DT> typename loco::DataTypeImpl<DT>::Type &CircleConst::scalar(void)
60{
61 assert(dtype() == DT);
62 return *(reinterpret_cast<typename loco::DataTypeImpl<DT>::Type *>(_data.data()));
63}
64
65#define INSTANTIATE(DT) \
66 template uint32_t CircleConst::size<DT>(void) const; \
67 template void CircleConst::size<DT>(uint32_t); \
68 template const typename loco::DataTypeImpl<DT>::Type &CircleConst::at<DT>(uint32_t) const; \
69 template typename loco::DataTypeImpl<DT>::Type &CircleConst::at<DT>(uint32_t); \
70 template const typename loco::DataTypeImpl<DT>::Type &CircleConst::scalar<DT>(void) const; \
71 template typename loco::DataTypeImpl<DT>::Type &CircleConst::scalar<DT>(void);
72
73INSTANTIATE(loco::DataType::S64);
74INSTANTIATE(loco::DataType::S32);
75INSTANTIATE(loco::DataType::S16);
76INSTANTIATE(loco::DataType::S8);
77INSTANTIATE(loco::DataType::S4);
78INSTANTIATE(loco::DataType::FLOAT32);
79INSTANTIATE(loco::DataType::U8);
80INSTANTIATE(loco::DataType::U4);
81INSTANTIATE(loco::DataType::BOOL);
82INSTANTIATE(loco::DataType::FLOAT16);
83
84#undef INSTANTIATE
85
86// CircleConst implementations for loco::DataType::STRING
87
88template <> uint32_t CircleConst::size<loco::DataType::STRING>(void) const
89{
90 assert(dtype() == loco::DataType::STRING);
91 assert(_data.size() == 0);
92 return _strings.size();
93}
94
95template <> void CircleConst::size<loco::DataType::STRING>(uint32_t l)
96{
97 assert(dtype() == loco::DataType::STRING);
98 assert(_data.size() == 0);
99 _strings.resize(l);
100}
101
102template <> const std::string &CircleConst::at<loco::DataType::STRING>(uint32_t n) const
103{
104 assert(dtype() == loco::DataType::STRING);
105 assert(n < _strings.size());
106 return _strings.at(n);
107}
108
109template <> std::string &CircleConst::at<loco::DataType::STRING>(uint32_t n)
110{
111 assert(dtype() == loco::DataType::STRING);
112 assert(n < _strings.size());
113 return _strings.at(n);
114}
115
116template <> const std::string &CircleConst::scalar<loco::DataType::STRING>(void) const
117{
118 assert(dtype() == loco::DataType::STRING);
119 assert(1 == _strings.size());
120 return _strings.at(0);
121}
122
123template <> std::string &CircleConst::scalar<loco::DataType::STRING>(void)
124{
125 assert(dtype() == loco::DataType::STRING);
126 assert(1 == _strings.size());
127 return _strings.at(0);
128}
129
130} // namespace luci
#define INSTANTIATE(DT)
Definition Tensor.cpp:74
const loco::DataTypeImpl< DT >::Type & scalar(void) const
const loco::DataTypeImpl< DT >::Type & at(uint32_t n) const
uint32_t size(void) const
C++ scalar type corresponding to each DataType.