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