nGraph passes clean up (#1742)
* Cleanup pass::Manager;Update VisualizeTree to inherit FunctionPass; Removed deprecated tranformations types * Removed legacy code; Updated docs
This commit is contained in:
parent
df7fb6c069
commit
e752911b62
|
|
@ -15,8 +15,7 @@ To visualize the nGraph function to the xDot format or to an image file, use the
|
|||
|
||||
std::shared_ptr<ngraph::Function> nGraph;
|
||||
...
|
||||
std::vector<std::shared_ptr<ngraph::Function>> g2{nGraph};
|
||||
ngraph::pass::VisualizeTree("after.png").run_on_module(g2); // Visualize the nGraph function to an image
|
||||
ngraph::pass::VisualizeTree("after.png").run_on_function(nGraph); // Visualize the nGraph function to an image
|
||||
```
|
||||
|
||||
## CNNNetwork
|
||||
|
|
|
|||
|
|
@ -91,8 +91,3 @@ void check_rt_info(const std::shared_ptr<ngraph::Function> & f) {
|
|||
throw ngraph::ngraph_error(err_msg);
|
||||
}
|
||||
}
|
||||
|
||||
void visualize_function(std::shared_ptr<ngraph::Function> f, const std::string & file_name) {
|
||||
std::vector<std::shared_ptr<ngraph::Function> > g{f};
|
||||
ngraph::pass::VisualizeTree(file_name).run_on_module(g);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ std::pair<bool, std::string> compare_functions(const std::shared_ptr<ngraph::Fun
|
|||
|
||||
void check_rt_info(const std::shared_ptr<ngraph::Function> & f);
|
||||
|
||||
void visualize_function(std::shared_ptr<ngraph::Function> f, const std::string & file_name);
|
||||
|
||||
namespace ngraph {
|
||||
namespace pass {
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@
|
|||
#include <typeinfo>
|
||||
#include <vector>
|
||||
|
||||
#include "ngraph/pass/manager_state.hpp"
|
||||
#include "ngraph/pass/pass.hpp"
|
||||
#include "ngraph/pass/pass_config.hpp"
|
||||
#include "ngraph/pass/validate.hpp"
|
||||
|
|
@ -31,7 +30,6 @@ namespace ngraph
|
|||
namespace pass
|
||||
{
|
||||
class Manager;
|
||||
class ManagerState;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -54,9 +52,6 @@ public:
|
|||
|
||||
void run_passes(std::shared_ptr<Function>, bool transitive = true);
|
||||
|
||||
ManagerState& get_state();
|
||||
PassConfig& get_pass_config() { return m_pass_config; }
|
||||
void set_pass_config(const PassConfig& pass_config) { m_pass_config = pass_config; }
|
||||
void set_pass_visualization(bool new_state) { m_visualize = new_state; }
|
||||
/// \brief Set flag to enable/disable running Validate pass after executing
|
||||
/// each registered pass
|
||||
|
|
@ -102,8 +97,6 @@ private:
|
|||
bool m_has_default_callback = true;
|
||||
|
||||
std::vector<std::shared_ptr<PassBase>> m_pass_list;
|
||||
ManagerState m_state;
|
||||
PassConfig m_pass_config;
|
||||
bool m_visualize = false;
|
||||
bool m_per_pass_validation = true;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,60 +0,0 @@
|
|||
//*****************************************************************************
|
||||
// Copyright 2017-2020 Intel Corporation
|
||||
//
|
||||
// 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.
|
||||
//*****************************************************************************
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <memory>
|
||||
#include <typeindex>
|
||||
#include <typeinfo>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "ngraph/function.hpp"
|
||||
#include "ngraph/node.hpp"
|
||||
|
||||
using visualize_tree_ops_map_t =
|
||||
std::unordered_map<ngraph::Node::type_info_t,
|
||||
std::function<void(const ngraph::Node&, std::ostream& ss)>>;
|
||||
|
||||
namespace ngraph
|
||||
{
|
||||
namespace pass
|
||||
{
|
||||
class ManagerState;
|
||||
}
|
||||
}
|
||||
|
||||
class ngraph::pass::ManagerState
|
||||
{
|
||||
public:
|
||||
void set_visualize_tree_ops_map(const visualize_tree_ops_map_t& ops_map)
|
||||
{
|
||||
m_visualize_tree_ops_map = ops_map;
|
||||
}
|
||||
|
||||
const visualize_tree_ops_map_t& get_visualize_tree_ops_map()
|
||||
{
|
||||
return m_visualize_tree_ops_map;
|
||||
}
|
||||
|
||||
void set_function(const std::shared_ptr<Function> function) { m_function = function; }
|
||||
std::shared_ptr<Function> get_function() const { return m_function; }
|
||||
private:
|
||||
visualize_tree_ops_map_t m_visualize_tree_ops_map;
|
||||
std::shared_ptr<Function> m_function;
|
||||
};
|
||||
|
|
@ -23,7 +23,6 @@
|
|||
#include "ngraph/deprecated.hpp"
|
||||
#include "ngraph/function.hpp"
|
||||
#include "ngraph/node.hpp"
|
||||
#include "ngraph/pass/manager_state.hpp"
|
||||
#include "ngraph/util.hpp"
|
||||
|
||||
namespace ngraph
|
||||
|
|
@ -62,8 +61,6 @@ namespace ngraph
|
|||
virtual const type_info_t& get_type_info() const = 0;
|
||||
|
||||
protected:
|
||||
ManagerState& get_state();
|
||||
void set_state(ManagerState&);
|
||||
void set_property(const PassPropertyMask& prop, bool value);
|
||||
|
||||
param_callback m_transformation_callback =
|
||||
|
|
@ -72,17 +69,9 @@ namespace ngraph
|
|||
|
||||
private:
|
||||
PassPropertyMask m_property;
|
||||
ManagerState* m_state{nullptr};
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
class NGRAPH_API ModulePass : public PassBase
|
||||
{
|
||||
public:
|
||||
virtual ~ModulePass();
|
||||
virtual bool run_on_module(std::vector<std::shared_ptr<ngraph::Function>>&) = 0;
|
||||
};
|
||||
|
||||
class NGRAPH_API FunctionPass : public PassBase
|
||||
{
|
||||
public:
|
||||
|
|
@ -100,15 +89,6 @@ namespace ngraph
|
|||
virtual bool run_on_node(std::shared_ptr<ngraph::Node>) = 0;
|
||||
};
|
||||
|
||||
class NGRAPH_DEPRECATED("Use MatcherPass or FunctionPass instead.") NGRAPH_API CallGraphPass
|
||||
: public PassBase
|
||||
{
|
||||
public:
|
||||
virtual ~CallGraphPass();
|
||||
virtual bool run_on_call_graph(const std::list<std::shared_ptr<ngraph::Node>>&) = 0;
|
||||
virtual bool run_on_call_graph(const std::vector<std::shared_ptr<ngraph::Node>>&);
|
||||
};
|
||||
|
||||
class Manager;
|
||||
enum class FusionType : uint32_t
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
//*****************************************************************************
|
||||
// Copyright 2017-2020 Intel Corporation
|
||||
//
|
||||
// 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.
|
||||
//*****************************************************************************
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib> // llvm 8.1 gets confused about `malloc` otherwise
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "ngraph/node.hpp"
|
||||
|
||||
namespace ngraph
|
||||
{
|
||||
namespace pass
|
||||
{
|
||||
std::function<bool(std::shared_ptr<Node>)> get_no_fan_out_function();
|
||||
}
|
||||
}
|
||||
|
|
@ -25,7 +25,6 @@
|
|||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#include "ngraph/pass/manager_state.hpp"
|
||||
#include "ngraph/pass/pass.hpp"
|
||||
|
||||
namespace ngraph
|
||||
|
|
@ -38,7 +37,11 @@ namespace ngraph
|
|||
|
||||
class HeightMap;
|
||||
|
||||
class NGRAPH_API ngraph::pass::VisualizeTree : public ModulePass
|
||||
using visualize_tree_ops_map_t =
|
||||
std::unordered_map<ngraph::Node::type_info_t,
|
||||
std::function<void(const ngraph::Node&, std::ostream& ss)>>;
|
||||
|
||||
class NGRAPH_API ngraph::pass::VisualizeTree : public FunctionPass
|
||||
{
|
||||
public:
|
||||
NGRAPH_RTTI_DECLARATION;
|
||||
|
|
@ -48,7 +51,7 @@ public:
|
|||
VisualizeTree(const std::string& file_name,
|
||||
node_modifiers_t nm = nullptr,
|
||||
bool dot_only = false);
|
||||
bool run_on_module(std::vector<std::shared_ptr<ngraph::Function>>&) override;
|
||||
bool run_on_function(std::shared_ptr<ngraph::Function>) override;
|
||||
|
||||
void set_ops_to_details(const visualize_tree_ops_map_t& ops_map) { m_ops_to_details = ops_map; }
|
||||
protected:
|
||||
|
|
|
|||
|
|
@ -49,9 +49,6 @@ void pass::Manager::run_passes(shared_ptr<Function> func, bool /* transitive */)
|
|||
|
||||
static bool profile_enabled = getenv_bool("NGRAPH_PROFILE_PASS_ENABLE");
|
||||
|
||||
get_state().set_function(func);
|
||||
vector<shared_ptr<Function>> f_array{func};
|
||||
|
||||
size_t index = 0;
|
||||
stopwatch pass_timer;
|
||||
stopwatch overall_timer;
|
||||
|
|
@ -60,22 +57,13 @@ void pass::Manager::run_passes(shared_ptr<Function> func, bool /* transitive */)
|
|||
for (auto& pass : m_pass_list)
|
||||
{
|
||||
pass_timer.start();
|
||||
pass->set_state(get_state());
|
||||
if (!m_has_default_callback)
|
||||
{
|
||||
pass->set_callback(m_transformation_callback);
|
||||
}
|
||||
|
||||
NGRAPH_SUPPRESS_DEPRECATED_START
|
||||
if (auto module_pass = dynamic_pointer_cast<ModulePass>(pass))
|
||||
{
|
||||
if (auto vt_pass = dynamic_pointer_cast<pass::VisualizeTree>(module_pass))
|
||||
{
|
||||
vt_pass->set_ops_to_details(get_state().get_visualize_tree_ops_map());
|
||||
}
|
||||
function_changed = module_pass->run_on_module(f_array);
|
||||
}
|
||||
else if (auto matcher_pass = dynamic_pointer_cast<MatcherPass>(pass))
|
||||
if (auto matcher_pass = dynamic_pointer_cast<MatcherPass>(pass))
|
||||
{
|
||||
// This checks is to skip the graph transformation when the graph pass relies on
|
||||
// static shape but the function state is dynamic.
|
||||
|
|
@ -128,17 +116,6 @@ void pass::Manager::run_passes(shared_ptr<Function> func, bool /* transitive */)
|
|||
function_changed |= node_pass->run_on_node(n);
|
||||
}
|
||||
}
|
||||
else if (auto call_graph_pass = dynamic_pointer_cast<CallGraphPass>(pass))
|
||||
{
|
||||
if (call_graph_pass->get_property(PassProperty::REQUIRE_STATIC_SHAPE) &&
|
||||
func->is_dynamic())
|
||||
{
|
||||
NGRAPH_DEBUG << "Pass " << pass->get_name() << " requires static shape but the "
|
||||
<< "function is dynamic. Skipping this transformation";
|
||||
continue;
|
||||
}
|
||||
function_changed = call_graph_pass->run_on_call_graph(func->get_ordered_ops());
|
||||
}
|
||||
NGRAPH_SUPPRESS_DEPRECATED_END
|
||||
|
||||
if (m_visualize)
|
||||
|
|
@ -147,7 +124,7 @@ void pass::Manager::run_passes(shared_ptr<Function> func, bool /* transitive */)
|
|||
const size_t num_digits_in_pass_index = 3;
|
||||
std::string index_str = std::to_string(index);
|
||||
index_str = std::string(num_digits_in_pass_index - index_str.length(), '0') + index_str;
|
||||
auto base_filename = f_array.at(0)->get_name() + std::string("_") + index_str +
|
||||
auto base_filename = func->get_name() + std::string("_") + index_str +
|
||||
std::string("_") + pass->get_name();
|
||||
|
||||
if (m_visualize)
|
||||
|
|
@ -155,8 +132,7 @@ void pass::Manager::run_passes(shared_ptr<Function> func, bool /* transitive */)
|
|||
static const string format = getenv_string("NGRAPH_VISUALIZE_TRACING_FORMAT");
|
||||
auto file_ext = format.empty() ? "svg" : format;
|
||||
pass::VisualizeTree vt(base_filename + std::string(".") + file_ext);
|
||||
vt.set_ops_to_details(get_state().get_visualize_tree_ops_map());
|
||||
vt.run_on_module(f_array);
|
||||
vt.run_on_function(func);
|
||||
}
|
||||
}
|
||||
index++;
|
||||
|
|
@ -171,8 +147,3 @@ void pass::Manager::run_passes(shared_ptr<Function> func, bool /* transitive */)
|
|||
cout << "passes done in " << overall_timer.get_milliseconds() << "ms\n";
|
||||
}
|
||||
}
|
||||
|
||||
pass::ManagerState& pass::Manager::get_state()
|
||||
{
|
||||
return m_state;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,16 +36,6 @@ pass::PassBase::PassBase()
|
|||
{
|
||||
}
|
||||
|
||||
pass::ManagerState& pass::PassBase::get_state()
|
||||
{
|
||||
return *m_state;
|
||||
}
|
||||
|
||||
void pass::PassBase::set_state(ManagerState& state)
|
||||
{
|
||||
m_state = &state;
|
||||
}
|
||||
|
||||
bool pass::PassBase::get_property(const PassPropertyMask& prop) const
|
||||
{
|
||||
return m_property.is_set(prop);
|
||||
|
|
@ -89,10 +79,6 @@ void pass::PassBase::set_callback(const param_callback& callback)
|
|||
|
||||
// The symbols are requiered to be in cpp file to workaround RTTI issue on Android LLVM
|
||||
|
||||
pass::ModulePass::~ModulePass()
|
||||
{
|
||||
}
|
||||
|
||||
pass::FunctionPass::~FunctionPass()
|
||||
{
|
||||
}
|
||||
|
|
@ -100,17 +86,3 @@ pass::FunctionPass::~FunctionPass()
|
|||
pass::NodePass::~NodePass()
|
||||
{
|
||||
}
|
||||
|
||||
pass::CallGraphPass::~CallGraphPass()
|
||||
{
|
||||
}
|
||||
|
||||
bool pass::CallGraphPass::run_on_call_graph(const std::vector<std::shared_ptr<ngraph::Node>>& nodes)
|
||||
{
|
||||
list<shared_ptr<Node>> node_list;
|
||||
for (auto op : nodes)
|
||||
{
|
||||
node_list.push_back(op);
|
||||
}
|
||||
return run_on_call_graph(node_list);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
//*****************************************************************************
|
||||
// Copyright 2017-2020 Intel Corporation
|
||||
//
|
||||
// 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 "ngraph/pass/pass_util.hpp"
|
||||
#include "ngraph/log.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace ngraph;
|
||||
|
||||
std::function<bool(std::shared_ptr<Node>)> ngraph::pass::get_no_fan_out_function()
|
||||
{
|
||||
auto ret_fun = [](std::shared_ptr<Node> n) {
|
||||
auto users = n->get_users(true);
|
||||
std::set<std::shared_ptr<Node>> user_set(users.begin(), users.end());
|
||||
size_t num_unique_users = user_set.size();
|
||||
if (num_unique_users == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
NGRAPH_DEBUG << n->get_name() << " has fan out\n";
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
return ret_fun;
|
||||
}
|
||||
|
|
@ -180,48 +180,44 @@ static std::string label_edge(const std::shared_ptr<Node>& /* src */,
|
|||
|
||||
NGRAPH_RTTI_DEFINITION(ngraph::pass::VisualizeTree, "ngraph::pass::VisualizeTree", 0);
|
||||
|
||||
bool pass::VisualizeTree::run_on_module(vector<shared_ptr<Function>>& functions)
|
||||
bool pass::VisualizeTree::run_on_function(std::shared_ptr<ngraph::Function> f)
|
||||
{
|
||||
for (shared_ptr<Function> f : functions)
|
||||
unordered_map<Node*, HeightMap> height_maps;
|
||||
|
||||
for (auto& node : f->get_ops())
|
||||
{
|
||||
unordered_map<Node*, HeightMap> height_maps;
|
||||
|
||||
for (auto& node : f->get_ops())
|
||||
if (node->description() == "Result")
|
||||
{
|
||||
if (node->description() == "Result")
|
||||
{
|
||||
height_maps[node.get()] = HeightMap({node.get()});
|
||||
}
|
||||
else
|
||||
{
|
||||
height_maps[node.get()] = HeightMap();
|
||||
}
|
||||
height_maps[node.get()] = HeightMap({node.get()});
|
||||
}
|
||||
|
||||
auto nodes = topological_sort(f->get_ops());
|
||||
|
||||
for (auto it = nodes.rbegin(); it != nodes.rend(); ++it)
|
||||
else
|
||||
{
|
||||
auto& node = *it;
|
||||
for (auto& output : node->outputs())
|
||||
{
|
||||
for (auto& input : output.get_target_inputs())
|
||||
{
|
||||
auto target_node = input.get_node();
|
||||
height_maps[node.get()].absorb(height_maps[target_node]);
|
||||
}
|
||||
}
|
||||
height_maps[node.get()] = HeightMap();
|
||||
}
|
||||
|
||||
// TODO(amprocte): Maybe find a way to make this tunable.
|
||||
|
||||
size_t fake_node_ctr = 0;
|
||||
|
||||
traverse_nodes(f, [&](shared_ptr<Node> node) {
|
||||
add_node_arguments(node, height_maps, fake_node_ctr);
|
||||
});
|
||||
}
|
||||
|
||||
auto nodes = topological_sort(f->get_ops());
|
||||
|
||||
for (auto it = nodes.rbegin(); it != nodes.rend(); ++it)
|
||||
{
|
||||
auto& node = *it;
|
||||
for (auto& output : node->outputs())
|
||||
{
|
||||
for (auto& input : output.get_target_inputs())
|
||||
{
|
||||
auto target_node = input.get_node();
|
||||
height_maps[node.get()].absorb(height_maps[target_node]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(amprocte): Maybe find a way to make this tunable.
|
||||
|
||||
size_t fake_node_ctr = 0;
|
||||
|
||||
traverse_nodes(
|
||||
f, [&](shared_ptr<Node> node) { add_node_arguments(node, height_maps, fake_node_ctr); });
|
||||
|
||||
render();
|
||||
|
||||
// Clean up local variable not to hold node pointers
|
||||
|
|
|
|||
Loading…
Reference in New Issue