binary_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  class NGRAPH_API BinaryConvolution : public Op
18  {
19  public:
20  static constexpr NodeTypeInfo type_info{"BinaryConvolution", 1};
21  const NodeTypeInfo& get_type_info() const override { return type_info; }
22  enum class BinaryConvolutionMode
23  {
24  // Interpret input data and kernel values: 0 as -1, 1 as 1
25  XNOR_POPCOUNT
26  };
27 
28  /// \brief Constructs a binary convolution operation.
29  BinaryConvolution() = default;
30  /// \brief Constructs a binary convolution operation.
31  /// \param data The node producing the input data batch tensor.
32  /// \param kernel The node producing the filters tensor.
33  /// \param strides The strides.
34  /// \param pads_begin The beginning of padding shape.
35  /// \param pads_end The end of padding shape.
36  /// \param dilations The dilations.
37  /// \param mode Defines how input tensor 0/1 values and weights 0/1 are interpreted.
38  /// \param pad_value Floating-point value used to fill pad area.
39  /// \param auto_pad The pad type for automatically computing padding sizes.
40  ///
41  /// Output `[N, C_OUT, R1, ... Rf]`
43  const Output<Node>& kernel,
44  const Strides& strides,
45  const CoordinateDiff& pads_begin,
46  const CoordinateDiff& pads_end,
47  const Strides& dilations,
48  BinaryConvolutionMode mode,
49  float pad_value,
50  const PadType& auto_pad = PadType::EXPLICIT);
51 
52  BinaryConvolution(const Output<Node>& data,
53  const Output<Node>& kernel,
54  const Strides& strides,
55  const CoordinateDiff& pads_begin,
56  const CoordinateDiff& pads_end,
57  const Strides& dilations,
58  const std::string& mode,
59  float pad_value,
60  const PadType& auto_pad = PadType::EXPLICIT);
61 
62  size_t get_version() const override { return 1; }
63  void validate_and_infer_types() override;
64 
65  bool visit_attributes(AttributeVisitor& visitor) override;
66 
67  std::shared_ptr<Node>
68  clone_with_new_inputs(const OutputVector& new_args) const override;
69 
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 mode of convolution.
86  const BinaryConvolutionMode& get_mode() const { return m_mode; }
87  void set_mode(const BinaryConvolutionMode& mode) { m_mode = mode; }
88  /// \return The pad value.
89  float get_pad_value() const { return m_pad_value; }
90  void set_pad_value(float pad_value) { m_pad_value = pad_value; }
91 
92  protected:
93  BinaryConvolutionMode mode_from_string(const std::string& mode) const;
94  Strides m_strides;
95  Strides m_dilations;
96  CoordinateDiff m_pads_begin;
97  CoordinateDiff m_pads_end;
98  BinaryConvolutionMode m_mode;
99  float m_pad_value;
100  PadType m_auto_pad;
101  };
102  } // namespace v1
103  } // namespace op
104 
105  NGRAPH_API
106  std::ostream& operator<<(std::ostream& s,
107  const op::v1::BinaryConvolution::BinaryConvolutionMode& type);
108 
109  template <>
110  class NGRAPH_API AttributeAdapter<op::v1::BinaryConvolution::BinaryConvolutionMode>
111  : public EnumAttributeAdapterBase<op::v1::BinaryConvolution::BinaryConvolutionMode>
112  {
113  public:
114  AttributeAdapter(op::v1::BinaryConvolution::BinaryConvolutionMode& value)
116  {
117  }
118 
119  static constexpr DiscreteTypeInfo type_info{
120  "AttributeAdapter<op::v1::BinaryConvolution::BinaryConvolutionMode>", 0};
121  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
122  };
123 
124 } // namespace ngraph
An AttributeAdapter "captures" an attribute as an AT& and makes it available as a ValueAccessor<VAT>.
Definition: attribute_adapter.hpp:161
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
Access an enum via a string.
Definition: attribute_adapter.hpp:168
A handle for one of a node's outputs.
Definition: node_output.hpp:33
Strides for a tensor.
Definition: strides.hpp:18
Root of all actual ops.
Definition: op.hpp:17
Definition: binary_convolution.hpp:18
const BinaryConvolutionMode & get_mode() const
Definition: binary_convolution.hpp:86
size_t get_version() const override
Definition: binary_convolution.hpp:62
const CoordinateDiff & get_pads_end() const
Definition: binary_convolution.hpp:80
const CoordinateDiff & get_pads_begin() const
Definition: binary_convolution.hpp:77
BinaryConvolution()=default
Constructs a binary convolution operation.
const Strides & get_strides() const
Definition: binary_convolution.hpp:71
const PadType & get_auto_pad() const
Definition: binary_convolution.hpp:83
const NodeTypeInfo & get_type_info() const override
Definition: binary_convolution.hpp:21
const Strides & get_dilations() const
Definition: binary_convolution.hpp:74
float get_pad_value() const
Definition: binary_convolution.hpp:89
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.
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
PadType
Padding Type used for Convolution and Pooling
Definition: attr_types.hpp:61
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16
Definition: type.hpp:27