group_conv.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include "ngraph/op/convolution.hpp"
8 #include "ngraph/op/op.hpp"
9 #include "ngraph/op/util/attr_types.hpp"
10 #include "ngraph/op/util/fused_op.hpp"
11 
12 namespace ngraph
13 {
14  namespace op
15  {
16  namespace v1
17  {
18  /// \brief Batched convolution operation, with optional window dilation and stride.
19  class NGRAPH_API GroupConvolution : public Op
20  {
21  public:
22  NGRAPH_RTTI_DECLARATION;
23 
24  /// \brief Constructs a batched convolution operation.
25  GroupConvolution() = 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  /// `[GROUPS, FC_OUT, FC_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, FC_OUT * GROUPS, R1, ... Rf]`
44  ///
45  GroupConvolution(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  bool visit_attributes(AttributeVisitor& visitor) override;
54  void validate_and_infer_types() override;
55 
56  virtual std::shared_ptr<Node>
57  clone_with_new_inputs(const OutputVector& new_args) const override;
58  /// \return The strides.
59  const Strides& get_strides() const { return m_strides; }
60  void set_strides(const Strides& strides) { m_strides = strides; }
61  /// \return The dilations.
62  const Strides& get_dilations() const { return m_dilations; }
63  void set_dilations(const Strides& dilations) { m_dilations = dilations; }
64  /// \return The padding-below sizes (possibly negative).
65  const CoordinateDiff& get_pads_begin() const { return m_pads_begin; }
66  void set_pads_begin(const CoordinateDiff& pads_begin) { m_pads_begin = pads_begin; }
67  /// \return The padding-above sizes (possibly negative).
68  const CoordinateDiff& get_pads_end() const { return m_pads_end; }
69  void set_adding_above(const CoordinateDiff& pads_end) { m_pads_end = pads_end; }
70  /// \return The pad type for convolution.
71  const PadType& get_auto_pad() const { return m_auto_pad; }
72  void set_auto_pad(const PadType& auto_pad) { m_auto_pad = auto_pad; }
73  /// \return The default value for Convolution.
74  virtual std::shared_ptr<Node> get_default_value() const override;
75 
76  protected:
77  Strides m_strides;
78  Strides m_dilations;
79  CoordinateDiff m_pads_begin;
80  CoordinateDiff m_pads_end;
81  PadType m_auto_pad;
82  };
83 
84  /// \brief Data batch backprop for batched convolution operation.
85  class NGRAPH_API GroupConvolutionBackpropData : public Op
86  {
87  public:
88  NGRAPH_RTTI_DECLARATION;
89 
90  /// \brief Constructs a batched-convolution data batch-backprop operation.
92  // clang-format off
93  //
94  // \brief Constructs a batched-convolution data batch-backprop operation.
95  //
96  // \param data The node producing data from forward-prop. Shape: [N,
97  // C_INPUT * GROUPS, X1, ..., XD].
98  // \param filter The node producing the filter from forward-prop. Shape:
99  // [GROUPS, C_INPUT, C_OUTPUT, K_D, ..., K_1]
100  // \param output_shape The shape of the data batch from forward-prop. It's size
101  // should be equal to number of data spatial dimensions.
102  // \param strides The strides from forward-prop.
103  // \param pads_begin The padding-below sizes from forward-prop.
104  // \param pads_end The padding-above sizes from forward-prop.
105  // \param dilations The dilations from forward-prop.
106  // \param auto_pad The pad type for automatically computing padding sizes.
107  // \param output_padding The output padding adds additional amount of paddings per
108  // each spatial axis in the output tensor.
109  //
110  // clang-format on
111  //
113  const Output<Node>& filter,
114  const Output<Node>& output_shape,
115  const Strides& strides,
116  const CoordinateDiff& pads_begin,
117  const CoordinateDiff& pads_end,
118  const Strides& dilations,
119  const PadType& auto_pad = PadType::EXPLICIT,
120  const CoordinateDiff& output_padding = {});
121 
122  // clang-format off
123  //
124  // \brief Constructs a batched-convolution data batch-backprop operation.
125  //
126  // \param data The node producing data from forward-prop. Shape: [N,
127  // C_INPUT * GROUPS, X1, ..., XD].
128  // \param filter The node producing the filter from forward-prop. Shape:
129  // [GROUPS, C_INPUT, C_OUTPUT, K_D, ..., K_1]
130  // \param output_shape The shape of the data batch from forward-prop. It's size
131  // should be equal to number of data spatial dimensions.
132  // \param strides The strides from forward-prop.
133  // \param dilations The dilations from forward-prop.
134  // \param auto_pad The pad type for automatically computing padding sizes.
135  // \param output_padding The output padding adds additional amount of paddings per
136  // each spatial axis in the output tensor.
137  //
138  // clang-format on
139  //
141  const Output<Node>& filter,
142  const Output<Node>& output_shape,
143  const Strides& strides,
144  const Strides& dilations,
145  const PadType& auto_pad,
146  const CoordinateDiff& output_padding = {});
147 
148  // clang-format off
149  //
150  // \brief Constructs a batched-convolution data batch-backprop operation.
151  //
152  // \param data The node producing data from forward-prop. Shape:
153  // [N, C_INPUT * GROUPS, X1, ..., XD].
154  // \param filter The node producing the filter from forward-prop. Shape:
155  // [GROUPS, C_INPUT, C_OUTPUT, K_D, ..., K_1]
156  // \param strides The strides from forward-prop.
157  // \param pads_begin The padding-below sizes from forward-prop.
158  // \param pads_end The padding-above sizes from forward-prop.
159  // \param dilations The dilations from forward-prop.
160  // \param auto_pad The pad type for automatically computing padding sizes.
161  // \param output_padding The output padding adds additional amount of paddings per
162  // each spatial axis in the output tensor.
163  //
164  // clang-format on
165  GroupConvolutionBackpropData(const Output<Node>& data,
166  const Output<Node>& filter,
167  const Strides& strides,
168  const CoordinateDiff& pads_begin,
169  const CoordinateDiff& pads_end,
170  const Strides& dilations,
171  const PadType& auto_pad = PadType::EXPLICIT,
172  const CoordinateDiff& output_padding = {});
173  ///
174  /// \brief Calculates output spatial features size.
175  ///
176  /// \param[in] input_data_shape The input data partial shape
177  /// \param[in] filters_shape The filters partial shape
178  /// \param[in] strides The strides values.
179  /// \param[in] dilations The dilations values.
180  /// \param[in] pads_begin The paddings at the beginning of axis.
181  /// \param[in] pads_end The paddings at the end of axis.
182  /// \param[in] output_padding The output padding values.
183  /// \param output_spatial_shape The placeholder for computed output spatial
184  /// partial
185  /// shape.
186  ///
188  const std::vector<Dimension>& input_data_shape,
189  const std::vector<Dimension>& filters_shape,
190  const Strides& strides,
191  const Strides& dilations,
192  const CoordinateDiff& pads_begin,
193  const CoordinateDiff& pads_end,
194  const CoordinateDiff& output_padding,
195  std::vector<Dimension>& output_spatial_shape);
196 
197  bool visit_attributes(AttributeVisitor& visitor) override;
198  virtual bool is_dynamic() const override;
199  void validate_and_infer_types() override;
200 
201  virtual std::shared_ptr<Node>
202  clone_with_new_inputs(const OutputVector& new_args) const override;
203 
204  /// \return The spatial shape of the output.
206  void set_output_shape(const Shape& output_shape);
207  /// \return The strides from the forward prop.
208  const Strides& get_strides() const { return m_strides; }
209  void set_strides(const Strides& strides) { m_strides = strides; }
210  /// \return The dilations from the forward prop.
211  const Strides& get_dilations() const { return m_dilations; }
212  void set_dilations(const Strides& dilations) { m_dilations = dilations; }
213  /// \return The number of pixels to add to the beginning along each axis.
214  const CoordinateDiff& get_pads_begin() const { return m_pads_begin; }
215  void set_pads_begin(const CoordinateDiff& pads_begin) { m_pads_begin = pads_begin; }
216  /// \return The number of pixels to add to the ending along each axis.
217  const CoordinateDiff& get_pads_end() const { return m_pads_end; }
218  void set_pads_end(const CoordinateDiff& pads_end) { m_pads_end = pads_end; }
219  /// \return The auto pad.
220  const PadType& get_auto_pad() const { return m_auto_pad; }
221  void set_auto_pad(const PadType& auto_pad) { m_auto_pad = auto_pad; }
222  /// \return The output padding.
223  const CoordinateDiff& get_output_padding() const { return m_output_padding; }
224  void set_output_padding(const CoordinateDiff& output_padding)
225  {
226  m_output_padding = output_padding;
227  }
228 
229  protected:
230  Strides m_strides;
231  Strides m_dilations;
232  CoordinateDiff m_pads_begin;
233  CoordinateDiff m_pads_end;
234  PadType m_auto_pad;
235  CoordinateDiff m_output_padding;
236  };
237 
238  } // namespace v1
239  } // namespace op
240 } // 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: group_conv.hpp:86
const PartialShape get_convolution_output_shape() const
const PadType & get_auto_pad() const
Definition: group_conv.hpp:220
const Strides & get_dilations() const
Definition: group_conv.hpp:211
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 Strides & get_strides() const
Definition: group_conv.hpp:208
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
const CoordinateDiff & get_pads_end() const
Definition: group_conv.hpp:217
GroupConvolutionBackpropData()
Constructs a batched-convolution data batch-backprop operation.
const CoordinateDiff & get_output_padding() const
Definition: group_conv.hpp:223
const CoordinateDiff & get_pads_begin() const
Definition: group_conv.hpp:214
Batched convolution operation, with optional window dilation and stride.
Definition: group_conv.hpp:20
const PadType & get_auto_pad() const
Definition: group_conv.hpp:71
const Strides & get_dilations() const
Definition: group_conv.hpp:62
GroupConvolution(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.
const CoordinateDiff & get_pads_begin() const
Definition: group_conv.hpp:65
GroupConvolution()=default
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....
virtual std::shared_ptr< Node > get_default_value() const override
const Strides & get_strides() const
Definition: group_conv.hpp:59
const CoordinateDiff & get_pads_end() const
Definition: group_conv.hpp:68
PadType
Padding Type used for Convolution and Pooling
Definition: attr_types.hpp:61
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16