From 33ae5d70a1ed85d215c1293ed63afbf3517b07d5 Mon Sep 17 00:00:00 2001 From: Chunseok Lee Date: Tue, 19 Oct 2021 11:32:46 +0900 Subject: Imported Upstream version 1.18.0 --- compiler/luci/pass/src/ForceQuantParamPass.cpp | 77 ++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 compiler/luci/pass/src/ForceQuantParamPass.cpp (limited to 'compiler/luci/pass/src/ForceQuantParamPass.cpp') diff --git a/compiler/luci/pass/src/ForceQuantParamPass.cpp b/compiler/luci/pass/src/ForceQuantParamPass.cpp new file mode 100644 index 000000000..32d482fc1 --- /dev/null +++ b/compiler/luci/pass/src/ForceQuantParamPass.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "luci/Pass/ForceQuantParamPass.h" +#include "luci/Profile/CircleNodeID.h" + +#include +#include + +namespace luci +{ + +namespace +{ + +void set_qparam(luci::CircleNode *node, float scale, int64_t zp) +{ + assert(node); // FIX_CALLER_UNLESS + + auto quantparam = std::make_unique(); + quantparam->scale.push_back(scale); + quantparam->zerop.push_back(zp); + + node->quantparam(std::move(quantparam)); +} + +} // namespace + +bool ForceQuantParamPass::run(loco::Graph *g) +{ + LOGGER(l); + INFO(l) << "ForceQuantParamPass Start" << std::endl; + + for (auto node : loco::active_nodes(loco::output_nodes(g))) + { + auto const cnode = loco::must_cast(node); + auto const name = cnode->name(); + auto target = std::find(_tensors.begin(), _tensors.end(), name); + if (target == _tensors.end()) + continue; + + auto index = target - _tensors.begin(); + auto scale = _scales[index]; + auto zp = _zerops[index]; + set_qparam(cnode, scale, zp); + + _tensors.erase(_tensors.begin() + index); + _scales.erase(_scales.begin() + index); + _zerops.erase(_zerops.begin() + index); + } + + if (_tensors.size() > 0) + { + std::string msg; + for (auto const &t : _tensors) + msg += "Tensor does not exist: " + t + ".\n"; + msg += "Please check tensor name.\n"; + throw std::runtime_error(msg); + } + + INFO(l) << "ForceQuantParamPass End" << std::endl; + return false; // one time run +} + +} // namespace luci -- cgit v1.2.3