ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CircleConst.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 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 "CircleCloneNode.h"
18
20
22
23#include <loco.h>
24#include <loco/IR/Graph.h>
25
26#include <oops/UserExn.h>
27
28#include <cassert>
29
30namespace
31{
32
33template <loco::DataType T>
34void copy_values(const luci::CircleConst *node, luci::CircleConst *cloned)
35{
36 assert(T == node->dtype());
37 assert(T == cloned->dtype());
38
39 const auto size = node->size<T>();
40 cloned->size<T>(size);
41 for (uint32_t i = 0; i < size; i++)
42 cloned->at<T>(i) = node->at<T>(i);
43}
44
45luci::CircleConst *clone_circleconst(const luci::CircleConst *node, loco::Graph *graph)
46{
47 auto cloned = graph->nodes()->create<luci::CircleConst>();
48
49 {
50 // dtype/shape
51 cloned->dtype(node->dtype());
52 cloned->rank(node->rank());
53
54 // values
55 switch (node->dtype())
56 {
57 case loco::DataType::FLOAT32:
58 copy_values<loco::DataType::FLOAT32>(node, cloned);
59 break;
60
61 case loco::DataType::U4:
62 copy_values<loco::DataType::U4>(node, cloned);
63 break;
64
65 case loco::DataType::U8:
66 copy_values<loco::DataType::U8>(node, cloned);
67 break;
68
69 case loco::DataType::S4:
70 copy_values<loco::DataType::S4>(node, cloned);
71 break;
72
73 case loco::DataType::S8:
74 copy_values<loco::DataType::S8>(node, cloned);
75 break;
76
77 case loco::DataType::S16:
78 copy_values<loco::DataType::S16>(node, cloned);
79 break;
80
81 case loco::DataType::S32:
82 copy_values<loco::DataType::S32>(node, cloned);
83 break;
84
85 case loco::DataType::S64:
86 copy_values<loco::DataType::S64>(node, cloned);
87 break;
88
89 case loco::DataType::BOOL:
90 copy_values<loco::DataType::BOOL>(node, cloned);
91 break;
92
93 default:
94 throw oops::UserExn("Unsupported tensor dtype");
95 }
96 }
97
98 return cloned;
99}
100
101} // namespace
102
103namespace luci
104{
105
107{
108 auto *cloned = clone_circleconst(node, node->graph());
109
110 copy_common_attributes(node, cloned);
111
112 return cloned;
113}
114
115} // namespace luci
116
117namespace luci
118{
119
121{
122 return clone_circleconst(node, _graph);
123}
124
125} // namespace luci
A neural network graph.
Definition Graph.h:161
Graph * graph(void)
Definition Node.h:70
Class to build tensor data.
Definition CircleConst.h:35
const loco::DataTypeImpl< DT >::Type & at(uint32_t n) const
uint32_t size(void) const
Exception to user.
Definition UserExn.h:42
void copy_common_attributes(const luci::CircleNode *src, luci::CircleNode *dst)
Copy common attributes of CircleNode from src to dst.
luci::CircleConst * clone(luci::CircleConst *node)
Return cloned object of CircleConst node.
int32_t size[5]
Definition Slice.cpp:35