Commit c23a972d authored by Nick Korovaiko's avatar Nick Korovaiko Committed by Robert Kimball

move weights conversion out of group convolution emitter to convert_layout (#1616)

parent 0866980a
......@@ -18,6 +18,7 @@
#include "ngraph/runtime/cpu/cpu_builder.hpp"
#include "ngraph/runtime/cpu/mkldnn_invoke.hpp"
#include "ngraph/runtime/cpu/mkldnn_utils.hpp"
#include "ngraph/runtime/cpu/op/group_conv.hpp"
using namespace std;
using namespace ngraph;
......@@ -41,6 +42,28 @@ namespace ngraph
auto input_desc = mkldnn_utils::get_input_mkldnn_md(node, 0);
auto result_desc = mkldnn_utils::get_output_mkldnn_md(node, 0);
if (input_desc.data.format == mkldnn_nchw &&
result_desc.data.format == mkldnn_goihw)
{
//becomes a copy
input_desc = result_desc;
}
else if (input_desc.data.format == mkldnn_nchw && input_desc.data.ndims == 4 &&
result_desc.data.ndims == 5 && node->get_users().size() == 1)
{
auto gconv = std::dynamic_pointer_cast<ngraph::op::GroupConvolution>(
*(begin(node->get_users())));
if (gconv)
{
Shape weights_shape_groups = gconv->get_weights_dimensions();
input_desc = mkldnn::memory::desc(
mkldnn::memory::dims(weights_shape_groups.begin(),
weights_shape_groups.end()),
mkldnn_utils::get_mkldnn_data_type(args[0].get_element_type()),
mkldnn::memory::format::goihw);
}
}
size_t reorder_index = mkldnn_emitter->build_reorder(input_desc, result_desc);
auto& deps = mkldnn_emitter->get_primitive_deps(reorder_index);
......
......@@ -453,81 +453,31 @@ namespace ngraph
}
auto& mkldnn_emitter = external_function->get_mkldnn_emitter();
auto input_data_desc = mkldnn_utils::get_input_mkldnn_md(node, 0);
Shape weights_shape_groups = convolution->get_weights_dimensions();
auto weights_desc_any = mkldnn::memory::desc(
mkldnn::memory::dims(weights_shape_groups.begin(),
weights_shape_groups.end()),
mkldnn_utils::get_mkldnn_data_type(args[1].get_element_type()),
mkldnn::memory::format::any);
auto input_data_desc = mkldnn_utils::get_input_mkldnn_md(node, 0);
auto weights_desc = mkldnn_utils::get_input_mkldnn_md(node, 1);
auto result_desc = mkldnn_utils::get_output_mkldnn_md(node, 0);
auto padding_below = convolution->get_padding_below();
auto padding_above = convolution->get_padding_above();
auto filter_strides = convolution->get_window_movement_strides();
auto result_desc = mkldnn_utils::get_output_mkldnn_md(node, 0);
auto weights_optimized_format =
mkldnn_emitter->query_convolution_forward_weight_format(
input_data_desc,
weights_desc_any,
result_desc,
filter_strides,
window_dilation_strides_adjusted,
padding_below,
padding_above);
// create workspace for holding the result of converting weights layouts
auto ws = std::unique_ptr<MKLDNNWorkspace>(new MKLDNNWorkspace(
shape_size(args[1].get_shape()) * args[1].get_element_type().size()));
auto ws_buf_index = mkldnn_emitter->insert_workspace(ws);
// descriptors for reorder operation
auto input_reorder_desc =
mkldnn_emitter->build_memory_descriptor(weights_shape_groups,
args[1].get_element_type(),
mkldnn::memory::format::goihw);
auto result_reorder_desc = mkldnn_emitter->build_memory_descriptor(
weights_shape_groups, args[1].get_element_type(), weights_optimized_format);
auto weights_desc = mkldnn::memory::desc(
mkldnn::memory::dims(weights_shape_groups.begin(),
weights_shape_groups.end()),
mkldnn_utils::get_mkldnn_data_type(args[1].get_element_type()),
weights_optimized_format);
auto prim_indices = mkldnn_emitter->build_group_convolution_forward(
input_reorder_desc, // weights
input_data_desc,
size_t conv_index =
mkldnn_emitter->build_convolution_forward(input_data_desc,
weights_desc,
result_reorder_desc,
result_desc,
convolution->get_window_movement_strides(),
filter_strides,
window_dilation_strides_adjusted,
padding_below,
padding_above);
size_t reorder_index = prim_indices.first;
auto& reorder_deps = mkldnn_emitter->get_primitive_deps(reorder_index);
size_t conv_index = prim_indices.second;
auto& deps = mkldnn_emitter->get_primitive_deps(conv_index);
auto functor =
[&, conv_index, reorder_index, ws_buf_index](CPURuntimeContext* ctx) {
// reorder
cpu::mkldnn_utils::set_memory_ptr(ctx, reorder_deps[0], arg1_tensor);
cpu::mkldnn_utils::set_memory_ptr(
ctx, reorder_deps[1], ctx->mkldnn_workspaces[ws_buf_index]);
cpu::mkldnn_utils::mkldnn_invoke_primitive(ctx, reorder_index);
auto functor = [&, conv_index](CPURuntimeContext* ctx) {
// group convolution
cpu::mkldnn_utils::set_memory_ptr(ctx, deps[0], arg0_tensor);
cpu::mkldnn_utils::set_memory_ptr(
ctx, deps[1], ctx->mkldnn_workspaces[ws_buf_index]);
cpu::mkldnn_utils::set_memory_ptr(ctx, deps[1], arg1_tensor);
cpu::mkldnn_utils::set_memory_ptr(ctx, deps[2], out_tensor);
cpu::mkldnn_utils::mkldnn_invoke_primitive(ctx, conv_index);
};
......
......@@ -2706,94 +2706,36 @@ namespace ngraph
}
auto& mkldnn_emitter = external_function->get_mkldnn_emitter();
auto input_data_desc = mkldnn_utils::get_input_mkldnn_md(node, 0);
Shape weights_shape_groups = convolution->get_weights_dimensions();
auto weights_desc_any = mkldnn::memory::desc(
mkldnn::memory::dims(weights_shape_groups.begin(),
weights_shape_groups.end()),
mkldnn_utils::get_mkldnn_data_type(args[1].get_element_type()),
mkldnn::memory::format::any);
auto input_data_desc = mkldnn_utils::get_input_mkldnn_md(node, 0);
auto weights_desc = mkldnn_utils::get_input_mkldnn_md(node, 1);
auto result_desc = mkldnn_utils::get_output_mkldnn_md(node, 0);
auto padding_below = convolution->get_padding_below();
auto padding_above = convolution->get_padding_above();
auto filter_strides = convolution->get_window_movement_strides();
auto result_desc = mkldnn_utils::get_output_mkldnn_md(node, 0);
auto weights_optimized_format =
mkldnn_emitter->query_convolution_forward_weight_format(
input_data_desc,
weights_desc_any,
result_desc,
filter_strides,
window_dilation_strides_adjusted,
padding_below,
padding_above);
// create workspace for holding the result of converting weights layouts
auto ws = std::unique_ptr<MKLDNNWorkspace>(new MKLDNNWorkspace(
shape_size(args[1].get_shape()) * args[1].get_element_type().size()));
auto ws_buf_index = mkldnn_emitter->insert_workspace(ws);
// descriptors for reorder operation
auto input_reorder_desc =
mkldnn_emitter->build_memory_descriptor(weights_shape_groups,
args[1].get_element_type(),
mkldnn::memory::format::goihw);
auto result_reorder_desc = mkldnn_emitter->build_memory_descriptor(
weights_shape_groups, args[1].get_element_type(), weights_optimized_format);
auto weights_desc = mkldnn::memory::desc(
mkldnn::memory::dims(weights_shape_groups.begin(),
weights_shape_groups.end()),
mkldnn_utils::get_mkldnn_data_type(args[1].get_element_type()),
weights_optimized_format);
auto prim_indices = mkldnn_emitter->build_group_convolution_forward(
input_reorder_desc, // weights
input_data_desc,
size_t conv_index =
mkldnn_emitter->build_convolution_forward(input_data_desc,
weights_desc,
result_reorder_desc,
result_desc,
convolution->get_window_movement_strides(),
filter_strides,
window_dilation_strides_adjusted,
padding_below,
padding_above);
// invoke reorder primitive
{
size_t reorder_index = prim_indices.first;
auto& deps = mkldnn_emitter->get_primitive_deps(reorder_index);
writer << "cpu::mkldnn_utils::set_memory_ptr(ctx, " << to_string(deps[0])
<< ", " << args[1].get_name() << ");\n";
writer << "cpu::mkldnn_utils::set_memory_ptr(ctx, " << to_string(deps[1])
<< ", "
<< "ctx->mkldnn_workspaces[" << ws_buf_index << "]);\n";
writer << "cpu::mkldnn_utils::mkldnn_invoke_primitive(ctx, "
<< to_string(reorder_index) << ");\n";
}
// invoke group convolution
{
size_t conv_index = prim_indices.second;
auto& deps = mkldnn_emitter->get_primitive_deps(conv_index);
writer << "cpu::mkldnn_utils::set_memory_ptr(ctx, " << to_string(deps[0])
<< ", " << args[0].get_name() << ");\n";
writer << "cpu::mkldnn_utils::set_memory_ptr(ctx, " << to_string(deps[1])
<< ", "
<< "ctx->mkldnn_workspaces[" << ws_buf_index << "]);\n";
<< ", " << args[1].get_name() << ");\n";
writer << "cpu::mkldnn_utils::set_memory_ptr(ctx, " << to_string(deps[2])
<< ", " << out[0].get_name() << ");\n";
writer << "cpu::mkldnn_utils::mkldnn_invoke_primitive(ctx, "
<< to_string(conv_index) << ");\n";
}
}
else
{
throw ngraph_error("unsupported parameters for GroupConvolution");
......@@ -3920,6 +3862,30 @@ namespace ngraph
auto input_desc = mkldnn_utils::get_input_mkldnn_md(node, 0);
auto result_desc = mkldnn_utils::get_output_mkldnn_md(node, 0);
//this is a special case to handle nchw(oihw) to goihw/Goihw16g/Goihw8g for GroupConvolution's weights
if (input_desc.data.format == mkldnn_nchw &&
result_desc.data.format == mkldnn_goihw)
{
input_desc = result_desc;
}
else if (input_desc.data.format == mkldnn_nchw &&
input_desc.data.ndims == 4 /*nchw*/ &&
result_desc.data.ndims == 5 /*Goihw16g/Goihw8g/etc*/ &&
node->get_users().size() == 1)
{
auto gconv = std::dynamic_pointer_cast<ngraph::op::GroupConvolution>(
*(begin(node->get_users())));
if (gconv)
{
Shape weights_shape_groups = gconv->get_weights_dimensions();
input_desc = mkldnn::memory::desc(
mkldnn::memory::dims(weights_shape_groups.begin(),
weights_shape_groups.end()),
mkldnn_utils::get_mkldnn_data_type(args[0].get_element_type()),
mkldnn::memory::format::goihw);
}
}
size_t reorder_index = mkldnn_emitter->build_reorder(input_desc, result_desc);
auto& deps = mkldnn_emitter->get_primitive_deps(reorder_index);
......
......@@ -227,29 +227,6 @@ mkldnn::memory::format MKLDNNEmitter::query_convolution_forward_weight_format(
prim_desc.weights_primitive_desc().desc().data.format);
}
std::pair<size_t, size_t> MKLDNNEmitter::build_group_convolution_forward(
const mkldnn::memory::desc& input_reorder_desc,
const mkldnn::memory::desc& input_conv_desc,
const mkldnn::memory::desc& weights_desc,
const mkldnn::memory::desc& result_reorder_desc,
const mkldnn::memory::desc& result_desc,
const ngraph::Strides& filter_strides,
const ngraph::Strides& window_dilation_strides_adjusted,
const ngraph::CoordinateDiff& padding_below,
const ngraph::CoordinateDiff& padding_above)
{
size_t reorder_index = this->build_reorder(input_reorder_desc, result_reorder_desc);
size_t conv_index = this->build_convolution_forward(input_conv_desc,
weights_desc,
result_desc,
filter_strides,
window_dilation_strides_adjusted,
padding_below,
padding_above);
return std::make_pair(reorder_index, conv_index);
}
size_t MKLDNNEmitter::build_convolution_forward(const mkldnn::memory::desc& input_data_desc,
const mkldnn::memory::desc& weights_desc,
const mkldnn::memory::desc& result_desc,
......
......@@ -231,17 +231,6 @@ namespace ngraph
const ngraph::CoordinateDiff& padding_below,
const ngraph::CoordinateDiff& padding_above);
std::pair<size_t, size_t> build_group_convolution_forward(
const mkldnn::memory::desc& input_reorder_desc,
const mkldnn::memory::desc& input_conv_desc,
const mkldnn::memory::desc& weights_desc,
const mkldnn::memory::desc& result_reorder_desc,
const mkldnn::memory::desc& result_desc,
const ngraph::Strides& filter_strides,
const ngraph::Strides& window_dilation_strides_adjusted,
const ngraph::CoordinateDiff& padding_below,
const ngraph::CoordinateDiff& padding_above);
size_t
build_convolution_backward_weights(const mkldnn::memory::desc& input_desc,
const mkldnn::memory::desc& delta_desc,
......
......@@ -374,17 +374,7 @@ namespace ngraph
}
convolution_forward::primitive_desc prim_desc(*fwd_desc, cpu_engine);
i_mds.push_back(prim_desc.src_primitive_desc().desc());
if (default_weights_format)
{
// note, we need the original shape (4D) while arg_shape1 is redefined
i_mds.push_back(mkldnn_utils::create_default_mkldnn_md(
node.get(), 1, false, memory::format::oihw));
}
else
{
i_mds.push_back(prim_desc.weights_primitive_desc().desc());
}
if (use_bias)
{
......
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