convolution.hpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include "ngraph/coordinate_diff.hpp"
20 #include "ngraph/op/op.hpp"
21 #include "ngraph/op/util/attr_types.hpp"
22 
23 namespace ngraph
24 {
25  namespace op
26  {
27  namespace v1
28  {
29  /// \brief Batched convolution operation, with optional window dilation and stride.
30  ///
31  class NGRAPH_API Convolution : public Op
32  {
33  public:
34  NGRAPH_RTTI_DECLARATION;
35 
36  /// \brief Constructs a batched convolution operation.
37  Convolution() = default;
38  /// \brief Constructs a batched convolution operation.
39  ///
40  /// \param data_batch The node producing the input data batch tensor.<br>
41  /// `[N, C_IN, D1, ... Df]`
42  /// \param filters The node producing the filters tensor.<br>
43  /// `[C_OUT, C_IN, F1, ... Ff]`
44  /// \param strides The strides.<br>
45  /// `[f]`
46  /// \param dilations The dilations.<br>
47  /// `[f]`
48  /// \param pads_begin The beginning of padding shape.<br>
49  /// `[f]`
50  /// \param pads_end The end of padding shape.<br>
51  /// `[f]`
52  /// \param auto_pad The pad type for automatically computing padding sizes.<br>
53  /// `[f]`
54  ///
55  /// Output `[N, C_OUT, R1, ... Rf]`
56  ///
57  Convolution(const Output<Node>& data_batch,
58  const Output<Node>& filters,
59  const Strides& strides,
60  const CoordinateDiff& pads_begin,
61  const CoordinateDiff& pads_end,
62  const Strides& dilations,
63  const PadType& auto_pad = PadType::EXPLICIT);
64 
65  void validate_and_infer_types() override;
66  bool visit_attributes(AttributeVisitor& visitor) override;
67 
68  virtual std::shared_ptr<Node>
69  clone_with_new_inputs(const OutputVector& new_args) const override;
70 
71  /// \return The strides.
72  const Strides& get_strides() const { return m_strides; }
73  void set_strides(const Strides& strides) { m_strides = strides; }
74  /// \return The dilations.
75  const Strides& get_dilations() const { return m_dilations; }
76  void set_dilations(const Strides& dilations) { m_dilations = dilations; }
77  /// \return The padding-below sizes (possibly negative).
78  const CoordinateDiff& get_pads_begin() const { return m_pads_begin; }
79  void set_pads_begin(const CoordinateDiff& pads_begin) { m_pads_begin = pads_begin; }
80  /// \return The padding-above sizes (possibly negative).
81  const CoordinateDiff& get_pads_end() const { return m_pads_end; }
82  void set_adding_above(const CoordinateDiff& pads_end) { m_pads_end = pads_end; }
83  /// \return The pad type for convolution.
84  const PadType& get_auto_pad() const { return m_auto_pad; }
85  void set_auto_pad(const PadType& auto_pad) { m_auto_pad = auto_pad; }
86  /// \return The default value for Convolution.
87  virtual std::shared_ptr<Node> get_default_value() const override;
88 
89  protected:
90  Strides m_strides;
91  Strides m_dilations;
92  CoordinateDiff m_pads_begin;
93  CoordinateDiff m_pads_end;
94  PadType m_auto_pad;
95  };
96 
97  /// \brief Data batch backprop for batched convolution operation.
98  class NGRAPH_API ConvolutionBackpropData : public Op
99  {
100  public:
101  static constexpr NodeTypeInfo type_info{"ConvolutionBackpropData", 1};
102  const NodeTypeInfo& get_type_info() const override { return type_info; }
103  /// \brief Constructs a batched-convolution data batch-backprop operation.
105  // clang-format off
106  //
107  // \brief Constructs a batched-convolution data batch-backprop operation.
108  //
109  // \param data The node producing data from forward-prop. Shape: [N,
110  // C_INPUT, X1, ..., XD].
111  // \param filters The node producing the filter from forward-prop. Shape:
112  // [C_INPUT, C_OUTPUT, K_D, ..., K_1]
113  // \param output_shape The shape of the data batch from forward-prop. It's size
114  // should be equal to number of data spatial dimensions.
115  // \param strides The strides from forward-prop.
116  // \param pads_begin The padding-below sizes from forward-prop.
117  // \param pads_end The padding-above sizes from forward-prop.
118  // \param dilations The dilations from forward-prop.
119  // \param auto_pad The pad type for automatically computing padding sizes.
120  // \param output_padding The output padding adds additional amount of paddings per
121  // each spatial axis in the output tensor. clang-format on
122  //
123  ConvolutionBackpropData(const Output<Node>& data,
124  const Output<Node>& filters,
125  const Output<Node>& output_shape,
126  const Strides& strides,
127  const CoordinateDiff& pads_begin,
128  const CoordinateDiff& pads_end,
129  const Strides& dilations,
130  const PadType& auto_pad = PadType::EXPLICIT,
131  const CoordinateDiff& output_padding = {});
132 
133  // clang-format off
134  //
135  // \brief Constructs a batched-convolution data batch-backprop operation.
136  //
137  // \param data The node producing data from forward-prop. Shape: [N,
138  // C_INPUT, X1, ..., XD].
139  // \param filters The node producing the filter from forward-prop. Shape:
140  // [C_INPUT, C_OUTPUT, K_D, ..., K_1]
141  // \param strides The strides from forward-prop.
142  // \param pads_begin The padding-below sizes from forward-prop.
143  // \param pads_end The padding-above sizes from forward-prop.
144  // \param dilations The dilations from forward-prop.
145  // \param auto_pad The pad type for automatically computing padding sizes.
146  // \param output_padding The output padding adds additional amount of paddings per
147  // each spatial axis in the output tensor. clang-format on
148  //
149  ConvolutionBackpropData(const Output<Node>& data,
150  const Output<Node>& filters,
151  const Strides& strides,
152  const CoordinateDiff& pads_begin,
153  const CoordinateDiff& pads_end,
154  const Strides& dilations,
155  const PadType& auto_pad = PadType::EXPLICIT,
156  const CoordinateDiff& output_padding = {});
157 
158  void validate_and_infer_types() override;
159  bool visit_attributes(AttributeVisitor& visitor) override;
160  virtual bool is_dynamic() const override;
161 
162  virtual std::shared_ptr<Node>
163  clone_with_new_inputs(const OutputVector& new_args) const override;
164 
165  /// \return The output spatial dimensions shape.
166  const PartialShape get_output_shape() const;
167  void set_output_shape(const Shape& output_shape);
168  /// \return The strides from the forward prop.
169  const Strides& get_strides() const { return m_strides; }
170  void set_strides(const Strides& strides) { m_strides = strides; }
171  /// \return The dilations from the forward prop.
172  const Strides& get_dilations() const { return m_dilations; }
173  void set_dilations(const Strides& dilations) { m_dilations = dilations; }
174  /// \return The padding-below sizes (possibly negative) from the forward prop.
175  const CoordinateDiff& get_pads_begin() const { return m_pads_begin; }
176  void set_pads_begin(const CoordinateDiff& pads_begin) { m_pads_begin = pads_begin; }
177  /// \return The padding-above sizes (possibly negative) from the forward prop.
178  const CoordinateDiff& get_pads_end() const { return m_pads_end; }
179  void set_pads_end(const CoordinateDiff& pads_end) { m_pads_end = pads_end; }
180  /// \return The auto pad.
181  const PadType& get_auto_pad() const { return m_auto_pad; }
182  void set_auto_pad(const PadType& auto_pad) { m_auto_pad = auto_pad; }
183  /// \return The output padding.
184  const CoordinateDiff& get_output_padding() const { return m_output_padding; }
185  void set_output_padding(const CoordinateDiff& output_padding)
186  {
187  m_output_padding = output_padding;
188  }
189  /// \brief Calculates output spatial features size.
190  ///
191  /// \param[in] input_data_shape The input data partial shape
192  /// \param[in] filters_shape The filters partial shape
193  /// \param[in] strides The strides values.
194  /// \param[in] dilations The dilations values.
195  /// \param[in] pads_begin The paddings at the beginning of axis.
196  /// \param[in] pads_end The paddings at the end of axis.
197  /// \param[in] output_padding The output padding values.
198  /// \param output_spatial_shape The placeholder for computed output spatial partial
199  /// shape.
200  ///
201  void
202  infer_conv_backprop_output_spatial_shape(const std::vector<Dimension>& input_data_shape,
203  const std::vector<Dimension>& filters_shape,
204  const Strides& strides,
205  const Strides& dilations,
206  const CoordinateDiff& pads_begin,
207  const CoordinateDiff& pads_end,
208  const CoordinateDiff& output_padding,
209  std::vector<Dimension>& output_spatial_shape);
210 
211  protected:
212  Strides m_strides;
213  Strides m_dilations;
214  CoordinateDiff m_pads_begin;
215  CoordinateDiff m_pads_end;
216  PadType m_auto_pad;
217  CoordinateDiff m_output_padding;
218  };
219  } // namespace v1
220  } // namespace op
221 } // namespace ngraph
ngraph::op::v1::Convolution::get_auto_pad
const PadType & get_auto_pad() const
Definition: convolution.hpp:84
ngraph::op::v1::ConvolutionBackpropData
Data batch backprop for batched convolution operation.
Definition: convolution.hpp:99
ngraph::op::PadType
PadType
Padding Type used for Convolution and Pooling
Definition: attr_types.hpp:71
ngraph::op::v1::ConvolutionBackpropData::get_type_info
const NodeTypeInfo & get_type_info() const override
Definition: convolution.hpp:102
ngraph::op::v1::Convolution::Convolution
Convolution(const Output< Node > &data_batch, const Output< Node > &filters, const Strides &strides, const CoordinateDiff &pads_begin, const CoordinateDiff &pads_end, const Strides &dilations, const PadType &auto_pad=PadType::EXPLICIT)
Constructs a batched convolution operation.
ngraph::op::v1::Convolution::get_pads_end
const CoordinateDiff & get_pads_end() const
Definition: convolution.hpp:81
ngraph::op::v1::Convolution::Convolution
Convolution()=default
Constructs a batched convolution operation.
ngraph::op::v1::ConvolutionBackpropData::get_pads_end
const CoordinateDiff & get_pads_end() const
Definition: convolution.hpp:178
ngraph::op::v1::ConvolutionBackpropData::get_strides
const Strides & get_strides() const
Definition: convolution.hpp:169
ngraph::op::v1::ConvolutionBackpropData::get_dilations
const Strides & get_dilations() const
Definition: convolution.hpp:172
ngraph::op::v1::Convolution::validate_and_infer_types
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
ngraph::op::v1::ConvolutionBackpropData::get_auto_pad
const PadType & get_auto_pad() const
Definition: convolution.hpp:181
ngraph::op::v1::Convolution::get_strides
const Strides & get_strides() const
Definition: convolution.hpp:72
ngraph::op::v1::ConvolutionBackpropData::infer_conv_backprop_output_spatial_shape
void infer_conv_backprop_output_spatial_shape(const std::vector< Dimension > &input_data_shape, const std::vector< Dimension > &filters_shape, const Strides &strides, const Strides &dilations, const CoordinateDiff &pads_begin, const CoordinateDiff &pads_end, const CoordinateDiff &output_padding, std::vector< Dimension > &output_spatial_shape)
Calculates output spatial features size.
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::op::v1::ConvolutionBackpropData::ConvolutionBackpropData
ConvolutionBackpropData()=default
Constructs a batched-convolution data batch-backprop operation.
ngraph::op::v1::ConvolutionBackpropData::validate_and_infer_types
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
ngraph::AttributeVisitor
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:70
ngraph::op::v1::ConvolutionBackpropData::get_output_padding
const CoordinateDiff & get_output_padding() const
Definition: convolution.hpp:184
ngraph::op::v1::Convolution
Batched convolution operation, with optional window dilation and stride.
Definition: convolution.hpp:32
ngraph::op::v1::ConvolutionBackpropData::get_pads_begin
const CoordinateDiff & get_pads_begin() const
Definition: convolution.hpp:175
ngraph::op::v1::ConvolutionBackpropData::get_output_shape
const PartialShape get_output_shape() const
ngraph::op::v1::Convolution::get_pads_begin
const CoordinateDiff & get_pads_begin() const
Definition: convolution.hpp:78
ngraph::op::v1::Convolution::get_default_value
virtual std::shared_ptr< Node > get_default_value() const override
ngraph::op::v1::Convolution::get_dilations
const Strides & get_dilations() const
Definition: convolution.hpp:75
ngraph::op::Op
Root of all actual ops.
Definition: op.hpp:29