binary_elementwise_arithmetic.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include "ngraph/op/op.hpp"
8 #include "ngraph/op/util/attr_types.hpp"
9 
10 namespace ngraph
11 {
12  namespace op
13  {
14  namespace util
15  {
16  // clang-format off
17  /// \brief Abstract base class for elementwise binary arithmetic operations, i.e.,
18  /// operations where the same scalar binary arithmetic operation is applied to
19  /// each corresponding pair of elements in the two input tensors. Implicit
20  /// broadcast of input tensors is supported through one of the AutoBroadcast
21  /// modes.
22  ///
23  /// For example, if the underlying arithmetic operation (determined by the subclass) is
24  /// \f$\mathit{op}(x,y)\f$, the input tensors
25  /// \f$[[x_0,y_0],[z_0,w_0]]\f$ and \f$[[x_1,y_1],[z_1,w_1]]\f$ will be mapped to
26  /// \f$[[\mathit{op}(x_0,x_1),\mathit{op}(y_0,y_1)],[\mathit{op}(z_0,z_1),\mathit{op}(w_0,w_1)]]\f$.
27  ///
28  /// ## Inputs
29  ///
30  /// | | Type | Description |
31  /// | ------ | --------------------------------- | ------------------------------------------------------------------------ |
32  /// | `arg0` | \f$N[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of any shape. The element type \f$N\f$ may be any numeric type. |
33  /// | `arg1` | \f$N[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of the same element type as `arg0`. |
34  /// | `autob`| AutoBroadcastSpec | Auto broadcast specification. |
35  ///
36  /// ## Output
37  ///
38  /// | Type | Description |
39  /// | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
40  /// | \f$N[d_1,\dots,d_n]\f$ | The tensor \f$T\f$, where \f$T[i_1,\dots,i_n] = \mathit{op}(\texttt{arg0}[i_1,\dots,i_n],\texttt{arg1}[i_1,\dots,i_n])\f$. This will always have the same shape and element type as the input tensors (after auto broadcasting). |
41  // clang-format on
42  class NGRAPH_API BinaryElementwiseArithmetic : public Op
43  {
44  protected:
46 
47  /// \brief Constructs a binary elementwise arithmetic operation.
48  ///
49  /// \param arg0 Output that produces the first input tensor.
50  /// \param arg1 Output that produces the second input tensor.
52  const Output<Node>& arg1,
53  const AutoBroadcastSpec& autob);
54 
55  public:
56  NGRAPH_RTTI_DECLARATION;
57 
58  void validate_and_infer_types() override;
59 
60  const AutoBroadcastSpec& get_autob() const override { return m_autob; }
61  void set_autob(const AutoBroadcastSpec& autob) { m_autob = autob; }
62  bool visit_attributes(AttributeVisitor& visitor) override;
63  bool evaluate_lower(const HostTensorVector& outputs) const override;
64  bool evaluate_upper(const HostTensorVector& outputs) const override;
65 
66  private:
67  AutoBroadcastSpec m_autob;
68  void validate_and_infer_elementwise_arithmetic(const op::AutoBroadcastSpec& autob);
69  };
70  } // namespace util
71  } // namespace op
72 } // namespace ngraph
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:59
A handle for one of a node's outputs.
Definition: node_output.hpp:33
Root of all actual ops.
Definition: op.hpp:17
Abstract base class for elementwise binary arithmetic operations, i.e., operations where the same sca...
Definition: binary_elementwise_arithmetic.hpp:43
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
const AutoBroadcastSpec & get_autob() const override
Definition: binary_elementwise_arithmetic.hpp:60
BinaryElementwiseArithmetic(const Output< Node > &arg0, const Output< Node > &arg1, const AutoBroadcastSpec &autob)
Constructs a binary elementwise arithmetic operation.
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16
Implicit broadcast specification.
Definition: attr_types.hpp:311