mvn.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/node.hpp"
20 #include "ngraph/op/op.hpp"
21 #include "ngraph/op/util/fused_op.hpp"
22 
23 namespace ngraph
24 {
25  namespace op
26  {
27  NGRAPH_SUPPRESS_DEPRECATED_START
28 
29  namespace v0
30  {
31  /// \brief Operator performing Mean Variance Normalization
32  ///
33  class NGRAPH_API MVN : public ngraph::op::util::FusedOp
34  {
35  public:
36  NGRAPH_RTTI_DECLARATION;
37 
38  MVN();
39  /// \brief Constructs an MVN operation.
40  ///
41  /// \param data Input tensor with data
42  /// \param normalize_variance flag that denotes whether to perform variance
43  /// normalization.
44  /// \param across_channels flag that denotes if mean values are shared across
45  /// channels.
46  /// \param eps the number to be added to the variance to avoid division by zero when
47  /// normalizing the value
48  ///
49  MVN(const Output<Node>& data,
50  bool across_channels = true,
51  bool normalize_variance = true,
52  double eps = 1e-9);
53 
54  /// \brief Constructs an MVN operation.
55  ///
56  /// \param data Input tensor with data
57  /// \param reduction_axes A list of axes, along which to reduce.
58  /// \param normalize_variance flag that denotes whether to perform variance
59  /// normalization.
60  /// \param eps the number to be added to the variance to avoid division by zero when
61  /// normalizing the value
62  ///
63  MVN(const Output<Node>& data,
64  AxisSet reduction_axes,
65  bool normalize_variance = true,
66  double eps = 1e-9);
67 
68  virtual OutputVector decompose_op() const override;
69 
70  virtual void validate_and_infer_types() override;
71 
72  virtual bool visit_attributes(AttributeVisitor& visitor) override;
73 
74  virtual std::shared_ptr<Node>
75  clone_with_new_inputs(const OutputVector& new_args) const override;
76 
77  double get_eps() const { return m_eps; }
78  bool get_across_channels() const { return m_across_channels; }
79  bool get_normalize_variance() const { return m_normalize_variance; }
80  AxisSet get_reduction_axes() const { return m_reduction_axes; }
81  void set_reduction_axes(AxisSet axes) { m_reduction_axes = axes; }
82  private:
83  double m_eps = 1e-9;
84  bool m_across_channels;
85  bool m_normalize_variance;
86  AxisSet m_reduction_axes;
87  };
88  }
89  using v0::MVN;
90 
91  NGRAPH_SUPPRESS_DEPRECATED_END
92 
93  /// \brief Specifies how eps is applied in MVN
94  enum class MVNEpsMode
95  {
96  // Apply eps inside sqrt
97  INSIDE_SQRT,
98  // Apply eps outside sqrt
99  OUTSIDE_SQRT
100  };
101 
102  NGRAPH_API
103  std::ostream& operator<<(std::ostream& s, const MVNEpsMode& type);
104 
105  namespace v6
106  {
107  /// \brief Operator performing Mean Variance Normalization
108  ///
109  class NGRAPH_API MVN : public ngraph::op::Op
110  {
111  public:
112  NGRAPH_RTTI_DECLARATION;
113 
114  MVN() = default;
115  /// \brief Constructs an MVN operation.
116  ///
117  /// \param data Input tensor with data
118  /// \param reduction_axes A list of axes, along which to reduce.
119  /// \param normalize_variance flag that denotes whether to perform variance
120  /// normalization.
121  /// \param eps the number to be added to the variance to avoid division by zero when
122  /// normalizing the value
123  /// \param eps_mode the mode of applying epsilon
124  ///
125  MVN(const Output<Node>& data,
126  const Output<Node>& reduction_axes,
127  bool normalize_variance,
128  float eps,
129  MVNEpsMode eps_mode);
130 
131  bool visit_attributes(AttributeVisitor& visitor) override;
132  void validate_and_infer_types() override;
133 
134  std::shared_ptr<Node>
135  clone_with_new_inputs(const OutputVector& new_args) const override;
136 
137  float get_eps() const { return m_eps; }
138  bool get_normalize_variance() const { return m_normalize_variance; }
139  MVNEpsMode get_eps_mode() const { return m_eps_mode; }
140  private:
141  bool m_normalize_variance = true;
142  float m_eps = (float)1e-6;
143  MVNEpsMode m_eps_mode = MVNEpsMode::INSIDE_SQRT;
144  };
145  } // namespace v6
146  } // namespace op
147 
148  template <>
149  class NGRAPH_API AttributeAdapter<op::MVNEpsMode>
150  : public EnumAttributeAdapterBase<op::MVNEpsMode>
151  {
152  public:
155  {
156  }
157 
158  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::MVNEpsMode>", 0};
159  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
160  };
161 } // namespace ngraph
An AttributeAdapter "captures" an attribute as an AT& and makes it available as a ValueAccessor<VAT>.
Definition: attribute_adapter.hpp:171
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:71
A set of axes.
Definition: axis_set.hpp:31
Access an enum via a string.
Definition: attribute_adapter.hpp:178
A handle for one of a node's outputs.
Definition: node_output.hpp:42
Root of all actual ops.
Definition: op.hpp:29
Operator performing Mean Variance Normalization.
Definition: mvn.hpp:34
MVN(const Output< Node > &data, AxisSet reduction_axes, bool normalize_variance=true, double eps=1e-9)
Constructs an MVN operation.
MVN(const Output< Node > &data, bool across_channels=true, bool normalize_variance=true, double eps=1e-9)
Constructs an MVN operation.
Operator performing Mean Variance Normalization.
Definition: mvn.hpp:110
MVN(const Output< Node > &data, const Output< Node > &reduction_axes, bool normalize_variance, float eps, MVNEpsMode eps_mode)
Constructs an MVN operation.
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
MVNEpsMode
Specifies how eps is applied in MVN.
Definition: mvn.hpp:95
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Definition: type.hpp:39