strided_slice.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include <memory>
8 #include <vector>
9 
10 #include "ngraph/node.hpp"
11 #include "ngraph/op/op.hpp"
12 #include "ngraph/op/util/attr_types.hpp"
13 
14 namespace ngraph
15 {
16  namespace op
17  {
18  namespace v1
19  {
20  /// \brief Takes a slice of an input tensor, i.e., the sub-tensor that resides within a
21  /// bounding box, optionally with stride.
22  class NGRAPH_API StridedSlice : public Op
23  {
24  public:
25  NGRAPH_RTTI_DECLARATION;
26 
27  StridedSlice() = default;
28 
29  /// \brief Constructs a dynamic tensor strided slice operation.
30  ///
31  /// \param data The tensor to be sliced.
32  /// \param begin 1D tensor with begin indexes for input blob slicing.
33  /// \param end 1D tensor with end indexes for input blob slicing.
34  /// \param strides The slicing strides; for example, strides of `{n,m}`
35  /// means to take every nth row and every mth column
36  /// of the input matrix.
37  /// \param begin_mask When begin_mask[i] equal to 1 means that the
38  /// corresponding dimension of the begin input is ignored.
39  /// \param end_mask When end_mask[i] is 1, the corresponding dimension of
40  /// the end input is ignored.
41  /// \param new_axis_mask If new_axis_mask[i] is 1, a length 1 dimension
42  /// is inserted on the i-th position.
43  /// \param shrink_axis_mask If shrink_axis_mask[i] is 1, the dimension
44  /// on the i-th position is deleted.
45  /// \param ellipsis_mask It inserts missing dimensions
46  /// on a position of a non-zero bit.
48  const Output<Node>& begin,
49  const Output<Node>& end,
50  const Output<Node>& strides,
51  const std::vector<int64_t>& begin_mask,
52  const std::vector<int64_t>& end_mask,
53  const std::vector<int64_t>& new_axis_mask = std::vector<int64_t>{},
54  const std::vector<int64_t>& shrink_axis_mask = std::vector<int64_t>{},
55  const std::vector<int64_t>& ellipsis_mask = std::vector<int64_t>{});
56 
57  /// \brief Constructs a dynamic tensor strided slice operation.
58  ///
59  /// \param data The tensor to be sliced.
60  /// \param begin 1D tensor with begin indexes for input blob slicing.
61  /// \param end 1D tensor with end indexes for input blob slicing.
62  /// \param begin_mask When begin_mask[i] equal to 1 means that the
63  /// corresponding dimension of the begin input is ignored.
64  /// \param end_mask When end_mask[i] is 1, the corresponding dimension of
65  /// the end input is ignored.
66  /// \param new_axis_mask If new_axis_mask[i] is 1, a length 1 dimension
67  /// is inserted on the i-th position.
68  /// \param shrink_axis_mask If shrink_axis_mask[i] is 1, the dimension
69  /// on the i-th position is deleted.
70  /// \param ellipsis_mask It inserts missing dimensions
71  /// on a position of a non-zero bit.
73  const Output<Node>& begin,
74  const Output<Node>& end,
75  const std::vector<int64_t>& begin_mask,
76  const std::vector<int64_t>& end_mask,
77  const std::vector<int64_t>& new_axis_mask = std::vector<int64_t>{},
78  const std::vector<int64_t>& shrink_axis_mask = std::vector<int64_t>{},
79  const std::vector<int64_t>& ellipsis_mask = std::vector<int64_t>{});
80 
81  bool visit_attributes(AttributeVisitor& visitor) override;
82  const std::vector<int64_t>& get_begin_mask() const { return m_begin_mask; }
83  const std::vector<int64_t>& get_end_mask() const { return m_end_mask; }
84  const std::vector<int64_t>& get_new_axis_mask() const { return m_new_axis_mask; }
85  const std::vector<int64_t>& get_shrink_axis_mask() const
86  {
87  return m_shrink_axis_mask;
88  }
89  const std::vector<int64_t>& get_ellipsis_mask() const { return m_ellipsis_mask; }
90  std::shared_ptr<Node>
91  clone_with_new_inputs(const OutputVector& new_args) const override;
92  void validate_and_infer_types() override;
93  size_t get_version() const override { return 1; }
94  bool evaluate(const HostTensorVector& output_values,
95  const HostTensorVector& input_values) const override;
96  bool has_evaluate() const override;
97  bool evaluate_lower(const HostTensorVector& outputs) const override;
98  bool evaluate_upper(const HostTensorVector& outputs) const override;
99 
100  private:
101  AxisSet convert_mask_to_axis_set(const std::vector<int64_t>& mask) const;
102 
103  std::vector<int64_t> m_begin_mask;
104  std::vector<int64_t> m_end_mask;
105  std::vector<int64_t> m_new_axis_mask;
106  std::vector<int64_t> m_shrink_axis_mask;
107  std::vector<int64_t> m_ellipsis_mask;
108  };
109  } // namespace v1
110  } // namespace op
111 } // namespace ngraph
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:59
A set of axes.
Definition: axis_set.hpp:19
A handle for one of a node's outputs.
Definition: node_output.hpp:33
Root of all actual ops.
Definition: op.hpp:17
Takes a slice of an input tensor, i.e., the sub-tensor that resides within a bounding box,...
Definition: strided_slice.hpp:23
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....
bool has_evaluate() const override
Allows to get information about availability of evaluate method for the current operation.
size_t get_version() const override
Definition: strided_slice.hpp:93
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:16