Commit 28002287 authored by Pruthvi's avatar Pruthvi Committed by Scott Cyphers

cse for convert layout (#1983)

* cse for convert layout

* addressed PR comments

* Addressed PR comments
parent 296ee2cf
......@@ -29,7 +29,26 @@ using namespace std;
static bool cse_convertlayout(std::shared_ptr<Node> a, std::shared_ptr<Node> b)
{
return false;
NGRAPH_DEBUG << "In cse_convertlayout for " << a->get_name() << " and " << b->get_name();
auto ar_a = std::static_pointer_cast<runtime::cpu::op::ConvertLayout>(a);
auto ar_b = std::static_pointer_cast<runtime::cpu::op::ConvertLayout>(b);
// gets the tensor layout from the given node
auto get_tensor_layout = [](std::shared_ptr<Node> node) {
auto tensor = node->get_output_tensor_ptr();
auto cpu_tl =
static_cast<ngraph::runtime::cpu::LayoutDescriptor*>(tensor->get_tensor_layout().get());
return cpu_tl;
};
auto a_layout_desc = get_tensor_layout(a);
auto b_layout_desc = get_tensor_layout(b);
bool is_args_same = (ar_a->get_argument(0) == ar_b->get_argument(0));
bool is_output_mem_desc_same = runtime::cpu::mkldnn_utils::compare_mkldnn_mds(
a_layout_desc->get_mkldnn_md(), b_layout_desc->get_mkldnn_md());
return is_args_same && is_output_mem_desc_same;
}
namespace ngraph
......
......@@ -542,3 +542,39 @@ TEST(cpu_test, collapse_dims2)
EXPECT_TRUE(test::all_close(cpu_results.at(i), int_results.at(i)));
}
}
TEST(cpu_test, convert_layout)
{
auto make_function = []() -> std::shared_ptr<Function> {
auto W = std::make_shared<op::Parameter>(element::f32, Shape{10, 400});
auto X = std::make_shared<op::Parameter>(element::f32, Shape{400, 10});
auto W_reshape = std::make_shared<op::Reshape>(W, AxisVector{1, 0}, Shape{400, 10});
auto add1 = std::make_shared<op::Add>(X, W_reshape);
auto sub1 = std::make_shared<op::Subtract>(X, W_reshape);
auto mul1 = std::make_shared<op::Multiply>(X, W_reshape);
return make_shared<Function>(NodeVector{add1, sub1, mul1}, op::ParameterVector{W, X});
};
auto backend = runtime::Backend::create("CPU");
auto cpu_f = make_function();
auto int_f = make_function();
test::Uniform<float> rng(-100.0f, 100.0f);
vector<vector<float>> args;
for (shared_ptr<op::Parameter> param : cpu_f->get_parameters())
{
vector<float> tensor_val(shape_size(param->get_shape()));
rng.initialize(tensor_val);
args.push_back(tensor_val);
}
auto int_results = execute(int_f, args, "INTERPRETER");
auto cpu_results = execute(cpu_f, args, "CPU");
size_t count = count_ops_of_type<runtime::cpu::op::ConvertLayout>(cpu_f);
ASSERT_EQ(count, 1);
for (size_t i = 0; i < cpu_results.size(); i++)
{
EXPECT_TRUE(test::all_close(cpu_results.at(i), int_results.at(i)));
}
}
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