group_conv.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 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/op/convolution.hpp"
20 #include "ngraph/op/op.hpp"
21 #include "ngraph/op/util/attr_types.hpp"
22 #include "ngraph/op/util/fused_op.hpp"
23 
24 namespace ngraph
25 {
26  namespace op
27  {
28  namespace v1
29  {
30  /// \brief Batched convolution operation, with optional window dilation and stride.
31  class NGRAPH_API GroupConvolution : public Op
32  {
33  public:
34  NGRAPH_RTTI_DECLARATION;
35 
36  /// \brief Constructs a batched convolution operation.
37  GroupConvolution() = 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  /// `[GROUPS, FC_OUT, FC_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, FC_OUT * GROUPS, R1, ... Rf]`
56  ///
57  GroupConvolution(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  bool visit_attributes(AttributeVisitor& visitor) override;
66  void validate_and_infer_types() override;
67 
68  virtual std::shared_ptr<Node>
69  clone_with_new_inputs(const OutputVector& new_args) const override;
70  /// \return The strides.
71  const Strides& get_strides() const { return m_strides; }
72  void set_strides(const Strides& strides) { m_strides = strides; }
73  /// \return The dilations.
74  const Strides& get_dilations() const { return m_dilations; }
75  void set_dilations(const Strides& dilations) { m_dilations = dilations; }
76  /// \return The padding-below sizes (possibly negative).
77  const CoordinateDiff& get_pads_begin() const { return m_pads_begin; }
78  void set_pads_begin(const CoordinateDiff& pads_begin) { m_pads_begin = pads_begin; }
79  /// \return The padding-above sizes (possibly negative).
80  const CoordinateDiff& get_pads_end() const { return m_pads_end; }
81  void set_adding_above(const CoordinateDiff& pads_end) { m_pads_end = pads_end; }
82  /// \return The pad type for convolution.
83  const PadType& get_auto_pad() const { return m_auto_pad; }
84  void set_auto_pad(const PadType& auto_pad) { m_auto_pad = auto_pad; }
85  /// \return The default value for Convolution.
86  virtual std::shared_ptr<Node> get_default_value() const override;
87 
88  protected:
89  Strides m_strides;
90  Strides m_dilations;
91  CoordinateDiff m_pads_begin;
92  CoordinateDiff m_pads_end;
93  PadType m_auto_pad;
94  };
95 
96  /// \brief Data batch backprop for batched convolution operation.
97  class NGRAPH_API GroupConvolutionBackpropData : public Op
98  {
99  public:
100  static constexpr NodeTypeInfo type_info{"GroupConvolutionBackpropData", 1};
101  const NodeTypeInfo& get_type_info() const override { return type_info; }
102  /// \brief Constructs a batched-convolution data batch-backprop operation.
104  // clang-format off
105  //
106  // \brief Constructs a batched-convolution data batch-backprop operation.
107  //
108  // \param data The node producing data from forward-prop. Shape: [N,
109  // C_INPUT * GROUPS, X1, ..., XD].
110  // \param filter The node producing the filter from forward-prop. Shape:
111  // [GROUPS, C_INPUT, C_OUTPUT, K_D, ..., K_1]
112  // \param output_shape The shape of the data batch from forward-prop. It's size
113  // should be equal to number of data spatial dimensions.
114  // \param strides The strides from forward-prop.
115  // \param pads_begin The padding-below sizes from forward-prop.
116  // \param pads_end The padding-above sizes from forward-prop.
117  // \param dilations The dilations from forward-prop.
118  // \param auto_pad The pad type for automatically computing padding sizes.
119  // \param output_padding The output padding adds additional amount of paddings per
120  // each spatial axis in the output tensor.
121  //
122  // clang-format on
123  //
125  const Output<Node>& filter,
126  const Output<Node>& output_shape,
127  const Strides& strides,
128  const CoordinateDiff& pads_begin,
129  const CoordinateDiff& pads_end,
130  const Strides& dilations,
131  const PadType& auto_pad = PadType::EXPLICIT,
132  const CoordinateDiff& output_padding = {});
133 
134  // clang-format off
135  //
136  // \brief Constructs a batched-convolution data batch-backprop operation.
137  //
138  // \param data The node producing data from forward-prop. Shape: [N,
139  // C_INPUT * GROUPS, X1, ..., XD].
140  // \param filter The node producing the filter from forward-prop. Shape:
141  // [GROUPS, C_INPUT, C_OUTPUT, K_D, ..., K_1]
142  // \param output_shape The shape of the data batch from forward-prop. It's size
143  // should be equal to number of data spatial dimensions.
144  // \param strides The strides from forward-prop.
145  // \param dilations The dilations from forward-prop.
146  // \param auto_pad The pad type for automatically computing padding sizes.
147  // \param output_padding The output padding adds additional amount of paddings per
148  // each spatial axis in the output tensor.
149  //
150  // clang-format on
151  //
153  const Output<Node>& filter,
154  const Output<Node>& output_shape,
155  const Strides& strides,
156  const Strides& dilations,
157  const PadType& auto_pad,
158  const CoordinateDiff& output_padding = {});
159 
160  // clang-format off
161  //
162  // \brief Constructs a batched-convolution data batch-backprop operation.
163  //
164  // \param data The node producing data from forward-prop. Shape:
165  // [N, C_INPUT * GROUPS, X1, ..., XD].
166  // \param filter The node producing the filter from forward-prop. Shape:
167  // [GROUPS, C_INPUT, C_OUTPUT, K_D, ..., K_1]
168  // \param strides The strides from forward-prop.
169  // \param pads_begin The padding-below sizes from forward-prop.
170  // \param pads_end The padding-above sizes from forward-prop.
171  // \param dilations The dilations from forward-prop.
172  // \param auto_pad The pad type for automatically computing padding sizes.
173  // \param output_padding The output padding adds additional amount of paddings per
174  // each spatial axis in the output tensor.
175  //
176  // clang-format on
177  GroupConvolutionBackpropData(const Output<Node>& data,
178  const Output<Node>& filter,
179  const Strides& strides,
180  const CoordinateDiff& pads_begin,
181  const CoordinateDiff& pads_end,
182  const Strides& dilations,
183  const PadType& auto_pad = PadType::EXPLICIT,
184  const CoordinateDiff& output_padding = {});
185  ///
186  /// \brief Calculates output spatial features size.
187  ///
188  /// \param[in] input_data_shape The input data partial shape
189  /// \param[in] filters_shape The filters partial shape
190  /// \param[in] strides The strides values.
191  /// \param[in] dilations The dilations values.
192  /// \param[in] pads_begin The paddings at the beginning of axis.
193  /// \param[in] pads_end The paddings at the end of axis.
194  /// \param[in] output_padding The output padding values.
195  /// \param output_spatial_shape The placeholder for computed output spatial
196  /// partial
197  /// shape.
198  ///
200  const std::vector<Dimension>& input_data_shape,
201  const std::vector<Dimension>& filters_shape,
202  const Strides& strides,
203  const Strides& dilations,
204  const CoordinateDiff& pads_begin,
205  const CoordinateDiff& pads_end,
206  const CoordinateDiff& output_padding,
207  std::vector<Dimension>& output_spatial_shape);
208 
209  bool visit_attributes(AttributeVisitor& visitor) override;
210  virtual bool is_dynamic() const override;
211  void validate_and_infer_types() override;
212 
213  virtual std::shared_ptr<Node>
214  clone_with_new_inputs(const OutputVector& new_args) const override;
215 
216  /// \return The spatial shape of the output.
218  void set_output_shape(const Shape& output_shape);
219  /// \return The strides from the forward prop.
220  const Strides& get_strides() const { return m_strides; }
221  void set_strides(const Strides& strides) { m_strides = strides; }
222  /// \return The dilations from the forward prop.
223  const Strides& get_dilations() const { return m_dilations; }
224  void set_dilations(const Strides& dilations) { m_dilations = dilations; }
225  /// \return The number of pixels to add to the beginning along each axis.
226  const CoordinateDiff& get_pads_begin() const { return m_pads_begin; }
227  void set_pads_begin(const CoordinateDiff& pads_begin) { m_pads_begin = pads_begin; }
228  /// \return The number of pixels to add to the ending along each axis.
229  const CoordinateDiff& get_pads_end() const { return m_pads_end; }
230  void set_pads_end(const CoordinateDiff& pads_end) { m_pads_end = pads_end; }
231  /// \return The auto pad.
232  const PadType& get_auto_pad() const { return m_auto_pad; }
233  void set_auto_pad(const PadType& auto_pad) { m_auto_pad = auto_pad; }
234  /// \return The output padding.
235  const CoordinateDiff& get_output_padding() const { return m_output_padding; }
236  void set_output_padding(const CoordinateDiff& output_padding)
237  {
238  m_output_padding = output_padding;
239  }
240 
241  protected:
242  Strides m_strides;
243  Strides m_dilations;
244  CoordinateDiff m_pads_begin;
245  CoordinateDiff m_pads_end;
246  PadType m_auto_pad;
247  CoordinateDiff m_output_padding;
248  };
249 
250  } // namespace v1
251  } // namespace op
252 } // namespace ngraph
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:71
A difference (signed) of tensor element coordinates.
Definition: coordinate_diff.hpp:30
A handle for one of a node's outputs.
Definition: node_output.hpp:42
Class representing a shape that may be partially or totally dynamic.
Definition: partial_shape.hpp:46
Shape for a tensor.
Definition: shape.hpp:31
Strides for a tensor.
Definition: strides.hpp:30
Root of all actual ops.
Definition: op.hpp:29
Data batch backprop for batched convolution operation.
Definition: group_conv.hpp:98
const PartialShape get_convolution_output_shape() const
const NodeTypeInfo & get_type_info() const override
Definition: group_conv.hpp:101
const PadType & get_auto_pad() const
Definition: group_conv.hpp:232
const Strides & get_dilations() const
Definition: group_conv.hpp:223
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:220
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:229
GroupConvolutionBackpropData()
Constructs a batched-convolution data batch-backprop operation.
const CoordinateDiff & get_output_padding() const
Definition: group_conv.hpp:235
const CoordinateDiff & get_pads_begin() const
Definition: group_conv.hpp:226
Batched convolution operation, with optional window dilation and stride.
Definition: group_conv.hpp:32
const PadType & get_auto_pad() const
Definition: group_conv.hpp:83
const Strides & get_dilations() const
Definition: group_conv.hpp:74
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:77
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:71
const CoordinateDiff & get_pads_end() const
Definition: group_conv.hpp:80
PadType
Padding Type used for Convolution and Pooling
Definition: attr_types.hpp:73
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Definition: type.hpp:39