Commit 268853d0 authored by Louis Feng's avatar Louis Feng Committed by Jayaram Bobba

reshape inplace without copy data if possible. (#1206)

parent 4659d60d
......@@ -67,6 +67,11 @@ op::Reshape::Reshape(const shared_ptr<Node>& arg,
"dimensions for reshape");
}
if (!std::is_sorted(m_input_order.begin(), m_input_order.end()))
{
m_is_transpose = true;
}
set_value_type_checked(input.get_element_type(), m_output_shape);
}
......
......@@ -78,12 +78,14 @@ namespace ngraph
const AxisVector& get_input_order() const { return m_input_order; }
/// \return The shape of the output tensor.
const Shape& get_output_shape() const { return m_output_shape; }
bool get_is_transpose() const { return m_is_transpose; }
protected:
virtual void generate_adjoints(autodiff::Adjoints& adjoints,
const NodeVector& deltas) override;
const AxisVector m_input_order;
const Shape m_output_shape;
bool m_is_transpose{false};
};
}
}
......@@ -1652,6 +1652,15 @@ namespace ngraph
void CPU_Emitter::EMITTER_DECL(ngraph::op::Reshape)
{
auto reshape = static_cast<const ngraph::op::Reshape*>(node);
auto annotation = reshape->get_op_annotations();
if (annotation && annotation->get_in_place_oi_pairs().size() > 0 &&
out[0].get_name() == args[0].get_name())
{
writer.block_begin();
writer << "// Stride change only, skipping.\n";
writer.block_end();
return;
}
writer.block_begin();
#if USE_EIGEN_CORE_INLINE == 1
auto arg_shape = args[0].get_shape();
......
......@@ -563,6 +563,30 @@ namespace ngraph
auto axis_order = reshape->get_input_order();
bool flag = true;
auto op_annotations =
std::make_shared<ngraph::runtime::cpu::CPUOpAnnotations>();
auto users = reshape->get_users();
auto arg = reshape->get_argument(0);
// we need to copy input data if reshape modifies the data or inputs are
// not in the memory pool, or has output users.
bool need_copy =
reshape->get_is_transpose() || arg->is_parameter() || arg->is_constant();
for (auto n = users.begin(); !need_copy && n != users.end(); ++n)
{
if ((*n)->is_output())
{
need_copy = true;
}
}
if (!need_copy)
{
// map output to the input memory
std::map<size_t, size_t> oi_pairs = {{0, 0}};
op_annotations->set_in_place_oi_pairs(oi_pairs);
reshape->set_op_annotations(op_annotations);
}
// Use Eigen for 3D
if (node->get_input_element_type(0) == element::f32 &&
arg0_shape.size() < TENSOR_MAX_DIMS && arg0_shape.size() > 3 &&
......@@ -579,8 +603,6 @@ namespace ngraph
if (flag)
{
auto op_annotations =
std::make_shared<ngraph::runtime::cpu::CPUOpAnnotations>();
op_annotations->set_mkldnn_op(true);
reshape->set_op_annotations(op_annotations);
}
......
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