binary_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  class NGRAPH_API BinaryConvolution : public Op
30  {
31  public:
32  static constexpr NodeTypeInfo type_info{"BinaryConvolution", 1};
33  const NodeTypeInfo& get_type_info() const override { return type_info; }
34  enum class BinaryConvolutionMode
35  {
36  // Interpret input data and kernel values: 0 as -1, 1 as 1
37  XNOR_POPCOUNT
38  };
39 
40  /// \brief Constructs a binary convolution operation.
41  BinaryConvolution() = default;
42  /// \brief Constructs a binary convolution operation.
43  /// \param data The node producing the input data batch tensor.
44  /// \param kernel The node producing the filters tensor.
45  /// \param strides The strides.
46  /// \param pads_begin The beginning of padding shape.
47  /// \param pads_end The end of padding shape.
48  /// \param dilations The dilations.
49  /// \param mode Defines how input tensor 0/1 values and weights 0/1 are interpreted.
50  /// \param pad_value Floating-point value used to fill pad area.
51  /// \param auto_pad The pad type for automatically computing padding sizes.
52  ///
53  /// Output `[N, C_OUT, R1, ... Rf]`
54  BinaryConvolution(const Output<Node>& data,
55  const Output<Node>& kernel,
56  const Strides& strides,
57  const CoordinateDiff& pads_begin,
58  const CoordinateDiff& pads_end,
59  const Strides& dilations,
60  BinaryConvolutionMode mode,
61  float pad_value,
62  const PadType& auto_pad = PadType::EXPLICIT);
63 
64  BinaryConvolution(const Output<Node>& data,
65  const Output<Node>& kernel,
66  const Strides& strides,
67  const CoordinateDiff& pads_begin,
68  const CoordinateDiff& pads_end,
69  const Strides& dilations,
70  const std::string& mode,
71  float pad_value,
72  const PadType& auto_pad = PadType::EXPLICIT);
73 
74  size_t get_version() const override { return 1; }
75  void validate_and_infer_types() override;
76 
77  bool visit_attributes(AttributeVisitor& visitor) override;
78 
79  std::shared_ptr<Node>
80  clone_with_new_inputs(const OutputVector& new_args) const override;
81 
82  /// \return The strides.
83  const Strides& get_strides() const { return m_strides; }
84  void set_strides(const Strides& strides) { m_strides = strides; }
85  /// \return The dilations.
86  const Strides& get_dilations() const { return m_dilations; }
87  void set_dilations(const Strides& dilations) { m_dilations = dilations; }
88  /// \return The padding-below sizes (possibly negative).
89  const CoordinateDiff& get_pads_begin() const { return m_pads_begin; }
90  void set_pads_begin(const CoordinateDiff& pads_begin) { m_pads_begin = pads_begin; }
91  /// \return The padding-above sizes (possibly negative).
92  const CoordinateDiff& get_pads_end() const { return m_pads_end; }
93  void set_adding_above(const CoordinateDiff& pads_end) { m_pads_end = pads_end; }
94  /// \return The pad type for convolution.
95  const PadType& get_auto_pad() const { return m_auto_pad; }
96  void set_auto_pad(const PadType& auto_pad) { m_auto_pad = auto_pad; }
97  /// \return The mode of convolution.
98  const BinaryConvolutionMode& get_mode() const { return m_mode; }
99  void set_mode(const BinaryConvolutionMode& mode) { m_mode = mode; }
100  /// \return The pad value.
101  float get_pad_value() const { return m_pad_value; }
102  void set_pad_value(float pad_value) { m_pad_value = pad_value; }
103  protected:
104  BinaryConvolutionMode mode_from_string(const std::string& mode) const;
105  Strides m_strides;
106  Strides m_dilations;
107  CoordinateDiff m_pads_begin;
108  CoordinateDiff m_pads_end;
109  BinaryConvolutionMode m_mode;
110  float m_pad_value;
111  PadType m_auto_pad;
112  };
113  }
114  } // namespace op
115 
116  NGRAPH_API
117  std::ostream& operator<<(std::ostream& s,
118  const op::v1::BinaryConvolution::BinaryConvolutionMode& type);
119 
120  template <>
121  class NGRAPH_API AttributeAdapter<op::v1::BinaryConvolution::BinaryConvolutionMode>
122  : public EnumAttributeAdapterBase<op::v1::BinaryConvolution::BinaryConvolutionMode>
123  {
124  public:
125  AttributeAdapter(op::v1::BinaryConvolution::BinaryConvolutionMode& value)
126  : EnumAttributeAdapterBase<op::v1::BinaryConvolution::BinaryConvolutionMode>(value)
127  {
128  }
129 
130  static constexpr DiscreteTypeInfo type_info{
131  "AttributeAdapter<op::v1::BinaryConvolution::BinaryConvolutionMode>", 0};
132  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
133  };
134 
135 } // namespace ngraph
ngraph::op::v1::BinaryConvolution::get_mode
const BinaryConvolutionMode & get_mode() const
Definition: binary_convolution.hpp:98
ngraph::op::PadType
PadType
Padding Type used for Convolution and Pooling
Definition: attr_types.hpp:71
ngraph::op::v1::BinaryConvolution::get_dilations
const Strides & get_dilations() const
Definition: binary_convolution.hpp:86
ngraph::op::v1::BinaryConvolution::BinaryConvolution
BinaryConvolution()=default
Constructs a binary convolution operation.
ngraph::op::v1::BinaryConvolution::get_pads_end
const CoordinateDiff & get_pads_end() const
Definition: binary_convolution.hpp:92
ngraph::op::v1::BinaryConvolution::BinaryConvolution
BinaryConvolution(const Output< Node > &data, const Output< Node > &kernel, const Strides &strides, const CoordinateDiff &pads_begin, const CoordinateDiff &pads_end, const Strides &dilations, BinaryConvolutionMode mode, float pad_value, const PadType &auto_pad=PadType::EXPLICIT)
Constructs a binary convolution operation.
ngraph::op::v1::BinaryConvolution
Definition: binary_convolution.hpp:30
ngraph::DiscreteTypeInfo
Definition: type.hpp:39
ngraph::op::v1::BinaryConvolution::get_version
size_t get_version() const override
Definition: binary_convolution.hpp:74
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::op::v1::BinaryConvolution::get_strides
const Strides & get_strides() const
Definition: binary_convolution.hpp:83
ngraph::AttributeVisitor
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:70
ngraph::op::v1::BinaryConvolution::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::BinaryConvolution::get_pads_begin
const CoordinateDiff & get_pads_begin() const
Definition: binary_convolution.hpp:89
ngraph::op::v1::BinaryConvolution::get_type_info
const NodeTypeInfo & get_type_info() const override
Definition: binary_convolution.hpp:33
ngraph::op::v1::BinaryConvolution::get_auto_pad
const PadType & get_auto_pad() const
Definition: binary_convolution.hpp:95
ngraph::op::v1::BinaryConvolution::get_pad_value
float get_pad_value() const
Definition: binary_convolution.hpp:101
ngraph::op::Op
Root of all actual ops.
Definition: op.hpp:29