From: 박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 Date: Wed, 21 Aug 2019 05:36:52 +0000 (+0900) Subject: [moco-tf] Introduce RemoveTFIdentityNodeTransform (#6772) X-Git-Tag: accepted/tizen/unified/20190903.052428~248 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2c395d8a5bac40f774da9a66f44bd4ea2ea9461b;p=platform%2Fcore%2Fml%2Fnnfw.git [moco-tf] Introduce RemoveTFIdentityNodeTransform (#6772) This will introduce RemoveTFIdentityNodeTransform that disconnects TFIdentity for optimization Signed-off-by: SaeHie Park --- diff --git a/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.cpp b/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.cpp new file mode 100644 index 0000000..50f7ab9 --- /dev/null +++ b/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2019 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 "RemoveTFIdentityNodeTransform.h" + +#include "Dialect/TFDialect.h" +#include "Dialect/TFNode.h" + +#include + +namespace moco +{ +namespace tf +{ + +bool RemoveTFIdentityNodeTransform::run(loco::Graph *g) +{ + struct Collector final : public moco::tf::TFNodeMutableVisitor + { + void visit(moco::tf::TFIdentity *node) final + { + if (node->input() != nullptr) + { + candidates.insert(node); + } + } + + void visit(moco::tf::TFNode *) final { return; } + + std::set candidates; + }; + + Collector collector; + + for (auto node : loco::all_nodes(g)) + { + if (node->dialect() == moco::tf::TFDialect::get()) + { + auto tf_node = dynamic_cast(node); + tf_node->accept(&collector); + } + } + + for (auto node : collector.candidates) + { + replace(node).with(node->input()); + node->input(nullptr); + } + + return collector.candidates.size() > 0; +} + +} // namespace tf +} // namespace moco diff --git a/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.h b/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.h new file mode 100644 index 0000000..534061e --- /dev/null +++ b/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_TF_REMOVE_TFIDENTITY_NODE_TRANSFORM_H__ +#define __MOCO_TF_REMOVE_TFIDENTITY_NODE_TRANSFORM_H__ + +#include "Transform.h" + +namespace moco +{ +namespace tf +{ + +/** + * @brief Use the input of "TFIdentity" node instead + * + * BEFORE: + * [X] -> [TFIdentity] -> [Y] + * + * AFTER: + * [X] -> [Y] + * [TFIdentity] + * + * NOTE This transform does not remove "TFIdentity" node + * This transform is identical to RemoveForwardNodeTransform + */ +struct RemoveTFIdentityNodeTransform final : public Transform +{ + const char *name(void) const final { return "RemoveTFIdentityNodeTransform"; } + + bool run(loco::Graph *g) final; +}; + +} // namespace tf +} // namespace moco + +#endif // __MOCO_TF_REMOVE_TFIDENTITY_NODE_TRANSFORM_H__