diff --git a/ngraph/core/include/ngraph/op/op.hpp b/ngraph/core/include/ngraph/op/op.hpp index 8e8ba1c011b9cf..4e3ce52957c84f 100644 --- a/ngraph/core/include/ngraph/op/op.hpp +++ b/ngraph/core/include/ngraph/op/op.hpp @@ -7,14 +7,10 @@ #include #include "ngraph/node.hpp" +#include "openvino/op/op.hpp" namespace ngraph { namespace op { -/// Root of all actual ops -class NGRAPH_API Op : public Node { -protected: - Op() : Node() {} - Op(const OutputVector& arguments); -}; +using ov::op::Op; } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/activation_functions.hpp b/ngraph/core/include/ngraph/op/util/activation_functions.hpp index b8285e643a311e..27e40e969df542 100644 --- a/ngraph/core/include/ngraph/op/util/activation_functions.hpp +++ b/ngraph/core/include/ngraph/op/util/activation_functions.hpp @@ -9,98 +9,25 @@ #include "ngraph/except.hpp" #include "ngraph/node.hpp" - -#ifdef _WIN32 -# pragma warning(push) - -# pragma warning(disable : 4100) -#endif - -// Prevents the compiler from complaining about or optimizing away variables -// that appear unused on Linux -#if (defined(__GNUC__) && !defined(__clang__)) -# undef NG_ATTRIBUTE_UNUSED -# define NG_ATTRIBUTE_UNUSED __attribute__((__unused__)) -#else -# define NG_ATTRIBUTE_UNUSED -#endif - -#define UNUSED_PARAMETER NG_ATTRIBUTE_UNUSED = 0 +#include "openvino/op/util/activation_functions.hpp" namespace ngraph { namespace op { namespace util { namespace error { -struct UnknownActivationFunction : ngraph_error { - UnknownActivationFunction(const std::string& func_name) - : ngraph_error{"Unknown activation function: " + func_name} {} -}; +using ov::op::util::error::UnknownActivationFunction; } // namespace error namespace detail { -std::shared_ptr sigmoid(const std::shared_ptr& arg, - float alpha UNUSED_PARAMETER, - float beta UNUSED_PARAMETER); -std::shared_ptr tanh(const std::shared_ptr& arg, float alpha UNUSED_PARAMETER, float beta UNUSED_PARAMETER); -std::shared_ptr relu(const std::shared_ptr& arg, float alpha UNUSED_PARAMETER, float beta UNUSED_PARAMETER); -std::shared_ptr hardsigmoid(const std::shared_ptr& arg, float alpha, float beta); +using ov::op::util::detail::hardsigmoid; +using ov::op::util::detail::relu; +using ov::op::util::detail::sigmoid; +using ov::op::util::detail::tanh; } // namespace detail -using ActivationFunctionType = std::shared_ptr (*)(const std::shared_ptr&, float, float); - -/// -/// \brief Class representing activation function used in RNN cells. -/// -class NGRAPH_API ActivationFunction { -public: - ActivationFunction(ActivationFunctionType f, float alpha, float beta); - ActivationFunction(ActivationFunctionType f, float alpha); - ActivationFunction(ActivationFunctionType f); - ActivationFunction() = default; - - /// - /// \brief Calls stored activation function with provided node argument. - /// - std::shared_ptr operator()(const std::shared_ptr& arg) const; - - void set_alpha(float alpha) { - m_alpha = alpha; - } - void set_beta(float beta) { - m_beta = beta; - } - -private: - /// \brief Activation function wrapper. - ActivationFunctionType m_function; - /// \brief Activation function alpha parameter (may be unused). - float m_alpha; - /// \brief Activation function beta parameter (may be unused). - float m_beta; -}; - -/// \brief Gets the activation function by name. -/// -/// \param[in] func_name The function name -/// -/// \throws UnknownActivationFunction When provided func_name is unknown. -/// -/// \return The activation function object. -/// -ActivationFunction get_activation_func_by_name(const std::string& func_name); +using ov::op::util::ActivationFunction; +using ov::op::util::ActivationFunctionType; +using ov::op::util::get_activation_func_by_name; } // namespace util - } // namespace op - } // namespace ngraph - -#ifdef _WIN32 -# pragma warning(pop) -#endif - -#ifdef UNUSED_PARAMETER -# undef UNUSED_PARAMETER -#endif -#ifdef NG_ATTRIBUTE_UNUSED -# undef NG_ATTRIBUTE_UNUSED -#endif diff --git a/ngraph/core/include/ngraph/op/util/arithmetic_reduction.hpp b/ngraph/core/include/ngraph/op/util/arithmetic_reduction.hpp index 483904ec339fdf..bbf18b0f8d925d 100644 --- a/ngraph/core/include/ngraph/op/util/arithmetic_reduction.hpp +++ b/ngraph/core/include/ngraph/op/util/arithmetic_reduction.hpp @@ -6,39 +6,12 @@ #include "ngraph/op/op.hpp" #include "ngraph/op/util/reduction_base.hpp" +#include "openvino/op/util/arithmetic_reduction.hpp" namespace ngraph { namespace op { namespace util { -/// \brief Abstract base class for arithmetic reduction operations, i.e., operations -/// where chosen axes of the input tensors are eliminated (reduced out) by -/// repeated application of a particular binary arithmetic operation. -class NGRAPH_API ArithmeticReduction : public ReductionBase { -protected: - /// \brief Constructs an arithmetic reduction operation. - ArithmeticReduction(); - - /// \brief Constructs an arithmetic reduction operation. - /// - /// \param arg Output that produces the first input tensor. - /// \param reduction_axes The axis positions (0-based) to be eliminated. - ArithmeticReduction(const Output& arg, const Output& reduction_axes); - -public: - NGRAPH_RTTI_DECLARATION; - void validate_and_infer_types() override; - - /// \return true if reduction axes are constant else false. - bool reduction_axes_constant() const; - - /// \return The axis positions (0-based) to be eliminated through reduction. - /// \throws CheckFailure if the reduction axes are not constant. (Use - /// reduction_axes_constant to check.) - const AxisSet get_reduction_axes() const; - - /// \brief Change the reduction axes - void set_reduction_axes(const AxisSet& reduction_axes); -}; +using ov::op::util::ArithmeticReduction; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/arithmetic_reductions_keep_dims.hpp b/ngraph/core/include/ngraph/op/util/arithmetic_reductions_keep_dims.hpp index ca60de06727e34..e320ba47ae3ee0 100644 --- a/ngraph/core/include/ngraph/op/util/arithmetic_reductions_keep_dims.hpp +++ b/ngraph/core/include/ngraph/op/util/arithmetic_reductions_keep_dims.hpp @@ -6,37 +6,12 @@ #include "ngraph/op/op.hpp" #include "ngraph/op/util/arithmetic_reduction.hpp" +#include "openvino/op/util/arithmetic_reductions_keep_dims.hpp" namespace ngraph { namespace op { namespace util { -class NGRAPH_API ArithmeticReductionKeepDims : public util::ArithmeticReduction { -protected: - ArithmeticReductionKeepDims() = default; - - /// \param arg The tensor to be summed. - /// \param reduction_axes The axis positions (0-based) to be eliminated. - /// \param keep_dims If set to 1 it holds axes that are used for reduction. - ArithmeticReductionKeepDims(const Output& arg, const Output& reduction_axes, bool keep_dims = false); - - bool visit_attributes(AttributeVisitor& visitor) override; - -public: - NGRAPH_RTTI_DECLARATION; - void validate_and_infer_types() override; - - /// \return If set to 1 it holds axes that are used for reduction. - /// For each such axis, output dimension is equal to 1. - bool get_keep_dims() const { - return m_keep_dims; - } - void set_keep_dims(bool keep_dims) { - m_keep_dims = keep_dims; - } - -private: - bool m_keep_dims = false; -}; +using ov::op::util::ArithmeticReductionKeepDims; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/attr_types.hpp b/ngraph/core/include/ngraph/op/util/attr_types.hpp index c14e996e8a5571..46bde99ba53874 100644 --- a/ngraph/core/include/ngraph/op/util/attr_types.hpp +++ b/ngraph/core/include/ngraph/op/util/attr_types.hpp @@ -10,332 +10,20 @@ #include "ngraph/attribute_adapter.hpp" #include "ngraph/ngraph_visibility.hpp" #include "ngraph/type.hpp" +#include "openvino/op/util/attr_types.hpp" namespace ngraph { namespace op { -/// \brief Modes for the `Pad` operator. -enum class PadMode { CONSTANT = 0, EDGE, REFLECT, SYMMETRIC }; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const PadMode& type); - -/// \brief Padding Type used for `Convolution` and `Pooling` -/// -/// Follows ONNX padding type definitions -/// EXPLICIT - Pad dimensions are explicity specified -/// SAME_LOWER - Pad dimensions computed to match input shape -/// Ceil(num_dims/2) at the beginning and -/// Floor(num_dims/2) at the end -/// SAME_UPPER - Pad dimensions computed to match input shape -/// Floor(num_dims/2) at the beginning and -/// Ceil(num_dims/2) at the end -/// VALID - No padding -/// AUTO - Deprecated. User should not use it in the future -/// NOTSET - Deprecated. User should not use it in the future - -enum class PadType { - EXPLICIT = 0, - SAME_LOWER, - SAME_UPPER, - VALID, - AUTO = SAME_UPPER, - NOTSET = EXPLICIT, -}; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const PadType& type); - -/// \brief Rounding Type used for `Pooling` operators. -enum class RoundingType { - FLOOR = 0, - CEIL = 1, -}; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const RoundingType& type); - -/// \brief Specifies the algorithm to use for implicit broadcasting of a tensor -/// to align with another tensor -/// -/// NONE - No implicit broadcasting of tensor -/// NUMPY - Numpy-style implicit broadcasting -/// (https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) -/// Right-align dimensions of the two tensors, with missing dimensions -/// treated as size 1 dimensions. After alignment, for each dimension, -/// their sizes should either match or one of them should be of size 1. -/// Size 1 dimension will be implicitly broadcast to match the other -/// size. -/// -/// E.g., -/// A: Shape(2, 1, 6) -/// B: Shape( 3, 1) -/// Result: Shape(2, 3, 6) -/// -/// A: Shape(2, 1, 6) -/// B: Shape( 3, 1) -/// Result: Shape(2, 3, 6) -/// PDPD - PaddlePaddle-style implicit broadcasting -/// (https://github.com/PaddlePaddle/Paddle/blob/release/1.5/paddle/ -/// fluid/operators/elementwise/elementwise_op.h#L126) -/// Broadcast B to match the shape of A, where axis is the start -/// dimension index to align B with A. If axis is -1 (default), i -/// axis = rank(A) - rank(B). The trailing dimensions of size 1 for B -/// will be ignored. -/// -/// E.g., -/// A: Shape(2, 3, 4, 5) -/// B: Shape( 3, 4 ) with axis =1 -/// Result: Shape(2, 3, 4, 5) -/// -/// A: Shape(2, 3, 4, 5) -/// B: Shape( 3, 1 ) with axis = 1 -/// Result: Shape(2, 3, 4, 5) -/// -/// TODO: Add more implicit broadcast modes used by frameworks -enum class AutoBroadcastType { - NONE = 0, - EXPLICIT = NONE, - NUMPY, - PDPD, -}; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const AutoBroadcastType& type); -/// \brief BroadcastType specifies rules used for mapping of input tensor axes to output -/// shape axes. -/// -/// \note Broadcasting rules are different for Broadcast op and for element-wise ops. -/// AutoBroadcastType::NUMPY is equivalent of BroadcastType::BIDIRECTIONAL -/// according to spec. -/// -/// EXPLICIT - Mapping of the input data shape to output shape -/// based on axes_mapping input. -/// NUMPY - Numpy broadcasting rules, aligned with ONNX Broadcasting. -/// (https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md) -/// PDPD - PaddlePaddle-style implicit broadcasting. -/// For more informaction see AutoBroadcastType documentation. -/// BIDIRECTIONAL - The broadcast rule is similar to -/// numpy.array(input) * numpy.ones(target_shape). -/// Dimensions are right alignment. -enum class BroadcastType { NONE, EXPLICIT = NONE, NUMPY, PDPD, BIDIRECTIONAL }; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const BroadcastType& type); - -/// \brief Specifies how eps is combined with L2 value -enum class EpsMode { - // Add bias to norm - ADD, - // Calculate max of norm and bias - MAX -}; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const EpsMode& type); - -enum class TopKSortType { - // Returned values are not sorte - NONE, - // Sort result based on element indices - SORT_INDICES, - // Sort result based on element values - SORT_VALUES, -}; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const TopKSortType& type); - -enum class TopKMode { - MAX, - MIN, -}; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const TopKMode& type); - -/// \brief Implicit broadcast specification -struct NGRAPH_API AutoBroadcastSpec { - AutoBroadcastSpec() : m_type(AutoBroadcastType::NONE), m_axis(0) {} - AutoBroadcastSpec(AutoBroadcastType type) : m_type(type), m_axis(0) {} - AutoBroadcastSpec(const char* type) : AutoBroadcastSpec(type_from_string(type)) {} - AutoBroadcastSpec(AutoBroadcastType type, int64_t axis) : m_type(type), m_axis(axis) {} - - AutoBroadcastType m_type; // Implicit broadcasting algorithm - int64_t m_axis; // Axis to start alignment on - - bool operator==(const AutoBroadcastSpec& a) const { - return a.m_type == m_type && a.m_axis == m_axis; - } - - bool operator!=(const AutoBroadcastSpec& a) const { - return !(*this == a); - } - static const AutoBroadcastSpec NUMPY; - static const AutoBroadcastSpec NONE; - -private: - AutoBroadcastType type_from_string(const std::string& type) const; -}; - -/// \brief Implicit broadcast specification -struct NGRAPH_API BroadcastModeSpec { - BroadcastModeSpec() : m_type(BroadcastType::NUMPY), m_axis(0) {} - BroadcastModeSpec(BroadcastType type) : m_type(type), m_axis(0) {} - BroadcastModeSpec(const char* type) : BroadcastModeSpec(as_enum(type)) {} - BroadcastModeSpec(BroadcastType type, int64_t axis) : m_type(type), m_axis(axis) {} - - BroadcastType m_type; // Implicit broadcasting algorithm - int64_t m_axis; // Axis to start alignment on - - bool operator==(const BroadcastModeSpec& a) const { - return a.m_type == m_type && a.m_axis == m_axis; - } -}; - -/// -/// \brief This class defines possible recurrent sequence directions. -/// -enum class RecurrentSequenceDirection { FORWARD, REVERSE, BIDIRECTIONAL }; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const RecurrentSequenceDirection& direction); +using ov::op::AutoBroadcastSpec; +using ov::op::AutoBroadcastType; +using ov::op::BroadcastModeSpec; +using ov::op::BroadcastType; +using ov::op::EpsMode; +using ov::op::PadMode; +using ov::op::PadType; +using ov::op::RecurrentSequenceDirection; +using ov::op::RoundingType; +using ov::op::TopKMode; +using ov::op::TopKSortType; } // namespace op } // namespace ngraph - -namespace ov { -template <> -class NGRAPH_API AttributeAdapter : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::PadMode& value) : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -template <> -class NGRAPH_API AttributeAdapter : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::PadType& value) : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -template <> -class NGRAPH_API AttributeAdapter - : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::RoundingType& value) : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -template <> -class NGRAPH_API AttributeAdapter - : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::AutoBroadcastType& value) - : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -template <> -class NGRAPH_API AttributeAdapter - : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::BroadcastType& value) : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -template <> -class NGRAPH_API AttributeAdapter : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::EpsMode& value) : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -template <> -class NGRAPH_API AttributeAdapter - : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::TopKSortType& value) : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -template <> -class NGRAPH_API AttributeAdapter : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::TopKMode& value) : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 1}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -template <> -class AttributeAdapter : public VisitorAdapter { -public: - AttributeAdapter(ngraph::op::AutoBroadcastSpec& value) : m_ref(value) {} - bool visit_attributes(AttributeVisitor& visitor) override; - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } - -protected: - ngraph::op::AutoBroadcastSpec& m_ref; -}; - -template <> -class AttributeAdapter : public VisitorAdapter { -public: - AttributeAdapter(ngraph::op::BroadcastModeSpec& value) : m_ref(value) {} - bool visit_attributes(AttributeVisitor& visitor) override; - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } - -protected: - ngraph::op::BroadcastModeSpec& m_ref; -}; - -template <> -class NGRAPH_API AttributeAdapter - : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::RecurrentSequenceDirection& value) - : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 1}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; -} // namespace ov diff --git a/ngraph/core/include/ngraph/op/util/binary_elementwise_arithmetic.hpp b/ngraph/core/include/ngraph/op/util/binary_elementwise_arithmetic.hpp index f1ff6b1e9cb977..91f3262756133e 100644 --- a/ngraph/core/include/ngraph/op/util/binary_elementwise_arithmetic.hpp +++ b/ngraph/core/include/ngraph/op/util/binary_elementwise_arithmetic.hpp @@ -6,65 +6,12 @@ #include "ngraph/op/op.hpp" #include "ngraph/op/util/attr_types.hpp" +#include "openvino/op/util/binary_elementwise_arithmetic.hpp" namespace ngraph { namespace op { namespace util { -// clang-format off - /// \brief Abstract base class for elementwise binary arithmetic operations, i.e., - /// operations where the same scalar binary arithmetic operation is applied to - /// each corresponding pair of elements in the two input tensors. Implicit - /// broadcast of input tensors is supported through one of the AutoBroadcast - /// modes. - /// - /// For example, if the underlying arithmetic operation (determined by the subclass) is - /// \f$\mathit{op}(x,y)\f$, the input tensors - /// \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 - /// \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$. - /// - /// ## Inputs - /// - /// | | Type | Description | - /// | ------ | --------------------------------- | ------------------------------------------------------------------------ | - /// | `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. | - /// | `arg1` | \f$N[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of the same element type as `arg0`. | - /// | `autob`| AutoBroadcastSpec | Auto broadcast specification. | - /// - /// ## Output - /// - /// | Type | Description | - /// | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | - /// | \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). | -// clang-format on -class NGRAPH_API BinaryElementwiseArithmetic : public Op { -protected: - BinaryElementwiseArithmetic(const AutoBroadcastSpec& autob); - - /// \brief Constructs a binary elementwise arithmetic operation. - /// - /// \param arg0 Output that produces the first input tensor. - /// \param arg1 Output that produces the second input tensor. - BinaryElementwiseArithmetic(const Output& arg0, const Output& arg1, const AutoBroadcastSpec& autob); - -public: - NGRAPH_RTTI_DECLARATION; - - void validate_and_infer_types() override; - - const AutoBroadcastSpec& get_autob() const override { - return m_autob; - } - void set_autob(const AutoBroadcastSpec& autob) { - m_autob = autob; - } - bool visit_attributes(AttributeVisitor& visitor) override; - bool evaluate_lower(const HostTensorVector& outputs) const override; - bool evaluate_upper(const HostTensorVector& outputs) const override; - -private: - AutoBroadcastSpec m_autob; - void validate_and_infer_elementwise_arithmetic(const op::AutoBroadcastSpec& autob); -}; +using ov::op::util::BinaryElementwiseArithmetic; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/binary_elementwise_comparison.hpp b/ngraph/core/include/ngraph/op/util/binary_elementwise_comparison.hpp index 2da954b649dac5..57470ebca4ceef 100644 --- a/ngraph/core/include/ngraph/op/util/binary_elementwise_comparison.hpp +++ b/ngraph/core/include/ngraph/op/util/binary_elementwise_comparison.hpp @@ -6,66 +6,12 @@ #include "ngraph/op/op.hpp" #include "ngraph/op/util/attr_types.hpp" +#include "openvino/op/util/binary_elementwise_comparison.hpp" namespace ngraph { namespace op { namespace util { -// clang-format off - /// \brief Abstract base class for elementwise binary comparison operations, i.e., - /// operations where the same scalar binary comparison operation is applied to - /// each corresponding pair of elements in two input tensors. Implicit - /// broadcast of input tensors is supported through one of the AutoBroadcast - /// modes. - /// - /// For example, if the underlying comparison operation (determined by the subclass) is - /// \f$\mathit{op}(x,y)\f$, the input tensors \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 - /// \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$. - /// - /// ## Inputs - /// - /// | | Type | Description | - /// | ------ | --------------------------------- | ------------------------------------------------------ | - /// | `arg0` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of any shape and element type. | - /// | `arg1` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of the same shape and element type as `arg0`. | - /// | `autob`| AutoBroadcastSpec | Auto broadcast specification. | - /// - /// ## Output - /// - /// | Type | Description | - /// | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | - /// | \f$\texttt{bool}[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 as the input tensors, and the element type `bool`. | -// clang-format on -class NGRAPH_API BinaryElementwiseComparison : public Op { -protected: - /// \brief Constructs a binary elementwise comparison operation. - BinaryElementwiseComparison(const AutoBroadcastSpec& autob); - - /// \brief Constructs a binary elementwise comparison operation. - /// - /// \param arg0 Output that produces the first input tensor. - /// \param arg1 Output that produces the second input tensor. - /// \param autob AutoBroadcast mode. - BinaryElementwiseComparison(const Output& arg0, - const Output& arg1, - const AutoBroadcastSpec& autob = AutoBroadcastSpec()); - -public: - NGRAPH_RTTI_DECLARATION; - - void validate_and_infer_types() override; - - const AutoBroadcastSpec& get_autob() const override { - return m_autob; - } - void set_autob(const AutoBroadcastSpec& autob) { - m_autob = autob; - } - bool visit_attributes(AttributeVisitor& visitor) override; - -private: - AutoBroadcastSpec m_autob; -}; +using ov::op::util::BinaryElementwiseComparison; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/binary_elementwise_logical.hpp b/ngraph/core/include/ngraph/op/util/binary_elementwise_logical.hpp index b48d8f69a1f509..20a8dc76699ce4 100644 --- a/ngraph/core/include/ngraph/op/util/binary_elementwise_logical.hpp +++ b/ngraph/core/include/ngraph/op/util/binary_elementwise_logical.hpp @@ -5,64 +5,12 @@ #pragma once #include "ngraph/op/op.hpp" +#include "openvino/op/util/binary_elementwise_logical.hpp" namespace ngraph { namespace op { namespace util { -// clang-format off - /// \brief Abstract base class for elementwise binary logical operations, i.e., - /// operations where the same scalar binary logical operation is applied to - /// each corresponding pair of elements in two boolean input tensors. Implicit - /// broadcast of input tensors is supported through one of the AutoBroadcast - /// modes. - /// - /// For example, if the underlying operation (determined by the subclass) is - /// \f$\mathit{op}(x,y)\f$, the input tensors \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 - /// \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$. - /// - /// ## Inputs - /// - /// | | Type | Description | - /// | ------ | --------------------------------------------- | ------------------------------------------------------ | - /// | `arg0` | \f$\texttt{bool}[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of any shape, with element type `bool`. | - /// | `arg1` | \f$\texttt{bool}[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of the same shape and element type as `arg0`. | - /// | `autob`| AutoBroadcastSpec | Auto broadcast specification. | - /// - /// ## Output - /// - /// | Type | Description | - /// | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | - /// | \f$\texttt{bool}[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 as the input tensors, and the element type `bool`. | -// clang-format on -class NGRAPH_API BinaryElementwiseLogical : public Op { -protected: - NGRAPH_RTTI_DECLARATION; - - BinaryElementwiseLogical(); - - /// \brief Constructs a binary elementwise logical operation. - /// - /// \param arg0 Output that produces the first input tensor. - /// \param arg1 Output that produces the second input tensor. - BinaryElementwiseLogical(const Output& arg0, - const Output& arg1, - const AutoBroadcastSpec& autob = AutoBroadcastSpec()); - -public: - void validate_and_infer_types() override; - - const AutoBroadcastSpec& get_autob() const override { - return m_autob; - } - void set_autob(const AutoBroadcastSpec& autob) { - m_autob = autob; - } - bool visit_attributes(AttributeVisitor& visitor) override; - -private: - AutoBroadcastSpec m_autob = AutoBroadcastSpec::NUMPY; -}; +using ov::op::util::BinaryElementwiseLogical; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/broadcast_base.hpp b/ngraph/core/include/ngraph/op/util/broadcast_base.hpp index c9af35af715eba..12dfb6833feda0 100644 --- a/ngraph/core/include/ngraph/op/util/broadcast_base.hpp +++ b/ngraph/core/include/ngraph/op/util/broadcast_base.hpp @@ -2,84 +2,18 @@ // SPDX-License-Identifier: Apache-2.0 // +#pragma once + #include "ngraph/axis_set.hpp" #include "ngraph/axis_vector.hpp" #include "ngraph/op/op.hpp" #include "ngraph/op/util/attr_types.hpp" - -#pragma once +#include "openvino/op/util/broadcast_base.hpp" namespace ngraph { namespace op { namespace util { -class NGRAPH_API BroadcastBase : public Op { -protected: - BroadcastBase() = default; - /// \brief Constructs a broadcast operation. - /// - /// \param arg The input tensor to be broadcast. - /// \param target_shape The shape of the output tensor. - /// \param axes_mapping The axis positions (0-based) in the result that correspond - /// to input axes. - /// \param broadcast_mode Broadcast specification to use for determining broadcast - /// axes. 'axes_mapping' should not be provided if mode other - /// - BroadcastBase(const Output& arg, - const Output& target_shape, - const Output& axes_mapping, - const BroadcastModeSpec& broadcast_mode = BroadcastType::EXPLICIT); - - /// \brief Constructs a broadcast operation. - /// - /// \param arg The input tensor to be broadcast. - /// \param target_shape The shape of the output tensor. - /// \param broadcast_mode Broadcast specification to use for determining broadcast - /// axes - BroadcastBase(const Output& arg, - const Output& target_shape, - const BroadcastModeSpec& broadcast_mode = BroadcastType::NUMPY); - -public: - NGRAPH_RTTI_DECLARATION; - - void validate_and_infer_types() override; - /// \return true and the AxisSet if broadcast axes can be fully determined. - virtual std::pair get_broadcast_axes() const; - - bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override; - -protected: - BroadcastModeSpec m_mode; - - bool evaluate_broadcast(const HostTensorPtr& arg0, - const HostTensorPtr& out, - const std::pair pair_broadcast_axes, - const Shape output_shape) const; - - bool evaluate_broadcast(const HostTensorPtr& arg0, const HostTensorPtr& out, const AxisSet& broadcast_axes) const; - - bool evaluate_lower(const HostTensorVector& outputs) const override; - bool evaluate_upper(const HostTensorVector& outputs) const override; - - PartialShape get_result_shape_pdpd(const PartialShape& arg0_shape, - const PartialShape& target_shape, - const op::BroadcastModeSpec& broadcast_spec) const; - - void validate_target_shape_numpy(const PartialShape& arg_shape, const PartialShape& target_shape) const; - - static std::pair get_broadcast_axes_numpy_pdpd(const Shape& arg_shape, - const Shape& result_shape, - const op::BroadcastModeSpec& broadcast_spec); - - static std::pair get_broadcast_axes_none(const AxisVector axes_mapping_val, - const size_t target_shape); - - void validate_target_shape_none(const PartialShape& arg_shape, - const AxisVector& axes_mapping_val, - const PartialShape& target_shape) const; - - Shape get_target_shape(const HostTensorPtr& input1) const; -}; +using ov::op::util::BroadcastBase; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/deformable_convolution_base.hpp b/ngraph/core/include/ngraph/op/util/deformable_convolution_base.hpp index dd8ed971089a13..09f6314071a9c0 100644 --- a/ngraph/core/include/ngraph/op/util/deformable_convolution_base.hpp +++ b/ngraph/core/include/ngraph/op/util/deformable_convolution_base.hpp @@ -7,99 +7,12 @@ #include "ngraph/coordinate_diff.hpp" #include "ngraph/op/op.hpp" #include "ngraph/op/util/attr_types.hpp" +#include "openvino/op/util/deformable_convolution_base.hpp" namespace ngraph { namespace op { namespace util { -/// \brief Base class for operations DeformableConvolution v1 and DeformableConvolution -/// v8. -class NGRAPH_API DeformableConvolutionBase : public Op { -public: - NGRAPH_RTTI_DECLARATION; - - /// \brief Constructs a conversion operation. - DeformableConvolutionBase() = default; - - /// \brief Constructs a conversion operation. - /// \param strides Convolution strides. - /// \param pads_begin Amount of padding to be added to the beginning along - /// each axis. For example in case of a 2D input the value - /// of (1, 2) means that 1 element will be added to the - /// top and 2 elements to the left. - /// \param pads_end Amount of padding to be added to the end along each - /// axis. - /// \param dilations The distance in width and height between the weights - /// in the filters tensor. - /// \param auto_pad Specifies how the automatic calculation of padding - /// should be done. - /// \param group The number of groups which both output and input - /// should be split into. - /// \param deformable_group The number of groups which deformable values and - /// output should be split into along the channel axis. - DeformableConvolutionBase(const OutputVector& arguments, - const Strides& strides, - const CoordinateDiff& pads_begin, - const CoordinateDiff& pads_end, - const Strides& dilations, - const PadType& auto_pad = PadType::EXPLICIT, - int64_t group = 1, - int64_t deformable_group = 1); - - bool visit_attributes(AttributeVisitor& visitor) override; - void validate_and_infer_types() override; - - const Strides& get_strides() const { - return m_strides; - } - void set_strides(const Strides& strides) { - m_strides = strides; - } - const Strides& get_dilations() const { - return m_dilations; - } - void set_dilations(const Strides& dilations) { - m_dilations = dilations; - } - const CoordinateDiff& get_pads_begin() const { - return m_pads_begin; - } - void set_pads_begin(const CoordinateDiff& pads_begin) { - m_pads_begin = pads_begin; - } - const CoordinateDiff& get_pads_end() const { - return m_pads_end; - } - void set_pads_end(const CoordinateDiff& pads_end) { - m_pads_end = pads_end; - } - const PadType& get_auto_pad() const { - return m_auto_pad; - } - void set_auto_pad(const PadType& auto_pad) { - m_auto_pad = auto_pad; - } - int64_t get_group() const { - return m_group; - } - void set_group(const int64_t group) { - m_group = group; - } - int64_t get_deformable_group() const { - return m_deformable_group; - } - void set_deformable_group(const int64_t deformable_group) { - m_deformable_group = deformable_group; - } - -protected: - Strides m_strides; - Strides m_dilations; - CoordinateDiff m_pads_begin; - CoordinateDiff m_pads_end; - PadType m_auto_pad; - int64_t m_group; - int64_t m_deformable_group; -}; +using ov::op::util::DeformableConvolutionBase; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/elementwise_args.hpp b/ngraph/core/include/ngraph/op/util/elementwise_args.hpp index 6d87523827d3db..faf9ce154d2e11 100644 --- a/ngraph/core/include/ngraph/op/util/elementwise_args.hpp +++ b/ngraph/core/include/ngraph/op/util/elementwise_args.hpp @@ -5,13 +5,12 @@ #pragma once #include "ngraph/node.hpp" +#include "openvino/op/util/elementwise_args.hpp" namespace ngraph { namespace op { namespace util { -std::tuple validate_and_infer_elementwise_args( - Node* node, - const op::AutoBroadcastSpec& autob = op::AutoBroadcastSpec()); +using ov::op::util::validate_and_infer_elementwise_args; } } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/embeddingbag_offsets_base.hpp b/ngraph/core/include/ngraph/op/util/embeddingbag_offsets_base.hpp index 40ca4d3f8f9b01..28005440fedde6 100644 --- a/ngraph/core/include/ngraph/op/util/embeddingbag_offsets_base.hpp +++ b/ngraph/core/include/ngraph/op/util/embeddingbag_offsets_base.hpp @@ -6,61 +6,12 @@ #include "ngraph/axis_set.hpp" #include "ngraph/op/util/index_reduction.hpp" +#include "openvino/op/util/embeddingbag_offsets_base.hpp" namespace ngraph { namespace op { namespace util { -/// \brief Returns embeddings for given indices -class NGRAPH_API EmbeddingBagOffsetsBase : public Op { -public: - static constexpr NodeTypeInfo type_info{"EmbeddingBagOffsetsBase", 3}; - const NodeTypeInfo& get_type_info() const override { - return type_info; - } - /// \brief Constructs a EmbeddingBagOffsetsBase operation. - EmbeddingBagOffsetsBase() = default; - /// \brief Constructs a EmbeddingBagOffsetsBase operation. - /// - /// EmbeddingBagOffsetsBase constructs an output tensor by replacing every index in - /// a - /// given - /// input tensor with a row (from the weights matrix) at that index - /// - /// \param emb_table tensor containing the embedding lookup table of the module of - /// shape [num_emb, emb_dim1, emb_dim2, ...] and of type T - /// \param tensor of shape [num_indices] and of type T_IND. Required - /// \param offsets tensor of shape [batch] and of type T_IND containing the starting - /// index positions of each "bag" in indices. Required. - /// \param per_sample_weigths tensor of the same shape as indices and of type T. - /// Each value in this tensor are multiplied with each - /// value pooled from embedding table for each index. Optional. - /// \param default_index scalar of type T_IND containing default index in embedding - /// table to fill empty "bags". If not provided empty "bags" - /// are filled with zeros. Optional. - - EmbeddingBagOffsetsBase(const Output& emb_table, - const Output& indices, - const Output& offsets, - const Output& default_index, - const Output& per_sample_weights); - - EmbeddingBagOffsetsBase(const Output& emb_table, - const Output& indices, - const Output& offsets, - const Output& default_index); - - EmbeddingBagOffsetsBase(const Output& emb_table, const Output& indices, const Output& offsets); - - void validate_and_infer_types() override; - bool visit_attributes(AttributeVisitor& visitor) override; - -private: - static constexpr int EMB_TABLE = 0; - static constexpr int INDICES = 1; - static constexpr int OFFSETS = 2; - static constexpr int DEFAULT_INDEX = 3; - static constexpr int PER_SAMPLE_WEIGHTS = 4; -}; +using ov::op::util::EmbeddingBagOffsetsBase; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/embeddingbag_packed_base.hpp b/ngraph/core/include/ngraph/op/util/embeddingbag_packed_base.hpp index 75409bc27f1115..b6891bd075d37a 100644 --- a/ngraph/core/include/ngraph/op/util/embeddingbag_packed_base.hpp +++ b/ngraph/core/include/ngraph/op/util/embeddingbag_packed_base.hpp @@ -6,47 +6,12 @@ #include "ngraph/axis_set.hpp" #include "ngraph/op/util/index_reduction.hpp" +#include "openvino/op/util/embeddingbag_packed_base.hpp" namespace ngraph { namespace op { namespace util { -/// \brief Returns embeddings for given indices -class NGRAPH_API EmbeddingBagPackedBase : public Op { -public: - static constexpr NodeTypeInfo type_info{"EmbeddingBagPackedBase", 3}; - const NodeTypeInfo& get_type_info() const override { - return type_info; - } - /// \brief Constructs a EmbeddingBagPackedBase operation. - EmbeddingBagPackedBase() = default; - /// \brief Constructs a EmbeddingBagPackedBase operation. - /// - /// EmbeddingBagPackedBase constructs an output tensor by replacing every index in a - /// given - /// input tensor with a row (from the weights matrix) at that index - /// - /// \param emb_table Tensor containing the embedding lookup table of the module of - /// shape [num_emb, emb_dim1, emb_dim2, ...] and of type T - /// \param indices Tensor of shape `[batch, indices_per_bag]` and of type *T_IND*. - /// Required. - /// \param per_sample_weigths tensor of the same shape as indices and of type T. - /// Each value in this tensor are multiplied with each - /// value pooled from embedding table for each index. Optional. - - EmbeddingBagPackedBase(const Output& emb_table, - const Output& indices, - const Output& per_sample_weights); - - EmbeddingBagPackedBase(const Output& emb_table, const Output& indices); - - void validate_and_infer_types() override; - bool visit_attributes(AttributeVisitor& visitor) override; - -private: - static constexpr int EMB_TABLE = 0; - static constexpr int INDICES = 1; - static constexpr int PER_SAMPLE_WEIGHTS = 2; -}; +using ov::op::util::EmbeddingBagPackedBase; } // namespace util using util::EmbeddingBagPackedBase; } // namespace op diff --git a/ngraph/core/include/ngraph/op/util/fft_base.hpp b/ngraph/core/include/ngraph/op/util/fft_base.hpp index c56302161cc0f0..03692ba624eff0 100644 --- a/ngraph/core/include/ngraph/op/util/fft_base.hpp +++ b/ngraph/core/include/ngraph/op/util/fft_base.hpp @@ -6,35 +6,12 @@ #include "ngraph/op/op.hpp" #include "ngraph/op/util/attr_types.hpp" +#include "openvino/op/util/fft_base.hpp" namespace ngraph { namespace op { namespace util { -/// \brief Base class for operations DFT and DFT. -class NGRAPH_API FFTBase : public Op { -public: - NGRAPH_RTTI_DECLARATION; - FFTBase() = default; - - void validate_and_infer_types() override; - bool visit_attributes(AttributeVisitor& visitor) override; - -protected: - /// \brief Constructs an FFT operation. FFT is performed for full size axes. - /// - /// \param data Input data - /// \param axes Axes to perform FFT - FFTBase(const Output& data, const Output& axes); - - /// \brief Constructs a FFT operation. - /// - /// \param data Input data - /// \param axes Axes to perform FFT - /// \param signal_size Signal sizes for 'axes' - FFTBase(const Output& data, const Output& axes, const Output& signal_size); - - void validate(); -}; +using ov::op::util::FFTBase; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/gather_base.hpp b/ngraph/core/include/ngraph/op/util/gather_base.hpp index b4f10630297374..9d6ce9bb44fcd8 100644 --- a/ngraph/core/include/ngraph/op/util/gather_base.hpp +++ b/ngraph/core/include/ngraph/op/util/gather_base.hpp @@ -5,38 +5,12 @@ #pragma once #include "ngraph/op/op.hpp" +#include "openvino/op/util/gather_base.hpp" namespace ngraph { namespace op { namespace util { -/// \brief GatherBase basic class for Gather v1 and v7 -class NGRAPH_API GatherBase : public Op { -public: - NGRAPH_RTTI_DECLARATION; - GatherBase() = default; - - /// \param data The tensor from which slices are gathered - /// \param indices Tensor with indexes to gather - /// \param axis The tensor is a dimension index to gather data from - /// \param batch_dims The number of batch dimension in data and indices tensors - GatherBase(const Output& data, - const Output& indices, - const Output& axis, - const int64_t batch_dims = 0); - - void validate_and_infer_types() override; - virtual int64_t get_axis() const; - - bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override; - - bool evaluate_lower(const HostTensorVector& outputs) const override; - bool evaluate_upper(const HostTensorVector& outputs) const override; - - bool constant_fold(OutputVector& output_values, const OutputVector& inputs_values) override; - -protected: - int64_t m_batch_dims = 0; -}; +using ov::op::util::GatherBase; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/index_reduction.hpp b/ngraph/core/include/ngraph/op/util/index_reduction.hpp index e34f49bf11a0e5..2e86e4400973e3 100644 --- a/ngraph/core/include/ngraph/op/util/index_reduction.hpp +++ b/ngraph/core/include/ngraph/op/util/index_reduction.hpp @@ -10,28 +10,12 @@ #include #include "ngraph/op/op.hpp" +#include "openvino/op/util/index_reduction.hpp" namespace ngraph { namespace op { namespace util { -class NGRAPH_API IndexReduction : public Op { -protected: - IndexReduction(); - - IndexReduction(const Output& arg, uint64_t axis, const element::Type& index_element_type); - -public: - uint64_t get_reduction_axis() const; - void set_reduction_axis(uint64_t value); - element::Type get_index_element_type() const; - void set_index_element_type(const element::Type& index_element_type); - void validate_and_infer_types() override; - bool visit_attributes(AttributeVisitor& visitor) override; - -protected: - uint64_t m_axis{0}; - element::Type m_index_element_type; -}; +using ov::op::util::IndexReduction; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/logical_reduction.hpp b/ngraph/core/include/ngraph/op/util/logical_reduction.hpp index f9fc564a3095ea..c84764199b7e24 100644 --- a/ngraph/core/include/ngraph/op/util/logical_reduction.hpp +++ b/ngraph/core/include/ngraph/op/util/logical_reduction.hpp @@ -6,41 +6,12 @@ #include "ngraph/op/op.hpp" #include "ngraph/op/util/reduction_base.hpp" +#include "openvino/op/util/logical_reduction.hpp" namespace ngraph { namespace op { namespace util { -/// \brief Abstract base class for logical reduction operations, i.e., operations where -/// chosen axes of the input tensors are eliminated (reduced out) by repeated -/// application of a particular binary logical operation. -class NGRAPH_API LogicalReduction : public ReductionBase { -protected: - /// \brief Constructs a logical reduction operation. - LogicalReduction(); - /// \brief Constructs a logical reduction operation. - /// - /// \param arg Output that produces the first input tensor. - /// \param reduction_axes The axis positions (0-based) to be eliminated. - LogicalReduction(const Output& arg, const AxisSet& reduction_axes); - /// \brief Constructs a 'dynamic' logical reduction operation. - /// - /// \param arg Node that produces the first input tensor. - /// \param reduction_axes The axis positions (0-based) to be eliminated. - LogicalReduction(const Output& arg, const Output& reduction_axes); - -public: - NGRAPH_RTTI_DECLARATION; - void validate_and_infer_types() override; - - /// \return true if reduction axes are constant else false. - bool reduction_axes_constant() const; - - /// \return The axis positions (0-based) to be eliminated through reduction. - /// \throws CheckFailure if the reduction axes are not constant. (Use - /// reduction_axes_constant to check.) - const AxisSet get_reduction_axes() const; - void set_reduction_axes(const AxisSet& reduction_axes); -}; +using ov::op::util::LogicalReduction; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/logical_reduction_keep_dims.hpp b/ngraph/core/include/ngraph/op/util/logical_reduction_keep_dims.hpp index edbd9a6254be6a..deab4fe1b22eeb 100644 --- a/ngraph/core/include/ngraph/op/util/logical_reduction_keep_dims.hpp +++ b/ngraph/core/include/ngraph/op/util/logical_reduction_keep_dims.hpp @@ -6,37 +6,12 @@ #include "ngraph/op/op.hpp" #include "ngraph/op/util/logical_reduction.hpp" +#include "openvino/op/util/logical_reduction_keep_dims.hpp" namespace ngraph { namespace op { namespace util { -class NGRAPH_API LogicalReductionKeepDims : public util::LogicalReduction { -protected: - LogicalReductionKeepDims() = default; - - /// \param arg The tensor to be reduced. - /// \param reduction_axes The axis positions (0-based) to be eliminated. - /// \param keep_dims If set to 1 it holds axes that are used for reduction. - LogicalReductionKeepDims(const Output& arg, const Output& reduction_axes, const bool keep_dims = false); - - bool visit_attributes(AttributeVisitor& visitor) override; - -public: - NGRAPH_RTTI_DECLARATION; - void validate_and_infer_types() override; - - /// \return If set to 1 it holds axes that are used for reduction. - /// For each such axis, output dimension is equal to 1. - bool get_keep_dims() const { - return m_keep_dims; - } - void set_keep_dims(bool keep_dims) { - m_keep_dims = keep_dims; - } - -private: - bool m_keep_dims = false; -}; +using ov::op::util::LogicalReductionKeepDims; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/max_pool_base.hpp b/ngraph/core/include/ngraph/op/util/max_pool_base.hpp index 48a42c7cf8843d..e049033e166b8d 100644 --- a/ngraph/core/include/ngraph/op/util/max_pool_base.hpp +++ b/ngraph/core/include/ngraph/op/util/max_pool_base.hpp @@ -6,91 +6,12 @@ #include "ngraph/op/op.hpp" #include "ngraph/op/util/attr_types.hpp" +#include "openvino/op/util/max_pool_base.hpp" namespace ngraph { namespace op { namespace util { -class NGRAPH_API MaxPoolBase : public Op { -public: - NGRAPH_RTTI_DECLARATION; - MaxPoolBase() = default; - - /// \param arg The node producing the input data batch tensor. - /// \param strides The strides. - /// \param pads_begin The beginning of padding shape. - /// \param pads_end The end of padding shape. - /// \param kernel The kernel shape. - /// \param rounding_mode Whether to use ceiling or floor rounding type while - /// computing output shape. - /// \param auto_pad The pad type for automatically computing padding sizes. - MaxPoolBase(const Output& arg, - const Strides& strides, - const Shape& pads_begin, - const Shape& pads_end, - const Shape& kernel, - const op::RoundingType rounding_mode = op::RoundingType::FLOOR, - const PadType auto_pad = op::PadType::EXPLICIT); - - void validate_and_infer_types() override; - - /// \return The kernel shape. - const Shape& get_kernel() const { - return m_kernel; - } - void set_kernel(const Shape& kernel) { - m_kernel = kernel; - } - /// \return The strides. - const Strides& get_strides() const { - return m_strides; - } - void set_strides(const Strides& strides) { - m_strides = strides; - } - /// \return The beginning of padding shape. - const Shape& get_pads_begin() const { - return m_pads_begin; - } - void set_pads_begin(const Shape& pads_begin) { - m_pads_begin = pads_begin; - } - /// \return The end of padding shape. - const Shape& get_pads_end() const { - return m_pads_end; - } - void set_adding_above(const Shape& pads_end) { - m_pads_end = pads_end; - } - /// \return The pad type for pooling. - PadType get_auto_pad() const { - return m_auto_pad; - } - void set_auto_pad(const PadType auto_pad) { - m_auto_pad = auto_pad; - } - /// \return The ceiling mode being used for output shape computations - op::RoundingType get_rounding_type() const { - return m_rounding_type; - } - void set_rounding_type(op::RoundingType rounding_type) { - m_rounding_type = rounding_type; - } - -protected: - bool update_auto_padding(const PartialShape& in_shape, - const Strides& filter_dilations, - Shape& new_pads_end, - Shape& new_pads_begin) const; - - PartialShape infer_output_shape(const Strides& dilations); - - Shape m_kernel; - Strides m_strides; - Shape m_pads_begin; - Shape m_pads_end; - PadType m_auto_pad; - op::RoundingType m_rounding_type; -}; +using ov::op::util::MaxPoolBase; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/multi_subgraph_base.hpp b/ngraph/core/include/ngraph/op/util/multi_subgraph_base.hpp index 16919a8a21b5f0..17571911213304 100644 --- a/ngraph/core/include/ngraph/op/util/multi_subgraph_base.hpp +++ b/ngraph/core/include/ngraph/op/util/multi_subgraph_base.hpp @@ -8,317 +8,16 @@ #include #include "ngraph/op/op.hpp" +#include "openvino/op/util/multi_subgraph_base.hpp" namespace ngraph { namespace op { namespace util { -/// \brief Abstract base class for sub-graph based ops, i.e ops that have some -/// sub-graphs -/// -class NGRAPH_API MultiSubGraphOp : public Op { -public: - NGRAPH_RTTI_DECLARATION; - /// \brief Abstract class describes a connection between a MultiSubGraphOp input and - /// the body. - class InputDescription { - protected: - /// - /// \brief Constructs a new instance. - /// - /// \param input_index Position of the MultiSubGraphOp input - /// \param body_parameter_index Body parameter to receive input - /// - InputDescription(uint64_t input_index, uint64_t body_parameter_index); - InputDescription() = default; - - public: - using type_info_t = DiscreteTypeInfo; - virtual ~InputDescription() = default; - virtual std::shared_ptr copy() const = 0; - - virtual const type_info_t& get_type_info() const = 0; - - uint64_t m_input_index{0}; - uint64_t m_body_parameter_index{0}; - }; - - /// \brief Abstract class describes how a MultiSubGraphOp output is produced from - /// the body. - class OutputDescription { - protected: - /// - /// \brief Constructs a new instance. - /// - /// \param body_value_index A body value that produces the output - /// \param output_index The MultiSubGraphOp output index - /// - OutputDescription(uint64_t body_value_index, uint64_t output_index); - OutputDescription() = default; - - public: - using type_info_t = DiscreteTypeInfo; - virtual ~OutputDescription() = default; - virtual std::shared_ptr copy() const = 0; - virtual const type_info_t& get_type_info() const = 0; - - uint64_t m_body_value_index{0}; - uint64_t m_output_index{0}; - }; - - /// - /// \brief Describes a body input formed from slices of an input to - /// MultiSubGraphOp. - /// - class NGRAPH_API SliceInputDescription : public InputDescription { - public: - NGRAPH_RTTI_DECLARATION; - /// - /// \brief Constructs a new instance. - /// - /// \param input_index Position of the MultiSubGraphOp input - /// \param body_parameter_index Body parameter position to receive input - /// \param start First index for slices - /// \param stride Step amount for slices - /// \param part_size Width of slices - /// \param end Last index for slices - /// \param axis Axis being sliced - /// - SliceInputDescription(uint64_t input_index, - uint64_t body_parameter_index, - int64_t start, - int64_t stride, - int64_t part_size, - int64_t end, - int64_t axis); - SliceInputDescription() = default; - std::shared_ptr copy() const override; - int64_t m_start{0}; - int64_t m_stride{0}; - int64_t m_part_size{0}; - int64_t m_end{0}; - int64_t m_axis{0}; - }; - - /// - /// \brief Describes a body input initialized from a MultiSubGraphOp input - /// on the first iteration, and then a body output thereafter. - /// - class NGRAPH_API MergedInputDescription : public InputDescription { - public: - NGRAPH_RTTI_DECLARATION; - /// - /// \brief Constructs a new instance. - /// - /// \param input_index Position of the MultiSubGraphOp input - /// supplying a value to body_parameter for - /// the initial iteration. - /// \param body_parameter_index Body parameter position to receive input. - /// \param body_value_index Body value to supply body_parameter for - /// successive - /// iterations. - /// - MergedInputDescription(uint64_t input_index, uint64_t body_parameter_index, uint64_t body_value_index); - MergedInputDescription() = default; - std::shared_ptr copy() const override; - uint64_t m_body_value_index{0}; - }; - - /// \brief Produces an output by concatenating an output from each iteration - class NGRAPH_API ConcatOutputDescription : public OutputDescription { - public: - NGRAPH_RTTI_DECLARATION; - /// - /// \brief Constructs a new instance. - /// - /// \param body_value_index A body value that produces the output - /// \param output_index The MultiSubGraphOp output index - /// \param start First index for slices - /// \param stride Step amount for slices - /// \param part_size Width of slices - /// \param end Last index for slices - /// \param axis Axis being sliced - /// - ConcatOutputDescription(uint64_t body_value_index, - uint64_t output_index, - int64_t start, - int64_t stride, - int64_t part_size, - int64_t end, - int64_t axis); - ConcatOutputDescription() = default; - - std::shared_ptr copy() const override; - int64_t m_start{0}; - int64_t m_stride{0}; - int64_t m_part_size{0}; - int64_t m_end{0}; - int64_t m_axis{0}; - }; - - /// \brief Produces an input - class NGRAPH_API InvariantInputDescription : public InputDescription { - public: - NGRAPH_RTTI_DECLARATION; - /// - /// \brief Constructs a new instance. - /// - /// \param input_index Position of the MultiSubGraphOp input - /// \param body_parameter_index Body parameter to receive input - /// - InvariantInputDescription(uint64_t input_index, uint64_t body_parameter_index); - InvariantInputDescription() = default; - std::shared_ptr copy() const override; - }; - - /// \brief Produces an output from a specific iteration - class NGRAPH_API BodyOutputDescription : public MultiSubGraphOp::OutputDescription { - public: - NGRAPH_RTTI_DECLARATION; - /// - /// \brief Constructs a new instance. - /// - /// \param body_value_index A body value that produces the output - /// \param output_index The SubGraphOp output index - /// \param iteration which iteration (typically -1, final) will - /// supply the value - /// - BodyOutputDescription(uint64_t body_value_index, uint64_t output_index, int64_t iteration = -1); - BodyOutputDescription() = default; - std::shared_ptr copy() const override; - int64_t m_iteration{0}; - }; - using MultiSubgraphInputDescriptionPtr = std::shared_ptr; - using MultiSubgraphOutputDescriptionPtr = std::shared_ptr; - using MultiSubgraphInputDescriptionVector = std::vector; - using MultiSubgraphOutputDescriptionVector = std::vector; - - /// \brief Gets internal sub-graph by index in MultiSubGraphOp - /// - /// \param index sub-graph's index in op - /// \return pointer to ngraph::Function with sub-graph - virtual const std::shared_ptr& get_function(int index) const { - return m_bodies[index]; - }; - /// \brief Adds sub-graph to MultiSubGraphOp - /// - /// \param index index of new sub-graph - /// \param func func new sub_graph as ngraph::Function - virtual void set_function(int index, const std::shared_ptr& func) { - m_bodies[index] = func; - } - /// \brief Gets vector with connections beewtwen operation inputs - /// and internal sub-graph parameters - /// - /// \param index index of internal sub-graph - /// \return vector of input descriptions - const MultiSubgraphInputDescriptionVector& get_input_descriptions(int index) const { - return m_input_descriptions[index]; - } - /// \brief Gets vector with connections beewtwen operation inputs - /// and internal sub-graph parameters - /// - /// \param index index of internal sub-graph - /// \return vector of input descriptions - MultiSubgraphInputDescriptionVector& get_input_descriptions(int index) { - return m_input_descriptions[index]; - } - /// \brief Gets vector with connections beewtwen operation outputs - /// and internal sub-graph results - /// - /// \param index index of internal sub-graph - /// \return vector of output descriptions - const MultiSubgraphOutputDescriptionVector& get_output_descriptions(int index) const { - return m_output_descriptions[index]; - } - /// \brief Gets vector with connections beewtwen operation outputs - /// and internal sub-graph results - /// - /// \param index index of internal sub-graph - /// \return vector of output descriptions - MultiSubgraphOutputDescriptionVector& get_output_descriptions(int index) { - return m_output_descriptions[index]; - } - /// \brief Sets vector with connections beewtwen operation inputs - /// and internal sub-graph parameters - /// - /// \param index index of internal sub-graph - /// \param inputs vector of input descriptions - void set_input_descriptions(int index, const MultiSubgraphInputDescriptionVector& inputs) { - m_input_descriptions[index] = inputs; - } - - /// \brief Sets vector with connections beewtwen operation outputs - /// and internal sub-graph results - /// - /// \param index index of internal sub-graph - /// \param outputs vector of input descriptions - void set_output_descriptions(int index, const MultiSubgraphOutputDescriptionVector& outputs) { - m_output_descriptions[index] = outputs; - } - - /// - /// \brief Set input decriptions for MultiSubGraphOp input. - /// - /// \param value The value supplied as an input to the block. - /// \param bodies_parameters vector of bodies parameters. - virtual void set_invariant_inputs(const Output& value, const ParameterVector& bodies_parameters); - /// - /// \brief Set output decriptions for MultiSubGraphOp output. - /// - /// \param bodies_results vector of bodies results for one output. - /// \return value Output node for bodies_results. - virtual Output set_body_outputs(const ResultVector& bodies_results); - - MultiSubGraphOp(const MultiSubGraphOp&) = delete; - MultiSubGraphOp(MultiSubGraphOp&&) = default; - - MultiSubGraphOp& operator=(const MultiSubGraphOp&) = delete; - MultiSubGraphOp& operator=(MultiSubGraphOp&&) = default; - -protected: - // Find an input corresponding to value, adding one if necessary. - Input input_for_value(const Output& value); - - MultiSubGraphOp(size_t number_of_bodies); - MultiSubGraphOp() = default; - MultiSubGraphOp(const OutputVector& args, size_t number_of_bodies); - explicit MultiSubGraphOp(const OutputVector& args); - - std::vector> m_bodies; - std::vector m_input_descriptions; - std::vector m_output_descriptions; -}; -using MultiSubgraphInputDescriptionPtr = util::MultiSubGraphOp::MultiSubgraphInputDescriptionPtr; -using MultiSubgraphOutputDescriptionPtr = util::MultiSubGraphOp::MultiSubgraphOutputDescriptionPtr; +using ov::op::util::MultiSubGraphOp; +using MultiSubgraphInputDescriptionPtr = ov::op::util::MultiSubGraphOp::InputDescription::Ptr; +using MultiSubgraphOutputDescriptionPtr = ov::op::util::MultiSubGraphOp::OutputDescription::Ptr; using MultiSubgraphInputDescriptionVector = util::MultiSubGraphOp::MultiSubgraphInputDescriptionVector; using MultiSubgraphOutputDescriptionVector = util::MultiSubGraphOp::MultiSubgraphOutputDescriptionVector; - } // namespace util } // namespace op } // namespace ngraph - -namespace ov { - -template <> -class NGRAPH_API AttributeAdapter>> - : public DirectValueAccessor>> { -public: - AttributeAdapter(std::vector>& value) - : DirectValueAccessor>>( - value) {} - - NGRAPH_RTTI_DECLARATION; -}; - -template <> -class NGRAPH_API AttributeAdapter>> - : public DirectValueAccessor>> { -public: - AttributeAdapter(std::vector>& value) - : DirectValueAccessor>>( - value) {} - - NGRAPH_RTTI_DECLARATION; -}; - -} // namespace ov diff --git a/ngraph/core/include/ngraph/op/util/nms_base.hpp b/ngraph/core/include/ngraph/op/util/nms_base.hpp index 1d5519354965c8..600c0c235d5b60 100644 --- a/ngraph/core/include/ngraph/op/util/nms_base.hpp +++ b/ngraph/core/include/ngraph/op/util/nms_base.hpp @@ -5,91 +5,12 @@ #pragma once #include "ngraph/op/op.hpp" +#include "openvino/op/util/nms_base.hpp" namespace ngraph { namespace op { namespace util { -/// \brief Base class for operations NmsBase and MatrixNms -/// -class NGRAPH_API NmsBase : public Op { -public: - NGRAPH_RTTI_DECLARATION; - enum class SortResultType { - CLASSID, // sort selected boxes by class id (ascending) in each batch element - SCORE, // sort selected boxes by score (descending) in each batch element - NONE // do not guarantee the order in each batch element - }; - - NmsBase() = delete; - - /// \brief Constructs a NmsBase operation - /// - /// \param output_type Specifies the output tensor type - /// \param nms_top_k Specifies maximum number of boxes to be selected per - /// class, -1 meaning to keep all boxes - /// \param keep_top_k Specifies maximum number of boxes to be selected per - /// batch element, -1 meaning to keep all boxes - NmsBase(ngraph::element::Type& output_type, int& nms_top_k, int& keep_top_k); - - /// \brief Constructs a NmsBase operation - /// - /// \param boxes Node producing the box coordinates - /// \param scores Node producing the box scores - /// \param output_type Specifies the output tensor type - /// \param nms_top_k Specifies maximum number of boxes to be selected per - /// class, -1 meaning to keep all boxes - /// \param keep_top_k Specifies maximum number of boxes to be selected per - /// batch element, -1 meaning to keep all boxes - NmsBase(const Output& boxes, - const Output& scores, - ngraph::element::Type& output_type, - int& nms_top_k, - int& keep_top_k); - - void validate_and_infer_types() override; - - const element::Type& get_output_type() const { - return m_output_type; - } - void set_output_type(const element::Type& output_type) { - m_output_type = output_type; - } - using Node::set_output_type; - - int get_nms_top_k() const { - return m_nms_top_k; - } - - int get_keep_top_k() const { - return m_keep_top_k; - } - -protected: - ngraph::element::Type& m_output_type; - int& m_nms_top_k; - int& m_keep_top_k; - virtual void validate(); -}; +using ov::op::util::NmsBase; } // namespace util } // namespace op - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const op::util::NmsBase::SortResultType& type); } // namespace ngraph - -namespace ov { - -template <> -class NGRAPH_API AttributeAdapter - : public EnumAttributeAdapterBase { -public: - AttributeAdapter(ngraph::op::util::NmsBase::SortResultType& value) - : EnumAttributeAdapterBase(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 1}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -} // namespace ov diff --git a/ngraph/core/include/ngraph/op/util/op_types.hpp b/ngraph/core/include/ngraph/op/util/op_types.hpp index 5c875830025d85..c18fb656f1eed3 100644 --- a/ngraph/core/include/ngraph/op/util/op_types.hpp +++ b/ngraph/core/include/ngraph/op/util/op_types.hpp @@ -8,57 +8,20 @@ #include "ngraph/ngraph_visibility.hpp" #include "ngraph/node.hpp" +#include "openvino/op/util/op_types.hpp" namespace ngraph { namespace op { -NGRAPH_API -bool is_unary_elementwise_arithmetic(const ngraph::Node* node); -NGRAPH_API -bool is_binary_elementwise_arithmetic(const ngraph::Node* node); -NGRAPH_API -bool is_binary_elementwise_comparison(const ngraph::Node* node); -NGRAPH_API -bool is_binary_elementwise_logical(const ngraph::Node* node); - -NGRAPH_API -bool supports_auto_broadcast(const ngraph::Node* node); - -NGRAPH_API -bool is_op(const ngraph::Node* node); -NGRAPH_API -bool is_parameter(const ngraph::Node* node); -NGRAPH_API -bool is_output(const ngraph::Node* node); -NGRAPH_API -bool is_sink(const ngraph::Node* node); -NGRAPH_API -bool is_constant(const ngraph::Node* node); -NGRAPH_API -bool is_commutative(const ngraph::Node* node); - -NGRAPH_API -bool is_unary_elementwise_arithmetic(const std::shared_ptr& node); -NGRAPH_API -bool is_binary_elementwise_arithmetic(const std::shared_ptr& node); -NGRAPH_API -bool is_binary_elementwise_comparison(const std::shared_ptr& node); -NGRAPH_API -bool is_binary_elementwise_logical(const std::shared_ptr& node); - -NGRAPH_API -bool supports_auto_broadcast(const std::shared_ptr& node); - -NGRAPH_API -bool is_op(const std::shared_ptr& node); -NGRAPH_API -bool is_parameter(const std::shared_ptr& node); -NGRAPH_API -bool is_output(const std::shared_ptr& node); -NGRAPH_API -bool is_sink(const std::shared_ptr& node); -NGRAPH_API -bool is_constant(const std::shared_ptr& node); -NGRAPH_API -bool is_commutative(const std::shared_ptr& node); +using ov::op::util::is_binary_elementwise_arithmetic; +using ov::op::util::is_binary_elementwise_comparison; +using ov::op::util::is_binary_elementwise_logical; +using ov::op::util::is_commutative; +using ov::op::util::is_constant; +using ov::op::util::is_op; +using ov::op::util::is_output; +using ov::op::util::is_parameter; +using ov::op::util::is_sink; +using ov::op::util::is_unary_elementwise_arithmetic; +using ov::op::util::supports_auto_broadcast; } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/reduction_base.hpp b/ngraph/core/include/ngraph/op/util/reduction_base.hpp index ffc70ddc14d894..6af3f84077bf37 100644 --- a/ngraph/core/include/ngraph/op/util/reduction_base.hpp +++ b/ngraph/core/include/ngraph/op/util/reduction_base.hpp @@ -5,31 +5,12 @@ #pragma once #include "ngraph/op/op.hpp" +#include "openvino/op/util/reduction_base.hpp" namespace ngraph { namespace op { namespace util { -class NGRAPH_API ReductionBase : public Op { -protected: - /// \brief Constructs a reduction operation. - ReductionBase(); - - /// \brief Constructs a reduction operation. - /// - /// \param arg Output that produces the first input tensor. - /// \param reduction_axes The axis positions (0-based) to be eliminated. - ReductionBase(const Output& arg, const Output& reduction_axes); - - /// \brief Infers reduction operations output shape. - /// - /// \param[in] keep_dims Reduction operation keeps dimensions. - /// - /// \return Partial shape of the output. - PartialShape infer_reduction_output_shape(const bool keep_dims); - -public: - NGRAPH_RTTI_DECLARATION; -}; +using ov::op::util::ReductionBase; } // namespace util } // namespace op -} // namespace ngraph \ No newline at end of file +} // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/rnn_cell_base.hpp b/ngraph/core/include/ngraph/op/util/rnn_cell_base.hpp index 509830bd922a69..d351dcf38ab10d 100644 --- a/ngraph/core/include/ngraph/op/util/rnn_cell_base.hpp +++ b/ngraph/core/include/ngraph/op/util/rnn_cell_base.hpp @@ -12,151 +12,14 @@ #include "ngraph/node.hpp" #include "ngraph/op/op.hpp" #include "ngraph/op/util/activation_functions.hpp" +#include "openvino/op/util/rnn_cell_base.hpp" namespace ngraph { namespace op { namespace util { -enum class LSTMWeightsFormat { - FICO, // IE - ICOF, // PyTorch - IFCO, // DNNL, TF, MxNet - IFOC, // Caffe - IOFC, // ONNX -}; - -/// -/// \brief Change data format of provided node. -/// -/// \param[in] node The input node to be permuted. -/// -/// -/// \param[in] from_format Original node weights format. -/// -/// -/// \param[in] to_format Weights format to convert to. -/// -/// \return Node representing reshaped tensor according to `to_format` weights -/// format. -/// -std::shared_ptr NGRAPH_API convert_lstm_node_format(const Output& node, - LSTMWeightsFormat from_format, - LSTMWeightsFormat to_format = LSTMWeightsFormat::FICO, - int64_t axis = 0); - -/// \brief Base class for all recurrent network cells. -/// -/// \note It holds all common attributes. -/// -class NGRAPH_API RNNCellBase : public Op { -public: - NGRAPH_RTTI_DECLARATION; - - /// - /// \brief Constructs a RNNCellBase class. - /// - /// \param[in] hidden_size The number of hidden units for recurrent cell. - /// \param[in] clip The value defining clipping range [-clip, clip] - /// on input of activation functions. - /// \param[in] activations The vector of activation functions used inside - /// recurrent cell. - /// \param[in] activations_alpha The vector of alpha parameters for activation - /// functions in order respective to activation list. - /// \param[in] activations_beta The vector of beta parameters for activation - /// functions in order respective to activation list. - /// - RNNCellBase(const OutputVector& args, - std::size_t hidden_size, - float clip, - const std::vector& activations, - const std::vector& activations_alpha, - const std::vector& activations_beta); - - RNNCellBase(); - virtual ~RNNCellBase() = default; - - /// - /// \brief Validates static rank and dimension for provided input parameters. - /// Additionally input_size dimension is checked for X and W inputs. - /// - /// - /// \param[in] input Vector with RNN-Cell op inputs in following order: - /// X, initial_hidden_state, W, R and B. - /// - void validate_input_rank_dimension(const std::vector& input); - - bool visit_attributes(AttributeVisitor& visitor) override; - std::size_t get_hidden_size() const { - return m_hidden_size; - } - float get_clip() const { - return m_clip; - } - const std::vector& get_activations() const { - return m_activations; - } - const std::vector& get_activations_alpha() const { - return m_activations_alpha; - } - const std::vector& get_activations_beta() const { - return m_activations_beta; - } - -protected: - /// - /// \brief Constructs activation function object. - /// - /// \param[in] idx The index of the activation function name. - /// - /// \return The object representing activation function. - /// - ActivationFunction get_activation_function(std::size_t idx) const; - /// - /// \brief Creates node with element-wise add operation with numpy - /// broadcasting. - /// - /// \param[in] lhs The left hand side argument node. - /// \param[in] rhs The right hand side argument node. - /// - /// \return Node with element-wise add operation. - /// - static std::shared_ptr add(const Output& lhs, const Output& rhs); - /// - /// \brief Creates node with element-wise subtract operation with numpy - /// broadcasting. - /// - /// \param[in] lhs The left hand side argument node. - /// \param[in] rhs The right hand side argument node. - /// - /// \return Node with element-wise subtract operation. - /// - static std::shared_ptr sub(const Output& lhs, const Output& rhs); - /// - /// \brief Creates node with element-wise multiply operation with numpy - /// broadcasting. - /// - /// \param[in] lhs The left hand side argument node. - /// \param[in] rhs The right hand side argument node. - /// - /// \return Node with element-wise multiply operation. - /// - static std::shared_ptr mul(const Output& lhs, const Output& rhs); - /// - /// \brief Creates node with element-wise clip operation with numpy - /// broadcasting. - /// - /// \param[in] data The input tensor for clipping. - /// - /// \return Node with element-wise clip operation. - /// - std::shared_ptr clip(const Output& data) const; - -protected: - std::size_t m_hidden_size; - float m_clip; - std::vector m_activations; - std::vector m_activations_alpha; - std::vector m_activations_beta; -}; +using ov::op::util::convert_lstm_node_format; +using ov::op::util::LSTMWeightsFormat; +using ov::op::util::RNNCellBase; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/scatter_base.hpp b/ngraph/core/include/ngraph/op/util/scatter_base.hpp index c76a557a0d2525..2127050aef571b 100644 --- a/ngraph/core/include/ngraph/op/util/scatter_base.hpp +++ b/ngraph/core/include/ngraph/op/util/scatter_base.hpp @@ -5,45 +5,12 @@ #pragma once #include "ngraph/op/op.hpp" +#include "openvino/op/util/scatter_base.hpp" namespace ngraph { namespace op { namespace util { -/// -/// \brief Base class for ScatterXXX operators. -/// -class NGRAPH_API ScatterBase : public Op { -public: - static constexpr NodeTypeInfo type_info{"ScatterBase", 3}; - const NodeTypeInfo& get_type_info() const override { - return type_info; - } - virtual void validate_and_infer_types() override; - bool visit_attributes(AttributeVisitor& visitor) override; - -protected: - ScatterBase() = default; - - /// - /// \brief Constructs ScatterBase object. - /// - /// \param inputs The input tensor to be updated. - /// \param indices The tensor with indexes which will be updated. - /// \param updates The tensor with update values. - /// \param[in] axis The axis at which elements will be updated. - /// - ScatterBase(const Output& inputs, - const Output& indices, - const Output& updates, - const Output& axis); - -private: - // Respective input ordinal number. - static constexpr int DATA = 0; - static constexpr int INDICES = 1; - static constexpr int UPDATES = 2; - static constexpr int AXIS = 3; -}; +using ov::op::util::ScatterBase; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/scatter_nd_base.hpp b/ngraph/core/include/ngraph/op/util/scatter_nd_base.hpp index 111d909f2a8e05..6b2215d68c95ca 100644 --- a/ngraph/core/include/ngraph/op/util/scatter_nd_base.hpp +++ b/ngraph/core/include/ngraph/op/util/scatter_nd_base.hpp @@ -5,38 +5,12 @@ #pragma once #include "ngraph/op/op.hpp" +#include "openvino/op/util/scatter_nd_base.hpp" namespace ngraph { namespace op { namespace util { -/// -/// \brief Base class for ScatterNDXXX operators. -/// -class NGRAPH_API ScatterNDBase : public Op { -public: - static constexpr NodeTypeInfo type_info{"ScatterNDBase", 3}; - const NodeTypeInfo& get_type_info() const override { - return type_info; - } - // Respective input ordinal number. - static constexpr int INPUTS = 0; - static constexpr int INDICES = 1; - static constexpr int UPDATES = 2; - virtual void validate_and_infer_types() override; - bool visit_attributes(AttributeVisitor& visitor) override; - -protected: - ScatterNDBase() = default; - - /// - /// \brief Constructs ScatterNDBase object. - /// - /// \param inputs The input tensor to be updated. - /// \param indices The tensor with indexes which will be updated. - /// \param updates The tensor with update values. - /// - ScatterNDBase(const Output& inputs, const Output& indices, const Output& updates); -}; +using ov::op::util::ScatterNDBase; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/sub_graph_base.hpp b/ngraph/core/include/ngraph/op/util/sub_graph_base.hpp index 9a72523e29f439..88d2b1a655c5ed 100644 --- a/ngraph/core/include/ngraph/op/util/sub_graph_base.hpp +++ b/ngraph/core/include/ngraph/op/util/sub_graph_base.hpp @@ -7,142 +7,14 @@ #include #include "ngraph/op/util/multi_subgraph_base.hpp" +#include "openvino/op/util/sub_graph_base.hpp" namespace ngraph { namespace op { namespace util { -/// \brief Abstract base class for sub-graph based ops, i.e ops that have only one -/// sub-graph -/// -class NGRAPH_API SubGraphOp : public MultiSubGraphOp { -public: - NGRAPH_RTTI_DECLARATION; - - virtual const std::shared_ptr& get_function() const { - return m_bodies[0]; - }; - virtual void set_function(const std::shared_ptr& func) { - m_bodies[0] = func; - }; - /// \return a reference to the input descriptions. - const std::vector>& get_input_descriptions() const { - return m_input_descriptions[0]; - } - /// \return a reference to the input descriptions. Can add input descriptions - /// before - /// validation. - std::vector>& get_input_descriptions() { - return m_input_descriptions[0]; - } - /// \return a reference to the output descriptions. - const std::vector>& get_output_descriptions() const { - return m_output_descriptions[0]; - } - /// \return a reference to the output descriptions. Can add output descriptions - /// before - /// validation. - std::vector>& get_output_descriptions() { - return m_output_descriptions[0]; - } - - /// - /// \brief Indicate that a body parameter comes from slices of a value - /// - /// \param parameter The parameter to receive the slices - /// \param value The value to be sliced. This will be added as an input to - /// SubGraphOp. - /// \param start First index on axis of the slicing - /// \param stride Stepping of the slice - /// \param part_size Size of the slice on axis - /// \param end The last index on axis of the slicing - /// \param axis The axis to slice along - /// - virtual void set_sliced_input(const std::shared_ptr& parameter, - const Output& value, - int64_t start, - int64_t stride, - int64_t part_size, - int64_t end, - int64_t axis); - /// - /// \brief Indicates that a body parameter has an initial value in the first - /// iteration and computed value thereafter - /// - /// \param[in] body_parameter The body parameter - /// \param initial_value Value for the parameter in first iteration. This - /// will be added as an input to Loop. - /// \param successive_value Value for the parameter in successive iterations. - /// The value is what is active in the most recent - /// completed iteration. - /// - virtual void set_merged_input(const std::shared_ptr& body_parameter, - const Output& initial_value, - const Output& successive_value); - /// - /// \brief Indicates that a body parameter has an invariant value during - /// iteration that may depend on values computed outside of the - /// iteration. - /// - /// \param body_parameter The body parameter - /// \param value The value supplied as an input to the block - /// - virtual void set_invariant_input(const std::shared_ptr& body_parameter, const Output& value); - /// - /// \brief Gets a value for a particular iteration point - /// - /// \param body_value The value - /// \param iteration The iteration that supplies the value. Negative values - /// are from the last iteration. - /// Default value -1 (the last iteration). - /// - /// \return The iterator value. - /// - virtual Output get_iter_value(const Output& body_value, int64_t iteration = -1); - /// - /// \brief Concatenates slices from all iterations - /// - /// \param value The value supplying slice values from each iteration. - /// \param start First index on axis of the slicing - /// \param stride Stepping of the slice - /// \param part_size Size of the slice on axis - /// \param end The last index on axis of the slicing - /// \param axis The axis to slice along - /// - /// \return The concatenated slices. - /// - virtual Output get_concatenated_slices(const Output& value, - int64_t start, - int64_t stride, - int64_t part_size, - int64_t end, - int64_t axis); - - SubGraphOp(const SubGraphOp&) = delete; - SubGraphOp(SubGraphOp&&) = default; - - SubGraphOp& operator=(const SubGraphOp&) = delete; - SubGraphOp& operator=(SubGraphOp&&) = default; - - int64_t get_num_iterations() const { - return m_num_iterations; - } - -protected: - int64_t m_num_iterations = -1; // -1 means infinity for Loop op, inconsistent for TensorIterator - - // Find an input corresponding to value, adding one if necessary. - Input input_for_value(const Output& value); - - SubGraphOp(); - explicit SubGraphOp(const OutputVector& args); - -private: - using MultiSubGraphOp::get_function; - - using MultiSubGraphOp::set_function; -}; -using InputDescriptionPtr = std::shared_ptr; -using OutputDescriptionPtr = std::shared_ptr; +using ov::op::util::SubGraphOp; +using InputDescriptionPtr = util::SubGraphOp::InputDescription::Ptr; +using OutputDescriptionPtr = util::SubGraphOp::OutputDescription::Ptr; using InputDescriptionVector = std::vector; using OutputDescriptionVector = std::vector; } // namespace util diff --git a/ngraph/core/include/ngraph/op/util/unary_elementwise_arithmetic.hpp b/ngraph/core/include/ngraph/op/util/unary_elementwise_arithmetic.hpp index bce715e01690c7..91499fd7f47fa6 100644 --- a/ngraph/core/include/ngraph/op/util/unary_elementwise_arithmetic.hpp +++ b/ngraph/core/include/ngraph/op/util/unary_elementwise_arithmetic.hpp @@ -5,49 +5,12 @@ #pragma once #include "ngraph/op/op.hpp" +#include "openvino/op/util/unary_elementwise_arithmetic.hpp" namespace ngraph { namespace op { namespace util { -// clang-format off - /// \brief Abstract base class for elementwise unary arithmetic operations, i.e., - /// operations where the same scalar arithmetic operation is applied to each - /// element. - /// - /// For example, if the underlying operation (determined by the subclass) is - /// \f$\mathit{op}(x)\f$, the input tensor \f$[[x,y],[z,w]]\f$ will be mapped to - /// \f$[[\mathit{op}(x),\mathit{op}(y)],[\mathit{op}(z),\mathit{op}(w)]]\f$. - /// - /// ## Inputs - /// - /// | | Type | Description | - /// | ----- | --------------------------------- | ------------------------------------------------------------------------ | - /// | `arg` | \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. | - /// - /// ## Output - /// - /// | Type | Description | - /// | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | - /// | \f$N[d_1,\dots,d_n]\f$ | The tensor \f$T\f$, where \f$T[i_1,\dots,i_n] = \mathit{op}(\texttt{arg}[i_1,\dots,i_n])\f$. This will always have the same shape and element type as the input tensor. | -// clang-format on -class NGRAPH_API UnaryElementwiseArithmetic : public Op { -protected: - /// \brief Constructs a unary elementwise arithmetic operation. - UnaryElementwiseArithmetic(); - /// \brief Constructs a unary elementwise arithmetic operation. - /// - /// \param arg Output that produces the input tensor. - UnaryElementwiseArithmetic(const Output& arg); - -public: - NGRAPH_RTTI_DECLARATION; - - void validate_and_infer_types() override; - bool visit_attributes(AttributeVisitor& visitor) override; - -private: - void validate_and_infer_elementwise_arithmetic(); -}; +using ov::op::util::UnaryElementwiseArithmetic; } // namespace util } // namespace op } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/variable.hpp b/ngraph/core/include/ngraph/op/util/variable.hpp index 04c873bb3ab85c..9f57262628bec9 100644 --- a/ngraph/core/include/ngraph/op/util/variable.hpp +++ b/ngraph/core/include/ngraph/op/util/variable.hpp @@ -10,51 +10,11 @@ #include "ngraph/partial_shape.hpp" #include "ngraph/type.hpp" #include "ngraph/type/element_type.hpp" +#include "openvino/op/util/variable.hpp" namespace ngraph { -struct VariableInfo { - PartialShape data_shape; - element::Type data_type; - std::string variable_id; - - inline bool operator==(const VariableInfo& other) const { - return data_shape == other.data_shape && data_type == other.data_type && variable_id == other.variable_id; - } -}; - -class NGRAPH_API Variable { -public: - Variable() = default; - - explicit Variable(const VariableInfo& variable_info) : m_info(variable_info) {} - - VariableInfo get_info() const { - return m_info; - } - void update(const VariableInfo& variable_info) { - m_info = variable_info; - } - -private: - VariableInfo m_info; -}; +using ov::op::util::Variable; +using ov::op::util::VariableInfo; using VariablePtr = std::shared_ptr; using VariableVector = std::vector; } // namespace ngraph - -namespace ov { - -template <> -class NGRAPH_API AttributeAdapter> - : public DirectValueAccessor> { -public: - explicit AttributeAdapter(std::shared_ptr& value) - : DirectValueAccessor>(value) {} - - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter>", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -} // namespace ov diff --git a/ngraph/core/include/ngraph/op/util/variable_context.hpp b/ngraph/core/include/ngraph/op/util/variable_context.hpp index 71253347123f33..2856c197803822 100644 --- a/ngraph/core/include/ngraph/op/util/variable_context.hpp +++ b/ngraph/core/include/ngraph/op/util/variable_context.hpp @@ -11,81 +11,9 @@ #include "ngraph/op/util/variable_value.hpp" #include "ngraph/output_vector.hpp" #include "ngraph/variant.hpp" +#include "openvino/op/util/variable_context.hpp" namespace ngraph { -using VariableValuePtr = std::shared_ptr; using VariableMap = std::unordered_map; - -/// VariableContext stores and manages a evaluation context for Variables. -class NGRAPH_API VariableContext { -public: - /// \brief Constructs an uninitialized VariableContext. - VariableContext() = default; - - /// \brief Constructor for VariableContext. - /// \param variable_values The values associated with a particular Variables. - explicit VariableContext(const VariableMap& variable_values) : m_variable_values(variable_values) {} - - /// \brief Sets the reset flags for all stored Variables to true. - void reset_variable_context() const { - for (const auto& el : m_variable_values) { - el.second->set_reset(true); - } - } - - /// \brief Sets the new values for Variables. - /// \param variable_values The new values associated with a particular Variable. - void set_variable_values(const VariableMap& variable_values) { - m_variable_values = variable_values; - } - - /// \brief Changes/sets the values for Variable. - /// \param variable New or stored Variable. - /// \param variable_value The values associated with the variable. - void set_variable_value(const VariablePtr& variable, const VariableValuePtr& variable_value) { - m_variable_values[variable] = variable_value; - } - - /// \brief Removes context for a particular Variable. - /// \param variable The variable for which the context will be cleared. - void remove_variable_value(const VariablePtr& variable) { - m_variable_values.erase(variable); - } - - /// \brief Returns the current values for Variables. - const VariableMap& get_variable_values() const { - return m_variable_values; - } - - /// \brief Returns the value for specified Variable. - VariableValuePtr get_variable_value(const VariablePtr& variable) const { - auto var_value = m_variable_values.find(variable); - if (var_value != m_variable_values.end()) { - return (*var_value).second; - } - return VariableValuePtr(); - } - -private: - /// The values associated with a particular Variable. - VariableMap m_variable_values; -}; +using ov::op::util::VariableContext; } // namespace ngraph - -namespace ov { -template <> -class NGRAPH_API VariantWrapper : public VariantImpl { -public: - static constexpr VariantTypeInfo type_info{"Variant::EvaluationContext::VariableContext", 0}; - - const VariantTypeInfo& get_type_info() const override { - return type_info; - } - - explicit VariantWrapper(const value_type& value) : VariantImpl(value) {} - -private: - using Variant::init; - using Variant::merge; -}; -} // namespace ov diff --git a/ngraph/core/include/ngraph/op/util/variable_extension.hpp b/ngraph/core/include/ngraph/op/util/variable_extension.hpp index e1145aa3dcb25b..90360d60a3bb36 100644 --- a/ngraph/core/include/ngraph/op/util/variable_extension.hpp +++ b/ngraph/core/include/ngraph/op/util/variable_extension.hpp @@ -4,37 +4,11 @@ #pragma once -#include #include -namespace ngraph { -class NGRAPH_API VariableExtension { -public: - VariableExtension() = default; - - /// \brief Returns variable connected to this node. - virtual std::shared_ptr get_variable() const { - return m_variable; - } - - /// \brief Sets a new variable to be connected to this node. - /// - /// \param variable New variable to be connected to this node. - virtual void set_variable(const std::shared_ptr& variable) { - m_variable = variable; - } +#include "ngraph/runtime/host_tensor.hpp" +#include "openvino/op/util/variable_extension.hpp" - /// \brief Sets the identifier to a variable - /// - /// \param variable_id New identifier of the variable. - virtual void set_variable_id(const std::string& variable_id) { - m_variable->get_info().variable_id = variable_id; - }; - - /// \brief Returns the identifier of corresponding variable. - virtual std::string get_variable_id() const = 0; - -protected: - std::shared_ptr m_variable; -}; +namespace ngraph { +using ov::op::util::VariableExtension; } // namespace ngraph diff --git a/ngraph/core/include/ngraph/op/util/variable_value.hpp b/ngraph/core/include/ngraph/op/util/variable_value.hpp index 9e0173001f0740..1d69ba314f36b0 100644 --- a/ngraph/core/include/ngraph/op/util/variable_value.hpp +++ b/ngraph/core/include/ngraph/op/util/variable_value.hpp @@ -4,51 +4,12 @@ #pragma once -#include #include -namespace ngraph { -/// VariableValue stores data and state (reset flag) for a Variable, -/// and provides an interface for changing them. -class NGRAPH_API VariableValue { -public: - /// \brief Constructs an uninitialized VariableValue. - VariableValue() = default; - - /// \brief Constructor for VariableValue. - /// \param value The data for Variable. - explicit VariableValue(HostTensorPtr value) : m_value(std::move(value)) {} - - /// \brief Constructor for VariableValue. - /// \param value Data for Variable. - /// \param reset The current state of the reset flag. - VariableValue(HostTensorPtr value, bool reset) : m_reset(reset), m_value(std::move(value)) {} - - /// \brief Sets the reset flag to a new state. - /// \param reset The new state of the reset flag. - void set_reset(bool reset) { - m_reset = reset; - } +#include "ngraph/runtime/host_tensor.hpp" +#include "openvino/op/util/variable_value.hpp" - /// \brief Returns the current reset flag state. - bool get_reset() const { - return m_reset; - } - - /// \brief Returns the current stored data. - const HostTensorPtr& get_value() const { - return m_value; - } - - /// \brief Sets new values for Variable. - /// \param value New data for Variable. - void set_value(const HostTensorPtr& value) { - m_value = value; - } - -private: - bool m_reset = true; - HostTensorPtr m_value; -}; +namespace ngraph { +using ov::op::util::VariableValue; using VariableValuePtr = std::shared_ptr; } // namespace ngraph diff --git a/ngraph/core/include/ngraph/strides.hpp b/ngraph/core/include/ngraph/strides.hpp index c07c2d79c8acb3..de9147b455a902 100644 --- a/ngraph/core/include/ngraph/strides.hpp +++ b/ngraph/core/include/ngraph/strides.hpp @@ -10,47 +10,8 @@ #include "ngraph/attribute_adapter.hpp" #include "ngraph/ngraph_visibility.hpp" +#include "openvino/core/strides.hpp" namespace ngraph { -/// \brief Strides for a tensor. -class Strides : public std::vector { -public: - NGRAPH_API Strides(); - - NGRAPH_API Strides(const std::initializer_list& axis_strides); - - NGRAPH_API Strides(const std::vector& axis_strides); - - NGRAPH_API Strides(const Strides& axis_strides); - - NGRAPH_API explicit Strides(size_t n, size_t initial_value = 0); - - template - Strides(InputIterator first, InputIterator last) : std::vector(first, last) {} - - NGRAPH_API Strides& operator=(const Strides& v); - - NGRAPH_API Strides& operator=(Strides&& v) noexcept; -}; - -NGRAPH_API -std::ostream& operator<<(std::ostream& s, const Strides& strides); +using ov::Strides; } // namespace ngraph - -namespace ov { - -template <> -class NGRAPH_API AttributeAdapter - : public IndirectVectorValueAccessor> - -{ -public: - AttributeAdapter(ngraph::Strides& value) - : IndirectVectorValueAccessor>(value) {} - static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; - const DiscreteTypeInfo& get_type_info() const override { - return type_info; - } -}; - -} // namespace ov diff --git a/ngraph/core/include/openvino/core/node.hpp b/ngraph/core/include/openvino/core/node.hpp index 3ba65a5cd78ddd..efbd99ffd429bf 100644 --- a/ngraph/core/include/openvino/core/node.hpp +++ b/ngraph/core/include/openvino/core/node.hpp @@ -20,11 +20,7 @@ #include "ngraph/check.hpp" #include "ngraph/deprecated.hpp" -#include "ngraph/op/util/attr_types.hpp" #include "ngraph/op/util/op_annotations.hpp" -#include "ngraph/op/util/variable.hpp" -#include "ngraph/op/util/variable_value.hpp" -#include "ngraph/strides.hpp" #include "openvino/core/attribute_visitor.hpp" #include "openvino/core/core_visibility.hpp" #include "openvino/core/descriptor/input.hpp" @@ -33,8 +29,12 @@ #include "openvino/core/node_input.hpp" #include "openvino/core/node_output.hpp" #include "openvino/core/node_vector.hpp" +#include "openvino/core/strides.hpp" #include "openvino/core/type.hpp" #include "openvino/core/variant.hpp" +#include "openvino/op/util/attr_types.hpp" +#include "openvino/op/util/variable.hpp" +#include "openvino/op/util/variable_value.hpp" namespace ngraph { @@ -43,8 +43,6 @@ class HostTensor; } // namespace runtime namespace op { -struct AutoBroadcastSpec; - namespace v0 { class Result; } // namespace v0 @@ -56,6 +54,9 @@ class Matcher; } // namespace ngraph namespace ov { +namespace op { +struct AutoBroadcastSpec; +} using HostTensor = ngraph::runtime::HostTensor; using HostTensorPtr = std::shared_ptr; using HostTensorVector = std::vector; @@ -194,7 +195,7 @@ class OPENVINO_API Node : public std::enable_shared_from_this { return false; } /// \returns the autobroadcasr spec - virtual const ngraph::op::AutoBroadcastSpec& get_autob() const; + virtual const ov::op::AutoBroadcastSpec& get_autob() const; /// \brief Allows to get information about availability of evaluate method for the current /// operation diff --git a/ngraph/core/include/openvino/core/partial_shape.hpp b/ngraph/core/include/openvino/core/partial_shape.hpp index eff5c651d36574..9b630a7017232b 100644 --- a/ngraph/core/include/openvino/core/partial_shape.hpp +++ b/ngraph/core/include/openvino/core/partial_shape.hpp @@ -12,15 +12,11 @@ #include "openvino/core/dimension.hpp" #include "openvino/core/rank.hpp" -namespace ngraph { +namespace ov { namespace op { struct AutoBroadcastSpec; } -} // namespace ngraph - -namespace ov { - /// \brief Class representing a shape that may be partially or totally dynamic. /// /// diff --git a/ngraph/core/include/openvino/core/strides.hpp b/ngraph/core/include/openvino/core/strides.hpp new file mode 100644 index 00000000000000..65d0b660d473d8 --- /dev/null +++ b/ngraph/core/include/openvino/core/strides.hpp @@ -0,0 +1,49 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include + +#include "openvino/core/attribute_adapter.hpp" +#include "openvino/core/core_visibility.hpp" + +namespace ov { +/// \brief Strides for a tensor. +class Strides : public std::vector { +public: + OPENVINO_API Strides(); + + OPENVINO_API Strides(const std::initializer_list& axis_strides); + + OPENVINO_API Strides(const std::vector& axis_strides); + + OPENVINO_API Strides(const Strides& axis_strides); + + OPENVINO_API explicit Strides(size_t n, size_t initial_value = 0); + + template + Strides(InputIterator first, InputIterator last) : std::vector(first, last) {} + + OPENVINO_API Strides& operator=(const Strides& v); + + OPENVINO_API Strides& operator=(Strides&& v) noexcept; +}; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const Strides& strides); + +template <> +class OPENVINO_API AttributeAdapter : public IndirectVectorValueAccessor> { +public: + AttributeAdapter(Strides& value) : IndirectVectorValueAccessor>(value) {} + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +} // namespace ov diff --git a/ngraph/core/include/openvino/op/op.hpp b/ngraph/core/include/openvino/op/op.hpp new file mode 100644 index 00000000000000..65a1493d630209 --- /dev/null +++ b/ngraph/core/include/openvino/op/op.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#include "openvino/core/node.hpp" + +namespace ov { +namespace op { +/// Root of all actual ops +class OPENVINO_API Op : public Node { +protected: + Op() : Node() {} + Op(const OutputVector& arguments); +}; +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/activation_functions.hpp b/ngraph/core/include/openvino/op/util/activation_functions.hpp new file mode 100644 index 00000000000000..a30b70f3e67fa5 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/activation_functions.hpp @@ -0,0 +1,86 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include + +#include "openvino/core/core_visibility.hpp" +#include "openvino/core/except.hpp" +#include "openvino/core/node.hpp" + +#ifdef _WIN32 +# pragma warning(push) + +# pragma warning(disable : 4100) +#endif + +namespace ov { +namespace op { +namespace util { +namespace error { +struct UnknownActivationFunction : Exception { + UnknownActivationFunction(const std::string& func_name) : Exception{"Unknown activation function: " + func_name} {} +}; +} // namespace error + +namespace detail { +std::shared_ptr sigmoid(const std::shared_ptr& arg, float alpha, float beta); +std::shared_ptr tanh(const std::shared_ptr& arg, float alpha, float beta); +std::shared_ptr relu(const std::shared_ptr& arg, float alpha, float beta); +std::shared_ptr hardsigmoid(const std::shared_ptr& arg, float alpha, float beta); +} // namespace detail + +using ActivationFunctionType = std::shared_ptr (*)(const std::shared_ptr&, float, float); + +/// +/// \brief Class representing activation function used in RNN cells. +/// +class OPENVINO_API ActivationFunction { +public: + ActivationFunction(ActivationFunctionType f, float alpha, float beta); + ActivationFunction(ActivationFunctionType f, float alpha); + ActivationFunction(ActivationFunctionType f); + ActivationFunction() = default; + + /// + /// \brief Calls stored activation function with provided node argument. + /// + std::shared_ptr operator()(const std::shared_ptr& arg) const; + + void set_alpha(float alpha) { + m_alpha = alpha; + } + void set_beta(float beta) { + m_beta = beta; + } + +private: + /// \brief Activation function wrapper. + ActivationFunctionType m_function; + /// \brief Activation function alpha parameter (may be unused). + float m_alpha; + /// \brief Activation function beta parameter (may be unused). + float m_beta; +}; + +/// \brief Gets the activation function by name. +/// +/// \param[in] func_name The function name +/// +/// \throws UnknownActivationFunction When provided func_name is unknown. +/// +/// \return The activation function object. +/// +ActivationFunction get_activation_func_by_name(const std::string& func_name); +} // namespace util + +} // namespace op + +} // namespace ov + +#ifdef _WIN32 +# pragma warning(pop) +#endif diff --git a/ngraph/core/include/openvino/op/util/arithmetic_reduction.hpp b/ngraph/core/include/openvino/op/util/arithmetic_reduction.hpp new file mode 100644 index 00000000000000..55931eaf04f0f4 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/arithmetic_reduction.hpp @@ -0,0 +1,44 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/core/core_visibility.hpp" +#include "openvino/op/util/reduction_base.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief Abstract base class for arithmetic reduction operations, i.e., operations +/// where chosen axes of the input tensors are eliminated (reduced out) by +/// repeated application of a particular binary arithmetic operation. +class OPENVINO_API ArithmeticReduction : public ReductionBase { +protected: + /// \brief Constructs an arithmetic reduction operation. + ArithmeticReduction(); + + /// \brief Constructs an arithmetic reduction operation. + /// + /// \param arg Output that produces the first input tensor. + /// \param reduction_axes The axis positions (0-based) to be eliminated. + ArithmeticReduction(const Output& arg, const Output& reduction_axes); + +public: + OPENVINO_RTTI_DECLARATION; + void validate_and_infer_types() override; + + /// \return true if reduction axes are constant else false. + bool reduction_axes_constant() const; + + /// \return The axis positions (0-based) to be eliminated through reduction. + /// \throws CheckFailure if the reduction axes are not constant. (Use + /// reduction_axes_constant to check.) + const AxisSet get_reduction_axes() const; + + /// \brief Change the reduction axes + void set_reduction_axes(const AxisSet& reduction_axes); +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/arithmetic_reductions_keep_dims.hpp b/ngraph/core/include/openvino/op/util/arithmetic_reductions_keep_dims.hpp new file mode 100644 index 00000000000000..805fe6ebe2d15c --- /dev/null +++ b/ngraph/core/include/openvino/op/util/arithmetic_reductions_keep_dims.hpp @@ -0,0 +1,41 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/util/arithmetic_reduction.hpp" + +namespace ov { +namespace op { +namespace util { +class OPENVINO_API ArithmeticReductionKeepDims : public util::ArithmeticReduction { +protected: + ArithmeticReductionKeepDims() = default; + + /// \param arg The tensor to be summed. + /// \param reduction_axes The axis positions (0-based) to be eliminated. + /// \param keep_dims If set to 1 it holds axes that are used for reduction. + ArithmeticReductionKeepDims(const Output& arg, const Output& reduction_axes, bool keep_dims = false); + + bool visit_attributes(AttributeVisitor& visitor) override; + +public: + OPENVINO_RTTI_DECLARATION; + void validate_and_infer_types() override; + + /// \return If set to 1 it holds axes that are used for reduction. + /// For each such axis, output dimension is equal to 1. + bool get_keep_dims() const { + return m_keep_dims; + } + void set_keep_dims(bool keep_dims) { + m_keep_dims = keep_dims; + } + +private: + bool m_keep_dims = false; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/attr_types.hpp b/ngraph/core/include/openvino/op/util/attr_types.hpp new file mode 100644 index 00000000000000..b7e37518d435c9 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/attr_types.hpp @@ -0,0 +1,333 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include + +#include "openvino/core/attribute_adapter.hpp" +#include "openvino/core/core_visibility.hpp" +#include "openvino/core/type.hpp" + +namespace ov { +namespace op { +/// \brief Modes for the `Pad` operator. +enum class PadMode { CONSTANT = 0, EDGE, REFLECT, SYMMETRIC }; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const PadMode& type); + +/// \brief Padding Type used for `Convolution` and `Pooling` +/// +/// Follows ONNX padding type definitions +/// EXPLICIT - Pad dimensions are explicity specified +/// SAME_LOWER - Pad dimensions computed to match input shape +/// Ceil(num_dims/2) at the beginning and +/// Floor(num_dims/2) at the end +/// SAME_UPPER - Pad dimensions computed to match input shape +/// Floor(num_dims/2) at the beginning and +/// Ceil(num_dims/2) at the end +/// VALID - No padding +/// AUTO - Deprecated. User should not use it in the future +/// NOTSET - Deprecated. User should not use it in the future + +enum class PadType { + EXPLICIT = 0, + SAME_LOWER, + SAME_UPPER, + VALID, + AUTO = SAME_UPPER, + NOTSET = EXPLICIT, +}; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const PadType& type); + +/// \brief Rounding Type used for `Pooling` operators. +enum class RoundingType { + FLOOR = 0, + CEIL = 1, +}; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const RoundingType& type); + +/// \brief Specifies the algorithm to use for implicit broadcasting of a tensor +/// to align with another tensor +/// +/// NONE - No implicit broadcasting of tensor +/// NUMPY - Numpy-style implicit broadcasting +/// (https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) +/// Right-align dimensions of the two tensors, with missing dimensions +/// treated as size 1 dimensions. After alignment, for each dimension, +/// their sizes should either match or one of them should be of size 1. +/// Size 1 dimension will be implicitly broadcast to match the other +/// size. +/// +/// E.g., +/// A: Shape(2, 1, 6) +/// B: Shape( 3, 1) +/// Result: Shape(2, 3, 6) +/// +/// A: Shape(2, 1, 6) +/// B: Shape( 3, 1) +/// Result: Shape(2, 3, 6) +/// PDPD - PaddlePaddle-style implicit broadcasting +/// (https://github.com/PaddlePaddle/Paddle/blob/release/1.5/paddle/ +/// fluid/operators/elementwise/elementwise_op.h#L126) +/// Broadcast B to match the shape of A, where axis is the start +/// dimension index to align B with A. If axis is -1 (default), i +/// axis = rank(A) - rank(B). The trailing dimensions of size 1 for B +/// will be ignored. +/// +/// E.g., +/// A: Shape(2, 3, 4, 5) +/// B: Shape( 3, 4 ) with axis =1 +/// Result: Shape(2, 3, 4, 5) +/// +/// A: Shape(2, 3, 4, 5) +/// B: Shape( 3, 1 ) with axis = 1 +/// Result: Shape(2, 3, 4, 5) +/// +enum class AutoBroadcastType { + NONE = 0, + EXPLICIT = NONE, + NUMPY, + PDPD, +}; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const AutoBroadcastType& type); +/// \brief BroadcastType specifies rules used for mapping of input tensor axes to output +/// shape axes. +/// +/// \note Broadcasting rules are different for Broadcast op and for element-wise ops. +/// AutoBroadcastType::NUMPY is equivalent of BroadcastType::BIDIRECTIONAL +/// according to spec. +/// +/// EXPLICIT - Mapping of the input data shape to output shape +/// based on axes_mapping input. +/// NUMPY - Numpy broadcasting rules, aligned with ONNX Broadcasting. +/// (https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md) +/// PDPD - PaddlePaddle-style implicit broadcasting. +/// For more informaction see AutoBroadcastType documentation. +/// BIDIRECTIONAL - The broadcast rule is similar to +/// numpy.array(input) * numpy.ones(target_shape). +/// Dimensions are right alignment. +enum class BroadcastType { NONE, EXPLICIT = NONE, NUMPY, PDPD, BIDIRECTIONAL }; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const BroadcastType& type); + +/// \brief Specifies how eps is combined with L2 value +enum class EpsMode { + // Add bias to norm + ADD, + // Calculate max of norm and bias + MAX +}; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const EpsMode& type); + +enum class TopKSortType { + // Returned values are not sorte + NONE, + // Sort result based on element indices + SORT_INDICES, + // Sort result based on element values + SORT_VALUES, +}; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const TopKSortType& type); + +enum class TopKMode { + MAX, + MIN, +}; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const TopKMode& type); + +/// \brief Implicit broadcast specification +struct OPENVINO_API AutoBroadcastSpec { + AutoBroadcastSpec() : m_type(AutoBroadcastType::NONE), m_axis(0) {} + AutoBroadcastSpec(AutoBroadcastType type) : m_type(type), m_axis(0) {} + AutoBroadcastSpec(const char* type) : AutoBroadcastSpec(type_from_string(type)) {} + AutoBroadcastSpec(AutoBroadcastType type, int64_t axis) : m_type(type), m_axis(axis) {} + + AutoBroadcastType m_type; // Implicit broadcasting algorithm + int64_t m_axis; // Axis to start alignment on + + bool operator==(const AutoBroadcastSpec& a) const { + return a.m_type == m_type && a.m_axis == m_axis; + } + + bool operator!=(const AutoBroadcastSpec& a) const { + return !(*this == a); + } + static const AutoBroadcastSpec NUMPY; + static const AutoBroadcastSpec NONE; + +private: + AutoBroadcastType type_from_string(const std::string& type) const; +}; + +/// \brief Implicit broadcast specification +struct OPENVINO_API BroadcastModeSpec { + BroadcastModeSpec() : m_type(BroadcastType::NUMPY), m_axis(0) {} + BroadcastModeSpec(BroadcastType type) : m_type(type), m_axis(0) {} + BroadcastModeSpec(const char* type) : BroadcastModeSpec(as_enum(type)) {} + BroadcastModeSpec(BroadcastType type, int64_t axis) : m_type(type), m_axis(axis) {} + + BroadcastType m_type; // Implicit broadcasting algorithm + int64_t m_axis; // Axis to start alignment on + + bool operator==(const BroadcastModeSpec& a) const { + return a.m_type == m_type && a.m_axis == m_axis; + } +}; + +/// +/// \brief This class defines possible recurrent sequence directions. +/// +enum class RecurrentSequenceDirection { FORWARD, REVERSE, BIDIRECTIONAL }; + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const RecurrentSequenceDirection& direction); +} // namespace op + +template <> +class OPENVINO_API AttributeAdapter : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::PadMode& value) : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +template <> +class OPENVINO_API AttributeAdapter : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::PadType& value) : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +template <> +class OPENVINO_API AttributeAdapter : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::RoundingType& value) : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +template <> +class OPENVINO_API AttributeAdapter : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::AutoBroadcastType& value) : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +template <> +class OPENVINO_API AttributeAdapter : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::BroadcastType& value) : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +template <> +class OPENVINO_API AttributeAdapter : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::EpsMode& value) : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +template <> +class OPENVINO_API AttributeAdapter : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::TopKSortType& value) : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +template <> +class OPENVINO_API AttributeAdapter : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::TopKMode& value) : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 1}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +template <> +class AttributeAdapter : public VisitorAdapter { +public: + AttributeAdapter(op::AutoBroadcastSpec& value) : m_ref(value) {} + bool visit_attributes(AttributeVisitor& visitor) override; + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } + +protected: + op::AutoBroadcastSpec& m_ref; +}; + +template <> +class AttributeAdapter : public VisitorAdapter { +public: + AttributeAdapter(op::BroadcastModeSpec& value) : m_ref(value) {} + bool visit_attributes(AttributeVisitor& visitor) override; + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } + +protected: + op::BroadcastModeSpec& m_ref; +}; + +template <> +class OPENVINO_API AttributeAdapter + : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::RecurrentSequenceDirection& value) + : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 1}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/binary_elementwise_arithmetic.hpp b/ngraph/core/include/openvino/op/util/binary_elementwise_arithmetic.hpp new file mode 100644 index 00000000000000..c1fe1e3b916283 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/binary_elementwise_arithmetic.hpp @@ -0,0 +1,70 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/op.hpp" +#include "openvino/op/util/attr_types.hpp" + +namespace ov { +namespace op { +namespace util { +// clang-format off + /// \brief Abstract base class for elementwise binary arithmetic operations, i.e., + /// operations where the same scalar binary arithmetic operation is applied to + /// each corresponding pair of elements in the two input tensors. Implicit + /// broadcast of input tensors is supported through one of the AutoBroadcast + /// modes. + /// + /// For example, if the underlying arithmetic operation (determined by the subclass) is + /// \f$\mathit{op}(x,y)\f$, the input tensors + /// \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 + /// \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$. + /// + /// ## Inputs + /// + /// | | Type | Description | + /// | ------ | --------------------------------- | ------------------------------------------------------------------------ | + /// | `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. | + /// | `arg1` | \f$N[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of the same element type as `arg0`. | + /// | `autob`| AutoBroadcastSpec | Auto broadcast specification. | + /// + /// ## Output + /// + /// | Type | Description | + /// | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | + /// | \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). | +// clang-format on +class OPENVINO_API BinaryElementwiseArithmetic : public Op { +protected: + BinaryElementwiseArithmetic(const AutoBroadcastSpec& autob); + + /// \brief Constructs a binary elementwise arithmetic operation. + /// + /// \param arg0 Output that produces the first input tensor. + /// \param arg1 Output that produces the second input tensor. + BinaryElementwiseArithmetic(const Output& arg0, const Output& arg1, const AutoBroadcastSpec& autob); + +public: + OPENVINO_RTTI_DECLARATION; + + void validate_and_infer_types() override; + + const AutoBroadcastSpec& get_autob() const override { + return m_autob; + } + void set_autob(const AutoBroadcastSpec& autob) { + m_autob = autob; + } + bool visit_attributes(AttributeVisitor& visitor) override; + bool evaluate_lower(const HostTensorVector& outputs) const override; + bool evaluate_upper(const HostTensorVector& outputs) const override; + +private: + AutoBroadcastSpec m_autob; + void validate_and_infer_elementwise_arithmetic(const op::AutoBroadcastSpec& autob); +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/binary_elementwise_comparison.hpp b/ngraph/core/include/openvino/op/util/binary_elementwise_comparison.hpp new file mode 100644 index 00000000000000..ffbf50e103bc49 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/binary_elementwise_comparison.hpp @@ -0,0 +1,71 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/op.hpp" +#include "openvino/op/util/attr_types.hpp" + +namespace ov { +namespace op { +namespace util { +// clang-format off + /// \brief Abstract base class for elementwise binary comparison operations, i.e., + /// operations where the same scalar binary comparison operation is applied to + /// each corresponding pair of elements in two input tensors. Implicit + /// broadcast of input tensors is supported through one of the AutoBroadcast + /// modes. + /// + /// For example, if the underlying comparison operation (determined by the subclass) is + /// \f$\mathit{op}(x,y)\f$, the input tensors \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 + /// \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$. + /// + /// ## Inputs + /// + /// | | Type | Description | + /// | ------ | --------------------------------- | ------------------------------------------------------ | + /// | `arg0` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of any shape and element type. | + /// | `arg1` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of the same shape and element type as `arg0`. | + /// | `autob`| AutoBroadcastSpec | Auto broadcast specification. | + /// + /// ## Output + /// + /// | Type | Description | + /// | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | + /// | \f$\texttt{bool}[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 as the input tensors, and the element type `bool`. | +// clang-format on +class OPENVINO_API BinaryElementwiseComparison : public Op { +protected: + /// \brief Constructs a binary elementwise comparison operation. + BinaryElementwiseComparison(const AutoBroadcastSpec& autob); + + /// \brief Constructs a binary elementwise comparison operation. + /// + /// \param arg0 Output that produces the first input tensor. + /// \param arg1 Output that produces the second input tensor. + /// \param autob AutoBroadcast mode. + BinaryElementwiseComparison(const Output& arg0, + const Output& arg1, + const AutoBroadcastSpec& autob = AutoBroadcastSpec()); + +public: + OPENVINO_RTTI_DECLARATION; + + void validate_and_infer_types() override; + + const AutoBroadcastSpec& get_autob() const override { + return m_autob; + } + void set_autob(const AutoBroadcastSpec& autob) { + m_autob = autob; + } + bool visit_attributes(AttributeVisitor& visitor) override; + +private: + AutoBroadcastSpec m_autob; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/binary_elementwise_logical.hpp b/ngraph/core/include/openvino/op/util/binary_elementwise_logical.hpp new file mode 100644 index 00000000000000..c2dc6f999f775d --- /dev/null +++ b/ngraph/core/include/openvino/op/util/binary_elementwise_logical.hpp @@ -0,0 +1,68 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/op.hpp" + +namespace ov { +namespace op { +namespace util { +// clang-format off + /// \brief Abstract base class for elementwise binary logical operations, i.e., + /// operations where the same scalar binary logical operation is applied to + /// each corresponding pair of elements in two boolean input tensors. Implicit + /// broadcast of input tensors is supported through one of the AutoBroadcast + /// modes. + /// + /// For example, if the underlying operation (determined by the subclass) is + /// \f$\mathit{op}(x,y)\f$, the input tensors \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 + /// \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$. + /// + /// ## Inputs + /// + /// | | Type | Description | + /// | ------ | --------------------------------------------- | ------------------------------------------------------ | + /// | `arg0` | \f$\texttt{bool}[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of any shape, with element type `bool`. | + /// | `arg1` | \f$\texttt{bool}[d_1,\dots,d_n]~(n \geq 0)\f$ | A tensor of the same shape and element type as `arg0`. | + /// | `autob`| AutoBroadcastSpec | Auto broadcast specification. | + /// + /// ## Output + /// + /// | Type | Description | + /// | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | + /// | \f$\texttt{bool}[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 as the input tensors, and the element type `bool`. | +// clang-format on +class OPENVINO_API BinaryElementwiseLogical : public Op { +protected: + OPENVINO_RTTI_DECLARATION; + + BinaryElementwiseLogical(); + + /// \brief Constructs a binary elementwise logical operation. + /// + /// \param arg0 Output that produces the first input tensor. + /// \param arg1 Output that produces the second input tensor. + BinaryElementwiseLogical(const Output& arg0, + const Output& arg1, + const AutoBroadcastSpec& autob = AutoBroadcastSpec()); + +public: + void validate_and_infer_types() override; + + const AutoBroadcastSpec& get_autob() const override { + return m_autob; + } + void set_autob(const AutoBroadcastSpec& autob) { + m_autob = autob; + } + bool visit_attributes(AttributeVisitor& visitor) override; + +private: + AutoBroadcastSpec m_autob = AutoBroadcastSpec::NUMPY; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/broadcast_base.hpp b/ngraph/core/include/openvino/op/util/broadcast_base.hpp new file mode 100644 index 00000000000000..10f436ccafb4dd --- /dev/null +++ b/ngraph/core/include/openvino/op/util/broadcast_base.hpp @@ -0,0 +1,84 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// +#pragma once + +#include "openvino/core/axis_set.hpp" +#include "openvino/core/axis_vector.hpp" +#include "openvino/op/op.hpp" +#include "openvino/op/util/attr_types.hpp" + +namespace ov { +namespace op { +namespace util { +class OPENVINO_API BroadcastBase : public Op { +protected: + BroadcastBase() = default; + /// \brief Constructs a broadcast operation. + /// + /// \param arg The input tensor to be broadcast. + /// \param target_shape The shape of the output tensor. + /// \param axes_mapping The axis positions (0-based) in the result that correspond + /// to input axes. + /// \param broadcast_mode Broadcast specification to use for determining broadcast + /// axes. 'axes_mapping' should not be provided if mode other + /// + BroadcastBase(const Output& arg, + const Output& target_shape, + const Output& axes_mapping, + const BroadcastModeSpec& broadcast_mode = BroadcastType::EXPLICIT); + + /// \brief Constructs a broadcast operation. + /// + /// \param arg The input tensor to be broadcast. + /// \param target_shape The shape of the output tensor. + /// \param broadcast_mode Broadcast specification to use for determining broadcast + /// axes + BroadcastBase(const Output& arg, + const Output& target_shape, + const BroadcastModeSpec& broadcast_mode = BroadcastType::NUMPY); + +public: + OPENVINO_RTTI_DECLARATION; + + void validate_and_infer_types() override; + /// \return true and the AxisSet if broadcast axes can be fully determined. + virtual std::pair get_broadcast_axes() const; + + bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override; + +protected: + BroadcastModeSpec m_mode; + + bool evaluate_broadcast(const HostTensorPtr& arg0, + const HostTensorPtr& out, + const std::pair pair_broadcast_axes, + const ngraph::Shape output_shape) const; + + bool evaluate_broadcast(const HostTensorPtr& arg0, const HostTensorPtr& out, const AxisSet& broadcast_axes) const; + + bool evaluate_lower(const HostTensorVector& outputs) const override; + bool evaluate_upper(const HostTensorVector& outputs) const override; + + PartialShape get_result_shape_pdpd(const PartialShape& arg0_shape, + const PartialShape& target_shape, + const op::BroadcastModeSpec& broadcast_spec) const; + + void validate_target_shape_numpy(const PartialShape& arg_shape, const PartialShape& target_shape) const; + + static std::pair get_broadcast_axes_numpy_pdpd(const ngraph::Shape& arg_shape, + const ngraph::Shape& result_shape, + const op::BroadcastModeSpec& broadcast_spec); + + static std::pair get_broadcast_axes_none(const AxisVector axes_mapping_val, + const size_t target_shape); + + void validate_target_shape_none(const PartialShape& arg_shape, + const AxisVector& axes_mapping_val, + const PartialShape& target_shape) const; + + ngraph::Shape get_target_shape(const HostTensorPtr& input1) const; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/deformable_convolution_base.hpp b/ngraph/core/include/openvino/op/util/deformable_convolution_base.hpp new file mode 100644 index 00000000000000..341f6ac82a12a7 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/deformable_convolution_base.hpp @@ -0,0 +1,105 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/core/coordinate_diff.hpp" +#include "openvino/op/op.hpp" +#include "openvino/op/util/attr_types.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief Base class for operations DeformableConvolution v1 and DeformableConvolution +/// v8. +class OPENVINO_API DeformableConvolutionBase : public Op { +public: + OPENVINO_RTTI_DECLARATION; + + /// \brief Constructs a conversion operation. + DeformableConvolutionBase() = default; + + /// \brief Constructs a conversion operation. + /// \param strides Convolution strides. + /// \param pads_begin Amount of padding to be added to the beginning along + /// each axis. For example in case of a 2D input the value + /// of (1, 2) means that 1 element will be added to the + /// top and 2 elements to the left. + /// \param pads_end Amount of padding to be added to the end along each + /// axis. + /// \param dilations The distance in width and height between the weights + /// in the filters tensor. + /// \param auto_pad Specifies how the automatic calculation of padding + /// should be done. + /// \param group The number of groups which both output and input + /// should be split into. + /// \param deformable_group The number of groups which deformable values and + /// output should be split into along the channel axis. + DeformableConvolutionBase(const OutputVector& arguments, + const Strides& strides, + const CoordinateDiff& pads_begin, + const CoordinateDiff& pads_end, + const Strides& dilations, + const PadType& auto_pad = PadType::EXPLICIT, + int64_t group = 1, + int64_t deformable_group = 1); + + bool visit_attributes(AttributeVisitor& visitor) override; + void validate_and_infer_types() override; + + const Strides& get_strides() const { + return m_strides; + } + void set_strides(const Strides& strides) { + m_strides = strides; + } + const Strides& get_dilations() const { + return m_dilations; + } + void set_dilations(const Strides& dilations) { + m_dilations = dilations; + } + const CoordinateDiff& get_pads_begin() const { + return m_pads_begin; + } + void set_pads_begin(const CoordinateDiff& pads_begin) { + m_pads_begin = pads_begin; + } + const CoordinateDiff& get_pads_end() const { + return m_pads_end; + } + void set_pads_end(const CoordinateDiff& pads_end) { + m_pads_end = pads_end; + } + const PadType& get_auto_pad() const { + return m_auto_pad; + } + void set_auto_pad(const PadType& auto_pad) { + m_auto_pad = auto_pad; + } + int64_t get_group() const { + return m_group; + } + void set_group(const int64_t group) { + m_group = group; + } + int64_t get_deformable_group() const { + return m_deformable_group; + } + void set_deformable_group(const int64_t deformable_group) { + m_deformable_group = deformable_group; + } + +protected: + Strides m_strides; + Strides m_dilations; + CoordinateDiff m_pads_begin; + CoordinateDiff m_pads_end; + PadType m_auto_pad; + int64_t m_group; + int64_t m_deformable_group; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/elementwise_args.hpp b/ngraph/core/include/openvino/op/util/elementwise_args.hpp new file mode 100644 index 00000000000000..90961bee7ac7eb --- /dev/null +++ b/ngraph/core/include/openvino/op/util/elementwise_args.hpp @@ -0,0 +1,17 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/core/node.hpp" + +namespace ov { +namespace op { +namespace util { +std::tuple validate_and_infer_elementwise_args( + Node* node, + const op::AutoBroadcastSpec& autob = op::AutoBroadcastSpec()); +} +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/embeddingbag_offsets_base.hpp b/ngraph/core/include/openvino/op/util/embeddingbag_offsets_base.hpp new file mode 100644 index 00000000000000..c7a6f5d7147768 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/embeddingbag_offsets_base.hpp @@ -0,0 +1,66 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/core/axis_set.hpp" +#include "openvino/op/util/index_reduction.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief Returns embeddings for given indices +class OPENVINO_API EmbeddingBagOffsetsBase : public Op { +public: + static constexpr NodeTypeInfo type_info{"EmbeddingBagOffsetsBase", 3}; + const NodeTypeInfo& get_type_info() const override { + return type_info; + } + /// \brief Constructs a EmbeddingBagOffsetsBase operation. + EmbeddingBagOffsetsBase() = default; + /// \brief Constructs a EmbeddingBagOffsetsBase operation. + /// + /// EmbeddingBagOffsetsBase constructs an output tensor by replacing every index in + /// a + /// given + /// input tensor with a row (from the weights matrix) at that index + /// + /// \param emb_table tensor containing the embedding lookup table of the module of + /// shape [num_emb, emb_dim1, emb_dim2, ...] and of type T + /// \param tensor of shape [num_indices] and of type T_IND. Required + /// \param offsets tensor of shape [batch] and of type T_IND containing the starting + /// index positions of each "bag" in indices. Required. + /// \param per_sample_weigths tensor of the same shape as indices and of type T. + /// Each value in this tensor are multiplied with each + /// value pooled from embedding table for each index. Optional. + /// \param default_index scalar of type T_IND containing default index in embedding + /// table to fill empty "bags". If not provided empty "bags" + /// are filled with zeros. Optional. + + EmbeddingBagOffsetsBase(const Output& emb_table, + const Output& indices, + const Output& offsets, + const Output& default_index, + const Output& per_sample_weights); + + EmbeddingBagOffsetsBase(const Output& emb_table, + const Output& indices, + const Output& offsets, + const Output& default_index); + + EmbeddingBagOffsetsBase(const Output& emb_table, const Output& indices, const Output& offsets); + + void validate_and_infer_types() override; + bool visit_attributes(AttributeVisitor& visitor) override; + +private: + static constexpr int EMB_TABLE = 0; + static constexpr int INDICES = 1; + static constexpr int OFFSETS = 2; + static constexpr int DEFAULT_INDEX = 3; + static constexpr int PER_SAMPLE_WEIGHTS = 4; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/embeddingbag_packed_base.hpp b/ngraph/core/include/openvino/op/util/embeddingbag_packed_base.hpp new file mode 100644 index 00000000000000..9e23eb38962796 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/embeddingbag_packed_base.hpp @@ -0,0 +1,52 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/core/axis_set.hpp" +#include "openvino/op/util/index_reduction.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief Returns embeddings for given indices +class OPENVINO_API EmbeddingBagPackedBase : public Op { +public: + static constexpr NodeTypeInfo type_info{"EmbeddingBagPackedBase", 3}; + const NodeTypeInfo& get_type_info() const override { + return type_info; + } + /// \brief Constructs a EmbeddingBagPackedBase operation. + EmbeddingBagPackedBase() = default; + /// \brief Constructs a EmbeddingBagPackedBase operation. + /// + /// EmbeddingBagPackedBase constructs an output tensor by replacing every index in a + /// given + /// input tensor with a row (from the weights matrix) at that index + /// + /// \param emb_table Tensor containing the embedding lookup table of the module of + /// shape [num_emb, emb_dim1, emb_dim2, ...] and of type T + /// \param indices Tensor of shape `[batch, indices_per_bag]` and of type *T_IND*. + /// Required. + /// \param per_sample_weigths tensor of the same shape as indices and of type T. + /// Each value in this tensor are multiplied with each + /// value pooled from embedding table for each index. Optional. + + EmbeddingBagPackedBase(const Output& emb_table, + const Output& indices, + const Output& per_sample_weights); + + EmbeddingBagPackedBase(const Output& emb_table, const Output& indices); + + void validate_and_infer_types() override; + bool visit_attributes(AttributeVisitor& visitor) override; + +private: + static constexpr int EMB_TABLE = 0; + static constexpr int INDICES = 1; + static constexpr int PER_SAMPLE_WEIGHTS = 2; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/fft_base.hpp b/ngraph/core/include/openvino/op/util/fft_base.hpp new file mode 100644 index 00000000000000..7e9f959520b278 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/fft_base.hpp @@ -0,0 +1,40 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "ngraph/op/op.hpp" +#include "ngraph/op/util/attr_types.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief Base class for operations DFT and DFT. +class OPENVINO_API FFTBase : public Op { +public: + OPENVINO_RTTI_DECLARATION; + FFTBase() = default; + + void validate_and_infer_types() override; + bool visit_attributes(AttributeVisitor& visitor) override; + +protected: + /// \brief Constructs an FFT operation. FFT is performed for full size axes. + /// + /// \param data Input data + /// \param axes Axes to perform FFT + FFTBase(const Output& data, const Output& axes); + + /// \brief Constructs a FFT operation. + /// + /// \param data Input data + /// \param axes Axes to perform FFT + /// \param signal_size Signal sizes for 'axes' + FFTBase(const Output& data, const Output& axes, const Output& signal_size); + + void validate(); +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/gather_base.hpp b/ngraph/core/include/openvino/op/util/gather_base.hpp new file mode 100644 index 00000000000000..694f17830bb7e0 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/gather_base.hpp @@ -0,0 +1,42 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/op.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief GatherBase basic class for Gather v1 and v7 +class OPENVINO_API GatherBase : public Op { +public: + OPENVINO_RTTI_DECLARATION; + GatherBase() = default; + + /// \param data The tensor from which slices are gathered + /// \param indices Tensor with indexes to gather + /// \param axis The tensor is a dimension index to gather data from + /// \param batch_dims The number of batch dimension in data and indices tensors + GatherBase(const Output& data, + const Output& indices, + const Output& axis, + const int64_t batch_dims = 0); + + void validate_and_infer_types() override; + virtual int64_t get_axis() const; + + bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override; + + bool evaluate_lower(const HostTensorVector& outputs) const override; + bool evaluate_upper(const HostTensorVector& outputs) const override; + + bool constant_fold(OutputVector& output_values, const OutputVector& inputs_values) override; + +protected: + int64_t m_batch_dims = 0; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/index_reduction.hpp b/ngraph/core/include/openvino/op/util/index_reduction.hpp new file mode 100644 index 00000000000000..adf1168edba435 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/index_reduction.hpp @@ -0,0 +1,37 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include +#include + +#include "openvino/op/op.hpp" + +namespace ov { +namespace op { +namespace util { +class NGRAPH_API IndexReduction : public Op { +protected: + IndexReduction(); + + IndexReduction(const Output& arg, uint64_t axis, const element::Type& index_element_type); + +public: + uint64_t get_reduction_axis() const; + void set_reduction_axis(uint64_t value); + element::Type get_index_element_type() const; + void set_index_element_type(const element::Type& index_element_type); + void validate_and_infer_types() override; + bool visit_attributes(AttributeVisitor& visitor) override; + +protected: + uint64_t m_axis{0}; + element::Type m_index_element_type; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/logical_reduction.hpp b/ngraph/core/include/openvino/op/util/logical_reduction.hpp new file mode 100644 index 00000000000000..75cd638725b307 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/logical_reduction.hpp @@ -0,0 +1,46 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/op.hpp" +#include "openvino/op/util/reduction_base.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief Abstract base class for logical reduction operations, i.e., operations where +/// chosen axes of the input tensors are eliminated (reduced out) by repeated +/// application of a particular binary logical operation. +class OPENVINO_API LogicalReduction : public ReductionBase { +protected: + /// \brief Constructs a logical reduction operation. + LogicalReduction(); + /// \brief Constructs a logical reduction operation. + /// + /// \param arg Output that produces the first input tensor. + /// \param reduction_axes The axis positions (0-based) to be eliminated. + LogicalReduction(const Output& arg, const AxisSet& reduction_axes); + /// \brief Constructs a 'dynamic' logical reduction operation. + /// + /// \param arg Node that produces the first input tensor. + /// \param reduction_axes The axis positions (0-based) to be eliminated. + LogicalReduction(const Output& arg, const Output& reduction_axes); + +public: + OPENVINO_RTTI_DECLARATION; + void validate_and_infer_types() override; + + /// \return true if reduction axes are constant else false. + bool reduction_axes_constant() const; + + /// \return The axis positions (0-based) to be eliminated through reduction. + /// \throws CheckFailure if the reduction axes are not constant. (Use + /// reduction_axes_constant to check.) + const AxisSet get_reduction_axes() const; + void set_reduction_axes(const AxisSet& reduction_axes); +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/logical_reduction_keep_dims.hpp b/ngraph/core/include/openvino/op/util/logical_reduction_keep_dims.hpp new file mode 100644 index 00000000000000..a9ce1ed1c6edb6 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/logical_reduction_keep_dims.hpp @@ -0,0 +1,42 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/op.hpp" +#include "openvino/op/util/logical_reduction.hpp" + +namespace ov { +namespace op { +namespace util { +class OPENVINO_API LogicalReductionKeepDims : public util::LogicalReduction { +protected: + LogicalReductionKeepDims() = default; + + /// \param arg The tensor to be reduced. + /// \param reduction_axes The axis positions (0-based) to be eliminated. + /// \param keep_dims If set to 1 it holds axes that are used for reduction. + LogicalReductionKeepDims(const Output& arg, const Output& reduction_axes, const bool keep_dims = false); + + bool visit_attributes(AttributeVisitor& visitor) override; + +public: + OPENVINO_RTTI_DECLARATION; + void validate_and_infer_types() override; + + /// \return If set to 1 it holds axes that are used for reduction. + /// For each such axis, output dimension is equal to 1. + bool get_keep_dims() const { + return m_keep_dims; + } + void set_keep_dims(bool keep_dims) { + m_keep_dims = keep_dims; + } + +private: + bool m_keep_dims = false; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/max_pool_base.hpp b/ngraph/core/include/openvino/op/util/max_pool_base.hpp new file mode 100644 index 00000000000000..99eb04b7c4fd53 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/max_pool_base.hpp @@ -0,0 +1,96 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/op.hpp" +#include "openvino/op/util/attr_types.hpp" + +namespace ov { +namespace op { +namespace util { +class OPENVINO_API MaxPoolBase : public Op { +public: + OPENVINO_RTTI_DECLARATION; + MaxPoolBase() = default; + + /// \param arg The node producing the input data batch tensor. + /// \param strides The strides. + /// \param pads_begin The beginning of padding shape. + /// \param pads_end The end of padding shape. + /// \param kernel The kernel shape. + /// \param rounding_mode Whether to use ceiling or floor rounding type while + /// computing output shape. + /// \param auto_pad The pad type for automatically computing padding sizes. + MaxPoolBase(const Output& arg, + const Strides& strides, + const ngraph::Shape& pads_begin, + const ngraph::Shape& pads_end, + const ngraph::Shape& kernel, + const op::RoundingType rounding_mode = op::RoundingType::FLOOR, + const PadType auto_pad = op::PadType::EXPLICIT); + + void validate_and_infer_types() override; + + /// \return The kernel shape. + const ngraph::Shape& get_kernel() const { + return m_kernel; + } + void set_kernel(const ngraph::Shape& kernel) { + m_kernel = kernel; + } + /// \return The strides. + const Strides& get_strides() const { + return m_strides; + } + void set_strides(const Strides& strides) { + m_strides = strides; + } + /// \return The beginning of padding shape. + const ngraph::Shape& get_pads_begin() const { + return m_pads_begin; + } + void set_pads_begin(const ngraph::Shape& pads_begin) { + m_pads_begin = pads_begin; + } + /// \return The end of padding shape. + const ngraph::Shape& get_pads_end() const { + return m_pads_end; + } + void set_adding_above(const ngraph::Shape& pads_end) { + m_pads_end = pads_end; + } + /// \return The pad type for pooling. + PadType get_auto_pad() const { + return m_auto_pad; + } + void set_auto_pad(const PadType auto_pad) { + m_auto_pad = auto_pad; + } + /// \return The ceiling mode being used for output shape computations + op::RoundingType get_rounding_type() const { + return m_rounding_type; + } + void set_rounding_type(op::RoundingType rounding_type) { + m_rounding_type = rounding_type; + } + +protected: + bool update_auto_padding(const PartialShape& in_shape, + const Strides& filter_dilations, + ngraph::Shape& new_pads_end, + ngraph::Shape& new_pads_begin) const; + + PartialShape infer_output_shape(const Strides& dilations); + + ngraph::Shape m_kernel; + Strides m_strides; + ngraph::Shape m_pads_begin; + ngraph::Shape m_pads_end; + PadType m_auto_pad; + op::RoundingType m_rounding_type; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/multi_subgraph_base.hpp b/ngraph/core/include/openvino/op/util/multi_subgraph_base.hpp new file mode 100644 index 00000000000000..95fc65bc59e661 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/multi_subgraph_base.hpp @@ -0,0 +1,313 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "ngraph/op/parameter.hpp" +#include "openvino/core/function.hpp" +#include "openvino/op/op.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief Abstract base class for sub-graph based ops, i.e ops that have some +/// sub-graphs +/// +class OPENVINO_API MultiSubGraphOp : public Op { +public: + OPENVINO_RTTI_DECLARATION; + /// \brief Abstract class describes a connection between a MultiSubGraphOp input and + /// the body. + class InputDescription { + protected: + /// + /// \brief Constructs a new instance. + /// + /// \param input_index Position of the MultiSubGraphOp input + /// \param body_parameter_index Body parameter to receive input + /// + InputDescription(uint64_t input_index, uint64_t body_parameter_index); + InputDescription() = default; + + public: + using Ptr = std::shared_ptr; + using type_info_t = DiscreteTypeInfo; + virtual ~InputDescription() = default; + virtual std::shared_ptr copy() const = 0; + + virtual const type_info_t& get_type_info() const = 0; + + uint64_t m_input_index{0}; + uint64_t m_body_parameter_index{0}; + }; + + /// \brief Abstract class describes how a MultiSubGraphOp output is produced from + /// the body. + class OutputDescription { + protected: + /// + /// \brief Constructs a new instance. + /// + /// \param body_value_index A body value that produces the output + /// \param output_index The MultiSubGraphOp output index + /// + OutputDescription(uint64_t body_value_index, uint64_t output_index); + OutputDescription() = default; + + public: + using Ptr = std::shared_ptr; + using type_info_t = DiscreteTypeInfo; + virtual ~OutputDescription() = default; + virtual std::shared_ptr copy() const = 0; + virtual const type_info_t& get_type_info() const = 0; + + uint64_t m_body_value_index{0}; + uint64_t m_output_index{0}; + }; + + /// + /// \brief Describes a body input formed from slices of an input to + /// MultiSubGraphOp. + /// + class OPENVINO_API SliceInputDescription : public InputDescription { + public: + OPENVINO_RTTI_DECLARATION; + /// + /// \brief Constructs a new instance. + /// + /// \param input_index Position of the MultiSubGraphOp input + /// \param body_parameter_index Body parameter position to receive input + /// \param start First index for slices + /// \param stride Step amount for slices + /// \param part_size Width of slices + /// \param end Last index for slices + /// \param axis Axis being sliced + /// + SliceInputDescription(uint64_t input_index, + uint64_t body_parameter_index, + int64_t start, + int64_t stride, + int64_t part_size, + int64_t end, + int64_t axis); + SliceInputDescription() = default; + std::shared_ptr copy() const override; + int64_t m_start{0}; + int64_t m_stride{0}; + int64_t m_part_size{0}; + int64_t m_end{0}; + int64_t m_axis{0}; + }; + + /// + /// \brief Describes a body input initialized from a MultiSubGraphOp input + /// on the first iteration, and then a body output thereafter. + /// + class OPENVINO_API MergedInputDescription : public InputDescription { + public: + OPENVINO_RTTI_DECLARATION; + /// + /// \brief Constructs a new instance. + /// + /// \param input_index Position of the MultiSubGraphOp input + /// supplying a value to body_parameter for + /// the initial iteration. + /// \param body_parameter_index Body parameter position to receive input. + /// \param body_value_index Body value to supply body_parameter for + /// successive + /// iterations. + /// + MergedInputDescription(uint64_t input_index, uint64_t body_parameter_index, uint64_t body_value_index); + MergedInputDescription() = default; + std::shared_ptr copy() const override; + uint64_t m_body_value_index{0}; + }; + + /// \brief Produces an output by concatenating an output from each iteration + class OPENVINO_API ConcatOutputDescription : public OutputDescription { + public: + OPENVINO_RTTI_DECLARATION; + /// + /// \brief Constructs a new instance. + /// + /// \param body_value_index A body value that produces the output + /// \param output_index The MultiSubGraphOp output index + /// \param start First index for slices + /// \param stride Step amount for slices + /// \param part_size Width of slices + /// \param end Last index for slices + /// \param axis Axis being sliced + /// + ConcatOutputDescription(uint64_t body_value_index, + uint64_t output_index, + int64_t start, + int64_t stride, + int64_t part_size, + int64_t end, + int64_t axis); + ConcatOutputDescription() = default; + + std::shared_ptr copy() const override; + int64_t m_start{0}; + int64_t m_stride{0}; + int64_t m_part_size{0}; + int64_t m_end{0}; + int64_t m_axis{0}; + }; + + /// \brief Produces an input + class OPENVINO_API InvariantInputDescription : public InputDescription { + public: + OPENVINO_RTTI_DECLARATION; + /// + /// \brief Constructs a new instance. + /// + /// \param input_index Position of the MultiSubGraphOp input + /// \param body_parameter_index Body parameter to receive input + /// + InvariantInputDescription(uint64_t input_index, uint64_t body_parameter_index); + InvariantInputDescription() = default; + std::shared_ptr copy() const override; + }; + + /// \brief Produces an output from a specific iteration + class OPENVINO_API BodyOutputDescription : public MultiSubGraphOp::OutputDescription { + public: + OPENVINO_RTTI_DECLARATION; + /// + /// \brief Constructs a new instance. + /// + /// \param body_value_index A body value that produces the output + /// \param output_index The SubGraphOp output index + /// \param iteration which iteration (typically -1, final) will + /// supply the value + /// + BodyOutputDescription(uint64_t body_value_index, uint64_t output_index, int64_t iteration = -1); + BodyOutputDescription() = default; + std::shared_ptr copy() const override; + int64_t m_iteration{0}; + }; + using MultiSubgraphInputDescriptionVector = std::vector; + using MultiSubgraphOutputDescriptionVector = std::vector; + + /// \brief Gets internal sub-graph by index in MultiSubGraphOp + /// + /// \param index sub-graph's index in op + /// \return pointer to Function with sub-graph + virtual const std::shared_ptr& get_function(int index) const { + return m_bodies[index]; + }; + /// \brief Adds sub-graph to MultiSubGraphOp + /// + /// \param index index of new sub-graph + /// \param func func new sub_graph as Function + virtual void set_function(int index, const std::shared_ptr& func) { + m_bodies[index] = func; + } + /// \brief Gets vector with connections beewtwen operation inputs + /// and internal sub-graph parameters + /// + /// \param index index of internal sub-graph + /// \return vector of input descriptions + const MultiSubgraphInputDescriptionVector& get_input_descriptions(int index) const { + return m_input_descriptions[index]; + } + /// \brief Gets vector with connections beewtwen operation inputs + /// and internal sub-graph parameters + /// + /// \param index index of internal sub-graph + /// \return vector of input descriptions + MultiSubgraphInputDescriptionVector& get_input_descriptions(int index) { + return m_input_descriptions[index]; + } + /// \brief Gets vector with connections beewtwen operation outputs + /// and internal sub-graph results + /// + /// \param index index of internal sub-graph + /// \return vector of output descriptions + const MultiSubgraphOutputDescriptionVector& get_output_descriptions(int index) const { + return m_output_descriptions[index]; + } + /// \brief Gets vector with connections beewtwen operation outputs + /// and internal sub-graph results + /// + /// \param index index of internal sub-graph + /// \return vector of output descriptions + MultiSubgraphOutputDescriptionVector& get_output_descriptions(int index) { + return m_output_descriptions[index]; + } + /// \brief Sets vector with connections beewtwen operation inputs + /// and internal sub-graph parameters + /// + /// \param index index of internal sub-graph + /// \param inputs vector of input descriptions + void set_input_descriptions(int index, const MultiSubgraphInputDescriptionVector& inputs) { + m_input_descriptions[index] = inputs; + } + + /// \brief Sets vector with connections beewtwen operation outputs + /// and internal sub-graph results + /// + /// \param index index of internal sub-graph + /// \param outputs vector of input descriptions + void set_output_descriptions(int index, const MultiSubgraphOutputDescriptionVector& outputs) { + m_output_descriptions[index] = outputs; + } + + /// + /// \brief Set input decriptions for MultiSubGraphOp input. + /// + /// \param value The value supplied as an input to the block. + /// \param bodies_parameters vector of bodies parameters. + virtual void set_invariant_inputs(const Output& value, const ngraph::ParameterVector& bodies_parameters); + /// + /// \brief Set output decriptions for MultiSubGraphOp output. + /// + /// \param bodies_results vector of bodies results for one output. + /// \return value Output node for bodies_results. + virtual Output set_body_outputs(const ResultVector& bodies_results); + + MultiSubGraphOp(const MultiSubGraphOp&) = delete; + MultiSubGraphOp(MultiSubGraphOp&&) = default; + + MultiSubGraphOp& operator=(const MultiSubGraphOp&) = delete; + MultiSubGraphOp& operator=(MultiSubGraphOp&&) = default; + +protected: + // Find an input corresponding to value, adding one if necessary. + Input input_for_value(const Output& value); + + MultiSubGraphOp(size_t number_of_bodies); + MultiSubGraphOp() = default; + MultiSubGraphOp(const OutputVector& args, size_t number_of_bodies); + explicit MultiSubGraphOp(const OutputVector& args); + + std::vector> m_bodies; + std::vector m_input_descriptions; + std::vector m_output_descriptions; +}; +} // namespace util +} // namespace op + +template <> +class OPENVINO_API AttributeAdapter>> + : public DirectValueAccessor>> { +public: + AttributeAdapter(std::vector>& value) + : DirectValueAccessor>>(value) {} + + OPENVINO_RTTI_DECLARATION; +}; + +template <> +class OPENVINO_API AttributeAdapter>> + : public DirectValueAccessor>> { +public: + AttributeAdapter(std::vector>& value) + : DirectValueAccessor>>(value) {} + + OPENVINO_RTTI_DECLARATION; +}; + +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/nms_base.hpp b/ngraph/core/include/openvino/op/util/nms_base.hpp new file mode 100644 index 00000000000000..ee7ca77708727e --- /dev/null +++ b/ngraph/core/include/openvino/op/util/nms_base.hpp @@ -0,0 +1,92 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "ngraph/op/op.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief Base class for operations NmsBase and MatrixNms +/// +class OPENVINO_API NmsBase : public Op { +public: + OPENVINO_RTTI_DECLARATION; + enum class SortResultType { + CLASSID, // sort selected boxes by class id (ascending) in each batch element + SCORE, // sort selected boxes by score (descending) in each batch element + NONE // do not guarantee the order in each batch element + }; + + NmsBase() = delete; + + /// \brief Constructs a NmsBase operation + /// + /// \param output_type Specifies the output tensor type + /// \param nms_top_k Specifies maximum number of boxes to be selected per + /// class, -1 meaning to keep all boxes + /// \param keep_top_k Specifies maximum number of boxes to be selected per + /// batch element, -1 meaning to keep all boxes + NmsBase(element::Type& output_type, int& nms_top_k, int& keep_top_k); + + /// \brief Constructs a NmsBase operation + /// + /// \param boxes Node producing the box coordinates + /// \param scores Node producing the box scores + /// \param output_type Specifies the output tensor type + /// \param nms_top_k Specifies maximum number of boxes to be selected per + /// class, -1 meaning to keep all boxes + /// \param keep_top_k Specifies maximum number of boxes to be selected per + /// batch element, -1 meaning to keep all boxes + NmsBase(const Output& boxes, + const Output& scores, + element::Type& output_type, + int& nms_top_k, + int& keep_top_k); + + void validate_and_infer_types() override; + + const element::Type& get_output_type() const { + return m_output_type; + } + void set_output_type(const element::Type& output_type) { + m_output_type = output_type; + } + using Node::set_output_type; + + int get_nms_top_k() const { + return m_nms_top_k; + } + + int get_keep_top_k() const { + return m_keep_top_k; + } + +protected: + element::Type& m_output_type; + int& m_nms_top_k; + int& m_keep_top_k; + virtual void validate(); +}; +} // namespace util +} // namespace op + +OPENVINO_API +std::ostream& operator<<(std::ostream& s, const op::util::NmsBase::SortResultType& type); + +template <> +class OPENVINO_API AttributeAdapter + : public EnumAttributeAdapterBase { +public: + AttributeAdapter(op::util::NmsBase::SortResultType& value) + : EnumAttributeAdapterBase(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter", 1}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; + +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/op_types.hpp b/ngraph/core/include/openvino/op/util/op_types.hpp new file mode 100644 index 00000000000000..29cd0edbcd16d5 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/op_types.hpp @@ -0,0 +1,66 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#include "openvino/core/core_visibility.hpp" +#include "openvino/core/node.hpp" + +namespace ov { +namespace op { +namespace util { +OPENVINO_API +bool is_unary_elementwise_arithmetic(const Node* node); +OPENVINO_API +bool is_binary_elementwise_arithmetic(const Node* node); +OPENVINO_API +bool is_binary_elementwise_comparison(const Node* node); +OPENVINO_API +bool is_binary_elementwise_logical(const Node* node); + +OPENVINO_API +bool supports_auto_broadcast(const Node* node); + +OPENVINO_API +bool is_op(const Node* node); +OPENVINO_API +bool is_parameter(const Node* node); +OPENVINO_API +bool is_output(const Node* node); +OPENVINO_API +bool is_sink(const Node* node); +OPENVINO_API +bool is_constant(const Node* node); +OPENVINO_API +bool is_commutative(const Node* node); + +OPENVINO_API +bool is_unary_elementwise_arithmetic(const std::shared_ptr& node); +OPENVINO_API +bool is_binary_elementwise_arithmetic(const std::shared_ptr& node); +OPENVINO_API +bool is_binary_elementwise_comparison(const std::shared_ptr& node); +OPENVINO_API +bool is_binary_elementwise_logical(const std::shared_ptr& node); + +OPENVINO_API +bool supports_auto_broadcast(const std::shared_ptr& node); + +OPENVINO_API +bool is_op(const std::shared_ptr& node); +OPENVINO_API +bool is_parameter(const std::shared_ptr& node); +OPENVINO_API +bool is_output(const std::shared_ptr& node); +OPENVINO_API +bool is_sink(const std::shared_ptr& node); +OPENVINO_API +bool is_constant(const std::shared_ptr& node); +OPENVINO_API +bool is_commutative(const std::shared_ptr& node); +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/reduction_base.hpp b/ngraph/core/include/openvino/op/util/reduction_base.hpp new file mode 100644 index 00000000000000..6cbffbd4bcdfd3 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/reduction_base.hpp @@ -0,0 +1,36 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/core/core_visibility.hpp" +#include "openvino/op/op.hpp" + +namespace ov { +namespace op { +namespace util { +class OPENVINO_API ReductionBase : public Op { +protected: + /// \brief Constructs a reduction operation. + ReductionBase(); + + /// \brief Constructs a reduction operation. + /// + /// \param arg Output that produces the first input tensor. + /// \param reduction_axes The axis positions (0-based) to be eliminated. + ReductionBase(const Output& arg, const Output& reduction_axes); + + /// \brief Infers reduction operations output shape. + /// + /// \param[in] keep_dims Reduction operation keeps dimensions. + /// + /// \return Partial shape of the output. + PartialShape infer_reduction_output_shape(const bool keep_dims); + +public: + OPENVINO_RTTI_DECLARATION; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/rnn_cell_base.hpp b/ngraph/core/include/openvino/op/util/rnn_cell_base.hpp new file mode 100644 index 00000000000000..fb4bb757714e41 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/rnn_cell_base.hpp @@ -0,0 +1,162 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include +#include + +#include "openvino/core/node.hpp" +#include "openvino/op/op.hpp" +#include "openvino/op/util/activation_functions.hpp" + +namespace ov { +namespace op { +namespace util { +enum class LSTMWeightsFormat { + FICO, // IE + ICOF, // PyTorch + IFCO, // DNNL, TF, MxNet + IFOC, // Caffe + IOFC, // ONNX +}; + +/// +/// \brief Change data format of provided node. +/// +/// \param[in] node The input node to be permuted. +/// +/// +/// \param[in] from_format Original node weights format. +/// +/// +/// \param[in] to_format Weights format to convert to. +/// +/// \return Node representing reshaped tensor according to `to_format` weights +/// format. +/// +std::shared_ptr OPENVINO_API convert_lstm_node_format(const Output& node, + LSTMWeightsFormat from_format, + LSTMWeightsFormat to_format = LSTMWeightsFormat::FICO, + int64_t axis = 0); + +/// \brief Base class for all recurrent network cells. +/// +/// \note It holds all common attributes. +/// +class OPENVINO_API RNNCellBase : public Op { +public: + OPENVINO_RTTI_DECLARATION; + + /// + /// \brief Constructs a RNNCellBase class. + /// + /// \param[in] hidden_size The number of hidden units for recurrent cell. + /// \param[in] clip The value defining clipping range [-clip, clip] + /// on input of activation functions. + /// \param[in] activations The vector of activation functions used inside + /// recurrent cell. + /// \param[in] activations_alpha The vector of alpha parameters for activation + /// functions in order respective to activation list. + /// \param[in] activations_beta The vector of beta parameters for activation + /// functions in order respective to activation list. + /// + RNNCellBase(const OutputVector& args, + std::size_t hidden_size, + float clip, + const std::vector& activations, + const std::vector& activations_alpha, + const std::vector& activations_beta); + + RNNCellBase(); + ~RNNCellBase() override = default; + + /// + /// \brief Validates static rank and dimension for provided input parameters. + /// Additionally input_size dimension is checked for X and W inputs. + /// + /// + /// \param[in] input Vector with RNN-Cell op inputs in following order: + /// X, initial_hidden_state, W, R and B. + /// + void validate_input_rank_dimension(const std::vector& input); + + bool visit_attributes(AttributeVisitor& visitor) override; + std::size_t get_hidden_size() const { + return m_hidden_size; + } + float get_clip() const { + return m_clip; + } + const std::vector& get_activations() const { + return m_activations; + } + const std::vector& get_activations_alpha() const { + return m_activations_alpha; + } + const std::vector& get_activations_beta() const { + return m_activations_beta; + } + +protected: + /// + /// \brief Constructs activation function object. + /// + /// \param[in] idx The index of the activation function name. + /// + /// \return The object representing activation function. + /// + ActivationFunction get_activation_function(std::size_t idx) const; + /// + /// \brief Creates node with element-wise add operation with numpy + /// broadcasting. + /// + /// \param[in] lhs The left hand side argument node. + /// \param[in] rhs The right hand side argument node. + /// + /// \return Node with element-wise add operation. + /// + static std::shared_ptr add(const Output& lhs, const Output& rhs); + /// + /// \brief Creates node with element-wise subtract operation with numpy + /// broadcasting. + /// + /// \param[in] lhs The left hand side argument node. + /// \param[in] rhs The right hand side argument node. + /// + /// \return Node with element-wise subtract operation. + /// + static std::shared_ptr sub(const Output& lhs, const Output& rhs); + /// + /// \brief Creates node with element-wise multiply operation with numpy + /// broadcasting. + /// + /// \param[in] lhs The left hand side argument node. + /// \param[in] rhs The right hand side argument node. + /// + /// \return Node with element-wise multiply operation. + /// + static std::shared_ptr mul(const Output& lhs, const Output& rhs); + /// + /// \brief Creates node with element-wise clip operation with numpy + /// broadcasting. + /// + /// \param[in] data The input tensor for clipping. + /// + /// \return Node with element-wise clip operation. + /// + std::shared_ptr clip(const Output& data) const; + +protected: + std::size_t m_hidden_size; + float m_clip; + std::vector m_activations; + std::vector m_activations_alpha; + std::vector m_activations_beta; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/scatter_base.hpp b/ngraph/core/include/openvino/op/util/scatter_base.hpp new file mode 100644 index 00000000000000..6f12ee2b326db8 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/scatter_base.hpp @@ -0,0 +1,49 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/op.hpp" + +namespace ov { +namespace op { +namespace util { +/// +/// \brief Base class for ScatterXXX operators. +/// +class OPENVINO_API ScatterBase : public Op { +public: + static constexpr NodeTypeInfo type_info{"ScatterBase", 3}; + const NodeTypeInfo& get_type_info() const override { + return type_info; + } + void validate_and_infer_types() override; + bool visit_attributes(AttributeVisitor& visitor) override; + +protected: + ScatterBase() = default; + + /// + /// \brief Constructs ScatterBase object. + /// + /// \param inputs The input tensor to be updated. + /// \param indices The tensor with indexes which will be updated. + /// \param updates The tensor with update values. + /// \param[in] axis The axis at which elements will be updated. + /// + ScatterBase(const Output& inputs, + const Output& indices, + const Output& updates, + const Output& axis); + +private: + // Respective input ordinal number. + static constexpr int DATA = 0; + static constexpr int INDICES = 1; + static constexpr int UPDATES = 2; + static constexpr int AXIS = 3; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/scatter_nd_base.hpp b/ngraph/core/include/openvino/op/util/scatter_nd_base.hpp new file mode 100644 index 00000000000000..84f71a912619e4 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/scatter_nd_base.hpp @@ -0,0 +1,42 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "ngraph/op/op.hpp" + +namespace ov { +namespace op { +namespace util { +/// +/// \brief Base class for ScatterNDXXX operators. +/// +class OPENVINO_API ScatterNDBase : public Op { +public: + static constexpr NodeTypeInfo type_info{"ScatterNDBase", 3}; + const NodeTypeInfo& get_type_info() const override { + return type_info; + } + // Respective input ordinal number. + static constexpr int INPUTS = 0; + static constexpr int INDICES = 1; + static constexpr int UPDATES = 2; + void validate_and_infer_types() override; + bool visit_attributes(AttributeVisitor& visitor) override; + +protected: + ScatterNDBase() = default; + + /// + /// \brief Constructs ScatterNDBase object. + /// + /// \param inputs The input tensor to be updated. + /// \param indices The tensor with indexes which will be updated. + /// \param updates The tensor with update values. + /// + ScatterNDBase(const Output& inputs, const Output& indices, const Output& updates); +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/sub_graph_base.hpp b/ngraph/core/include/openvino/op/util/sub_graph_base.hpp new file mode 100644 index 00000000000000..f489330e1984a0 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/sub_graph_base.hpp @@ -0,0 +1,149 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "ngraph/op/parameter.hpp" +#include "ngraph/op/util/multi_subgraph_base.hpp" + +namespace ov { +namespace op { +namespace util { +/// \brief Abstract base class for sub-graph based ops, i.e ops that have only one +/// sub-graph +/// +class OPENVINO_API SubGraphOp : public MultiSubGraphOp { +public: + OPENVINO_RTTI_DECLARATION; + + virtual const std::shared_ptr& get_function() const { + return m_bodies[0]; + }; + virtual void set_function(const std::shared_ptr& func) { + m_bodies[0] = func; + }; + /// \return a reference to the input descriptions. + const std::vector>& get_input_descriptions() const { + return m_input_descriptions[0]; + } + /// \return a reference to the input descriptions. Can add input descriptions + /// before + /// validation. + std::vector>& get_input_descriptions() { + return m_input_descriptions[0]; + } + /// \return a reference to the output descriptions. + const std::vector>& get_output_descriptions() const { + return m_output_descriptions[0]; + } + /// \return a reference to the output descriptions. Can add output descriptions + /// before + /// validation. + std::vector>& get_output_descriptions() { + return m_output_descriptions[0]; + } + + /// + /// \brief Indicate that a body parameter comes from slices of a value + /// + /// \param parameter The parameter to receive the slices + /// \param value The value to be sliced. This will be added as an input to + /// SubGraphOp. + /// \param start First index on axis of the slicing + /// \param stride Stepping of the slice + /// \param part_size Size of the slice on axis + /// \param end The last index on axis of the slicing + /// \param axis The axis to slice along + /// + virtual void set_sliced_input(const std::shared_ptr& parameter, + const Output& value, + int64_t start, + int64_t stride, + int64_t part_size, + int64_t end, + int64_t axis); + /// + /// \brief Indicates that a body parameter has an initial value in the first + /// iteration and computed value thereafter + /// + /// \param[in] body_parameter The body parameter + /// \param initial_value Value for the parameter in first iteration. This + /// will be added as an input to Loop. + /// \param successive_value Value for the parameter in successive iterations. + /// The value is what is active in the most recent + /// completed iteration. + /// + virtual void set_merged_input(const std::shared_ptr& body_parameter, + const Output& initial_value, + const Output& successive_value); + /// + /// \brief Indicates that a body parameter has an invariant value during + /// iteration that may depend on values computed outside of the + /// iteration. + /// + /// \param body_parameter The body parameter + /// \param value The value supplied as an input to the block + /// + virtual void set_invariant_input(const std::shared_ptr& body_parameter, + const Output& value); + /// + /// \brief Gets a value for a particular iteration point + /// + /// \param body_value The value + /// \param iteration The iteration that supplies the value. Negative values + /// are from the last iteration. + /// Default value -1 (the last iteration). + /// + /// \return The iterator value. + /// + virtual Output get_iter_value(const Output& body_value, int64_t iteration = -1); + /// + /// \brief Concatenates slices from all iterations + /// + /// \param value The value supplying slice values from each iteration. + /// \param start First index on axis of the slicing + /// \param stride Stepping of the slice + /// \param part_size Size of the slice on axis + /// \param end The last index on axis of the slicing + /// \param axis The axis to slice along + /// + /// \return The concatenated slices. + /// + virtual Output get_concatenated_slices(const Output& value, + int64_t start, + int64_t stride, + int64_t part_size, + int64_t end, + int64_t axis); + + SubGraphOp(const SubGraphOp&) = delete; + SubGraphOp(SubGraphOp&&) = default; + + SubGraphOp& operator=(const SubGraphOp&) = delete; + SubGraphOp& operator=(SubGraphOp&&) = default; + + int64_t get_num_iterations() const { + return m_num_iterations; + } + +protected: + int64_t m_num_iterations = -1; // -1 means infinity for Loop op, inconsistent for TensorIterator + + // Find an input corresponding to value, adding one if necessary. + Input input_for_value(const Output& value); + + SubGraphOp(); + explicit SubGraphOp(const OutputVector& args); + +private: + using MultiSubGraphOp::get_function; + + using MultiSubGraphOp::set_function; +}; +using InputDescriptionVector = std::vector; +using OutputDescriptionVector = std::vector; +} // namespace util +} // namespace op + +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/unary_elementwise_arithmetic.hpp b/ngraph/core/include/openvino/op/util/unary_elementwise_arithmetic.hpp new file mode 100644 index 00000000000000..e17ec61ec04f3f --- /dev/null +++ b/ngraph/core/include/openvino/op/util/unary_elementwise_arithmetic.hpp @@ -0,0 +1,53 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/op.hpp" + +namespace ov { +namespace op { +namespace util { +// clang-format off + /// \brief Abstract base class for elementwise unary arithmetic operations, i.e., + /// operations where the same scalar arithmetic operation is applied to each + /// element. + /// + /// For example, if the underlying operation (determined by the subclass) is + /// \f$\mathit{op}(x)\f$, the input tensor \f$[[x,y],[z,w]]\f$ will be mapped to + /// \f$[[\mathit{op}(x),\mathit{op}(y)],[\mathit{op}(z),\mathit{op}(w)]]\f$. + /// + /// ## Inputs + /// + /// | | Type | Description | + /// | ----- | --------------------------------- | ------------------------------------------------------------------------ | + /// | `arg` | \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. | + /// + /// ## Output + /// + /// | Type | Description | + /// | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | + /// | \f$N[d_1,\dots,d_n]\f$ | The tensor \f$T\f$, where \f$T[i_1,\dots,i_n] = \mathit{op}(\texttt{arg}[i_1,\dots,i_n])\f$. This will always have the same shape and element type as the input tensor. | +// clang-format on +class OPENVINO_API UnaryElementwiseArithmetic : public Op { +protected: + /// \brief Constructs a unary elementwise arithmetic operation. + UnaryElementwiseArithmetic(); + /// \brief Constructs a unary elementwise arithmetic operation. + /// + /// \param arg Output that produces the input tensor. + UnaryElementwiseArithmetic(const Output& arg); + +public: + OPENVINO_RTTI_DECLARATION; + + void validate_and_infer_types() override; + bool visit_attributes(AttributeVisitor& visitor) override; + +private: + void validate_and_infer_elementwise_arithmetic(); +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/variable.hpp b/ngraph/core/include/openvino/op/util/variable.hpp new file mode 100644 index 00000000000000..b5faa267588d90 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/variable.hpp @@ -0,0 +1,60 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include + +#include "openvino/core/partial_shape.hpp" +#include "openvino/core/type.hpp" +#include "openvino/core/type/element_type.hpp" + +namespace ov { +namespace op { +namespace util { +struct VariableInfo { + PartialShape data_shape; + element::Type data_type; + std::string variable_id; + + inline bool operator==(const VariableInfo& other) const { + return data_shape == other.data_shape && data_type == other.data_type && variable_id == other.variable_id; + } +}; + +class OPENVINO_API Variable { +public: + Variable() = default; + + explicit Variable(const VariableInfo& variable_info) : m_info(variable_info) {} + + VariableInfo get_info() const { + return m_info; + } + void update(const VariableInfo& variable_info) { + m_info = variable_info; + } + +private: + VariableInfo m_info; +}; +using VariablePtr = std::shared_ptr; +using VariableVector = std::vector; + +} // namespace util +} // namespace op +template <> +class OPENVINO_API AttributeAdapter> + : public DirectValueAccessor> { +public: + explicit AttributeAdapter(std::shared_ptr& value) + : DirectValueAccessor>(value) {} + + static constexpr DiscreteTypeInfo type_info{"AttributeAdapter>", 0}; + const DiscreteTypeInfo& get_type_info() const override { + return type_info; + } +}; +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/variable_context.hpp b/ngraph/core/include/openvino/op/util/variable_context.hpp new file mode 100644 index 00000000000000..4f698c655863bd --- /dev/null +++ b/ngraph/core/include/openvino/op/util/variable_context.hpp @@ -0,0 +1,91 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include + +#include "openvino/core/node_vector.hpp" +#include "openvino/core/variant.hpp" +#include "openvino/op/util/variable.hpp" +#include "openvino/op/util/variable_value.hpp" + +namespace ov { +namespace op { +namespace util { +using VariableMap = std::unordered_map; + +/// VariableContext stores and manages a evaluation context for Variables. +class NGRAPH_API VariableContext { +public: + /// \brief Constructs an uninitialized VariableContext. + VariableContext() = default; + + /// \brief Constructor for VariableContext. + /// \param variable_values The values associated with a particular Variables. + explicit VariableContext(const VariableMap& variable_values) : m_variable_values(variable_values) {} + + /// \brief Sets the reset flags for all stored Variables to true. + void reset_variable_context() const { + for (const auto& el : m_variable_values) { + el.second->set_reset(true); + } + } + + /// \brief Sets the new values for Variables. + /// \param variable_values The new values associated with a particular Variable. + void set_variable_values(const VariableMap& variable_values) { + m_variable_values = variable_values; + } + + /// \brief Changes/sets the values for Variable. + /// \param variable New or stored Variable. + /// \param variable_value The values associated with the variable. + void set_variable_value(const VariablePtr& variable, const VariableValue::Ptr& variable_value) { + m_variable_values[variable] = variable_value; + } + + /// \brief Removes context for a particular Variable. + /// \param variable The variable for which the context will be cleared. + void remove_variable_value(const VariablePtr& variable) { + m_variable_values.erase(variable); + } + + /// \brief Returns the current values for Variables. + const VariableMap& get_variable_values() const { + return m_variable_values; + } + + /// \brief Returns the value for specified Variable. + VariableValue::Ptr get_variable_value(const VariablePtr& variable) const { + auto var_value = m_variable_values.find(variable); + if (var_value != m_variable_values.end()) { + return (*var_value).second; + } + return VariableValue::Ptr(); + } + +private: + /// The values associated with a particular Variable. + VariableMap m_variable_values; +}; +} // namespace util +} // namespace op +template <> +class NGRAPH_API VariantWrapper : public VariantImpl { +public: + static constexpr VariantTypeInfo type_info{"Variant::EvaluationContext::VariableContext", 0}; + + const VariantTypeInfo& get_type_info() const override { + return type_info; + } + + explicit VariantWrapper(const value_type& value) : VariantImpl(value) {} + +private: + using Variant::init; + using Variant::merge; +}; +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/variable_extension.hpp b/ngraph/core/include/openvino/op/util/variable_extension.hpp new file mode 100644 index 00000000000000..53c16c22065d30 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/variable_extension.hpp @@ -0,0 +1,47 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#include "ngraph/runtime/host_tensor.hpp" +#include "openvino/core/core_visibility.hpp" +#include "openvino/op/util/variable.hpp" + +namespace ov { +namespace op { +namespace util { +class OPENVINO_API VariableExtension { +public: + VariableExtension() = default; + + /// \brief Returns variable connected to this node. + virtual std::shared_ptr get_variable() const { + return m_variable; + } + + /// \brief Sets a new variable to be connected to this node. + /// + /// \param variable New variable to be connected to this node. + virtual void set_variable(const std::shared_ptr& variable) { + m_variable = variable; + } + + /// \brief Sets the identifier to a variable + /// + /// \param variable_id New identifier of the variable. + virtual void set_variable_id(const std::string& variable_id) { + m_variable->get_info().variable_id = variable_id; + }; + + /// \brief Returns the identifier of corresponding variable. + virtual std::string get_variable_id() const = 0; + +protected: + std::shared_ptr m_variable; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/include/openvino/op/util/variable_value.hpp b/ngraph/core/include/openvino/op/util/variable_value.hpp new file mode 100644 index 00000000000000..39425604034097 --- /dev/null +++ b/ngraph/core/include/openvino/op/util/variable_value.hpp @@ -0,0 +1,60 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#include "ngraph/runtime/host_tensor.hpp" +#include "openvino/core/core_visibility.hpp" + +namespace ov { +namespace op { +namespace util { +/// VariableValue stores data and state (reset flag) for a Variable, +/// and provides an interface for changing them. +class OPENVINO_API VariableValue { +public: + using Ptr = std::shared_ptr; + /// \brief Constructs an uninitialized VariableValue. + VariableValue() = default; + + /// \brief Constructor for VariableValue. + /// \param value The data for Variable. + explicit VariableValue(ngraph::HostTensorPtr value) : m_value(std::move(value)) {} + + /// \brief Constructor for VariableValue. + /// \param value Data for Variable. + /// \param reset The current state of the reset flag. + VariableValue(ngraph::HostTensorPtr value, bool reset) : m_reset(reset), m_value(std::move(value)) {} + + /// \brief Sets the reset flag to a new state. + /// \param reset The new state of the reset flag. + void set_reset(bool reset) { + m_reset = reset; + } + + /// \brief Returns the current reset flag state. + bool get_reset() const { + return m_reset; + } + + /// \brief Returns the current stored data. + const ngraph::HostTensorPtr& get_value() const { + return m_value; + } + + /// \brief Sets new values for Variable. + /// \param value New data for Variable. + void set_value(const ngraph::HostTensorPtr& value) { + m_value = value; + } + +private: + bool m_reset = true; + ngraph::HostTensorPtr m_value; +}; +} // namespace util +} // namespace op +} // namespace ov diff --git a/ngraph/core/src/strides.cpp b/ngraph/core/src/strides.cpp index 123ad53bc8e635..1603c79587b418 100644 --- a/ngraph/core/src/strides.cpp +++ b/ngraph/core/src/strides.cpp @@ -6,34 +6,31 @@ #include "ngraph/util.hpp" -using namespace std; -using namespace ngraph; - -std::ostream& ngraph::operator<<(std::ostream& s, const Strides& strides) { +std::ostream& ov::operator<<(std::ostream& s, const ov::Strides& strides) { s << "Strides{"; s << ngraph::join(strides); s << "}"; return s; } -ngraph::Strides::Strides() : std::vector() {} +ov::Strides::Strides() : std::vector() {} -ngraph::Strides::Strides(const std::initializer_list& axis_strides) : std::vector(axis_strides) {} +ov::Strides::Strides(const std::initializer_list& axis_strides) : std::vector(axis_strides) {} -ngraph::Strides::Strides(const std::vector& axis_strides) : std::vector(axis_strides) {} +ov::Strides::Strides(const std::vector& axis_strides) : std::vector(axis_strides) {} -ngraph::Strides::Strides(const Strides& axis_strides) : std::vector(axis_strides) {} +ov::Strides::Strides(const Strides& axis_strides) : std::vector(axis_strides) {} -ngraph::Strides::Strides(size_t n, size_t initial_value) : std::vector(n, initial_value) {} +ov::Strides::Strides(size_t n, size_t initial_value) : std::vector(n, initial_value) {} -ngraph::Strides& ngraph::Strides::operator=(const Strides& v) { +ov::Strides& ov::Strides::operator=(const Strides& v) { static_cast*>(this)->operator=(v); return *this; } -ngraph::Strides& ngraph::Strides::operator=(Strides&& v) noexcept { +ov::Strides& ov::Strides::operator=(Strides&& v) noexcept { static_cast*>(this)->operator=(v); return *this; } -constexpr DiscreteTypeInfo ov::AttributeAdapter::type_info; +constexpr ov::DiscreteTypeInfo ov::AttributeAdapter::type_info;