depth_to_space.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/attr_types.hpp"
22 #include "ngraph/op/util/fused_op.hpp"
23 #include "ngraph/runtime/host_tensor.hpp"
24 
25 NGRAPH_SUPPRESS_DEPRECATED_START
26 
27 namespace ngraph
28 {
29  namespace op
30  {
31  namespace v0
32  {
33  /// \brief DepthToSpace permutes data from the depth dimension of the input blob into
34  /// spatial dimensions.
35  ///
36  /// \note Values from the depth dimension (assuming NCHW layout) are moved in
37  /// spatial blocks to the height and width dimensions.
38  ///
39  /// Output node produces a tensor with shape:
40  /// [N, C/(blocksize * blocksize), H * blocksize, W * blocksize]
41  class NGRAPH_API DepthToSpace : public Op
42  {
43  public:
44  NGRAPH_RTTI_DECLARATION;
45 
46  enum class DepthToSpaceMode
47  {
48  // The input depth is divided to [block_size, ..., block_size, new_depth]
49  BLOCKS_FIRST,
50  // The input depth is divided to [new_depth, block_size, ..., block_size]
51  DEPTH_FIRST
52  };
53 
54  DepthToSpace() = default;
55  /// \brief Constructs a DepthToSpace operation.
56  ///
57  /// \param data Node producing the input tensor
58  /// \param mode Specifies how the input depth dimension is split to block
59  /// coordinates
60  /// \param block_size The size of the block of values to be moved
62  const DepthToSpaceMode& mode,
63  std::size_t block_size = 1);
64 
65  DepthToSpace(const Output<Node>& data,
66  const std::string& mode,
67  std::size_t block_size = 1);
68  bool visit_attributes(AttributeVisitor& visitor) override;
69 
70  std::size_t get_block_size() const { return m_blocksize; }
71  DepthToSpaceMode get_mode() const { return m_mode; }
72  virtual std::shared_ptr<Node>
73  clone_with_new_inputs(const OutputVector& new_args) const override;
74  void validate_and_infer_types() override;
75  bool evaluate(const HostTensorVector& outputs,
76  const HostTensorVector& inputs) const override;
77 
78  protected:
79  std::size_t m_blocksize;
80  DepthToSpaceMode m_mode;
81  DepthToSpaceMode mode_from_string(const std::string& mode) const;
82 
83  private:
84  bool evaluate_depth_to_space(const HostTensorVector& outputs,
85  const HostTensorVector& inputs) const;
86  };
87  }
88  using v0::DepthToSpace;
89  }
90 
91  NGRAPH_API
92  std::ostream& operator<<(std::ostream& s, const op::v0::DepthToSpace::DepthToSpaceMode& type);
93 
94  template <>
95  class NGRAPH_API AttributeAdapter<op::v0::DepthToSpace::DepthToSpaceMode>
96  : public EnumAttributeAdapterBase<op::v0::DepthToSpace::DepthToSpaceMode>
97  {
98  public:
99  AttributeAdapter(op::v0::DepthToSpace::DepthToSpaceMode& value)
101  {
102  }
103 
104  static constexpr DiscreteTypeInfo type_info{
105  "AttributeAdapter<op::v0::DepthToSpace::DepthToSpaceMode>", 0};
106  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
107  };
108 }
109 
110 NGRAPH_SUPPRESS_DEPRECATED_END
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
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
DepthToSpace permutes data from the depth dimension of the input blob into spatial dimensions.
Definition: depth_to_space.hpp:42
DepthToSpace(const Output< Node > &data, const DepthToSpaceMode &mode, std::size_t block_size=1)
Constructs a DepthToSpace operation.
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
bool evaluate(const HostTensorVector &outputs, const HostTensorVector &inputs) const override
Evaluates the op on input_values putting results in output_values.
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Definition: type.hpp:39