space_to_depth.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/util/fused_op.hpp"
9 #include "ngraph/runtime/host_tensor.hpp"
10 
11 namespace ngraph
12 {
13  namespace op
14  {
15  namespace v0
16  {
17  /// \brief SpaceToDepth permutes input tensor blocks of spatial data into depth
18  /// dimension.
19  ///
20  /// \note Values from the height and width dimensions are moved to the depth dimension.
21  ///
22  /// Output node produces a tensor with shape:
23  /// [N, C * blocksize * blocksize, H / blocksize, W / blocksize]
24  class NGRAPH_API SpaceToDepth : public Op
25  {
26  public:
27  static constexpr NodeTypeInfo type_info{"SpaceToDepth", 0};
28  const NodeTypeInfo& get_type_info() const override { return type_info; }
29  enum class SpaceToDepthMode
30  {
31  // The output depth is gathered from [block_size, ..., block_size, C]
32  BLOCKS_FIRST,
33  // The output depth is gathered from [C, block_size, ..., block_size]
34  DEPTH_FIRST
35  };
36 
37  SpaceToDepth() = default;
38  /// \brief Constructs a SpaceToDepth operation.
39  ///
40  /// \param data - Node producing the input tensor
41  /// \param mode Specifies how the output depth dimension is gathered
42  /// from block coordinates and the old depth dimension.
43  /// \param block_size - the size of the block of values to be moved
45  const SpaceToDepthMode& mode,
46  std::size_t block_size = 1);
47 
48  SpaceToDepth(const Output<Node>& data,
49  const std::string& mode,
50  std::size_t block_size = 1);
51 
52  bool visit_attributes(AttributeVisitor& visitor) override;
53  std::size_t get_block_size() const { return m_blocksize; }
54  SpaceToDepthMode get_mode() const { return m_mode; }
55  void validate_and_infer_types() override;
56  virtual std::shared_ptr<Node>
57  clone_with_new_inputs(const OutputVector& new_args) const override;
58 
59  bool evaluate(const HostTensorVector& outputs,
60  const HostTensorVector& inputs) const override;
61  bool has_evaluate() const override;
62 
63  protected:
64  std::size_t m_blocksize;
65  SpaceToDepthMode m_mode;
66 
67  private:
68  bool evaluate_space_to_depth(const HostTensorVector& outputs,
69  const HostTensorVector& inputs) const;
70  };
71  } // namespace v0
72  using v0::SpaceToDepth;
73  } // namespace op
74 
75  NGRAPH_API
76  std::ostream& operator<<(std::ostream& s, const op::v0::SpaceToDepth::SpaceToDepthMode& type);
77 
78  template <>
79  class NGRAPH_API AttributeAdapter<op::v0::SpaceToDepth::SpaceToDepthMode>
80  : public EnumAttributeAdapterBase<op::v0::SpaceToDepth::SpaceToDepthMode>
81  {
82  public:
83  AttributeAdapter(op::v0::SpaceToDepth::SpaceToDepthMode& value)
85  {
86  }
87 
88  static constexpr DiscreteTypeInfo type_info{
89  "AttributeAdapter<op::v0::SpaceToDepth::SpaceToDepthMode>", 0};
90  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
91  };
92 } // 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
SpaceToDepth permutes input tensor blocks of spatial data into depth dimension.
Definition: space_to_depth.hpp:25
SpaceToDepth(const Output< Node > &data, const SpaceToDepthMode &mode, std::size_t block_size=1)
Constructs a SpaceToDepth operation.
bool evaluate(const HostTensorVector &outputs, const HostTensorVector &inputs) const override
Evaluates the op on input_values putting results in output_values.
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
const NodeTypeInfo & get_type_info() const override
Definition: space_to_depth.hpp:28
bool has_evaluate() const override
Allows to get information about availability of evaluate method for the current operation.
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16
Definition: type.hpp:27