From 08ed4d2371ecc99b0d5fcee2cf2b13cedaedcecc Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 28 Jun 2019 10:29:08 +0900 Subject: [PATCH] [moco.tf] Extract Phase infrastructure (#4008) This commit extracts "transform" loop in Import as reusable Phase class. Signed-off-by: Jonghyun Park --- contrib/moco-tf/src/Import.cpp | 22 +++------------ contrib/moco-tf/src/Phase.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++ contrib/moco-tf/src/Phase.h | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 contrib/moco-tf/src/Phase.cpp create mode 100644 contrib/moco-tf/src/Phase.h diff --git a/contrib/moco-tf/src/Import.cpp b/contrib/moco-tf/src/Import.cpp index dd8b76b..b0b4c2b 100644 --- a/contrib/moco-tf/src/Import.cpp +++ b/contrib/moco-tf/src/Import.cpp @@ -20,6 +20,7 @@ #include "GraphBuilderContext.h" #include "GraphBuilderRegistry.h" #include "Transforms.h" +#include "Phase.h" #include "Annotations/ShapeInferenceData.h" @@ -204,7 +205,7 @@ void transform_graph(loco::Graph *graph) LOGGER(transform_graph); std::vector> prepare; - std::vector> transforms; + moco::tf::Phase transforms; // Transforms that run only once for preparation and finalization { @@ -224,24 +225,9 @@ void transform_graph(loco::Graph *graph) tr->run(graph); } - bool changed; - do - { - changed = false; - - for (auto &tr : transforms) - { - INFO(transform_graph) << "Before transform"; - INFO(transform_graph) << locop::fmt(graph); - - if (tr->run(graph)) - changed = true; - - INFO(transform_graph) << "After transform (changed: " << (changed ? 'Y' : 'N') << ")"; - INFO(transform_graph) << locop::fmt(graph); - } + moco::tf::PhaseRunner runner{graph}; - } while (changed); + runner.run(transforms); // TODO would be better to run this code only when log is enabled { diff --git a/contrib/moco-tf/src/Phase.cpp b/contrib/moco-tf/src/Phase.cpp new file mode 100644 index 0000000..a663a3e --- /dev/null +++ b/contrib/moco-tf/src/Phase.cpp @@ -0,0 +1,63 @@ +/* + * 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 "Phase.h" + +#include +#include + +namespace +{ + +char to_char(bool b) { return b ? 'Y' : 'N'; } + +} // namespace + +namespace moco +{ +namespace tf +{ + +void PhaseRunner::run(const Phase &phase) const +{ + LOGGER(l); + + INFO(l) << "PhaseRunner"; + + INFO(l) << "Initial graph"; + INFO(l) << locop::fmt(_graph); + + for (bool changed = true; changed;) + { + changed = false; + + for (auto &tr : phase) + { + if (tr->run(_graph)) + { + changed = true; + } + + INFO(l) << "After " << transform_name(tr.get()) << " (changed: " << to_char(changed) << ")"; + INFO(l) << locop::fmt(_graph); + } + } + + INFO(l) << "PhaseRunner - done"; +} + +} // namespace tf +} // namespace moco diff --git a/contrib/moco-tf/src/Phase.h b/contrib/moco-tf/src/Phase.h new file mode 100644 index 0000000..e95449b --- /dev/null +++ b/contrib/moco-tf/src/Phase.h @@ -0,0 +1,61 @@ +/* + * 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_PHASE_H__ +#define __MOCO_TF_PHASE_H__ + +#include "Transforms/Transform.h" + +#include + +#include +#include + +namespace moco +{ +namespace tf +{ + +// Phase is a collection of Transform(s) +using Phase = std::vector>; + +enum class PhaseStrategy +{ + // Run all the transforms until there is no transform that makes a change + Saturate, +}; + +template class PhaseRunner; + +template <> class PhaseRunner +{ +public: + PhaseRunner(loco::Graph *graph) : _graph{graph} + { + // DO NOTHING + } + +public: + void run(const Phase &) const; + +private: + loco::Graph *_graph; +}; + +} // namespace tf +} // namespace moco + +#endif // __MOCO_TF_PHASE_H__ -- 2.7.4