#ifndef GRAPH_UTILS_PM_PASS_BASE_HPP
#define GRAPH_UTILS_PM_PASS_BASE_HPP
#include <functional>
#include <list>
#include <memory>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include "graph/interface/graph.hpp"
#include "graph/utils/debug.hpp"
#include "graph/utils/json.hpp"
#include "graph/utils/pm/pbuilder.hpp"
namespace dnnl {
namespace impl {
namespace graph {
namespace pass {
using pb_graph_t = utils::pm::pb_graph_t;
class pass_base_t;
using pass_base_ptr = std::shared_ptr<pass_base_t>;
using FCreatePattern
= std::function<void(const std::shared_ptr<pb_graph_t> &pattern_graph)>;
using Pattern = std::shared_ptr<pb_graph_t>;
class pass_base_t {
public:
pass_base_t(std::string pbackend, std::string pname)
: backend_(std::move(pbackend)), name_(std::move(pname)) {}
pass_base_t() = default;
virtual impl::status_t run(graph_t &agraph) {
UNUSED(agraph);
return impl::status::success;
}
virtual void save(utils::json::json_writer_t *writer) {
writer->begin_object();
writer->write_keyvalue("pass_name", name_);
writer->write_keyvalue("pass_backend", backend_);
writer->write_keyvalue("priority", priority_);
writer->write_keyvalue("enable", enable_);
writer->write_keyvalue("kind", utils::partition_kind2str(pkind_));
writer->end_object();
}
virtual void load(utils::json::json_reader_t *reader) {
utils::json::read_helper_t helper;
std::string kind;
helper.declare_field("pass_name", &name_);
helper.declare_field("pass_backend", &backend_);
helper.declare_field("priority", &priority_);
helper.declare_field("enable", &enable_);
helper.declare_field("kind", &kind);
helper.read_fields(reader);
pkind_ = utils::str2partition_kind(kind);
}
virtual ~pass_base_t() = default;
std::string get_pass_backend() { return backend_; }
std::string get_pass_name() { return name_; }
pass_base_t &set_priority(float priority) {
priority_ = priority;
return *this;
}
float get_priority() const { return priority_; }
pass_base_t &set_enable(bool enable) {
enable_ = enable;
return *this;
}
bool get_enable() const { return enable_; }
pass_base_t &set_kind(partition_kind_t pkind) {
pkind_ = pkind;
return *this;
}
partition_kind_t get_kind() const { return pkind_; }
pass_base_t &set_engine_kind(engine_kind_t kind) {
engine_kind_ = kind;
return *this;
}
engine_kind_t get_engine_kind() const { return engine_kind_; }
template <typename value_type>
pass_base_t &set_attr(const std::string &attr_name, const value_type &value) {
attrs_.insert(make_pair(attr_name, value));
return *this;
}
template <typename value_type>
std::vector<value_type> get_attr(const std::string &attr_name) {
std::vector<value_type> attr_vec;
for (auto it = attrs_.begin(); it != attrs_.end(); ++it) {
if (it->first == attr_name) {
attr_vec.push_back(utils::any_cast<value_type>(it->second));
}
}
return attr_vec;
}
bool has_attr(const std::string &attr_name) {
return attrs_.find(attr_name) != attrs_.end();
}
protected:
std::unordered_multimap<std::string, utils::any_t> attrs_;
private:
std::string backend_;
std::string name_;
float priority_ {5.0f};
bool enable_ {true};
partition_kind_t pkind_ {partition_kind_t::undef};
engine_kind_t engine_kind_ {engine_kind::any_engine};
};
template <>
pass_base_t &pass_base_t::set_attr<FCreatePattern>(
const std::string &attr_name, const FCreatePattern &func);
} } } }
#endif