depth_to_space.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include "ngraph/node.hpp"
8 #include "ngraph/op/op.hpp"
9 #include "ngraph/op/util/attr_types.hpp"
10 #include "ngraph/op/util/fused_op.hpp"
11 #include "ngraph/runtime/host_tensor.hpp"
12 
13 namespace ngraph
14 {
15  namespace op
16  {
17  namespace v0
18  {
19  /// \brief DepthToSpace permutes data from the depth dimension of the input blob into
20  /// spatial dimensions.
21  ///
22  /// \note Values from the depth dimension (assuming NCHW layout) are moved in
23  /// spatial blocks to the height and width dimensions.
24  ///
25  /// Output node produces a tensor with shape:
26  /// [N, C/(blocksize * blocksize), H * blocksize, W * blocksize]
27  class NGRAPH_API DepthToSpace : public Op
28  {
29  public:
30  NGRAPH_RTTI_DECLARATION;
31 
32  enum class DepthToSpaceMode
33  {
34  // The input depth is divided to [block_size, ..., block_size, new_depth]
35  BLOCKS_FIRST,
36  // The input depth is divided to [new_depth, block_size, ..., block_size]
37  DEPTH_FIRST
38  };
39 
40  DepthToSpace() = default;
41  /// \brief Constructs a DepthToSpace operation.
42  ///
43  /// \param data Node producing the input tensor
44  /// \param mode Specifies how the input depth dimension is split to block
45  /// coordinates
46  /// \param block_size The size of the block of values to be moved
48  const DepthToSpaceMode& mode,
49  std::size_t block_size = 1);
50 
51  DepthToSpace(const Output<Node>& data,
52  const std::string& mode,
53  std::size_t block_size = 1);
54  bool visit_attributes(AttributeVisitor& visitor) override;
55 
56  std::size_t get_block_size() const { return m_blocksize; }
57  DepthToSpaceMode get_mode() const { return m_mode; }
58  virtual std::shared_ptr<Node>
59  clone_with_new_inputs(const OutputVector& new_args) const override;
60  void validate_and_infer_types() override;
61  bool evaluate(const HostTensorVector& outputs,
62  const HostTensorVector& inputs) const override;
63  bool has_evaluate() const override;
64 
65  protected:
66  std::size_t m_blocksize;
67  DepthToSpaceMode m_mode;
68  DepthToSpaceMode mode_from_string(const std::string& mode) const;
69 
70  private:
71  bool evaluate_depth_to_space(const HostTensorVector& outputs,
72  const HostTensorVector& inputs) const;
73  };
74  } // namespace v0
75  using v0::DepthToSpace;
76  } // namespace op
77 
78  NGRAPH_API
79  std::ostream& operator<<(std::ostream& s, const op::v0::DepthToSpace::DepthToSpaceMode& type);
80 
81  template <>
82  class NGRAPH_API AttributeAdapter<op::v0::DepthToSpace::DepthToSpaceMode>
83  : public EnumAttributeAdapterBase<op::v0::DepthToSpace::DepthToSpaceMode>
84  {
85  public:
86  AttributeAdapter(op::v0::DepthToSpace::DepthToSpaceMode& value)
88  {
89  }
90 
91  static constexpr DiscreteTypeInfo type_info{
92  "AttributeAdapter<op::v0::DepthToSpace::DepthToSpaceMode>", 0};
93  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
94  };
95 } // 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
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
Root of all actual ops.
Definition: op.hpp:17
DepthToSpace permutes data from the depth dimension of the input blob into spatial dimensions.
Definition: depth_to_space.hpp:28
bool has_evaluate() const override
Allows to get information about availability of evaluate method for the current operation.
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:16
Definition: type.hpp:27