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