2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "luci/Import/Nodes/CircleReshape.h"
19 #include <luci/IR/Nodes/CircleConst.h>
20 #include <luci/IR/Nodes/CircleReshape.h>
25 bool CircleReshapeGraphBuilder::validate(const ValidateArgs &args) const
27 if (args.op.inputs.size() != 1 && args.op.inputs.size() != 2)
30 if (args.op.outputs.size() != 1)
36 static void setup_shape_attribute(const std::vector<int32_t> &shape, CircleReshape *node)
38 node->newShape()->rank(shape.size());
39 for (uint32_t i = 0; i < shape.size(); ++i)
41 node->newShape()->dim(i) = shape[i];
45 static CircleNode *create_shape_node(const std::vector<int32_t> &shape, loco::Graph *graph)
47 auto *shape_node = graph->nodes()->create<luci::CircleConst>();
48 shape_node->dtype(loco::DataType::S32);
50 shape_node->dim(0) = shape.size();
51 shape_node->size<loco::DataType::S32>(shape.size());
52 for (uint32_t i = 0; i < shape.size(); ++i)
54 shape_node->at<loco::DataType::S32>(i) = shape[i];
59 CircleNode *CircleReshapeGraphBuilder::build_node(const circle::OperatorT &op,
60 const std::vector<CircleNode *> &inputs,
61 loco::Graph *graph) const
63 // If the second input is not provided, generate it based on the value of the attribute.
64 // TODO Presence of the second input is the current requirement of the IR.
65 auto *shape_node = (inputs.size() == 2) ? inputs.at(1) : nullptr;
66 if (shape_node == nullptr)
68 const auto *options = op.builtin_options.AsReshapeOptions();
69 if (options != nullptr)
70 shape_node = create_shape_node(options->new_shape, graph);
73 shape_node = graph->nodes()->create<CircleOutputDummy>();
74 shape_node->dtype(loco::DataType::S32);
79 auto *node = graph->nodes()->create<CircleReshape>();
80 node->tensor(inputs.at(0));
81 node->shape(shape_node);
83 const auto *options = op.builtin_options.AsReshapeOptions();
85 setup_shape_attribute(options->new_shape, node);