Unverified Commit 3b40c127 authored by Scott Cyphers's avatar Scott Cyphers Committed by GitHub

Create a variant schema and allow nodes to have a rt info (#3702)

* Create a variant schema and allow nodes to have a rt info

* Use map for rt_info

* style

* Another variant

* variant

* style

* Macro didn't work in gcc

* Compiler issues
parent 5fe48a06
......@@ -538,6 +538,8 @@ set (SRC
util.hpp
validation_util.cpp
validation_util.hpp
variant.cpp
variant.hpp
)
set(SRC ${SRC}
......
......@@ -50,6 +50,7 @@ namespace ngraph
template <typename NodeType>
class Output;
class Variant;
class Node;
using NodeVector = std::vector<std::shared_ptr<Node>>;
using OutputVector = std::vector<Output<Node>>;
......@@ -410,6 +411,10 @@ namespace ngraph
/// Set device placement
void set_placement_index(size_t placement);
using RTMap = std::map<std::string, std::shared_ptr<Variant>>;
RTMap& get_rt_info() { return m_rt_info; }
const RTMap& get_rt_info() const { return m_rt_info; }
const std::unordered_set<std::string>& get_provenance_tags() const;
void add_provenance_tag(const std::string& tag);
template <typename T>
......@@ -515,6 +520,7 @@ namespace ngraph
Placement m_placement = Placement::DEFAULT;
size_t m_placement_index = placement_invalid;
std::shared_ptr<ngraph::op::util::OpAnnotations> m_op_annotations;
std::map<std::string, std::shared_ptr<Variant>> m_rt_info;
};
/// \brief A handle for one of a node's inputs.
......
//*****************************************************************************
// Copyright 2017-2019 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/variant.hpp"
using namespace ngraph;
// Define variant for std::string
constexpr VariantTypeInfo VariantWrapper<std::string>::type_info;
constexpr VariantTypeInfo VariantWrapper<int64_t>::type_info;
//*****************************************************************************
// Copyright 2017-2019 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 <string>
#include "ngraph/ngraph_visibility.hpp"
#include "ngraph/type.hpp"
namespace ngraph
{
using VariantTypeInfo = DiscreteTypeInfo;
class Variant
{
public:
virtual ~Variant() {}
virtual const VariantTypeInfo& get_type_info() const = 0;
};
template <typename VT>
class VariantImpl : public Variant
{
public:
using value_type = VT;
VariantImpl(const value_type& value)
: m_value(value)
{
}
const value_type& get() const { return m_value; }
value_type& get() { return m_value; }
void set(const value_type& value) { m_value = value; }
protected:
value_type m_value;
};
template <typename VT>
class VariantWrapper
{
};
template <>
class VariantWrapper<std::string> : public VariantImpl<std::string>
{
public:
NGRAPH_API
static constexpr VariantTypeInfo type_info{"Variant::std::string", 0};
const VariantTypeInfo& get_type_info() const override { return type_info; }
VariantWrapper(const value_type& value)
: VariantImpl<value_type>(value)
{
}
};
template <>
class VariantWrapper<int64_t> : public VariantImpl<int64_t>
{
public:
NGRAPH_API
static constexpr VariantTypeInfo type_info{"Variant::int64_t", 0};
const VariantTypeInfo& get_type_info() const override { return type_info; }
VariantWrapper(const value_type& value)
: VariantImpl<value_type>(value)
{
}
};
}
......@@ -24,6 +24,7 @@
#include "ngraph/graph_util.hpp"
#include "ngraph/ngraph.hpp"
#include "ngraph/variant.hpp"
using namespace std;
using namespace ngraph;
......@@ -59,6 +60,56 @@ TEST(op, provenance_tag)
ASSERT_TRUE(tags.find(tag2) != tags.end());
}
struct Ship
{
std::string name;
int16_t x;
int16_t y;
};
namespace ngraph
{
template <>
class VariantWrapper<Ship> : public VariantImpl<Ship>
{
public:
static constexpr VariantTypeInfo type_info{"Variant::Ship", 0};
const VariantTypeInfo& get_type_info() const override { return type_info; }
VariantWrapper(const value_type& value)
: VariantImpl<value_type>(value)
{
}
};
constexpr VariantTypeInfo VariantWrapper<Ship>::type_info;
}
TEST(op, variant)
{
shared_ptr<Variant> var_std_string = make_shared<VariantWrapper<std::string>>("My string");
ASSERT_TRUE((is_type<VariantWrapper<std::string>>(var_std_string)));
EXPECT_EQ((as_type_ptr<VariantWrapper<std::string>>(var_std_string)->get()), "My string");
shared_ptr<Variant> var_int64_t = make_shared<VariantWrapper<int64_t>>(27);
ASSERT_TRUE((is_type<VariantWrapper<int64_t>>(var_int64_t)));
EXPECT_FALSE((is_type<VariantWrapper<std::string>>(var_int64_t)));
EXPECT_EQ((as_type_ptr<VariantWrapper<int64_t>>(var_int64_t)->get()), 27);
shared_ptr<Variant> var_ship = make_shared<VariantWrapper<Ship>>(Ship{"Lollipop", 3, 4});
ASSERT_TRUE((is_type<VariantWrapper<Ship>>(var_ship)));
Ship& ship = as_type_ptr<VariantWrapper<Ship>>(var_ship)->get();
EXPECT_EQ(ship.name, "Lollipop");
EXPECT_EQ(ship.x, 3);
EXPECT_EQ(ship.y, 4);
auto node = make_shared<op::Parameter>(element::f32, Shape{1});
node->get_rt_info()["A"] = var_ship;
auto node_var_ship = node->get_rt_info().at("A");
ASSERT_TRUE((is_type<VariantWrapper<Ship>>(node_var_ship)));
Ship& node_ship = as_type_ptr<VariantWrapper<Ship>>(node_var_ship)->get();
EXPECT_EQ(&node_ship, &ship);
}
// TODO: Need to mock Node, Op etc to be able to unit test functions like replace_node().
// Mocking them directly isn't possible because google test requires methods to be
// non-virtual. For non-virtual methods we will need to templatize these classes and call using
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment