2 * Copyright (c) 2019 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 "ConstantInitializer.h"
27 ConstantInitializer::ConstantInitializer(const ir::Operands &operands,
28 const std::shared_ptr<ITensorRegistry> &tensor_reg)
29 : IConstantInitializer{operands}, _tensor_reg{tensor_reg}
34 void ConstantInitializer::registerDefaultInitializer(const ir::OperandIndex &index,
35 const ir::Operand &obj)
37 registerExternalInitializer(index, obj);
40 void ConstantInitializer::registerExternalInitializer(const ir::OperandIndex &index,
41 const ir::Operand &obj)
44 // TODO Add to check if tensor has been allocated
45 if (!obj.isConstant())
48 _init_map[index] = [](const onert::ir::Operand &model_obj, onert::backend::ITensor &itensor) {
49 auto data = model_obj.shareData();
50 assert(data && data->base());
51 ExternalTensor &tensor = dynamic_cast<ExternalTensor &>(itensor);
56 void ConstantInitializer::visit(const ir::operation::Conv2D &node)
58 const auto &kernel_index = node.getInputs().at(ir::operation::Conv2D::KERNEL);
59 const auto &kernel_obj = _operands.at(kernel_index);
60 registerExternalInitializer(kernel_index, kernel_obj);
62 const auto &bias_index = node.getInputs().at(ir::operation::Conv2D::BIAS);
63 const auto &bias_obj = _operands.at(bias_index);
64 registerExternalInitializer(bias_index, bias_obj);
67 void ConstantInitializer::visit(const ir::operation::DepthwiseConv2D &node)
69 const auto &kernel_index = node.getInputs().at(ir::operation::DepthwiseConv2D::KERNEL);
70 const auto &kernel_obj = _operands.at(kernel_index);
71 registerExternalInitializer(kernel_index, kernel_obj);
73 const auto &bias_index = node.getInputs().at(ir::operation::DepthwiseConv2D::BIAS);
74 const auto &bias_obj = _operands.at(bias_index);
75 registerExternalInitializer(bias_index, bias_obj);
78 void ConstantInitializer::visit(const ir::operation::FullyConnected &node)
80 const auto &weight_index = node.getInputs().at(ir::operation::FullyConnected::WEIGHT);
81 const auto &weight_obj = _operands.at(weight_index);
82 registerExternalInitializer(weight_index, weight_obj);
84 const auto &bias_index = node.getInputs().at(ir::operation::FullyConnected::BIAS);
85 if (!bias_index.undefined())
87 const auto &bias_obj = _operands.at(bias_index);
88 registerExternalInitializer(bias_index, bias_obj);
93 } // namespace backend