Commit 8b7f042d authored by Louis Feng's avatar Louis Feng

refactor and clean up.

parent dd5bd9ad
......@@ -2386,37 +2386,27 @@ namespace ngraph
template <>
void CPU_Emitter::EMITTER_DECL(ngraph::op::ConvolutionBias)
{
auto convolution = static_cast<const ngraph::op::Convolution*>(node);
auto convolution = static_cast<const ngraph::op::ConvolutionBias*>(node);
const TensorViewWrapper& data = args[0];
const TensorViewWrapper& weights = args[1];
const TensorViewWrapper& bias = args[2];
const TensorViewWrapper& result = out[0];
const vector<size_t>& data_shape = data.get_shape();
const vector<size_t>& weights_shape = weights.get_shape();
const vector<size_t>& bias_shape = bias.get_shape();
const vector<size_t>& result_shape = result.get_shape();
if (runtime::cpu::mkldnn_utils::use_mkldnn_kernel(node))
using namespace runtime::cpu::mkldnn_utils;
if (mkldnn_utils::use_mkldnn_kernel(node))
{
auto data_format =
runtime::cpu::mkldnn_utils::get_input_mkldnn_format(node, 0);
auto weights_format =
runtime::cpu::mkldnn_utils::get_input_mkldnn_format(node, 1);
auto bias_format =
runtime::cpu::mkldnn_utils::get_input_mkldnn_format(node, 2);
auto result_format =
runtime::cpu::mkldnn_utils::get_output_mkldnn_format(node, 0);
auto data_format = mkldnn_utils::get_input_mkldnn_format(node, 0);
auto weights_format = mkldnn_utils::get_input_mkldnn_format(node, 1);
auto bias_format = mkldnn_utils::get_input_mkldnn_format(node, 2);
auto result_format = mkldnn_utils::get_output_mkldnn_format(node, 0);
auto& mkldnn_emitter = external_function->get_mkldnn_emitter();
auto data_desc = mkldnn_emitter->build_memory_descriptor(
data, data_format);
auto weights_desc = mkldnn_emitter->build_memory_descriptor(
weights, weights_format);
auto bias_desc = mkldnn_emitter->build_memory_descriptor(
bias, bias_format);
auto result_desc = mkldnn_emitter->build_memory_descriptor(
result, result_format);
auto data_desc = mkldnn_emitter->build_memory_descriptor(data, data_format);
auto weights_desc = mkldnn_emitter->build_memory_descriptor(weights, weights_format);
auto bias_desc = mkldnn_emitter->build_memory_descriptor(bias, bias_format);
auto result_desc = mkldnn_emitter->build_memory_descriptor(result, result_format);
// For dilation, MKLDNN wants to know how many elements to insert between, not how far
// apart to space the elements like nGraph. So we have to subtract 1 from each pos.
......@@ -2463,33 +2453,28 @@ namespace ngraph
const TensorViewWrapper& delta = args[1];
const TensorViewWrapper& weights_delta = out[0];
const TensorViewWrapper& bias_delta = out[1];
const vector<size_t>& data_shape = data.get_shape();
const vector<size_t>& delta_shape = delta.get_shape();
const vector<size_t>& weights_delta_shape = weights_delta.get_shape();
const vector<size_t>& bias_delta_shape = bias_delta.get_shape();
if (runtime::cpu::mkldnn_utils::use_mkldnn_kernel(node))
using namespace runtime::cpu::mkldnn_utils;
if (mkldnn_utils::use_mkldnn_kernel(node))
{
const string& elem_type =
runtime::cpu::mkldnn_utils::get_mkldnn_data_type_string(
data.get_element_type());
Strides window_dilation_strides_adjusted;
for (size_t s : convolution->get_window_dilation_strides_forward())
{
window_dilation_strides_adjusted.push_back(s - 1);
}
auto data_format = runtime::cpu::mkldnn_utils::get_input_mkldnn_format(node, 0);
auto delta_format = runtime::cpu::mkldnn_utils::get_input_mkldnn_format(node, 1);
auto weights_delta_format = runtime::cpu::mkldnn_utils::get_output_mkldnn_format(node, 0);
auto bias_delta_format = runtime::cpu::mkldnn_utils::get_output_mkldnn_format(node, 1);
auto data_format = mkldnn_utils::get_input_mkldnn_format(node, 0);
auto delta_format = mkldnn_utils::get_input_mkldnn_format(node, 1);
auto weights_delta_format = mkldnn_utils::get_output_mkldnn_format(node, 0);
auto bias_delta_format = mkldnn_utils::get_output_mkldnn_format(node, 1);
auto& mkldnn_emitter = external_function->get_mkldnn_emitter();
auto data_desc = mkldnn_emitter->build_memory_descriptor(data, data_format);
auto delta_desc = mkldnn_emitter->build_memory_descriptor(delta, delta_format);
auto weights_delta_desc = mkldnn_emitter->build_memory_descriptor(weights_delta, weights_delta_format);
auto bias_delta_desc = mkldnn_emitter->build_memory_descriptor(bias_delta, bias_delta_format);
size_t conv_index = mkldnn_emitter->build_convolution_backward_filters_bias(
data_desc,
delta_desc,
......
......@@ -225,11 +225,9 @@ static const runtime::cpu::OpMap dispatcher{
{TI(ngraph::op::ConvolutionBackpropData),
&runtime::cpu::CPU_Emitter::emit<op::ConvolutionBackpropData>},
{TI(ngraph::op::ConvolutionBias), &runtime::cpu::CPU_Emitter::emit<op::ConvolutionBias>},
// conv+bias backprop for data share the same implementation as ConvolutionBackpropData
{TI(ngraph::op::ConvolutionBiasBackpropFiltersBias),
&runtime::cpu::CPU_Emitter::emit<op::ConvolutionBiasBackpropFiltersBias>},
// conv+bias backprop for data share the same implementation as conv backprop data
{TI(ngraph::op::ConvolutionBiasBackpropData),
&runtime::cpu::CPU_Emitter::emit<op::ConvolutionBackpropData>},
{TI(ngraph::runtime::cpu::op::ConvertLayout),
&runtime::cpu::CPU_Emitter::emit<runtime::cpu::op::ConvertLayout>},
{TI(ngraph::op::Not), &runtime::cpu::CPU_Emitter::emit<op::Not>},
......
......@@ -16,6 +16,7 @@
#include <numeric>
#include "ngraph/ops/convolution.hpp"
#include "ngraph/runtime/cpu/ops/conv_bias.hpp"
#include "ngraph/ops/get_output_element.hpp"
#include "ngraph/util.hpp"
......@@ -88,7 +89,7 @@ void op::ConvolutionBias::generate_adjoints(autodiff::Adjoints& adjoints,
const auto bias_shape = bias->get_shape();
adjoints.add_delta(data,
std::make_shared<op::ConvolutionBiasBackpropData>(data_shape,
std::make_shared<op::ConvolutionBackpropData>(data_shape,
filter,
delta,
m_window_movement_strides,
......@@ -113,107 +114,6 @@ void op::ConvolutionBias::generate_adjoints(autodiff::Adjoints& adjoints,
adjoints.add_delta(bias, bias_delta);
}
op::ConvolutionBiasBackpropData::ConvolutionBiasBackpropData(const Shape& data_batch_shape,
const std::shared_ptr<Node>& filters,
const std::shared_ptr<Node>& output_delta,
const Strides& window_movement_strides_forward,
const Strides& window_dilation_strides_forward,
const CoordinateDiff& padding_below_forward,
const CoordinateDiff& padding_above_forward,
const Strides& data_dilation_strides_forward)
: RequiresTensorViewArgs("ConvolutionBackpropData", {filters, output_delta})
, m_data_batch_shape(data_batch_shape)
, m_window_movement_strides_forward(window_movement_strides_forward)
, m_window_dilation_strides_forward(window_dilation_strides_forward)
, m_padding_below_forward(padding_below_forward)
, m_padding_above_forward(padding_above_forward)
, m_data_dilation_strides_forward(data_dilation_strides_forward)
{
auto& filters_shape = get_input_shape(0);
auto& filters_et = get_input_element_type(0);
auto& output_delta_shape = get_input_shape(0);
auto& output_delta_et = get_input_element_type(1);
//
// Make sure filter and output delta element types match.
//
if (filters_et != output_delta_et)
{
throw ngraph_error(
"Convolution data batch backprop filter and output delta element types do not match");
}
// Forward Backward
// Window movement strides q p_x
// Window dilation strides p_f p_f
// Padding below a_x (S_F - 1)p_f - a_x
// Padding above b_x (S_f - 1)p_f + ((a_x + (S_x - 1)p_x + b_x - (S_f - 1)p_f) % q) - b_x
// Data dilation strides p_x q
for (size_t i = 0; i < data_batch_shape.size() - 2; i++)
{
m_window_movement_strides_backward.push_back(data_dilation_strides_forward[i]);
m_window_dilation_strides_backward.push_back(window_dilation_strides_forward[i]);
m_padding_below_backward.push_back((filters_shape[i + 2] - 1) *
window_dilation_strides_forward[i] -
padding_below_forward[i]);
m_padding_above_backward.push_back(
(filters_shape[i + 2] - 1) * window_dilation_strides_forward[i] +
((padding_below_forward[i] +
(data_batch_shape[i + 2] - 1) * data_dilation_strides_forward[i] +
padding_above_forward[i] -
(filters_shape[i + 2] - 1) * window_dilation_strides_forward[i]) %
window_movement_strides_forward[i]) -
padding_above_forward[i]);
m_data_dilation_strides_backward.push_back(window_movement_strides_forward[i]);
}
// Shape inferred_convolution_output_shape =
// infer_convolution_output_shape(output_delta_shape,
// filters_shape,
// m_window_movement_strides_backward,
// m_window_dilation_strides_backward,
// m_padding_below_backward,
// m_padding_above_backward,
// m_data_dilation_strides_backward,
// 0,
// 1,
// 0,
// 1,
// 0,
// 1,
// "In ConvolutionBiasBackpropData: ");
//
// // Not sure if this can ever actually happen (i.e., I think it will trip on something else
// // inside infer_convolution_output_shape before we get here) but it seems worth checking.
// if (inferred_convolution_output_shape != data_batch_shape)
// {
// throw ngraph_error(
// "Convolution data batch backprop inferred output shape does not match "
// "specified data batch shape");
// }
set_value_type_checked(filters_et, data_batch_shape);
}
std::shared_ptr<Node> op::ConvolutionBiasBackpropData::copy_with_new_args(
const NodeVector& new_args) const
{
if (new_args.size() != 2)
{
throw ngraph_error("Incorrect number of new arguments");
}
return std::make_shared<ConvolutionBiasBackpropData>(m_data_batch_shape,
new_args.at(0),
new_args.at(1),
m_window_movement_strides_forward,
m_window_dilation_strides_forward,
m_padding_below_forward,
m_padding_above_forward,
m_data_dilation_strides_forward);
}
op::ConvolutionBiasBackpropFiltersBias::ConvolutionBiasBackpropFiltersBias(
const std::shared_ptr<Node>& data_batch,
const Shape& filters_shape,
......
......@@ -60,100 +60,6 @@ namespace ngraph
const Strides& data_dilation_strides);
};
/// \brief Data batch backprop for batched convolution + bias operation.
class ConvolutionBiasBackpropData : public util::RequiresTensorViewArgs
{
public:
/// \brief Constructs a batched-convolution data batch-backprop operation.
///
/// \param data_batch_shape The shape of the data batch from forward-prop.
/// \param filters The node producing the filters from forward-prop.
/// \param output_delta The node producing output delta.
/// \param window_movement_strides_forward The window movement strides from forward-prop.
/// \param window_dilation_strides_forward The window dilation strides from forward-prop.
/// \param padding_below_forward The padding-below sizes from forward-prop.
/// \param padding_above_forward The padding-above sizes from forward-prop.
/// \param data_dilation_strides_forward The data dilation strides from forward-prop.
ConvolutionBiasBackpropData(const Shape& data_batch_shape,
const std::shared_ptr<Node>& filters,
const std::shared_ptr<Node>& output_delta,
const Strides& window_movement_strides_forward,
const Strides& window_dilation_strides_forward,
const CoordinateDiff& padding_below_forward,
const CoordinateDiff& padding_above_forward,
const Strides& data_dilation_strides_forward);
virtual std::shared_ptr<Node> copy_with_new_args(const NodeVector& new_args) const override;
/// \return The data batch shape.
const Shape& get_data_batch_shape() const { return m_data_batch_shape; }
/// \return The window movement strides from the forward prop.
const Strides& get_window_movement_strides_forward() const
{
return m_window_movement_strides_forward;
}
/// \return The window dilation strides from the forward prop.
const Strides& get_window_dilation_strides_forward() const
{
return m_window_dilation_strides_forward;
}
/// \return The padding-below sizes (possibly negative) from the forward prop.
const CoordinateDiff& get_padding_below_forward() const
{
return m_padding_below_forward;
}
/// \return The padding-above sizes (possibly negative) from the forward prop.
const CoordinateDiff& get_padding_above_forward() const
{
return m_padding_above_forward;
}
/// \return The input data dilation strides from the forward prop.
const Strides& get_data_dilation_strides_forward() const
{
return m_data_dilation_strides_forward;
}
/// \return The window movement strides for the backward prop.
const Strides& get_window_movement_strides_backward() const
{
return m_window_movement_strides_backward;
}
/// \return The window dilation strides for the backward prop.
const Strides& get_window_dilation_strides_backward() const
{
return m_window_dilation_strides_backward;
}
/// \return The padding-below sizes (possibly negative) for the backward prop.
const CoordinateDiff& get_padding_below_backward() const
{
return m_padding_below_backward;
}
/// \return The padding-above sizes (possibly negative) for the backward prop.
const CoordinateDiff& get_padding_above_backward() const
{
return m_padding_above_backward;
}
/// \return The input data dilation strides for the backward prop.
const Strides& get_data_dilation_strides_backward() const
{
return m_data_dilation_strides_backward;
}
protected:
Shape m_data_batch_shape;
Strides m_window_movement_strides_forward;
Strides m_window_dilation_strides_forward;
CoordinateDiff m_padding_below_forward;
CoordinateDiff m_padding_above_forward;
Strides m_data_dilation_strides_forward;
Strides m_window_movement_strides_backward;
Strides m_window_dilation_strides_backward;
CoordinateDiff m_padding_below_backward;
CoordinateDiff m_padding_above_backward;
Strides m_data_dilation_strides_backward;
};
/// \brief Filters and bias backprop for batched convolution operation.
class ConvolutionBiasBackpropFiltersBias : public util::RequiresTensorViewArgs
{
......
......@@ -185,7 +185,7 @@ namespace ngraph
auto convolution = static_cast<op::ConvolutionBiasBackpropFiltersBias*>(node);
auto data_shape = node->get_input_shape(0);
auto delta_shape = node->get_input_shape(0);
auto delta_shape = node->get_input_shape(1);
auto data_rank = data_shape.size();
auto delta_rank = delta_shape.size();
......@@ -195,10 +195,8 @@ namespace ngraph
data_dilated = data_dilated || (s != 1);
}
std::cout << "testing ConvolutionBiasBackpropFiltersBias" << std::endl;
if (!data_dilated && data_rank == 4 && node->get_input_element_type(0) == element::f32)
if (!data_dilated && data_rank == 4 && delta_rank == 4 && node->get_input_element_type(0) == element::f32)
{
std::cout << "assigned ConvolutionBiasBackpropFiltersBias" << std::endl;
auto op_annotations =
std::make_shared<ngraph::runtime::cpu::CPUOpAnnotations>();
op_annotations->set_mkldnn_op(true);
......
......@@ -52,5 +52,5 @@ private:
void construct_fprop_bn();
void construct_zero_padded_reshaped_conv();
void construct_zero_padded_conv();
construct_conv_bias();
void construct_conv_bias();
};
This diff is collapsed.
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