ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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 if (cloned != nullptr)
50 {
51 // dtype/shape
52 cloned->dtype(node->dtype());
53 cloned->rank(node->rank());
54
55 // values
56 switch (node->dtype())
57 {
58 case loco::DataType::FLOAT32:
59 copy_values<loco::DataType::FLOAT32>(node, cloned);
60 break;
61
62 case loco::DataType::U4:
63 copy_values<loco::DataType::U4>(node, cloned);
64 break;
65
66 case loco::DataType::U8:
67 copy_values<loco::DataType::U8>(node, cloned);
68 break;
69
70 case loco::DataType::S4:
71 copy_values<loco::DataType::S4>(node, cloned);
72 break;
73
74 case loco::DataType::S8:
75 copy_values<loco::DataType::S8>(node, cloned);
76 break;
77
78 case loco::DataType::S16:
79 copy_values<loco::DataType::S16>(node, cloned);
80 break;
81
82 case loco::DataType::S32:
83 copy_values<loco::DataType::S32>(node, cloned);
84 break;
85
86 case loco::DataType::S64:
87 copy_values<loco::DataType::S64>(node, cloned);
88 break;
89
90 case loco::DataType::BOOL:
91 copy_values<loco::DataType::BOOL>(node, cloned);
92 break;
93
94 default:
95 throw oops::UserExn("Unsupported tensor dtype");
96 }
97 }
98
99 return cloned;
100}
101
102} // namespace
103
104namespace luci
105{
106
108{
109 auto *cloned = clone_circleconst(node, node->graph());
110
111 copy_common_attributes(node, cloned);
112
113 return cloned;
114}
115
116} // namespace luci
117
118namespace luci
119{
120
122{
123 return clone_circleconst(node, _graph);
124}
125
126} // 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