ONE - On-device Neural Engine
Loading...
Searching...
No Matches
CircleReshape.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
21
22namespace luci
23{
24
26{
27 if (args.op.inputs.size() != 1 && args.op.inputs.size() != 2)
28 return false;
29
30 if (args.op.outputs.size() != 1)
31 return false;
32
33 // for two inputs, check if type is S32 or S64
34 if (args.op.inputs.size() == 2)
35 {
36 const auto &inputs = args.op.inputs;
37 const auto tensors = args.reader.tensors();
38 const auto tensor_in = tensors.at(inputs.at(1));
39 assert(tensor_in != nullptr);
40
41 if (tensor_in->type() != circle::TensorType::TensorType_INT32 &&
42 tensor_in->type() != circle::TensorType::TensorType_INT64)
43 return false;
44 }
45
46 return true;
47}
48
49static void setup_shape_attribute(const std::vector<int32_t> &shape, CircleReshape *node)
50{
51 node->newShape()->rank(shape.size());
52 for (uint32_t i = 0; i < shape.size(); ++i)
53 {
54 node->newShape()->dim(i) = shape[i];
55 }
56}
57
58static CircleNode *create_shape_node(const std::vector<int32_t> &shape, loco::Graph *graph)
59{
60 auto *shape_node = graph->nodes()->create<luci::CircleConst>();
61 shape_node->dtype(loco::DataType::S32);
62 shape_node->rank(1);
63 shape_node->dim(0) = shape.size();
64 shape_node->size<loco::DataType::S32>(shape.size());
65 for (uint32_t i = 0; i < shape.size(); ++i)
66 {
67 shape_node->at<loco::DataType::S32>(i) = shape[i];
68 }
69 shape_node->name("Reshape/shape");
70 return shape_node;
71}
72
73CircleNode *CircleReshapeGraphBuilder::build_node(const circle::OperatorT &op,
74 const std::vector<CircleNode *> &inputs,
75 loco::Graph *graph) const
76{
77 // If the second input is not provided, generate it based on the value of the attribute.
78 // TODO Presence of the second input is the current requirement of the IR.
79 auto *shape_node = (inputs.size() == 2) ? inputs.at(1) : nullptr;
80 if (shape_node == nullptr)
81 {
82 const auto *options = op.builtin_options.AsReshapeOptions();
83 if (options != nullptr)
84 shape_node = create_shape_node(options->new_shape, graph);
85 else
86 {
87 shape_node = graph->nodes()->create<CircleOutputDummy>();
88 shape_node->dtype(loco::DataType::S32);
89 shape_node->rank(0);
90 shape_node->name("Reshape/dummy");
91 }
92 }
93
94 auto *node = graph->nodes()->create<CircleReshape>();
95 node->tensor(inputs.at(0));
96 node->shape(shape_node);
97
98 const auto *options = op.builtin_options.AsReshapeOptions();
99 if (options)
100 setup_shape_attribute(options->new_shape, node);
101
102 return node;
103}
104
105} // namespace luci
A neural network graph.
Definition Graph.h:161
Class to build tensor data.
Definition CircleConst.h:35
uint32_t size(void) const
uint32_t rank(void) const
int32_t dim(uint32_t n) const
bool validate(const ValidateArgs &args) const final
RESHAPE in Circle.
const Shape * newShape(void) const
loco::Node * shape(void) const
loco::Node * tensor(void) const