Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ConstantInitializer.cc
1 /*
2  * Copyright (c) 2019 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 "ConstantInitializer.h"
18
19 namespace onert
20 {
21 namespace backend
22 {
23 namespace cpu
24 {
25
26 ConstantInitializer::ConstantInitializer(const ir::Operands &operands,
27                                          const std::shared_ptr<TensorBuilder> &tensor_builder)
28     : IConstantInitializer{operands}, _tensor_builder{tensor_builder}
29 {
30   // DO NOTHING
31 }
32
33 void ConstantInitializer::visit(const ir::operation::Conv2D &node)
34 {
35   const auto &kernel_index = node.getInputs().at(ir::operation::Conv2D::KERNEL);
36   const auto &kernel_obj = _operands.at(kernel_index);
37   registerCopyInitializer(kernel_index, kernel_obj);
38
39   const auto &bias_index = node.getInputs().at(ir::operation::Conv2D::BIAS);
40   const auto &bias_obj = _operands.at(bias_index);
41   registerCopyInitializer(bias_index, bias_obj);
42 }
43
44 void ConstantInitializer::visit(const ir::operation::DepthwiseConv2D &node)
45 {
46   const auto &kernel_index = node.getInputs().at(ir::operation::DepthwiseConv2D::KERNEL);
47   const auto &kernel_obj = _operands.at(kernel_index);
48   registerCopyInitializer(kernel_index, kernel_obj);
49
50   const auto &bias_index = node.getInputs().at(ir::operation::DepthwiseConv2D::BIAS);
51   const auto &bias_obj = _operands.at(bias_index);
52   registerCopyInitializer(bias_index, bias_obj);
53 }
54
55 void ConstantInitializer::visit(const ir::operation::FullyConnected &node)
56 {
57   const auto &weight_index = node.getInputs().at(ir::operation::FullyConnected::WEIGHT);
58   const auto &weight_obj = _operands.at(weight_index);
59   registerCopyInitializer(weight_index, weight_obj);
60
61   const auto &bias_index = node.getInputs().at(ir::operation::FullyConnected::BIAS);
62   if (!bias_index.undefined())
63   {
64     const auto &bias_obj = _operands.at(bias_index);
65     registerCopyInitializer(bias_index, bias_obj);
66   }
67 }
68
69 } // namespace cpu
70 } // namespace backend
71 } // namespace onert