reshape.hpp
1 //*****************************************************************************
2 // Copyright 2017-2020 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/axis_vector.hpp"
20 #include "ngraph/node.hpp"
21 #include "ngraph/op/op.hpp"
22 #include "ngraph/runtime/host_tensor.hpp"
23 
24 namespace ngraph
25 {
26  namespace op
27  {
28  namespace v0
29  {
30  // clang-format off
31  /// \brief Tensor reshape operation.
32  ///
33  /// "Converts" an input tensor into a new shape with the same number of elements.
34  ///
35  /// Given that the input tensor has shape \f$[d_1,\dots,d_n]\f$, the output may have any
36  /// shape \f$[d'_1,\dots,d'_m]\f$ such that
37  /// \f$\Pi_{0 \leq i \lt n}(d_i) = \Pi_{0 \leq i \lt m}(d'_i)\f$. For example, a
38  /// \f$3\times{}4\f$ matrix can be reshaped into a 3-tensor of shape
39  /// \f$3\times{}2\times{}2\f$, a matrix of shape \f$6\times{}2\f$, or a vector of size
40  /// \f$12\f$, but not, for example, a matrix of size \f$4\times{}4\f$.
41  ///
42  /// The parameter `input_order` indicates the order in which to "walk" over the input axes.
43  /// Given a tensor of shape \f$(d_1,\dots,d_n)\f$, an input order of
44  /// \f$(a_0, a_1, \dots, a_{n-1})\f$ results in the coordinate for axis \f$a_{n-1}\f$ being
45  /// varied most frequently, followed by axis \f$a-2\f$, and so on down to \f$a_0\f$.
46  ///
47  /// (TODO: example.)
48  ///
49  /// ## Parameters
50  ///
51  /// | | Description |
52  /// | -------------- | ---------------------------------------------------------- |
53  /// | `input_order` | The order in which to walk over the input axes. |
54  /// | `output_shape` | The shape \f$[d'_1,\dots,d'_m]\f$ for the reshaped tensor. |
55  ///
56  /// ## Inputs
57  ///
58  /// | | Type | Description |
59  /// | ----- | --------------------------------- | ------------------------------------------------------------------------------------------------------------ |
60  /// | `arg` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | An input tensor of any type and shape, as long as the product of \f$d_i\f$ equals the product of \f$d'_i\f$. |
61  ///
62  /// ## Output
63  ///
64  /// | Type | Description |
65  /// | ------------------------ | ------------------------------------------------------------------------------------------------------ |
66  /// | \f$E[d'_1,\dots,d'_m]\f$ | The tensor \f$T\f$, where \f$T\f$ is the input tensor with its elements rearranged as described above. |
67  // clang-format on
68  class NGRAPH_DEPRECATED(
69  "This operation is deprecated and will be removed soon. "
70  "Use v1::Reshape instead of it.") NGRAPH_API Reshape : public Op
71  {
72  NGRAPH_SUPPRESS_DEPRECATED_START
73  public:
74  static constexpr NodeTypeInfo type_info{"Reshape", 0};
75  const NodeTypeInfo& get_type_info() const override { return type_info; }
76  /// \brief Constructs a reshape operation.
77  Reshape() = default;
78  /// \brief Constructs a reshape operation.
79  ///
80  /// \param arg The tensor to be reshaped.
81  /// \param input_order The order in which to iterate over input axes. This must be a
82  /// permutation of the sequence \f$(0,\dots,n-1)\f$ where \f$n\f$
83  /// is
84  /// the rank of the input tensor.
85  /// \param output_shape The output shape. If the input shape is
86  /// \f$(a_0,\dots,a_{k-1})\f$ then the output shape must
87  /// be of the form \f$(b_0,\dots,b_{j-1})\f$ where
88  /// \f$\Pi(a_i) = \Pi(b_i)\f$.
89  Reshape(const Output<Node>& arg,
90  const AxisVector& input_order,
91  const Shape& output_shape);
92 
93  void validate_and_infer_types() override;
94 
95  virtual std::shared_ptr<Node>
96  clone_with_new_inputs(const OutputVector& new_args) const override;
97  bool visit_attributes(AttributeVisitor& visitor) override;
98 
99  /// \return The order in which to iterate over input axes.
100  const AxisVector& get_input_order() const { return m_input_order; }
101  void set_input_order(const AxisVector& input_order) { m_input_order = input_order; }
102  /// \return The shape of the output tensor.
103  const Shape& get_reshape_output_shape() const { return m_output_shape; }
104  void set_output_shape(const Shape& output_shape) { m_output_shape = output_shape; }
105  bool get_is_transpose() const { return m_is_transpose; }
106  void set_is_transpose(bool is_transpose) { m_is_transpose = is_transpose; }
107  bool evaluate(const HostTensorVector& outputs,
108  const HostTensorVector& inputs) const override;
109 
110  protected:
111  AxisVector m_input_order;
112  Shape m_output_shape;
113  bool m_is_transpose{false};
114  NGRAPH_SUPPRESS_DEPRECATED_END
115  };
116  }
117 
118  namespace v1
119  {
120  /// \brief Tensor dynamic reshape operation.
121  ///
122  /// "Converts" an input tensor into a new shape with the same number of elements.
123  /// This op does not touch the actual data. If needed, use Transpose for that purpose.
124  ///
125  class NGRAPH_API Reshape : public Op
126  {
127  public:
128  NGRAPH_RTTI_DECLARATION;
129  Reshape() = default;
130  /// \brief Constructs a dynamic reshape operation. This operation does not perform
131  /// transpose.
132  ///
133  /// \param arg The tensor to be reshaped.
134  /// \param pattern The node that defines output shape pattern.
135  /// If the input shape is \f$(a_0,\dots,a_{k-1})\f$ then the output shape
136  /// must
137  /// be of the form \f$(b_0,\dots,b_{j-1})\f$ where \f$\Pi(a_i) = \Pi(b_i)\f$.
138  /// A value of -1 is allowed for at most one dimension, in which case the
139  /// dimension size is inferred based on element count of input tensor.
140  /// \param special_zero Treats zeros in `pattern` as wildcard flags indicating a
141  /// copy from input shape at the same index.
142  ///
143  Reshape(const Output<Node>& arg, const Output<Node>& pattern, bool special_zero);
144 
145  bool visit_attributes(AttributeVisitor& visitor) override;
146  void validate_and_infer_types() override;
147 
148  size_t get_version() const override { return 1; }
149  virtual std::shared_ptr<Node>
150  clone_with_new_inputs(const OutputVector& new_args) const override;
151 
152  bool get_special_zero() const { return m_special_zero; }
153  void set_special_zero(bool special_zero) { m_special_zero = special_zero; }
154  bool evaluate(const HostTensorVector& outputs,
155  const HostTensorVector& inputs) const override;
156 
157  protected:
158  bool m_special_zero;
159  };
160  }
161  NGRAPH_SUPPRESS_DEPRECATED_START
162  using v0::Reshape;
163  NGRAPH_SUPPRESS_DEPRECATED_END
164  }
165 }
ngraph::op::v1::Reshape::get_version
size_t get_version() const override
Definition: reshape.hpp:148
ngraph::op::v1::Reshape::validate_and_infer_types
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::op::v1::Reshape::Reshape
Reshape(const Output< Node > &arg, const Output< Node > &pattern, bool special_zero)
Constructs a dynamic reshape operation. This operation does not perform transpose.
ngraph::AttributeVisitor
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:70
ngraph::op::v1::Reshape
Tensor dynamic reshape operation.
Definition: reshape.hpp:126
ngraph::op::Op
Root of all actual ops.
Definition: op.hpp:29