Commit 8eed208b authored by Adam Procter's avatar Adam Procter

Unify folders for arithmetic reduction ops; add Max and Min

parent d8d940d0
...@@ -40,7 +40,9 @@ ...@@ -40,7 +40,9 @@
#include "ngraph/op/greater_eq.hpp" #include "ngraph/op/greater_eq.hpp"
#include "ngraph/op/less.hpp" #include "ngraph/op/less.hpp"
#include "ngraph/op/less_eq.hpp" #include "ngraph/op/less_eq.hpp"
#include "ngraph/op/max.hpp"
#include "ngraph/op/maximum.hpp" #include "ngraph/op/maximum.hpp"
#include "ngraph/op/min.hpp"
#include "ngraph/op/minimum.hpp" #include "ngraph/op/minimum.hpp"
#include "ngraph/op/multiply.hpp" #include "ngraph/op/multiply.hpp"
#include "ngraph/op/negative.hpp" #include "ngraph/op/negative.hpp"
...@@ -76,7 +78,9 @@ ...@@ -76,7 +78,9 @@
#include "ngraph/runtime/reference/greater_eq.hpp" #include "ngraph/runtime/reference/greater_eq.hpp"
#include "ngraph/runtime/reference/less.hpp" #include "ngraph/runtime/reference/less.hpp"
#include "ngraph/runtime/reference/less_eq.hpp" #include "ngraph/runtime/reference/less_eq.hpp"
#include "ngraph/runtime/reference/max.hpp"
#include "ngraph/runtime/reference/maximum.hpp" #include "ngraph/runtime/reference/maximum.hpp"
#include "ngraph/runtime/reference/min.hpp"
#include "ngraph/runtime/reference/minimum.hpp" #include "ngraph/runtime/reference/minimum.hpp"
#include "ngraph/runtime/reference/multiply.hpp" #include "ngraph/runtime/reference/multiply.hpp"
#include "ngraph/runtime/reference/negate.hpp" #include "ngraph/runtime/reference/negate.hpp"
...@@ -1584,180 +1588,138 @@ void pass::ConstantFolding::construct_constant_reverse() ...@@ -1584,180 +1588,138 @@ void pass::ConstantFolding::construct_constant_reverse()
} }
template <typename T> template <typename T>
static shared_ptr<op::Constant> fold_constant_product_helper(shared_ptr<op::Constant> constant, static shared_ptr<op::Constant>
const AxisSet& reduction_axes, fold_constant_arithmetic_reduction_helper(shared_ptr<op::Constant> constant,
const Shape& result_shape) shared_ptr<Node> reduction_node)
{ {
vector<T> out_vec(shape_size(result_shape)); vector<T> out_vec(shape_size(reduction_node->get_shape()));
runtime::reference::product<T>(constant->get_vector<T>().data(), if (auto p = dynamic_pointer_cast<op::Max>(reduction_node))
{
runtime::reference::max<T>(constant->get_vector<T>().data(),
out_vec.data(), out_vec.data(),
constant->get_output_shape(0), constant->get_output_shape(0),
result_shape, reduction_node->get_shape(),
reduction_axes); p->get_reduction_axes());
}
return make_shared<op::Constant>(constant->get_output_element_type(0), result_shape, out_vec); else if (auto p = dynamic_pointer_cast<op::Min>(reduction_node))
}
static shared_ptr<op::Constant> fold_constant_product(shared_ptr<op::Constant> constant,
const AxisSet& reduction_axes,
const Shape& result_shape)
{
auto& input_element_type = constant->get_output_element_type(0);
switch (input_element_type.get_type_enum())
{ {
case element::Type_t::undefined: runtime::reference::min<T>(constant->get_vector<T>().data(),
NGRAPH_CHECK(false, "Encountered 'undefined' element type in fold_constant_product"); out_vec.data(),
break; constant->get_output_shape(0),
case element::Type_t::dynamic: reduction_node->get_shape(),
NGRAPH_CHECK(false, "Encountered 'dynamic' element type in fold_constant_product"); p->get_reduction_axes());
break; }
case element::Type_t::boolean: else if (auto p = dynamic_pointer_cast<op::Product>(reduction_node))
return fold_constant_product_helper<char>(constant, reduction_axes, result_shape); {
case element::Type_t::bf16: runtime::reference::product<T>(constant->get_vector<T>().data(),
return fold_constant_product_helper<bfloat16>(constant, reduction_axes, result_shape); out_vec.data(),
case element::Type_t::f16: constant->get_output_shape(0),
return fold_constant_product_helper<float16>(constant, reduction_axes, result_shape); reduction_node->get_shape(),
case element::Type_t::f32: p->get_reduction_axes());
return fold_constant_product_helper<float>(constant, reduction_axes, result_shape); }
case element::Type_t::f64: else if (auto s = dynamic_pointer_cast<op::Sum>(reduction_node))
return fold_constant_product_helper<double>(constant, reduction_axes, result_shape); {
case element::Type_t::i8: runtime::reference::sum<T>(constant->get_vector<T>().data(),
return fold_constant_product_helper<int8_t>(constant, reduction_axes, result_shape); out_vec.data(),
case element::Type_t::i16: constant->get_output_shape(0),
return fold_constant_product_helper<int16_t>(constant, reduction_axes, result_shape); reduction_node->get_shape(),
case element::Type_t::i32: s->get_reduction_axes());
return fold_constant_product_helper<int32_t>(constant, reduction_axes, result_shape); }
case element::Type_t::i64: else
return fold_constant_product_helper<int64_t>(constant, reduction_axes, result_shape); {
case element::Type_t::u8: NGRAPH_CHECK(false,
return fold_constant_product_helper<uint8_t>(constant, reduction_axes, result_shape); "Internal nGraph error: Ops handled in "
case element::Type_t::u16: "fold_constant_arithmetic_reduction_helper must be consistent with those "
return fold_constant_product_helper<uint16_t>(constant, reduction_axes, result_shape); "matched in construct_constant_arithmetic_reduction");
case element::Type_t::u32:
return fold_constant_product_helper<uint32_t>(constant, reduction_axes, result_shape);
case element::Type_t::u64:
return fold_constant_product_helper<uint64_t>(constant, reduction_axes, result_shape);
} }
NGRAPH_UNREACHABLE("Unexpected switch case"); return make_shared<op::Constant>(
} reduction_node->get_output_element_type(0), reduction_node->get_shape(), out_vec);
void pass::ConstantFolding::construct_constant_product()
{
auto constant_label = make_shared<pattern::op::Label>(
element::i32, Shape{2, 3, 4}, pattern::has_class<op::Constant>());
auto convert_op = make_shared<op::Product>(constant_label, AxisSet{0, 1, 2});
auto constant_product_callback = [constant_label](pattern::Matcher& m) {
NGRAPH_DEBUG << "In callback for constant_product_callback against node = "
<< m.get_match_root()->get_name();
auto pattern_map = m.get_pattern_map();
auto constant_match = static_pointer_cast<op::Constant>(pattern_map[constant_label]);
auto product_match = static_pointer_cast<op::Product>(m.get_match_root());
replace_node(m.get_match_root(),
fold_constant_product(constant_match,
product_match->get_reduction_axes(),
product_match->get_output_shape(0)));
return true;
};
auto convert_matcher =
make_shared<pattern::Matcher>(convert_op, "ConstantFolding.ConstantProduct");
this->add_matcher(convert_matcher, constant_product_callback, all_pass_property_off);
}
// TODO(amprocte): Find a way to reduce duplication with Product. (The fact
// that we bottom out in a reference call makes it a bit tricky.)
template <typename T>
static shared_ptr<op::Constant> fold_constant_sum_helper(shared_ptr<op::Constant> constant,
const AxisSet& reduction_axes,
const Shape& result_shape)
{
vector<T> out_vec(shape_size(result_shape));
runtime::reference::sum<T>(constant->get_vector<T>().data(),
out_vec.data(),
constant->get_output_shape(0),
result_shape,
reduction_axes);
return make_shared<op::Constant>(constant->get_output_element_type(0), result_shape, out_vec);
} }
static shared_ptr<op::Constant> fold_constant_sum(shared_ptr<op::Constant> constant, static shared_ptr<op::Constant>
const AxisSet& reduction_axes, fold_constant_arithmetic_reduction(shared_ptr<op::Constant> constant,
const Shape& result_shape) shared_ptr<Node> reduction_node)
{ {
auto& input_element_type = constant->get_output_element_type(0); auto& input_element_type = constant->get_output_element_type(0);
switch (input_element_type.get_type_enum()) switch (input_element_type.get_type_enum())
{ {
case element::Type_t::undefined: case element::Type_t::undefined:
NGRAPH_CHECK(false, "Encountered 'undefined' element type in fold_constant_sum"); NGRAPH_CHECK(false,
"Encountered 'undefined' element type in fold_constant_arithmetic_reduction");
break; break;
case element::Type_t::dynamic: case element::Type_t::dynamic:
NGRAPH_CHECK(false, "Encountered 'dynamic' element type in fold_constant_sum"); NGRAPH_CHECK(false,
"Encountered 'dynamic' element type in fold_constant_arithmetic_reduction");
break; break;
case element::Type_t::boolean: case element::Type_t::boolean:
return fold_constant_sum_helper<char>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<char>(constant, reduction_node);
case element::Type_t::bf16: case element::Type_t::bf16:
return fold_constant_sum_helper<bfloat16>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<bfloat16>(constant, reduction_node);
case element::Type_t::f16: case element::Type_t::f16:
return fold_constant_sum_helper<float16>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<float16>(constant, reduction_node);
case element::Type_t::f32: case element::Type_t::f32:
return fold_constant_sum_helper<float>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<float>(constant, reduction_node);
case element::Type_t::f64: case element::Type_t::f64:
return fold_constant_sum_helper<double>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<double>(constant, reduction_node);
case element::Type_t::i8: case element::Type_t::i8:
return fold_constant_sum_helper<int8_t>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<int8_t>(constant, reduction_node);
case element::Type_t::i16: case element::Type_t::i16:
return fold_constant_sum_helper<int16_t>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<int16_t>(constant, reduction_node);
case element::Type_t::i32: case element::Type_t::i32:
return fold_constant_sum_helper<int32_t>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<int32_t>(constant, reduction_node);
case element::Type_t::i64: case element::Type_t::i64:
return fold_constant_sum_helper<int64_t>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<int64_t>(constant, reduction_node);
case element::Type_t::u8: case element::Type_t::u8:
return fold_constant_sum_helper<uint8_t>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<uint8_t>(constant, reduction_node);
case element::Type_t::u16: case element::Type_t::u16:
return fold_constant_sum_helper<uint16_t>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<uint16_t>(constant, reduction_node);
case element::Type_t::u32: case element::Type_t::u32:
return fold_constant_sum_helper<uint32_t>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<uint32_t>(constant, reduction_node);
case element::Type_t::u64: case element::Type_t::u64:
return fold_constant_sum_helper<uint64_t>(constant, reduction_axes, result_shape); return fold_constant_arithmetic_reduction_helper<uint64_t>(constant, reduction_node);
} }
NGRAPH_UNREACHABLE("Unexpected switch case"); NGRAPH_UNREACHABLE("Unexpected switch case");
} }
void pass::ConstantFolding::construct_constant_sum() void pass::ConstantFolding::construct_constant_arithmetic_reduction()
{ {
auto constant_label = make_shared<pattern::op::Label>( auto constant_data_label = make_shared<pattern::op::Label>(
element::i32, Shape{2, 3, 4}, pattern::has_class<op::Constant>()); element::i32, Shape{2, 3, 4}, pattern::has_class<op::Constant>());
auto convert_op = make_shared<op::Sum>(constant_label, AxisSet{0, 1, 2}); auto constant_axes_label =
make_shared<pattern::op::Label>(element::i64, Shape{2}, pattern::has_class<op::Constant>());
auto constant_sum_callback = [constant_label](pattern::Matcher& m) { auto is_supported_reduction = [](std::shared_ptr<Node> n) {
NGRAPH_DEBUG << "In callback for constant_sum_callback against node = " return (pattern::has_class<op::Max>()(n) || pattern::has_class<op::Min>()(n) ||
pattern::has_class<op::Product>()(n) || pattern::has_class<op::Sum>()(n));
};
auto reduction =
std::make_shared<pattern::op::Any>(element::i32,
Shape{2},
is_supported_reduction,
NodeVector{constant_data_label, constant_axes_label});
auto constant_arithmetic_reduction_callback = [constant_data_label](pattern::Matcher& m) {
NGRAPH_DEBUG << "In callback for constant_arithmetic_reduction_callback against node = "
<< m.get_match_root()->get_name(); << m.get_match_root()->get_name();
auto pattern_map = m.get_pattern_map(); auto pattern_map = m.get_pattern_map();
auto constant_match = static_pointer_cast<op::Constant>(pattern_map[constant_label]); auto constant_match = static_pointer_cast<op::Constant>(pattern_map[constant_data_label]);
auto sum_match = static_pointer_cast<op::Sum>(m.get_match_root()); auto reduction_match = m.get_match_root();
replace_node(m.get_match_root(), replace_node(reduction_match,
fold_constant_sum(constant_match, fold_constant_arithmetic_reduction(constant_match, reduction_match));
sum_match->get_reduction_axes(),
sum_match->get_output_shape(0)));
return true; return true;
}; };
auto convert_matcher = make_shared<pattern::Matcher>(convert_op, "ConstantFolding.ConstantSum"); auto arithmetic_reduction_matcher =
this->add_matcher(convert_matcher, constant_sum_callback, all_pass_property_off); make_shared<pattern::Matcher>(reduction, "ConstantFolding.ConstantArithmeticReduction");
this->add_matcher(arithmetic_reduction_matcher,
constant_arithmetic_reduction_callback,
all_pass_property_off);
} }
template <typename T> template <typename T>
......
...@@ -42,8 +42,7 @@ public: ...@@ -42,8 +42,7 @@ public:
CONVERT, CONVERT,
SHAPE_OF, SHAPE_OF,
REVERSE, REVERSE,
PRODUCT, ARITHMETIC_REDUCTION,
SUM,
CONCAT, CONCAT,
GATHER, GATHER,
SLICE, SLICE,
...@@ -66,8 +65,7 @@ public: ...@@ -66,8 +65,7 @@ public:
construct_constant_convert(); construct_constant_convert();
construct_constant_shape_of(); construct_constant_shape_of();
construct_constant_reverse(); construct_constant_reverse();
construct_constant_product(); construct_constant_arithmetic_reduction();
construct_constant_sum();
construct_constant_concat(); construct_constant_concat();
construct_constant_gather(); construct_constant_gather();
construct_constant_slice(); construct_constant_slice();
...@@ -97,8 +95,9 @@ public: ...@@ -97,8 +95,9 @@ public:
case CFTransformations::CONVERT: construct_constant_convert(); break; case CFTransformations::CONVERT: construct_constant_convert(); break;
case CFTransformations::SHAPE_OF: construct_constant_shape_of(); break; case CFTransformations::SHAPE_OF: construct_constant_shape_of(); break;
case CFTransformations::REVERSE: construct_constant_reverse(); break; case CFTransformations::REVERSE: construct_constant_reverse(); break;
case CFTransformations::PRODUCT: construct_constant_product(); break; case CFTransformations::ARITHMETIC_REDUCTION:
case CFTransformations::SUM: construct_constant_sum(); break; construct_constant_arithmetic_reduction();
break;
case CFTransformations::CONCAT: construct_constant_concat(); break; case CFTransformations::CONCAT: construct_constant_concat(); break;
case CFTransformations::GATHER: construct_constant_gather(); break; case CFTransformations::GATHER: construct_constant_gather(); break;
case CFTransformations::SLICE: construct_constant_slice(); break; case CFTransformations::SLICE: construct_constant_slice(); break;
...@@ -120,8 +119,7 @@ private: ...@@ -120,8 +119,7 @@ private:
void construct_constant_convert(); void construct_constant_convert();
void construct_constant_shape_of(); void construct_constant_shape_of();
void construct_constant_reverse(); void construct_constant_reverse();
void construct_constant_product(); void construct_constant_arithmetic_reduction();
void construct_constant_sum();
void construct_constant_concat(); void construct_constant_concat();
void construct_constant_gather(); void construct_constant_gather();
void construct_constant_slice(); void construct_constant_slice();
......
...@@ -36,7 +36,7 @@ namespace ngraph ...@@ -36,7 +36,7 @@ namespace ngraph
const AxisSet& reduction_axes) const AxisSet& reduction_axes)
{ {
T minval = std::numeric_limits<T>::has_infinity T minval = std::numeric_limits<T>::has_infinity
? -std::numeric_limits<T>::infinity() ? T(-std::numeric_limits<T>::infinity())
: std::numeric_limits<T>::min(); : std::numeric_limits<T>::min();
CoordinateTransform output_transform(out_shape); CoordinateTransform output_transform(out_shape);
......
...@@ -434,6 +434,58 @@ TEST(constant_folding, const_sum) ...@@ -434,6 +434,58 @@ TEST(constant_folding, const_sum)
ASSERT_EQ(values_expected, values_out); ASSERT_EQ(values_expected, values_out);
} }
TEST(constant_folding, const_max)
{
Shape input_shape{3, 3};
vector<int32_t> values_in{1, 2, 3, 4, 5, 6, 7, 8, 9};
auto constant = op::Constant::create(element::i32, input_shape, values_in);
auto convert = make_shared<op::Max>(constant, AxisSet{1});
auto f = make_shared<Function>(convert, ParameterVector{});
pass::Manager pass_manager;
pass_manager.register_pass<pass::ConstantFolding>();
pass_manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<op::Max>(f), 0);
ASSERT_EQ(count_ops_of_type<op::Constant>(f), 1);
auto new_const =
std::dynamic_pointer_cast<op::Constant>(f->get_results().at(0)->get_argument(0));
ASSERT_TRUE(new_const);
auto values_out = new_const->get_vector<int32_t>();
vector<int32_t> values_expected{3, 6, 9};
ASSERT_EQ(values_expected, values_out);
}
TEST(constant_folding, const_min)
{
Shape input_shape{3, 3};
vector<int32_t> values_in{1, 2, 3, 4, 5, 6, 7, 8, 9};
auto constant = op::Constant::create(element::i32, input_shape, values_in);
auto convert = make_shared<op::Min>(constant, AxisSet{1});
auto f = make_shared<Function>(convert, ParameterVector{});
pass::Manager pass_manager;
pass_manager.register_pass<pass::ConstantFolding>();
pass_manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<op::Min>(f), 0);
ASSERT_EQ(count_ops_of_type<op::Constant>(f), 1);
auto new_const =
std::dynamic_pointer_cast<op::Constant>(f->get_results().at(0)->get_argument(0));
ASSERT_TRUE(new_const);
auto values_out = new_const->get_vector<int32_t>();
vector<int32_t> values_expected{1, 4, 7};
ASSERT_EQ(values_expected, values_out);
}
TEST(constant_folding, const_concat) TEST(constant_folding, const_concat)
{ {
auto constant0 = auto constant0 =
......
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