ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CircleConcatenation.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
18
19#include "CircleCloneNode.h"
20
22
23namespace luci
24{
25
27{
29 return nullptr;
30
31 auto *cloned = _graph->nodes()->create<luci::CircleConcatenation>(node->numValues());
32 {
34 cloned->axis(node->axis());
35 }
36 return cloned;
37}
38
39namespace sinf
40{
41
43{
44 // TODO Support when CircleConcatenation has 0 input
45 assert(node->numValues() > 0);
46
47 auto first_shape = luci::shape_get(node->values(0)).as<loco::TensorShape>();
48 auto axis = node->axis();
49 if (axis < 0)
50 axis += first_shape.rank();
51
52 assert(0 <= axis);
53 assert(first_shape.rank() > static_cast<uint32_t>(axis));
54
56
57 output_shape.rank(first_shape.rank());
58 for (uint32_t i = 0; i < output_shape.rank(); ++i)
59 output_shape.dim(i) = first_shape.dim(i);
60
61 for (uint32_t i = 1; i < node->numValues(); ++i)
62 {
63 auto input_shape = luci::shape_get(node->values(i)).as<loco::TensorShape>();
64 if (input_shape.rank() != output_shape.rank())
65 INTERNAL_EXN_V("Input has incompatible shape", node->name());
66
67 for (uint32_t j = 0; j < output_shape.rank(); ++j)
68 {
69 if (j == static_cast<uint32_t>(axis))
70 {
71 if (output_shape.dim(j).known() and input_shape.dim(j).known())
72 {
73 output_shape.dim(j) = output_shape.dim(j).value() + input_shape.dim(j).value();
74 }
75 else
76 {
77 // If any of inputs is unknown, just mark it as unknown.
78 output_shape.dim(j).unset();
79 }
80 }
81 else
82 {
83 if (output_shape.dim(j).known() and input_shape.dim(j).known())
84 {
85 if (output_shape.dim(j).value() != input_shape.dim(j).value())
86 {
87 INTERNAL_EXN_V("Input has incompatible shape.", node->name());
88 }
89 }
90 else
91 {
92 if (input_shape.dim(j).known())
93 {
94 assert(not output_shape.dim(j).known()); // FIX_ME_UNLESS
95 output_shape.dim(j) = input_shape.dim(j);
96 }
97 // For unknown input_shape, leave output_shape as-is
98 }
99 }
100 }
101 }
102
103 return output_shape;
104}
105
106} // namespace sinf
107} // namespace luci
#define INTERNAL_EXN_V(msg, val)
@ brief throw internal exception with message and value
Definition InternalExn.h:28
ShapeType as(void) const
uint32_t rank(void) const
Definition TensorShape.h:35
CONCATENATION in Circle.
uint32_t numValues(void) const
Node * values(uint32_t index) const
loco::TensorShape visit(const luci::CircleNode *node) final
Default fallback.
const luci_interpreter::RuntimeShape output_shape
loco::NodeShape shape_get(const loco::Node *node)
NodeName name(void) const