select.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/op/op.hpp"
20 
21 namespace ngraph
22 {
23  namespace op
24  {
25  namespace v1
26  {
27  // clang-format off
28  /// \brief Elementwise selection operation.
29  ///
30  /// ## Inputs
31  ///
32  /// | | Type | Description |
33  /// | ------ | --------------------------------------------- | ------------------------------------------------------------ |
34  /// | `arg0` | \f$\texttt{bool}[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of any shape, with element `bool`. |
35  /// | `arg1` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of a shape that is broadcast-compatible with `arg0`, with any element type. |
36  /// | `arg2` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of a shape that is broadcast-compatible with `arg0`, and same element type as `arg1`. |
37  /// | `auto_broadcast`| AutoBroadcastSpec | Auto broadcast specification. |
38  ///
39  /// ## Output
40  ///
41  /// | Type | Description |
42  /// | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
43  /// | \f$E[d_1,\dots,d_n]\f$ | The tensor \f$T\f$, where \f$T[i_1,\dots,i_n] = \texttt{arg1}[i_1,\dots,i_n]\text{ if }\texttt{arg0}[i_1,\dots,i_n] \neq 0\text{, else }\texttt{arg2}[i_1,\dots,i_n]\f$ |
44  // clang-format on
45  class NGRAPH_API Select : public Op
46  {
47  public:
48  NGRAPH_RTTI_DECLARATION;
49  /// \brief Constructs a selection operation.
51  : m_auto_broadcast(AutoBroadcastSpec(AutoBroadcastType::NUMPY))
52  {
53  }
54 
55  /// \brief Constructs a selection operation.
56  ///
57  /// \param arg0 Node that produces the first input tensor.
58  /// \param arg1 Node that produces the second input tensor.
59  /// \param arg2 Node that produces the third input tensor.
60  /// \param auto_broadcast Auto broadcast specification. Default is Numpy-style
61  /// implicit broadcasting.
62  Select(const Output<Node>& arg0,
63  const Output<Node>& arg1,
64  const Output<Node>& arg2,
65  const AutoBroadcastSpec& auto_broadcast =
66  AutoBroadcastSpec(AutoBroadcastType::NUMPY));
67 
68  virtual std::shared_ptr<Node>
69  clone_with_new_inputs(const OutputVector& new_args) const override;
70  bool visit_attributes(AttributeVisitor& visitor) override;
71  void validate_and_infer_types() override;
72 
73  const AutoBroadcastSpec& get_auto_broadcast() const { return m_auto_broadcast; }
74  void set_auto_broadcast(const AutoBroadcastSpec& auto_broadcast)
75  {
76  m_auto_broadcast = auto_broadcast;
77  }
78  // TODO: Move all uses of get_autob to get_auto_broadcast() and remove this.
79  const AutoBroadcastSpec& get_autob() const override { return m_auto_broadcast; }
80  virtual bool evaluate(const HostTensorVector& output_values,
81  const HostTensorVector& input_values) const override;
82 
83  private:
84  AutoBroadcastSpec m_auto_broadcast;
85  };
86  } // namespace v1
87  } // namespace op
88 } // namespace ngraph
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:71
A handle for one of a node's outputs.
Definition: node_output.hpp:42
Root of all actual ops.
Definition: op.hpp:29
Elementwise selection operation.
Definition: select.hpp:46
Select(const Output< Node > &arg0, const Output< Node > &arg1, const Output< Node > &arg2, const AutoBroadcastSpec &auto_broadcast=AutoBroadcastSpec(AutoBroadcastType::NUMPY))
Constructs a selection operation.
virtual bool evaluate(const HostTensorVector &output_values, const HostTensorVector &input_values) const override
Evaluates the op on input_values putting results in output_values.
const AutoBroadcastSpec & get_autob() const override
Definition: select.hpp:79
Select()
Constructs a selection operation.
Definition: select.hpp:50
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
AutoBroadcastType
Specifies the algorithm to use for implicit broadcasting of a tensor to align with another tensor.
Definition: attr_types.hpp:167
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Implicit broadcast specification.
Definition: attr_types.hpp:323