strided_slice.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 <memory>
20 #include <vector>
21 
22 #include "ngraph/node.hpp"
23 #include "ngraph/op/op.hpp"
24 #include "ngraph/op/util/attr_types.hpp"
25 
26 namespace ngraph
27 {
28  namespace op
29  {
30  namespace v1
31  {
32  /// \brief Takes a slice of an input tensor, i.e., the sub-tensor that resides within a
33  /// bounding box, optionally with stride.
34  class NGRAPH_API StridedSlice : public Op
35  {
36  public:
37  NGRAPH_RTTI_DECLARATION;
38 
39  StridedSlice() = default;
40 
41  /// \brief Constructs a dynamic tensor strided slice operation.
42  ///
43  /// \param data The tensor to be sliced.
44  /// \param begin 1D tensor with begin indexes for input blob slicing.
45  /// \param end 1D tensor with end indexes for input blob slicing.
46  /// \param strides The slicing strides; for example, strides of `{n,m}`
47  /// means to take every nth row and every mth column
48  /// of the input matrix.
49  /// \param begin_mask When begin_mask[i] equal to 1 means that the
50  /// corresponding dimension of the begin input is ignored.
51  /// \param end_mask When end_mask[i] is 1, the corresponding dimension of
52  /// the end input is ignored.
53  /// \param new_axis_mask If new_axis_mask[i] is 1, a length 1 dimension
54  /// is inserted on the i-th position.
55  /// \param shrink_axis_mask If shrink_axis_mask[i] is 1, the dimension
56  /// on the i-th position is deleted.
57  /// \param ellipsis_mask It inserts missing dimensions
58  /// on a position of a non-zero bit.
60  const Output<Node>& begin,
61  const Output<Node>& end,
62  const Output<Node>& strides,
63  const std::vector<int64_t>& begin_mask,
64  const std::vector<int64_t>& end_mask,
65  const std::vector<int64_t>& new_axis_mask = std::vector<int64_t>{},
66  const std::vector<int64_t>& shrink_axis_mask = std::vector<int64_t>{},
67  const std::vector<int64_t>& ellipsis_mask = std::vector<int64_t>{});
68 
69  /// \brief Constructs a dynamic tensor strided slice operation.
70  ///
71  /// \param data The tensor to be sliced.
72  /// \param begin 1D tensor with begin indexes for input blob slicing.
73  /// \param end 1D tensor with end indexes for input blob slicing.
74  /// \param begin_mask When begin_mask[i] equal to 1 means that the
75  /// corresponding dimension of the begin input is ignored.
76  /// \param end_mask When end_mask[i] is 1, the corresponding dimension of
77  /// the end input is ignored.
78  /// \param new_axis_mask If new_axis_mask[i] is 1, a length 1 dimension
79  /// is inserted on the i-th position.
80  /// \param shrink_axis_mask If shrink_axis_mask[i] is 1, the dimension
81  /// on the i-th position is deleted.
82  /// \param ellipsis_mask It inserts missing dimensions
83  /// on a position of a non-zero bit.
85  const Output<Node>& begin,
86  const Output<Node>& end,
87  const std::vector<int64_t>& begin_mask,
88  const std::vector<int64_t>& end_mask,
89  const std::vector<int64_t>& new_axis_mask = std::vector<int64_t>{},
90  const std::vector<int64_t>& shrink_axis_mask = std::vector<int64_t>{},
91  const std::vector<int64_t>& ellipsis_mask = std::vector<int64_t>{});
92 
93  bool visit_attributes(AttributeVisitor& visitor) override;
94  const std::vector<int64_t>& get_begin_mask() const { return m_begin_mask; }
95  const std::vector<int64_t>& get_end_mask() const { return m_end_mask; }
96  const std::vector<int64_t>& get_new_axis_mask() const { return m_new_axis_mask; }
97  const std::vector<int64_t>& get_shrink_axis_mask() const
98  {
99  return m_shrink_axis_mask;
100  }
101  const std::vector<int64_t>& get_ellipsis_mask() const { return m_ellipsis_mask; }
102  std::shared_ptr<Node>
103  clone_with_new_inputs(const OutputVector& new_args) const override;
104  void validate_and_infer_types() override;
105  size_t get_version() const override { return 1; }
106  bool evaluate(const HostTensorVector& output_values,
107  const HostTensorVector& input_values) const override;
108  bool evaluate_lower(const HostTensorVector& outputs) const override;
109  bool evaluate_upper(const HostTensorVector& outputs) const override;
110 
111  private:
112  AxisSet convert_mask_to_axis_set(const std::vector<int64_t>& mask) const;
113 
114  std::vector<int64_t> m_begin_mask;
115  std::vector<int64_t> m_end_mask;
116  std::vector<int64_t> m_new_axis_mask;
117  std::vector<int64_t> m_shrink_axis_mask;
118  std::vector<int64_t> m_ellipsis_mask;
119  };
120  }
121  }
122 }
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:71
A set of axes.
Definition: axis_set.hpp:31
A handle for one of a node's outputs.
Definition: node_output.hpp:42
Root of all actual ops.
Definition: op.hpp:29
Takes a slice of an input tensor, i.e., the sub-tensor that resides within a bounding box,...
Definition: strided_slice.hpp:35
StridedSlice(const Output< Node > &data, const Output< Node > &begin, const Output< Node > &end, const Output< Node > &strides, const std::vector< int64_t > &begin_mask, const std::vector< int64_t > &end_mask, const std::vector< int64_t > &new_axis_mask=std::vector< int64_t >{}, const std::vector< int64_t > &shrink_axis_mask=std::vector< int64_t >{}, const std::vector< int64_t > &ellipsis_mask=std::vector< int64_t >{})
Constructs a dynamic tensor strided slice operation.
StridedSlice(const Output< Node > &data, const Output< Node > &begin, const Output< Node > &end, const std::vector< int64_t > &begin_mask, const std::vector< int64_t > &end_mask, const std::vector< int64_t > &new_axis_mask=std::vector< int64_t >{}, const std::vector< int64_t > &shrink_axis_mask=std::vector< int64_t >{}, const std::vector< int64_t > &ellipsis_mask=std::vector< int64_t >{})
Constructs a dynamic tensor strided slice operation.
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
size_t get_version() const override
Definition: strided_slice.hpp:105
bool evaluate(const HostTensorVector &output_values, const HostTensorVector &input_values) const override
Evaluates the op on input_values putting results in output_values.
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28