Remove openvino_builders (#21637)

* Remove openvino_builders

* Apply comments

---------

Co-authored-by: Pavel Durandin <pavel.durandin@intel.com>
This commit is contained in:
Oleg Pipikin 2023-12-28 12:32:56 +01:00 committed by GitHub
parent 5cbd6dec0f
commit 50d484db58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
68 changed files with 739 additions and 1751 deletions

View File

@ -23,4 +23,6 @@ add_subdirectory(bindings)
if(ENABLE_TESTS)
add_subdirectory(core/tests)
add_subdirectory(tests)
else()
add_subdirectory(tests/ov_helpers/ov_models/ov_builders)
endif()

View File

@ -42,7 +42,6 @@ target_include_directories(${TARGET_NAME} PUBLIC
$<BUILD_INTERFACE:${OpenVINO_SOURCE_DIR}/src/inference/include/ie>)
target_link_libraries(${TARGET_NAME} PRIVATE openvino::reference
openvino::builders
openvino::shape_inference
openvino::pugixml
${CMAKE_DL_LIBS}

View File

@ -25,7 +25,7 @@ ov_build_target_faster(${TARGET_NAME}_obj
PCH PRIVATE "src/precomp.hpp"
)
target_link_libraries(${TARGET_NAME}_obj PRIVATE openvino::reference openvino::itt openvino::builders openvino::core::dev openvino::shape_inference)
target_link_libraries(${TARGET_NAME}_obj PRIVATE openvino::reference openvino::itt openvino::core::dev openvino::shape_inference)
target_include_directories(${TARGET_NAME}_obj PRIVATE "${PUBLIC_HEADERS_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/src")

View File

@ -15,7 +15,6 @@ file(GLOB_RECURSE LIBRARY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
file(GLOB_RECURSE PUBLIC_HEADERS ${OV_CORE_INCLUDE_PATH}/*.hpp)
file(GLOB_RECURSE DEV_HEADERS ${OV_CORE_DEV_API_PATH}/*.hpp)
add_subdirectory(builder)
add_subdirectory(reference)
add_subdirectory(shape_inference)
@ -88,7 +87,7 @@ ov_build_target_faster(ngraph_obj
ov_add_version_defines(src/version.cpp ngraph_obj)
target_link_libraries(ngraph_obj PRIVATE openvino::builders openvino::reference openvino::util
target_link_libraries(ngraph_obj PRIVATE openvino::reference openvino::util
openvino::pugixml openvino::shape_inference openvino::core::dev)
ov_mark_target_as_cc(ngraph_obj)
@ -106,12 +105,12 @@ if(NOT BUILD_SHARED_LIBS)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# ngraph is linked against openvino::builders, openvino::reference, openvino::shape_inference static libraries
# ngraph is linked against openvino::reference, openvino::shape_inference static libraries
# which include ngraph headers with dllimport attribute. Linker complains about it
# but no way to fix this: linking with no attribute defaults to dllexport and we have
# multiple defitions for ngraph symbols.
#
# The possible way is to use object libraries for openvino::builders, openvino::reference
# The possible way is to use object libraries for openvino::reference
# but it's not convinient since these libraries are exported from build tree
# and it's better to use them as static libraries in 3rd party projects
if(BUILD_SHARED_LIBS)

View File

@ -1,229 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <memory>
#include <utility>
#include "ngraph/except.hpp"
#include "ngraph/node.hpp"
#include "ngraph/op/broadcast.hpp"
#include "ngraph/op/constant.hpp"
namespace ngraph {
namespace builder {
class numpy_autobroadcast_incompatible_shapes : public ngraph::ngraph_error {
public:
numpy_autobroadcast_incompatible_shapes(const ngraph::Shape& shape1, const ngraph::Shape& shape2);
private:
const ngraph::Shape m_shape1;
const ngraph::Shape m_shape2;
static std::string error_str(const ngraph::Shape& shape1, const ngraph::Shape& shape2);
};
///
/// \brief Broadcast all values, if necessary, to obtain equal shapes according
/// to NumPy's auto-broadcasting scheme.
///
/// \note There are some shape combinations which the autobroadcast algoritm cannot
/// handle. An exception is thrown when such combinations are provided to this
/// function.
///
/// \param values Vector of output values.
///
/// \exception ngraph::builder::numpy_autobroadcast_incompatible_shapes
///
/// \return Vector of broadcasted values.
///
OutputVector numpy_broadcast_outputs(const OutputVector& values);
///
/// \brief Broadcast input value to provided shape using NumPy's auto-broadcasting
/// rules.
///
/// \param value Input value
/// \param shape Requested output shape
///
/// \return Node producing values with requested shape.
///
std::shared_ptr<Node> numpy_broadcast(const Output<Node>& value, const Shape& shape);
/// \brief Wrap two graph values, if necessary, to obtain values with identical shapes,
/// using NumPy's auto-broadcast rules.
///
/// The elements in the std::pair returned by this function correspond to those supplied
/// in the std::pair provided via \p args.
///
/// If \p args.first and \p args.second produce identical shapes, then the returned
/// std::pair will have the same value as \p args.
///
/// If \p args.first and \p args.second produce different shapes, then this function creates
/// new ngraph::op::Reshape and/or ngraph::op::Broadcast nodes, as needed, to wrap
/// \p args.first and/or \p args.second in a manner that yields values with the same shape.
///
/// There are some shape combinations which the autobroadcast algoritm cannot handle.
/// An exception is thrown when such combinations are provided to this function.
///
/// \pre
/// - \p args.first is not null
/// - \p args.second is not null
///
/// \post
/// - The ngraph::Node objects pointed to by \p args.first and \p args.second have not been
/// altered by this function, except by possibly having added consumers of their values.
///
/// - If an exception was not thrown, then the return value's \p first and \p second
/// elements point to ngraph::Node objects whose output values have the same shape.
///
/// \exception ngraph::builder::numpy_autobroadcast_incompatible_shapes
std::pair<std::shared_ptr<Node>, std::shared_ptr<Node>> numpy_broadcast(
const std::pair<Output<Node>, Output<Node>>& args);
/// \brief Broadcast shape of two nodes to make them compatible for a matrix
/// multiplication.
///
/// \note This function is reflecting broadcasting behaviour of NumPy's `matmul`
/// operation.
/// (https://docs.scipy.org/doc/numpy/reference/generated/numpy.matmul.html)
/// This mean that only \"stack of matrices\" axes are bidirectionally
/// broadcasted. The last two dimension are left untouched.
///
/// \param[in] left The Node providing data for the left-hand side of matrix
/// multiplication.
/// \param[in] right The Node providing data for the right-hand side of matrix
/// multiplication.
///
/// \return The vector containing both outputs broadcasted.
///
OutputVector numpy_broadcast_for_matmul_operation(const Output<Node>& left, const Output<Node>& right);
/// \brief Cast shape of all input nodes for an element-wise operation that requires
/// shape-compatibility
///
/// \param inputs Original list of inputs
/// \param axis Index starting to align
///
/// \return pdpd-style broadcasted list of nodes.
OutputVector pdpd_broadcast(const OutputVector& inputs, int64_t axis);
/// \brief Generate a list of broadcast axes.
///
/// \details Informally, a broadcast "adds" axes to the input tensor, replicating
/// elements from the input tensor as needed to fill the new dimensions.
/// Function calculate which of the output axes are added in this way.
///
/// \param output_shape The new shape for the output tensor.
/// \param input_shape The shape of input tensor.
/// \param start_match_axis The axis along which we want to replicate elements.
/// The starting axis position (0-based) int the output
/// shape from which the current shape of the tensor
/// matches the desired new shape.
///
/// \return The indices of added axes.
std::shared_ptr<Node> calculate_broadcast_axes(const Shape& output_shape,
const Shape& input_shape,
std::size_t start_match_axis);
///
/// \brief Calculate the output shape of numpy-style broadcast operation for all input
/// shapes.
///
/// This function finds the maximum tensor shape that will be the result of
/// element-wise operation that will be applied to the input shapes vector.
/// The function also prepares the shape of each input for the element-wise
/// operation by left-padding those shapes so that their rank is equal to the
/// left_shape's rank.
///
/// \param input_shapes A vector of input shapes for which a common shape should be
/// found
///
/// \return A pair that contains the target shape as its first object and a vector of
/// padded input shapes ready to be broadcasted as the second object
///
std::pair<Shape, std::vector<Shape>> get_numpy_broadcast_shapes(const std::vector<Shape>& input_shapes);
inline std::shared_ptr<Node> make_broadcast_node(const Output<Node>& value,
const Shape& new_shape,
std::size_t start_match_axis) {
auto shape_const = op::Constant::create(element::u64, Shape{new_shape.size()}, new_shape);
return std::make_shared<op::v1::Broadcast>(
value,
shape_const,
calculate_broadcast_axes(new_shape, value.get_shape(), start_match_axis));
}
namespace opset1 {
///
/// \brief Broadcast right node to left node's shape using legacy scheme.
///
/// \param[in] left The left hand side node of binary operation.
/// \param[in] right The right hand side node of binary operation. The one
/// to be broadcasted.
/// \param[in] start_match_axis The axis index starting mutually equal shapes
/// of both nodes.
///
/// \return The Output object connected to node producing broadcasted right node.
///
Output<Node> legacy_broadcast_for_binary_operation(const Output<Node>& left,
const Output<Node>& right,
size_t start_match_axis);
///
/// \brief Reconstructs axes mapping vector for Broadcast:v1 operation.
///
/// \param[in] output_shape The output shape of Broadcast operation.
/// \param[in] broadcast_axes The broadcast axes used for Broadcast:v0 operator.
///
/// \return The vector with axes indexes mapping .
///
std::vector<std::size_t> get_axes_mapping(const Shape& output_shape, const AxisSet& broadcast_axes);
///
/// \brief Creates Node returning the axes mapping for Broadcast:v1 operation.
///
/// \param[in] output_shape The output shape of Broadcast operation.
/// \param[in] input_shape The input shape.
/// \param[in] start_match_axis The axis index at which input shape starts to be
/// identical as the output shape.
///
/// \return Returns the Output object pointing to node with the axes mapping.
///
Output<Node> get_axes_mapping_output(const Shape& output_shape, const Shape& input_shape, std::size_t start_match_axis);
///
/// \brief Creates Node returning the axes mapping for Broadcast operation.
/// \note Shapes' ranks need to be static.
///
/// \param[in] output_shape The output shape of Broadcast operation.
/// \param[in] input_shape The input shape.
/// \param[in] start_match_axis The axis index at which input shape starts to be
/// identical to consecutive subset of output shape
/// dimensions.
///
/// \return Returns the Output object pointing to node with the axes mapping.
///
Output<Node> get_axes_mapping_output(const PartialShape& output_shape,
const PartialShape& input_shape,
std::size_t start_match_axis);
///
/// \brief Creates Node returning the axes mapping for Broadcast:v1 operation.
///
/// \param[in] output_shape The output shape of Broadcast operation.
/// \param[in] broadcast_axes The broadcast axes used for Broadcast:v0 operator.
///
/// \return The Output object with Node returning axes mapping.
///
Output<Node> get_axes_mapping_output(const Shape& output_shape, const AxisSet& broadcast_axes);
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, const AxisSet& broadcast_axes);
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, std::size_t start_match_axis);
} // namespace opset1
} // namespace builder
} // namespace ngraph

View File

@ -1,142 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "autobroadcast.hpp"
#include "ngraph/node.hpp"
#include "ngraph/op/broadcast.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/type/float16.hpp"
namespace ngraph {
namespace builder {
template <class T>
std::shared_ptr<Node> make_constant(const element::Type& type, const Shape& shape, const T& num) {
std::shared_ptr<Node> val = nullptr;
#if defined(__GNUC__) && !(__GNUC__ == 4 && __GNUC_MINOR__ == 8)
# pragma GCC diagnostic push
# pragma GCC diagnostic error "-Wswitch"
# pragma GCC diagnostic error "-Wswitch-enum"
#endif
std::string unsupported_data_type;
switch (type) {
case element::Type_t::f32:
val =
std::make_shared<ngraph::op::Constant>(type, ngraph::Shape{}, std::vector<float>{static_cast<float>(num)});
break;
case element::Type_t::f64:
val = std::make_shared<ngraph::op::Constant>(type,
ngraph::Shape{},
std::vector<double>{static_cast<double>(num)});
break;
case element::Type_t::f16:
val = std::make_shared<ngraph::op::Constant>(
type,
ngraph::Shape{},
std::vector<ngraph::float16>{ngraph::float16(static_cast<float>(num))});
break;
case element::Type_t::bf16:
val = std::make_shared<ngraph::op::Constant>(
type,
ngraph::Shape{},
std::vector<ngraph::bfloat16>{ngraph::bfloat16(static_cast<float>(num))});
break;
case element::Type_t::i64:
val = std::make_shared<ngraph::op::Constant>(type,
ngraph::Shape{},
std::vector<int64_t>{static_cast<int64_t>(num)});
break;
case element::Type_t::i32:
val = std::make_shared<ngraph::op::Constant>(type,
ngraph::Shape{},
std::vector<int32_t>{static_cast<int32_t>(num)});
break;
case element::Type_t::i16:
val = std::make_shared<ngraph::op::Constant>(type,
ngraph::Shape{},
std::vector<int16_t>{static_cast<int16_t>(num)});
break;
case element::Type_t::i8:
val = std::make_shared<ngraph::op::Constant>(type,
ngraph::Shape{},
std::vector<int8_t>{static_cast<int8_t>(num)});
break;
case element::Type_t::u64:
val = std::make_shared<ngraph::op::Constant>(type,
ngraph::Shape{},
std::vector<uint64_t>{static_cast<uint64_t>(num)});
break;
case element::Type_t::u32:
val = std::make_shared<ngraph::op::Constant>(type,
ngraph::Shape{},
std::vector<uint32_t>{static_cast<uint32_t>(num)});
break;
case element::Type_t::u16:
val = std::make_shared<ngraph::op::Constant>(type,
ngraph::Shape{},
std::vector<uint16_t>{static_cast<uint16_t>(num)});
break;
case element::Type_t::u8:
val = std::make_shared<ngraph::op::Constant>(type,
ngraph::Shape{},
std::vector<uint8_t>{static_cast<uint8_t>(num)});
break;
case element::Type_t::dynamic:
unsupported_data_type = "dynamic";
break;
case element::Type_t::boolean:
unsupported_data_type = "boolean";
break;
case element::Type_t::u1:
unsupported_data_type = "u1";
break;
case element::Type_t::i4:
unsupported_data_type = "i4";
break;
case element::Type_t::u4:
unsupported_data_type = "u4";
break;
case element::Type_t::nf4:
unsupported_data_type = "nf4";
break;
case element::Type_t::string:
unsupported_data_type = "string";
break;
case element::Type_t::undefined:
unsupported_data_type = "undefined";
break;
}
if (!unsupported_data_type.empty())
OPENVINO_THROW("make_constant: Unsupported element type '", unsupported_data_type, "'");
#if defined(__GNUC__) && !(__GNUC__ == 4 && __GNUC_MINOR__ == 8)
# pragma GCC diagnostic pop
#endif
if (shape.size() > 0) {
ngraph::AxisSet axes;
for (size_t i = 0; i < shape.size(); i++) {
axes.insert(i);
}
val = builder::opset1::make_broadcast(val, shape, axes).get_node_shared_ptr();
}
return val;
}
/// \brief Create constant filled with double value
///
/// \note If num value exeeds capacity of type, the value is clamped.
///
/// \param[in] type The type of produced Constant node.
/// \param[in] shape The shape of produced Constant node.
/// \param[in] num The value used to fill Constant node.
///
/// \return The Constant node which have expected type, shape and value.
///
std::shared_ptr<Node> make_constant_from_double(const element::Type& type, const Shape& shape, double num);
} // namespace builder
} // namespace ngraph

View File

@ -1,89 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <cstddef>
#include <memory>
#include "ngraph/axis_set.hpp"
#include "ngraph/node.hpp"
namespace ngraph {
namespace builder {
/// \brief Specifies method of bias application to avoid numerical problems
enum class BiasMode {
// Add bias to intermediate result
ADD,
// Calculate max of intermediate result and bias
MAX
};
namespace opset1 {
/// \brief Calculates L-0 norm of input tensor.
///
/// \note The L-0 norm represents the cardinality of elements different
/// from zero. This actually is not a "true" norm.
///
/// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] keep_dims The flag indicates if axes will be removed or kept.
///
/// \return L-0 norm of value. The output sub-graph is composed of v1 ops.
///
std::shared_ptr<Node> l0_norm(const Output<Node>& value, const Output<Node>& reduction_axes, bool keep_dims = false);
/// \brief Calculates L-1 norm of a value.
///
/// \note The L-1 norm represents the sum of absolute values.
///
/// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] bias The bias added to the calculated sum.
/// \param[in] keep_dims The flag indicates if axes will be removed or kept.
///
/// \return L-1 norm of value. The output sub-graph is composed of v1 ops.
///
std::shared_ptr<Node> l1_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
float bias = 0.f,
bool keep_dims = false);
/// \brief Calculates L-2 norm of input tensor.
///
/// \note The L-2 norm represents the square root of sum of squares of each
/// individual element.
///
/// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] bias The bias combined with calculated sum.
/// \param[in] bias_mode The method of bias application.
/// \param[in] keep_dims The flag indicates if axes will be removed or kept.
///
/// \return L-2 norm of value. The output sub-graph is composed of v1 ops.
///
std::shared_ptr<Node> l2_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
float bias = 0.f,
BiasMode bias_mode = BiasMode::ADD,
bool keep_dims = false);
/// \brief Creates node which calculates L-p norm on input tensor.
///
/// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] p_norm The p norm to calculate.
/// \param[in] bias The bias added to the calculated sum.
/// \param[in] keep_dims The flag indicates if axes will be removed or kept.
///
/// \return L-p norm of value. The output sub-graph is composed of v1 ops.
///
std::shared_ptr<Node> lp_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
std::size_t p_norm = 2,
float bias = 0.f,
bool keep_dims = false);
} // namespace opset1
} // namespace builder
} // namespace ngraph

View File

@ -1,78 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "ngraph/axis_set.hpp"
#include "ngraph/node.hpp"
namespace ngraph {
namespace builder {
namespace opset1 {
// clang-format off
/// \brief Sum-based Mean of a Tensor.
///
/// Calculates
///
/// \f$\sum_{i=1}^{N} \frac{x_i}{N}\f$
///
/// Where `i` traverses all of the axes provided in `reduction_axes`
///
/// ## Inputs
///
/// | | Type | Description | |
/// | ---------------- | --------------------------------- | -------------------------------------------------------|
/// | `node` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | An input tensor of any shape |
/// | `reduction_axes` | AxesSet | The axes to eliminate through reduction (0 indexed). |
/// | `keep_dims` | bool | If set to true it holds reduced axes. |
///
/// ## Output
///
/// | Type | Description |
/// | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
/// | \f$E[\textit{delete}(A,d_1,\dots,d_n)]\f$ | The tensor \f$T\f$, where \f$T\f$ is the input tensor with the `reduction_axes` \f$A\f$ eliminated by reduction. |
// clang-format on
std::shared_ptr<Node> mean(const Output<Node>& node, const AxisSet& reduction_axes, bool keep_dims = false);
std::shared_ptr<Node> mean(const Output<Node>& node, const Output<Node>& reduction_axes, bool keep_dims = false);
// clang-format off
/// \brief Sum-based Variance of a Tensor.
///
/// If bessel_correct is true, calculates
///
/// \f$\frac{\sum_{i=1}^{N}\left(x_i-\bar{x}\right)^2}{N-1}\f$
///
/// else, calculates
///
/// \f$\frac{\sum_{i=1}^{N}\left(x_i-\bar{x}\right)^2}{N}\f$
///
/// Where `i` traverses all of the axes provided in `reduction_axes` and \f$\bar{x} = \sum_{i=1}^{N} \frac{x_i}{N}\f$
///
/// ## Inputs
///
/// | | Type | Description |
/// | ------------------- | --------------------------------- | ------------------------------------------------------------ |
/// | `value | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | An input tensor of any shape |
/// | `reduction_axes` | AxesSet | The axes to eliminate through reduction (0 indexed). |
/// | `bessel_correction` | bool (default = false) | Enable Bessel's correction to std_dev for Small sample sizes |
///
/// ## Output
///
/// | Type | Description |
/// | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
/// | \f$E[\textit{delete}(A,d_1,\dots,d_n)]\f$ | The tensor \f$T\f$, where \f$T\f$ is the input tensor with the `reduction_axes` \f$A\f$ eliminated by reduction. |
// clang-format on
std::shared_ptr<Node> variance(const Output<Node>& value,
const AxisSet& reduction_axes,
const bool bessel_correction = false);
std::shared_ptr<Node> variance(const Output<Node>& value,
const Output<Node>& reduction_axes,
bool keep_dims = false,
bool bessel_correction = false);
} // namespace opset1
} // namespace builder
} // namespace ngraph

View File

@ -1,80 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <cstddef>
#include <memory>
#include <vector>
#include "ngraph/node.hpp"
#include "ngraph/shape.hpp"
namespace ngraph {
namespace builder {
namespace opset1 {
/// \brief Change shape of a value
///
/// \param[in] value The value to be reshaped.
/// \param[in] shape The new shape.
///
/// \return Reshape:v1 op.
std::shared_ptr<Node> reshape(const Output<Node>& value, const Shape& shape);
/// \brief Permute axes according to specified axes_order parameter.
///
/// \param The vlaue whose axes we want to permute.
/// \param axes_order The permutation of axes.
///
/// \return Transpose:v1 op.
std::shared_ptr<Node> reorder_axes(const Output<Node>& value, std::vector<size_t> axes_order = {});
/// \brief Return transposed value (with axes in reversed order).
///
/// \param Value to transpose.
///
/// \return Transpose:v1 op.
std::shared_ptr<Node> transpose(const Output<Node>& value);
/// \brief Flatten a value into a 2D matrix, with a static dividing axis.
///
/// \param The tensor to be flattened.
/// \param The axis dividing shape.
///
/// \return The new value will be a 2D matrix representing the flattened input
/// node.
std::shared_ptr<Node> flatten(const Output<Node>& value, int axis);
/// \brief Expands node tensor shape with empty axis at
/// specified position.
///
/// \param[in] value The value to be expanded.
/// \param[in] axis The position in the expanded axes where the
/// new axis is placed.
///
/// \return Reshape:v1 op.
std::shared_ptr<Node> expand_dims(const Output<Node>& value, std::size_t axis = 0);
/// \brief Remove empty axes from input tensor.
///
/// \param[in] value The value to be squeezed.
/// \param[in] axes The vector defining indexes of axes to be removed.
///
/// \return Reshape:v1 op.
std::shared_ptr<Node> squeeze(const Output<Node>& value, std::vector<std::size_t> axes = {0});
/// \brief Collapse specified axes into single one.
///
/// \note Collapsed axes create a continuous range starting from outermost axis.
///
/// \param[in] value The value to be reshaped.
/// \param[in] start_axis The start axis index.
/// \param[in] end_axis The end axis (inclusive) index.
///
/// \return The node with collapsed specified axes.
///
std::shared_ptr<Node> collapse(const Output<Node>& value, const std::size_t start_axis, const std::size_t end_axis);
} // namespace opset1
} // namespace builder
} // namespace ngraph

View File

@ -1,400 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/builder/autobroadcast.hpp"
#include <memory>
#include <numeric>
#include <sstream>
#include "ngraph/axis_vector.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/check.hpp"
#include "ngraph/op/broadcast.hpp"
#include "ngraph/op/reshape.hpp"
#include "ngraph/opsets/opset7.hpp"
#include "ngraph/util.hpp"
using namespace std;
namespace ngraph {
namespace builder {
OPENVINO_SUPPRESS_DEPRECATED_START
numpy_autobroadcast_incompatible_shapes::numpy_autobroadcast_incompatible_shapes(const Shape& shape1,
const Shape& shape2)
: ngraph_error(error_str(shape1, shape2)),
m_shape1(shape1),
m_shape2(shape2) {}
string numpy_autobroadcast_incompatible_shapes::error_str(const Shape& shape1, const Shape& shape2) {
ostringstream os;
os << "Auto-broadcast not possible for these input shapes:"
<< " shape1=" << vector_to_string(shape1) << " shape2=" << vector_to_string(shape2);
return os.str();
}
OPENVINO_SUPPRESS_DEPRECATED_END
///
/// \brief Calculate the output shape of numpy-style broadcast operation for two
/// shapes.
///
/// \note More info:
/// https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html#general-broadcasting-rules
/// Example: left: [3, 1, 10] right: [5, 1] return: [3, 5, 10]
///
/// \param lhs_shape First input shape.
/// \param rhs_shape Second input Shape.
///
/// \return Broadcast shape of input shapes.
///
static Shape calculate_broadcast_shape(Shape lhs_shape, Shape rhs_shape) {
Shape result;
auto lhs_rank = lhs_shape.size();
auto rhs_rank = rhs_shape.size();
auto max_rank = max(lhs_rank, rhs_rank);
// left-pad the lhs_shape with ones
lhs_shape.insert(begin(lhs_shape), max_rank - lhs_rank, 1);
// left-pad the rhs_shape with ones
rhs_shape.insert(begin(rhs_shape), max_rank - rhs_rank, 1);
for (size_t index = 0; index < max_rank; ++index) {
size_t lhs_dim = lhs_shape.at(index);
size_t rhs_dim = rhs_shape.at(index);
if (lhs_dim != rhs_dim && lhs_dim != 1 && rhs_dim != 1) {
throw numpy_autobroadcast_incompatible_shapes(lhs_shape, rhs_shape);
}
result.push_back(max(lhs_dim, rhs_dim));
}
return result;
};
pair<Shape, vector<Shape>> get_numpy_broadcast_shapes(const vector<Shape>& input_shapes) {
Shape target_shape = accumulate(begin(input_shapes), end(input_shapes), Shape{}, calculate_broadcast_shape);
vector<Shape> full_shapes;
for (const Shape& input : input_shapes) {
Shape padded_shape{input};
padded_shape.insert(begin(padded_shape), target_shape.size() - padded_shape.size(), 1);
full_shapes.push_back(std::move(padded_shape));
}
return {target_shape, full_shapes};
}
static pair<Shape, vector<Shape>> get_numpy_broadcast_shapes(const OutputVector& values) {
vector<Shape> input_shapes;
for (const auto& input : values) {
input_shapes.push_back(input.get_shape());
}
return get_numpy_broadcast_shapes(input_shapes);
}
/// \brief Broadcast input node.
///
/// \note The source shape does not have to be the actual shape of input node. However
/// it should be a superset of it (containing it as a continuous subset). This
/// implies we may expand the number of axes of input node. The ranks of
/// source_shape and output_shape must be equal. This means that the
/// source_shape has to be padded with ones for this operation.
///
/// \param[in] value The input Node to be broadcast.
/// \param[in] output_shape The output shape.
/// \param[in] source_shape The source shape from which we want to broadcast input node.
///
/// \return The broadcasted Node.
///
static shared_ptr<Node> numpy_broadcast_node(const Output<Node>& value,
const Shape& output_shape,
const Shape& source_shape) {
shared_ptr<Node> broadcasted_node = value.get_node_shared_ptr();
// If node already has the required shape, return original node
if (output_shape == value.get_shape()) {
return broadcasted_node;
}
NGRAPH_CHECK(source_shape.size() == output_shape.size(),
"Ranks of source_shape and output_shape dont match: ",
source_shape.size(),
" vs ",
output_shape.size());
AxisVector broadcast_axes;
Shape squeezed_shape;
// Positions of axes which have length of 1 are needed to calculate broadcast_axes
// for nGraph broadcast operation. We need to remove ones from source shape
// to avoid broadcasting axis conflict.
for (size_t index = 0; index < output_shape.size(); ++index) {
if (source_shape.at(index) == 1 && output_shape.at(index) != 1) {
broadcast_axes.push_back(index);
} else {
squeezed_shape.push_back(source_shape.at(index));
}
}
if (squeezed_shape != value.get_shape()) {
broadcasted_node = builder::opset1::reshape(value, squeezed_shape);
}
if (!broadcast_axes.empty()) {
auto shape_const = op::Constant::create(element::u64, Shape{output_shape.size()}, output_shape);
broadcasted_node =
make_shared<op::v1::Broadcast>(broadcasted_node,
shape_const,
opset1::get_axes_mapping_output(output_shape, broadcast_axes));
}
return broadcasted_node;
}
/// \brief Broadcast input node.
///
/// \param[in] value The input Node to be broadcast.
/// \param[in] output_shape The output shape.
/// \param[in] axis The start index to align with output_shape
///
/// \return The broadcasted Node.
///
static shared_ptr<Node> broadcast_value_pdpd_style(const Output<Node>& value, const Shape& output_shape, int64_t axis) {
auto value_shape = value.get_shape();
// If node already has the required shape, return original node
if (output_shape == value_shape) {
return value.get_node_shared_ptr();
}
if (axis == -1) {
axis = output_shape.size() - value_shape.size();
}
auto trimmed_value_shape = value_shape;
while (trimmed_value_shape.size() > 0 && trimmed_value_shape.back() == 1) {
trimmed_value_shape.pop_back();
}
AxisSet axes;
for (int64_t i = 0; i < axis; ++i) {
axes.insert(static_cast<size_t>(i));
}
for (size_t i = axis + trimmed_value_shape.size(); i < output_shape.size(); ++i) {
axes.insert(i);
}
auto trimmed_value = value;
if (value_shape != trimmed_value_shape) {
trimmed_value = builder::opset1::reshape(value, trimmed_value_shape);
}
auto shape_const = op::Constant::create(element::u64, Shape{output_shape.size()}, output_shape);
auto value_bcast =
make_shared<op::v1::Broadcast>(trimmed_value, shape_const, opset1::get_axes_mapping_output(output_shape, axes));
return std::move(value_bcast);
}
pair<shared_ptr<Node>, shared_ptr<Node>> numpy_broadcast(const pair<Output<Node>, Output<Node>>& args) {
NGRAPH_CHECK(args.first.get_node());
NGRAPH_CHECK(args.second.get_node());
const Shape& arg1_in_shape = args.first.get_shape();
const Shape& arg2_in_shape = args.second.get_shape();
// Handle the trivial case...
if (arg1_in_shape == arg2_in_shape) {
return make_pair(args.first.get_node_shared_ptr(), args.second.get_node_shared_ptr());
}
NodeVector bcasted_outputs = as_node_vector(numpy_broadcast_outputs({args.first, args.second}));
return make_pair(bcasted_outputs.at(0), bcasted_outputs.at(1));
}
OutputVector numpy_broadcast_outputs(const OutputVector& values) {
if (values.size() <= 1) {
return values;
}
// find the output tensor's shape, then broadcast all inputs so that they are compatible
auto bcast_shapes = get_numpy_broadcast_shapes(values);
OutputVector broadcasted_inputs;
for (size_t i = 0; i < values.size(); ++i) {
broadcasted_inputs.push_back(numpy_broadcast_node(values[i], bcast_shapes.first, bcast_shapes.second[i]));
}
return broadcasted_inputs;
}
shared_ptr<Node> numpy_broadcast(const Output<Node>& value, const Shape& shape) {
auto bcast_shape = get_numpy_broadcast_shapes({value.get_shape(), shape});
return numpy_broadcast_node(value, bcast_shape.first, bcast_shape.second[0]);
}
OutputVector numpy_broadcast_for_matmul_operation(const Output<Node>& left, const Output<Node>& right) {
const auto& left_shape = left.get_shape();
const auto& right_shape = right.get_shape();
// Broadcast only _stack of matrices_ axes.
const auto& numpy_shapes = get_numpy_broadcast_shapes(
{Shape{begin(left_shape), next(end(left_shape), -2)}, Shape{begin(right_shape), next(end(right_shape), -2)}});
// Prepare tensors output shapes with broadcasted _stack of matrices_ axes.
auto left_output_shape = numpy_shapes.first;
auto right_output_shape = numpy_shapes.first;
// Append the last two axes original dimensions.
left_output_shape.insert(end(left_output_shape), next(begin(left_shape), left_shape.size() - 2), end(left_shape));
right_output_shape.insert(end(right_output_shape),
next(begin(right_shape), right_shape.size() - 2),
end(right_shape));
auto left_full_shape = numpy_shapes.second.at(0);
auto right_full_shape = numpy_shapes.second.at(1);
// Append the last two axes original dimensions.
left_full_shape.insert(end(left_full_shape), next(begin(left_shape), left_shape.size() - 2), end(left_shape));
right_full_shape.insert(end(right_full_shape), next(begin(right_shape), right_shape.size() - 2), end(right_shape));
return {numpy_broadcast_node(left, left_output_shape, left_full_shape),
numpy_broadcast_node(right, right_output_shape, right_full_shape)};
}
OutputVector pdpd_broadcast(const OutputVector& inputs, int64_t axis) {
if (inputs.size() <= 1) {
return inputs;
}
OutputVector broadcasted_inputs{inputs[0]};
for (size_t i = 1; i < inputs.size(); ++i) {
broadcasted_inputs.push_back(broadcast_value_pdpd_style(inputs[i], inputs[0].get_shape(), axis));
}
return broadcasted_inputs;
}
std::shared_ptr<Node> calculate_broadcast_axes(const Shape& output_shape,
const Shape& input_shape,
size_t start_match_axis) {
vector<size_t> axes(output_shape.size() - input_shape.size());
// Populate the axes vector with monotonic increasing series from 0 until
// output_shape_size, excluding values in range:
// [start_match_axis, start_match_axis + input_shape.size()]
iota(begin(axes), begin(axes) + start_match_axis, 0);
iota(begin(axes) + start_match_axis, end(axes), start_match_axis + input_shape.size());
auto axes_mapping = opset1::get_axes_mapping(output_shape, axes);
return op::Constant::create(element::i64, Shape{axes_mapping.size()}, axes_mapping);
}
namespace opset1 {
Output<Node> legacy_broadcast_for_binary_operation(const Output<Node>& left,
const Output<Node>& right,
size_t start_match_axis) {
const auto& left_shape = left.get_shape();
const auto& right_shape = right.get_shape();
bool dimensions_identical = (left_shape == right_shape);
if (dimensions_identical) {
return right;
}
// Prepare new shape of right operand for broadcasting
// Remove dimensions with length=1 from back
auto new_right_shape = right_shape;
for (int dimension = static_cast<int>(new_right_shape.size()) - 1; dimension >= 0; --dimension) {
if (new_right_shape.at(dimension) == 1) {
new_right_shape.pop_back();
} else {
break;
}
}
// Find first dimensions at front with length different from 1
size_t num_ones = 0;
for (size_t dimension : new_right_shape) {
if (dimension == 1) {
++num_ones;
} else {
break;
}
}
// Remove dimensions with length=1 from front
new_right_shape.erase(begin(new_right_shape), next(begin(new_right_shape), num_ones));
auto reshape_right = reshape(right, new_right_shape);
// Move broadcast start axis parameter to right
start_match_axis += num_ones;
return make_broadcast(reshape_right, left_shape, start_match_axis);
}
vector<size_t> get_axes_mapping(const Shape& output_shape, const AxisSet& broadcast_axes) {
NGRAPH_CHECK((broadcast_axes.size() <= output_shape.size()));
vector<size_t> axes_mapping(output_shape.size());
iota(axes_mapping.begin(), axes_mapping.end(), 0);
for (auto i = broadcast_axes.rbegin(); i != broadcast_axes.rend(); ++i) {
axes_mapping.erase(axes_mapping.begin() + *i);
}
return axes_mapping;
}
Output<Node> get_axes_mapping_output(const PartialShape& output_shape,
const PartialShape& input_shape,
std::size_t start_match_axis) {
NGRAPH_CHECK((input_shape.rank().is_static() && output_shape.rank().is_static()),
"Tensor's rank has to be static.");
NGRAPH_CHECK(
(input_shape.rank().get_length() + static_cast<int64_t>(start_match_axis) <= output_shape.rank().get_length()),
"Unable to figure out axes mapping.");
vector<int64_t> mapping(input_shape.rank().get_length());
iota(begin(mapping), end(mapping), start_match_axis);
return op::Constant::create(element::i64, Shape{mapping.size()}, mapping);
}
Output<Node> get_axes_mapping_output(const Shape& output_shape, const AxisSet& broadcast_axes) {
vector<size_t> axes_mapping{get_axes_mapping(output_shape, broadcast_axes)};
return op::Constant::create(element::i64, Shape{axes_mapping.size()}, axes_mapping);
}
static Output<Node> get_axes_mapping_output(const PartialShape& output_shape,
const Output<Node>& input_shape,
std::size_t start_match_axis) {
const auto one_node = opset7::Constant::create(element::i64, Shape{}, {1});
const auto zero_node = opset7::Constant::create(element::i64, Shape{}, {0});
const auto start_match_axis_node = opset7::Constant::create(element::i64, Shape{}, {start_match_axis});
const auto target_shape_rank_node =
builder::opset1::reshape(std::make_shared<opset7::ShapeOf>(input_shape), Shape{});
const auto range_node = std::make_shared<opset7::Range>(zero_node, target_shape_rank_node, one_node, element::i64);
// workaround for GPU plugin type incompatibility
const auto range_node_converted =
std::make_shared<opset7::Convert>(range_node, start_match_axis_node->get_element_type());
// end of workaround
const auto result = std::make_shared<opset7::Add>(range_node_converted, start_match_axis_node);
return result;
}
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, const AxisSet& broadcast_axes) {
return make_shared<op::v1::Broadcast>(node,
op::Constant::create(element::i64, Shape{target_shape.size()}, target_shape),
get_axes_mapping_output(target_shape, broadcast_axes));
}
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, size_t start_match_axis) {
const auto node_shape = std::make_shared<opset7::ShapeOf>(node);
return make_shared<op::v1::Broadcast>(node,
op::Constant::create(element::i64, Shape{target_shape.size()}, target_shape),
get_axes_mapping_output(target_shape, node_shape, start_match_axis));
}
} // namespace opset1
} // namespace builder
} // namespace ngraph

View File

@ -1,72 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/builder/make_constant.hpp"
NGRAPH_SUPPRESS_DEPRECATED_START
namespace ngraph {
namespace builder {
std::shared_ptr<Node> make_constant_from_double(const element::Type& type, const Shape& shape, double num) {
auto ceil_func = [](double x) {
return ceil(x);
};
std::shared_ptr<ngraph::Node> result = nullptr;
switch (type) {
case element::Type_t::i8: {
result = std::make_shared<ngraph::op::Constant>(type, shape, double_to_int<int8_t>(num, ceil_func));
break;
}
case element::Type_t::i16: {
result = std::make_shared<ngraph::op::Constant>(type, shape, double_to_int<int16_t>(num, ceil_func));
break;
}
case element::Type_t::i32: {
result = std::make_shared<ngraph::op::Constant>(type, shape, double_to_int<int32_t>(num, ceil_func));
break;
}
case element::Type_t::i64: {
result = std::make_shared<ngraph::op::Constant>(type, shape, double_to_int<int64_t>(num, ceil_func));
break;
}
case element::Type_t::u8: {
result = std::make_shared<ngraph::op::Constant>(type, shape, double_to_int<uint8_t>(num, ceil_func));
break;
}
case element::Type_t::u16: {
result = std::make_shared<ngraph::op::Constant>(type, shape, double_to_int<uint16_t>(num, ceil_func));
break;
}
case element::Type_t::u32: {
result = std::make_shared<ngraph::op::Constant>(type, shape, double_to_int<uint32_t>(num, ceil_func));
break;
}
case element::Type_t::u64: {
result = std::make_shared<ngraph::op::Constant>(type, shape, double_to_int<uint64_t>(num, ceil_func));
break;
}
case element::Type_t::f16: {
result = builder::make_constant(type, shape, static_cast<float16>(num));
break;
}
case element::Type_t::bf16: {
result = builder::make_constant(type, shape, static_cast<bfloat16>(num));
break;
}
case element::Type_t::f32: {
result = builder::make_constant(type, shape, static_cast<float>(num));
break;
}
case element::Type_t::f64: {
result = builder::make_constant(type, shape, num);
break;
}
default:
OPENVINO_THROW("Unsupported data type during make_constant_from_double");
break;
}
return result;
}
} // namespace builder
} // namespace ngraph

View File

@ -1,130 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/builder/norm.hpp"
#include "ngraph/op/abs.hpp"
#include "ngraph/op/add.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/op/convert.hpp"
#include "ngraph/op/maximum.hpp"
#include "ngraph/op/multiply.hpp"
#include "ngraph/op/not_equal.hpp"
#include "ngraph/op/power.hpp"
#include "ngraph/op/reduce_sum.hpp"
#include "ngraph/op/sqrt.hpp"
#include "ngraph/opsets/opset1.hpp"
#include "ngraph/shape.hpp"
using namespace std;
namespace ngraph {
namespace builder {
namespace detail {
namespace opset1 {
namespace {
shared_ptr<Node> lp_norm(const Output<Node>& value,
size_t p_norm,
const Output<Node>& reduction_axes,
float bias,
bool keep_dims) {
// In general "entrywise" lp-norm for matrix `A` is defined as following double
// sum:
// ||A||_p = ||vec(A)||_p = [sum_{i=1}^m sum_{j=1}^n abs(a_{i,j})^p]^{1/p}
shared_ptr<Node> abs_values{make_shared<ngraph::opset1::Abs>(value)};
shared_ptr<Node> p_node = ngraph::opset1::Constant::create(value.get_element_type(), Shape{}, {p_norm});
// Get inner part of equation: abs_values^p_node, then sum over reduction_axes.
shared_ptr<Node> values{make_shared<ngraph::opset1::Power>(abs_values, p_node)};
values = make_shared<ngraph::opset1::ReduceSum>(values, reduction_axes, keep_dims);
shared_ptr<Node> bias_node{ngraph::opset1::Constant::create(values->get_element_type(), Shape{}, {bias})};
values = make_shared<ngraph::opset1::Add>(values, bias_node);
// Get outer part of equation: raise values to 1/p_norm exponent.
shared_ptr<Node> inv_p_node = ngraph::opset1::Constant::create(values->get_element_type(), Shape{}, {1.f / p_norm});
return {make_shared<ngraph::opset1::Power>(values, inv_p_node)};
}
} // namespace
} // namespace opset1
} // namespace detail
shared_ptr<Node> builder::opset1::l0_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
bool keep_dims) {
// L0 norm returns number of elements different from zero.
const shared_ptr<Node> zero_node{ngraph::opset1::Constant::create(value.get_element_type(), Shape{}, {0.f})};
// Convert bool values to input node data type.
const shared_ptr<Node> non_zero_values =
make_shared<ngraph::opset1::Convert>(make_shared<ngraph::opset1::NotEqual>(value, zero_node),
value.get_element_type());
return make_shared<ngraph::opset1::ReduceSum>(non_zero_values, reduction_axes, keep_dims);
}
shared_ptr<Node> builder::opset1::l1_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
float bias,
bool keep_dims) {
const shared_ptr<Node> values{
make_shared<ngraph::opset1::ReduceSum>(make_shared<ngraph::opset1::Abs>(value), reduction_axes, keep_dims)};
const shared_ptr<Node> bias_node{ngraph::opset1::Constant::create(values->get_element_type(), Shape{}, {bias})};
return make_shared<ngraph::opset1::Add>(values, bias_node);
}
shared_ptr<Node> builder::opset1::l2_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
float bias,
BiasMode bias_mode,
bool keep_dims) {
shared_ptr<Node> pow =
make_shared<ngraph::opset1::Power>(value,
make_shared<ngraph::opset1::Constant>(value.get_element_type(), Shape{}, 2));
shared_ptr<Node> values{make_shared<ngraph::opset1::ReduceSum>(pow, reduction_axes, keep_dims)};
shared_ptr<Node> bias_node{ngraph::opset1::Constant::create(values->get_element_type(), Shape{}, {bias})};
shared_ptr<Node> result;
switch (bias_mode) {
case BiasMode::MAX: {
result = make_shared<ngraph::opset1::Sqrt>(make_shared<ngraph::opset1::Maximum>(values, bias_node));
break;
}
case BiasMode::ADD:
default:
result = make_shared<ngraph::opset1::Sqrt>(make_shared<ngraph::opset1::Add>(values, bias_node));
}
return result;
}
shared_ptr<Node> builder::opset1::lp_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
size_t p_norm,
float bias,
bool keep_dims) {
// The number of non-zero elements
if (p_norm == 0) {
return opset1::l0_norm(value, reduction_axes, keep_dims);
}
// sum of absolute values.
else if (p_norm == 1) {
return opset1::l1_norm(value, reduction_axes, bias, keep_dims);
}
// sqrt of sum of squares - Euclidean norm
else if (p_norm == 2) {
return opset1::l2_norm(value, reduction_axes, bias, BiasMode::ADD, keep_dims);
}
// generic case
else {
return detail::opset1::lp_norm(value, p_norm, reduction_axes, bias, keep_dims);
}
}
} // namespace builder
} // namespace ngraph

View File

@ -1,127 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/builder/reduce_ops.hpp"
#include <numeric>
#include "ngraph/axis_set.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/op/divide.hpp"
#include "ngraph/op/multiply.hpp"
#include "ngraph/op/reshape.hpp"
#include "ngraph/op/sqrt.hpp"
#include "ngraph/op/subtract.hpp"
#include "ngraph/opsets/opset1.hpp"
#include "ngraph/util.hpp"
namespace ngraph {
namespace builder {
namespace {
size_t get_num_elements(const Shape& shape, const AxisSet& reduction_axes) {
size_t N = 1;
for (auto a : reduction_axes) {
N *= shape[a];
}
return N;
}
std::shared_ptr<Node> get_num_elements(const Output<Node>& value, const Output<Node>& reduction_axes) {
const auto value_shape = std::make_shared<ngraph::opset1::ShapeOf>(value);
const auto dim_values =
std::make_shared<ngraph::opset1::Gather>(value_shape,
reduction_axes,
ngraph::opset1::Constant::create(element::i64, {}, {0}));
return std::make_shared<ngraph::opset1::ReduceProd>(dim_values,
ngraph::opset1::Constant::create(element::i64, {}, {0}));
}
} // namespace
std::shared_ptr<Node> builder::opset1::mean(const Output<Node>& value, const AxisSet& reduction_axes, bool keep_dims) {
std::shared_ptr<Node> elems_number;
const auto value_elem_type = value.get_element_type();
const auto reduction_axes_const =
ngraph::opset1::Constant::create(element::i64, Shape{reduction_axes.size()}, reduction_axes.to_vector());
const auto value_elems_sum = std::make_shared<ngraph::opset1::ReduceSum>(value, reduction_axes_const, keep_dims);
if (value.get_partial_shape().is_static()) {
const auto elems_number_value = get_num_elements(value.get_shape(), reduction_axes);
elems_number = ngraph::opset1::Constant::create(value_elem_type, Shape{}, {elems_number_value});
} else {
elems_number = get_num_elements(value, reduction_axes_const);
elems_number = std::make_shared<ngraph::opset1::Convert>(elems_number, value_elem_type);
}
return std::make_shared<ngraph::opset1::Divide>(value_elems_sum, elems_number);
}
std::shared_ptr<Node> builder::opset1::mean(const Output<Node>& value,
const Output<Node>& reduction_axes,
bool keep_dims) {
std::shared_ptr<Node> elems_number;
const auto value_elem_type = value.get_element_type();
const auto value_elems_sum = std::make_shared<ngraph::opset1::ReduceSum>(value, reduction_axes, keep_dims);
elems_number = get_num_elements(value, reduction_axes);
elems_number = std::make_shared<ngraph::opset1::Convert>(elems_number, value_elem_type);
return std::make_shared<ngraph::opset1::Divide>(value_elems_sum, elems_number);
}
std::shared_ptr<Node> builder::opset1::variance(const Output<Node>& value,
const AxisSet& reduction_axes,
const bool bessel_correction) {
const bool keep_dims = true;
std::shared_ptr<Node> mu = opset1::mean(value, reduction_axes, keep_dims);
Output<Node> diff = std::make_shared<ngraph::opset1::Subtract>(value, mu);
diff = std::make_shared<ngraph::opset1::ReduceSum>(
std::make_shared<ngraph::opset1::Multiply>(diff, diff),
ngraph::opset1::Constant::create(element::i64, Shape{reduction_axes.size()}, reduction_axes.to_vector()),
false);
const auto& et = value.get_element_type();
const auto N = get_num_elements(value.get_shape(), reduction_axes);
std::shared_ptr<Node> result;
if (bessel_correction) {
const auto N1const = ngraph::opset1::Constant::create(et, Shape{}, {N - 1});
result = std::make_shared<ngraph::opset1::Divide>(diff, N1const);
} else {
const auto Nconst = ngraph::opset1::Constant::create(et, Shape{}, {N});
result = std::make_shared<ngraph::opset1::Divide>(diff, Nconst);
}
return result;
}
std::shared_ptr<Node> builder::opset1::variance(const Output<Node>& value,
const Output<Node>& reduction_axes,
bool keep_dims,
bool bessel_correction) {
std::shared_ptr<Node> mu = opset1::mean(value, reduction_axes, keep_dims);
Output<Node> diff = std::make_shared<ngraph::opset1::Subtract>(value, mu);
diff = std::make_shared<ngraph::opset1::ReduceSum>(std::make_shared<ngraph::opset1::Multiply>(diff, diff),
reduction_axes,
keep_dims);
const auto& et = value.get_element_type();
auto N = get_num_elements(value, reduction_axes);
N = std::make_shared<ngraph::opset1::Convert>(N, et);
std::shared_ptr<Node> result;
if (bessel_correction) {
const auto one = std::make_shared<ngraph::opset1::Constant>(et, Shape{}, 1);
N = std::make_shared<ngraph::opset1::Subtract>(N, one);
}
result = std::make_shared<ngraph::opset1::Divide>(diff, N);
return result;
}
} // namespace builder
} // namespace ngraph

View File

@ -1,200 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/builder/reshape.hpp"
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include "ngraph/axis_vector.hpp"
#include "ngraph/op/concat.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/op/reduce_prod.hpp"
#include "ngraph/op/reshape.hpp"
#include "ngraph/op/shape_of.hpp"
#include "ngraph/op/squeeze.hpp"
#include "ngraph/op/transpose.hpp"
#include "ngraph/op/variadic_split.hpp"
#include "ngraph/opsets/opset1.hpp"
#include "ngraph/util.hpp"
#include "ngraph/validation_util.hpp"
using namespace ngraph;
using namespace std;
shared_ptr<Node> builder::opset1::reshape(const Output<Node>& value, const Shape& shape) {
if (value.get_partial_shape().same_scheme(shape)) {
return value.get_node_shared_ptr();
} else if (is_scalar(shape)) {
auto value_rank = value.get_shape().size();
AxisVector axes_vector(value_rank);
std::iota(axes_vector.begin(), axes_vector.end(), 0);
auto axes = op::Constant::create(element::i64, Shape{value_rank}, axes_vector);
return std::make_shared<op::Squeeze>(value, axes);
} else {
auto out_pattern =
op::Constant::create(element::i64, Shape{shape.size()}, vector<int64_t>(shape.begin(), shape.end()));
return make_shared<ngraph::opset1::Reshape>(value, out_pattern, false);
}
}
shared_ptr<Node> builder::opset1::reorder_axes(const Output<Node>& value, vector<size_t> axes_order) {
const auto axes_order_const = op::Constant::create(element::i64,
Shape{axes_order.size()},
vector<int64_t>(axes_order.begin(), axes_order.end()));
return make_shared<ngraph::opset1::Transpose>(value, axes_order_const);
}
shared_ptr<Node> builder::opset1::transpose(const Output<Node>& value) {
// This part is left to preserve backward compatibility and ensure passing ONNX tests.
if (value.get_partial_shape().is_static()) {
vector<size_t> axes_order(value.get_shape().size());
iota(begin(axes_order), end(axes_order), 0);
reverse(begin(axes_order), end(axes_order));
return builder::opset1::reorder_axes(value, axes_order);
}
const auto input_rank = std::make_shared<ngraph::opset1::ShapeOf>(std::make_shared<ngraph::opset1::ShapeOf>(value));
const auto neg_one = ngraph::opset1::Constant::create(element::i64, Shape{}, {-1});
const auto start_node = std::make_shared<ngraph::opset1::Add>(input_rank, neg_one);
const auto reverse_axes_order = std::make_shared<ngraph::opset1::Range>(reshape(start_node, Shape{}), // start
neg_one, // stop (exclusive)
neg_one); // step
return std::make_shared<ngraph::opset1::Transpose>(value, reverse_axes_order);
}
namespace ngraph {
namespace builder {
namespace opset1 {
namespace {
///
/// \brief Return the node representing normalized axis with respect to
/// provided rank.
///
/// \param[in] node_rank The node representing rank used for normalization.
/// \param[in] axis The axis value to be normalized.
///
/// \return The new Constant node representing normalized axis value.
///
std::shared_ptr<Node> get_normalized_axis_node(const std::shared_ptr<Node> node_rank, int64_t axis) {
auto axis_node = ngraph::opset1::Constant::create(element::i64, Shape{1}, {axis});
// shortcut for already positive value
if (axis >= 0) {
return axis_node;
}
// TODO: What if axis value is beyond acceptable values? [-node_rank,
// node_rank-1]
return make_shared<ngraph::opset1::Add>(node_rank, axis_node);
}
} // namespace
} // namespace opset1
} // namespace builder
} // namespace ngraph
shared_ptr<Node> builder::opset1::flatten(const Output<Node>& value, int axis) {
// First dimension of output tensor is the product of [d_0, ... d_{axis-1}] dimensions of
// input tensor. The last dimension is the product of the rest of input tensor dimensions:
// [d_{axis}, ..., d_n]
shared_ptr<Node> output_shape;
if (axis == 0) {
output_shape = ngraph::opset1::Constant::create(element::i64, Shape{2}, {1, -1});
} else if (axis == 1) {
output_shape = ngraph::opset1::Constant::create(element::i64, Shape{2}, {0, -1});
} else {
const auto value_shape = make_shared<ngraph::opset1::ShapeOf>(value);
const auto value_rank = make_shared<ngraph::opset1::ShapeOf>(value_shape);
const auto axis_node = get_normalized_axis_node(value_rank, axis);
const auto first_part_dims =
make_shared<ngraph::opset1::StridedSlice>(value_shape,
ngraph::opset1::Constant::create(element::i64, {1}, {0}),
axis_node,
vector<int64_t>{0},
vector<int64_t>{0});
const auto first_part_dims_length =
make_shared<ngraph::opset1::ReduceProd>(first_part_dims,
ngraph::opset1::Constant::create(element::i64, {}, {0}),
true);
const auto remaining_part_length = ngraph::opset1::Constant::create(element::i64, {1}, {-1});
output_shape =
make_shared<ngraph::opset1::Concat>(OutputVector{first_part_dims_length, remaining_part_length}, 0);
}
return make_shared<ngraph::opset1::Reshape>(value, output_shape, true);
}
shared_ptr<Node> builder::opset1::expand_dims(const Output<Node>& value, size_t axis) {
Shape output_shape(value.get_shape());
// Add empty axis at specified position.
auto empty_axis_it = begin(output_shape);
advance(empty_axis_it, axis);
output_shape.insert(empty_axis_it, 1);
return builder::opset1::reshape(value, output_shape);
}
shared_ptr<Node> builder::opset1::squeeze(const Output<Node>& value, vector<size_t> axes) {
if (axes.empty()) {
return value.get_node_shared_ptr();
}
Shape in_shape{value.get_shape()};
for (size_t idx = 0; idx < axes.size(); ++idx) {
in_shape.at(axes.at(idx)) = 0;
}
Shape output_shape;
for (auto axis : in_shape) {
if (axis != 0) {
output_shape.push_back(axis);
}
}
return builder::opset1::reshape(value, output_shape);
}
shared_ptr<Node> builder::opset1::collapse(const Output<Node>& value, const size_t start_axis, const size_t end_axis) {
if (start_axis == end_axis) {
return value.get_node_shared_ptr();
}
if (value.get_partial_shape().is_static()) {
auto shape = value.get_shape();
// Multiply all elements of shape from start_axis to end_axis inclusive
size_t collapsed_axis_size = accumulate(next(begin(shape), start_axis),
next(begin(shape), end_axis + 1),
size_t{1},
multiplies<size_t>());
Shape output_shape{};
output_shape.insert(begin(output_shape), begin(shape), next(begin(shape), start_axis));
output_shape.insert(end(output_shape), collapsed_axis_size);
output_shape.insert(end(output_shape), next(begin(shape), end_axis + 1), end(shape));
return builder::opset1::reshape(value, output_shape);
}
const auto shape = make_shared<ngraph::opset1::ShapeOf>(value);
const auto rank = make_shared<ngraph::opset1::ShapeOf>(shape);
// Split lengths used in VariadicSplit
const auto start_axis_node = ngraph::opset1::Constant::create(element::i64, {1}, {start_axis});
const auto end_axis_node = ngraph::opset1::Constant::create(element::i64, {1}, {end_axis + 1});
const auto collapsed_axis = make_shared<ngraph::opset1::Subtract>(end_axis_node, start_axis_node);
const auto post_axis = make_shared<ngraph::opset1::Subtract>(rank, end_axis_node);
const auto split_lengths =
make_shared<ngraph::opset1::Concat>(OutputVector{start_axis_node, collapsed_axis, post_axis}, 0);
const auto split_axis = ngraph::opset1::Constant::create(element::i64, {}, {0});
const auto split_node = make_shared<ngraph::opset1::VariadicSplit>(shape, split_axis, split_lengths);
const auto reduced_axis = ngraph::opset1::Constant::create(element::i64, {1}, {0});
const auto collapsed_axis_size = make_shared<ngraph::opset1::ReduceProd>(split_node->output(1), reduced_axis, true);
const auto collapsed_shape = make_shared<ngraph::opset1::Concat>(
OutputVector{split_node->output(0), collapsed_axis_size, split_node->output(2)},
0);
return make_shared<ngraph::opset1::Reshape>(value, collapsed_shape, false);
}

View File

@ -1,27 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/builder/split.hpp"
#include "ngraph/opsets/opset1.hpp"
using namespace ngraph;
OutputVector builder::opset1::split(const Output<Node>& value,
const std::vector<int64_t>& split_lengths,
int64_t axis) {
const auto axis_node = ngraph::opset1::Constant::create(element::i64, Shape{}, {axis});
const auto split_lengths_node =
ngraph::opset1::Constant::create(element::i64, Shape{split_lengths.size()}, split_lengths);
const auto variadic_split = std::make_shared<ngraph::opset1::VariadicSplit>(value, axis_node, split_lengths_node);
return variadic_split->outputs();
}
OutputVector builder::opset1::split(const Output<Node>& value, int64_t num_splits, int64_t axis) {
const auto axis_node = ngraph::opset1::Constant::create(element::i64, Shape{}, {axis});
const auto split = std::make_shared<ngraph::opset1::Split>(value, axis_node, num_splits);
return split->outputs();
}

View File

@ -1,16 +0,0 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <algorithm>
#include <functional>
#include <iterator>
#include <memory>
#include <numeric>
#include <sstream>
#include <utility>
#include <vector>
#include <cstddef>

View File

@ -9,7 +9,6 @@
#include "common_test_utils/graph_comparator.hpp"
#include "common_test_utils/test_tools.hpp"
#include "common_test_utils/type_prop.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/graph_util.hpp"
#include "openvino/core/except.hpp"
#include "openvino/op/abs.hpp"
@ -26,6 +25,7 @@
#include "openvino/op/split.hpp"
#include "openvino/op/squeeze.hpp"
#include "openvino/op/util/variable.hpp"
#include "ov_models/ov_builders/broadcast.hpp"
using namespace std;
using namespace ov;
@ -36,8 +36,8 @@ TEST(build_graph, build_simple) {
auto arg1 = make_shared<ov::op::v0::Parameter>(element::f32, Shape{3});
auto arg2 = make_shared<ov::op::v0::Parameter>(element::f32, Shape{32, 7});
auto arg3 = make_shared<ov::op::v0::Parameter>(element::f32, Shape{32, 7});
auto broadcast_1 = ngraph::builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto b1 = ngraph::builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto broadcast_1 = ov::op::util::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto b1 = ov::op::util::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto dot = make_shared<op::v0::MatMul>(arg2, arg0);
ASSERT_EQ(dot->input_value(0).get_node_shared_ptr(), arg2);
ASSERT_EQ(dot->input_value(1).get_node_shared_ptr(), arg0);
@ -92,8 +92,8 @@ TEST(build_graph, function_undeclared_parameters) {
auto arg1 = make_shared<ov::op::v0::Parameter>(element::f32, Shape{3});
auto arg2 = make_shared<ov::op::v0::Parameter>(element::f32, Shape{32, 7});
auto arg3 = make_shared<ov::op::v0::Parameter>(element::f32, Shape{32, 7});
auto broadcast_1 = ngraph::builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto b1 = ngraph::builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto broadcast_1 = ov::op::util::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto b1 = ov::op::util::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto dot = make_shared<op::v0::MatMul>(arg2, arg0);
ASSERT_EQ(dot->input_values()[0].get_node_shared_ptr(), arg2);
ASSERT_EQ(dot->input_values()[1].get_node_shared_ptr(), arg0);
@ -438,8 +438,8 @@ TEST(build_graph, build_graph_parameters_autodetection) {
auto arg1 = make_shared<op::v0::Parameter>(element::f32, Shape{3});
auto arg2 = make_shared<op::v0::Parameter>(element::f32, Shape{32, 7});
auto arg3 = make_shared<op::v0::Parameter>(element::f32, Shape{32, 7});
auto broadcast_1 = ngraph::builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto b1 = ngraph::builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto broadcast_1 = ov::op::util::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto b1 = ov::op::util::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
auto dot = make_shared<op::v0::MatMul>(arg2, arg0);
auto f = make_shared<Model>(OutputVector{dot});

View File

@ -7,9 +7,9 @@
#include <map>
#include "common_test_utils/type_prop.hpp"
#include "ngraph/builder/reshape.hpp"
#include "openvino/core/model.hpp"
#include "openvino/opsets/opset5.hpp"
#include "ov_models/ov_builders/reshape.hpp"
using namespace std;
using namespace ov;
@ -34,14 +34,14 @@ TEST(type_prop, tensor_iterator_lstm) {
auto X = make_shared<ov::op::v0::Parameter>(element::f32, Shape{N, 1, I});
auto W_body = make_shared<ov::op::v0::Parameter>(element::f32, Shape{4 * H, I});
auto R_body = make_shared<ov::op::v0::Parameter>(element::f32, Shape{4 * H, H});
auto LSTM_cell = make_shared<opset5::LSTMCell>(ngraph::builder::opset1::reshape(X, Shape{N, I}),
ngraph::builder::opset1::reshape(H_t, Shape{N, H}),
ngraph::builder::opset1::reshape(C_t, Shape{N, H}),
auto LSTM_cell = make_shared<opset5::LSTMCell>(ov::op::util::reshape(X, Shape{N, I}),
ov::op::util::reshape(H_t, Shape{N, H}),
ov::op::util::reshape(C_t, Shape{N, H}),
W_body,
R_body,
H);
auto H_o = ngraph::builder::opset1::reshape(LSTM_cell->output(0), Shape{N, 1, H});
auto C_o = ngraph::builder::opset1::reshape(LSTM_cell->output(1), Shape{N, 1, H});
auto H_o = ov::op::util::reshape(LSTM_cell->output(0), Shape{N, 1, H});
auto C_o = ov::op::util::reshape(LSTM_cell->output(1), Shape{N, 1, H});
auto body = make_shared<ov::Model>(OutputVector{H_o, C_o}, ParameterVector{X, H_t, C_t, W_body, R_body});
auto tensor_iterator = make_shared<op::v0::TensorIterator>();
@ -197,14 +197,14 @@ TEST(type_prop, tensor_iterator_with_dynamic_reshape) {
auto X = make_shared<ov::op::v0::Parameter>(element::f32, Shape{N, 1, I});
auto W_body = make_shared<ov::op::v0::Parameter>(element::f32, Shape{4 * H, I});
auto R_body = make_shared<ov::op::v0::Parameter>(element::f32, Shape{4 * H, H});
auto LSTM_cell = make_shared<opset5::LSTMCell>(ngraph::builder::opset1::reshape(X, Shape{N, I}),
ngraph::builder::opset1::reshape(H_t, Shape{N, H}),
ngraph::builder::opset1::reshape(C_t, Shape{N, H}),
auto LSTM_cell = make_shared<opset5::LSTMCell>(ov::op::util::reshape(X, Shape{N, I}),
ov::op::util::reshape(H_t, Shape{N, H}),
ov::op::util::reshape(C_t, Shape{N, H}),
W_body,
R_body,
H);
auto H_o = ngraph::builder::opset1::reshape(LSTM_cell->output(0), Shape{N, 1, H});
auto C_o = ngraph::builder::opset1::reshape(LSTM_cell->output(1), Shape{N, 1, H});
auto H_o = ov::op::util::reshape(LSTM_cell->output(0), Shape{N, 1, H});
auto C_o = ov::op::util::reshape(LSTM_cell->output(1), Shape{N, 1, H});
auto body = make_shared<ov::Model>(OutputVector{H_o, C_o}, ParameterVector{X, H_t, C_t, W_body, R_body});
auto tensor_iterator = make_shared<op::v0::TensorIterator>();

View File

@ -6,10 +6,10 @@
#include <gtest/gtest.h>
#include "ngraph/builder/reshape.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/lstm_cell.hpp"
#include "openvino/op/multiply.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "visitors/visitors.hpp"
using namespace std;
@ -39,14 +39,14 @@ TEST(attributes, tensor_iterator_lstm) {
auto X = make_shared<ov::op::v0::Parameter>(element::f32, Shape{N, 1, I});
auto W_body = make_shared<ov::op::v0::Parameter>(element::f32, Shape{4 * H, I});
auto R_body = make_shared<ov::op::v0::Parameter>(element::f32, Shape{4 * H, H});
auto LSTM_cell = make_shared<ov::op::v4::LSTMCell>(ngraph::builder::opset1::reshape(X, Shape{N, I}),
ngraph::builder::opset1::reshape(H_t, Shape{N, H}),
ngraph::builder::opset1::reshape(C_t, Shape{N, H}),
auto LSTM_cell = make_shared<ov::op::v4::LSTMCell>(ov::op::util::reshape(X, Shape{N, I}),
ov::op::util::reshape(H_t, Shape{N, H}),
ov::op::util::reshape(C_t, Shape{N, H}),
W_body,
R_body,
H);
auto H_o = ngraph::builder::opset1::reshape(LSTM_cell->output(0), Shape{N, 1, H});
auto C_o = ngraph::builder::opset1::reshape(LSTM_cell->output(1), Shape{N, 1, H});
auto H_o = ov::op::util::reshape(LSTM_cell->output(0), Shape{N, 1, H});
auto C_o = ov::op::util::reshape(LSTM_cell->output(1), Shape{N, 1, H});
auto body = make_shared<ov::Model>(OutputVector{H_o, C_o}, ParameterVector{X, H_t, C_t, W_body, R_body});
auto tensor_iterator = make_shared<op::v0::TensorIterator>();

View File

@ -6,7 +6,6 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/shape.hpp"
#include "utils/common.hpp"

View File

@ -6,7 +6,6 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/shape.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START

View File

@ -8,7 +8,6 @@
#include <memory>
#include "default_opset.hpp"
#include "ngraph/builder/make_constant.hpp"
#include "ngraph/validation_util.hpp"
#include "onnx_import/core/null_node.hpp"

View File

@ -5,8 +5,8 @@
#include "op/com.microsoft/attention.hpp"
#include "default_opset.hpp"
#include "ngraph/builder/split.hpp"
#include "onnx_import/core/null_node.hpp"
#include "ov_models/ov_builders/split.hpp"
namespace ngraph {
namespace onnx_import {
@ -122,7 +122,7 @@ NodeVector split_to_QKV(const std::shared_ptr<default_opset::Add>& node,
// head_size = hidden_size / num_heads
head_size = std::make_shared<default_opset::Divide>(hidden_size, num_heads_node);
// split the node into 3 even parts Q, K, V with shape (batch_size, sequence_len, hidden_size)
split = ngraph::builder::opset1::split(node, 3, 2);
split = ov::op::util::split(node, 3, 2);
// and reshape each part to new shape (batch_size, sequence_len, num_heads, head_size)
auto new_shape =
std::make_shared<default_opset::Concat>(NodeVector{batch_size_seq_len, num_heads_node, head_size}, 0);
@ -141,7 +141,7 @@ NodeVector split_to_QKV(const std::shared_ptr<default_opset::Add>& node,
// Q: (batch_size, sequence_len, qkv_hidden_sizes[0])
// K: (batch_size, sequence_len, qkv_hidden_sizes[1])
// V: (batch_size, sequence_len, qkv_hidden_sizes[2])
split = ngraph::builder::opset1::split(node, qkv_hidden_sizes, 2);
split = ov::op::util::split(node, qkv_hidden_sizes, 2);
// and reshape each part to new shape (batch_size, sequence_len, num_heads, head_size)
for (size_t i = 0; i < split.size(); i++) {
auto new_shape = std::make_shared<default_opset::Concat>(
@ -455,7 +455,7 @@ std::shared_ptr<ngraph::Node> attention_softmax(const OutputVector& op_inputs,
// (2, batch_size, num_heads, past_sequence_length + sequence_length, head_size)
// so we need to split it into two parts, remove first dimension from each part and concatenate first part
// with current K and second part with current V
const auto split = ngraph::builder::opset1::split(past, 2, 0);
const auto split = ov::op::util::split(past, 2, 0);
const auto past_K = std::make_shared<default_opset::Squeeze>(split[0], zero);
K = std::make_shared<default_opset::Concat>(NodeVector{past_K, K}, 2);
const auto past_V = std::make_shared<default_opset::Squeeze>(split[1], zero);

View File

@ -7,7 +7,6 @@
#include <memory>
#include "default_opset.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/op/add.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/op/matmul.hpp"

View File

@ -7,7 +7,7 @@
#include <memory>
#include "default_opset.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ov_models/ov_builders/reshape.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
@ -22,7 +22,7 @@ OutputVector compress(const Node& node) {
if (node.has_attribute("axis")) {
axis = node.get_attribute_value<int64_t>("axis");
} else {
data = std::make_shared<default_opset::Squeeze>(ngraph::builder::opset1::flatten(data, static_cast<int>(axis)));
data = std::make_shared<default_opset::Squeeze>(ov::op::util::flatten(data, static_cast<int>(axis)));
}
auto axis_node = default_opset::Constant::create(element::i64, Shape{}, {axis});
auto zero_node = default_opset::Constant::create(element::i64, Shape{}, {0});

View File

@ -10,10 +10,10 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/op/group_conv.hpp"
#include "ngraph/op/util/attr_types.hpp"
#include "onnx_import/core/null_node.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "utils/conv_factory.hpp"
#include "utils/convpool.hpp"
#include "utils/reshape.hpp"

View File

@ -13,14 +13,13 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/coordinate_diff.hpp"
#include "ngraph/op/util/attr_types.hpp"
#include "ngraph/output_vector.hpp"
#include "ngraph/partial_shape.hpp"
#include "ngraph/shape.hpp"
#include "ngraph/validation_util.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "utils/convpool.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START

View File

@ -6,7 +6,6 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/shape.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START

View File

@ -9,7 +9,6 @@
#include "default_opset.hpp"
#include "ngraph/axis_set.hpp"
#include "ngraph/builder/make_constant.hpp"
#include "ngraph/op/convert.hpp"
#include "ngraph/shape.hpp"
#include "ngraph/validation_util.hpp"

View File

@ -10,7 +10,6 @@ OPENVINO_SUPPRESS_DEPRECATED_START
#include <memory>
#include "default_opset.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/node.hpp"
#include "ngraph/shape.hpp"
#include "onnx_import/core/node.hpp"

View File

@ -9,7 +9,6 @@
#include "default_opset.hpp"
#include "ngraph/axis_set.hpp"
#include "ngraph/builder/make_constant.hpp"
#include "ngraph/op/convert.hpp"
#include "ngraph/shape.hpp"
#include "ngraph/validation_util.hpp"

View File

@ -7,8 +7,8 @@
#include <cinttypes>
#include "exceptions.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/validation_util.hpp"
#include "ov_models/ov_builders/reshape.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
@ -28,7 +28,7 @@ OutputVector flatten(const Node& node) {
axis = ngraph::normalize_axis(node.get_description(), axis, data_rank_value, -data_rank_value, data_rank_value);
OPENVINO_SUPPRESS_DEPRECATED_END
}
return {ngraph::builder::opset1::flatten(data, static_cast<int>(axis))};
return {ov::op::util::flatten(data, static_cast<int>(axis))};
}
} // namespace set_1

View File

@ -7,11 +7,11 @@
#include <memory>
#include "default_opset.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/op/add.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/op/matmul.hpp"
#include "ngraph/op/multiply.hpp"
#include "ov_models/ov_builders/reshape.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
@ -37,15 +37,15 @@ OutputVector gemm(const Node& node) {
const bool trans_b = node.get_attribute_value<int64_t>("transB", 0);
if (trans_a) {
input_a = ngraph::builder::opset1::transpose(input_a);
input_a = ov::op::util::transpose(input_a);
}
if (trans_b) {
input_b = ngraph::builder::opset1::transpose(input_b);
input_b = ov::op::util::transpose(input_b);
}
input_a = ngraph::builder::opset1::flatten(input_a, 1);
input_b = ngraph::builder::opset1::flatten(input_b, 1);
input_a = ov::op::util::flatten(input_a, 1);
input_b = ov::op::util::flatten(input_b, 1);
std::shared_ptr<ngraph::Node> matmul_node = std::make_shared<default_opset::MatMul>(input_a, input_b);

View File

@ -8,10 +8,10 @@
#include <vector>
#include "default_opset.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/builder/split.hpp"
#include "ngraph/shape.hpp"
#include "onnx_import/core/null_node.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "ov_models/ov_builders/split.hpp"
#include "utils/recurrent.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
@ -33,7 +33,7 @@ struct GRUInputMap : public recurrent::OpInputMap {
auto bias = ng_inputs.at(3);
// gates_count * 2 since B is: [Wb, Rb]
const int split_parts = 2 * 3;
const auto split_bias = builder::opset1::split(bias, split_parts, 1);
const auto split_bias = ov::op::util::split(bias, split_parts, 1);
const auto wr_z_bias = std::make_shared<default_opset::Add>(split_bias.at(0), split_bias.at(3));
const auto wr_r_bias = std::make_shared<default_opset::Add>(split_bias.at(1), split_bias.at(4));
// The result has shape: [num_directions, 4 * hidden_size]
@ -98,7 +98,7 @@ OutputVector gru(const Node& node) {
const auto Y = gru_sequence->output(0);
const auto Y_h = gru_sequence->output(1);
return {builder::opset1::reorder_axes(Y, {2, 1, 0, 3}), builder::opset1::reorder_axes(Y_h, {1, 0, 2})};
return {ov::op::util::reorder_axes(Y, {2, 1, 0, 3}), ov::op::util::reorder_axes(Y_h, {1, 0, 2})};
}
} // namespace set_1

View File

@ -5,10 +5,10 @@
#include "op/hardmax.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/op/one_hot.hpp"
#include "ngraph/op/topk.hpp"
#include "ngraph/validation_util.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "utils/common.hpp"
#include "utils/reshape.hpp"
@ -29,7 +29,7 @@ OutputVector hardmax(const Node& node) {
}
// reshape to 2D - "batch size" x "input feature dimensions" (NxD)
const auto coerced_tensor = ngraph::builder::opset1::flatten(input, static_cast<int>(axis));
const auto coerced_tensor = ov::op::util::flatten(input, static_cast<int>(axis));
const auto coerced_tensor_shape = std::make_shared<default_opset::ShapeOf>(coerced_tensor);
Output<ngraph::Node> row_size =

View File

@ -10,8 +10,6 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/axis_set.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/builder/reduce_ops.hpp"
#include "ngraph/op/add.hpp"
#include "ngraph/op/divide.hpp"
#include "ngraph/op/multiply.hpp"

View File

@ -7,15 +7,15 @@
#include <memory>
#include "default_opset.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/validation_util.hpp"
#include "ov_models/ov_builders/reshape.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
namespace onnx_import {
namespace {
std::shared_ptr<ngraph::Node> onnx_logsoftmax(const Output<ngraph::Node> data, const int64_t axis) {
const auto coerced_data = ngraph::builder::opset1::flatten(data, static_cast<int>(axis));
const auto coerced_data = ov::op::util::flatten(data, static_cast<int>(axis));
const auto result = std::make_shared<default_opset::LogSoftmax>(coerced_data, 1);
const auto data_shape = std::make_shared<default_opset::ShapeOf>(data);
return std::make_shared<default_opset::Reshape>(result, data_shape, false);

View File

@ -13,9 +13,9 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/axis_set.hpp"
#include "ngraph/builder/norm.hpp"
#include "ngraph/op/divide.hpp"
#include "ngraph/validation_util.hpp"
#include "ov_models/ov_builders/norm.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
@ -42,7 +42,7 @@ OutputVector lp_norm(const Node& node) {
const auto normalize_axis_const = default_opset::Constant::create(element::i64, {}, {normalize_axis});
std::shared_ptr<ngraph::Node> norm =
ngraph::builder::opset1::lp_norm(data, normalize_axis_const, static_cast<std::size_t>(p_norm), 0.0f, true);
ov::op::util::lp_norm(data, normalize_axis_const, static_cast<std::size_t>(p_norm), 0.0f, true);
return {std::make_shared<default_opset::Divide>(data, norm)};
}

View File

@ -11,9 +11,9 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/axis_set.hpp"
#include "ngraph/builder/norm.hpp"
#include "ngraph/builder/split.hpp"
#include "ngraph/util.hpp"
#include "ov_models/ov_builders/norm.hpp"
#include "ov_models/ov_builders/split.hpp"
#include "utils/common.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
@ -36,13 +36,13 @@ OutputVector global_lp_pool(const Node& node) {
CHECK_VALID_NODE(node, p_norm >= 0, "Only positive (including zero) values are supported for 'p' attribute.");
OutputVector slices = ngraph::builder::opset1::split(data, channels_count, channel_axis);
OutputVector slices = ov::op::util::split(data, channels_count, channel_axis);
for (auto& slice : slices) {
// all dimensions except spatial/feature
const auto reduction_axes = common::get_monotonic_range_along_node_rank(data, 2);
slice = ngraph::builder::opset1::lp_norm(slice, reduction_axes, static_cast<std::size_t>(p_norm));
slice = ov::op::util::lp_norm(slice, reduction_axes, static_cast<std::size_t>(p_norm));
// output shape is all ones except N channel
Shape output_shape(data_shape.rank().get_length(), 1);

View File

@ -13,8 +13,6 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/builder/split.hpp"
#include "ngraph/enum_names.hpp"
#include "ngraph/log.hpp"
#include "ngraph/op/add.hpp"
@ -25,6 +23,8 @@
#include "ngraph/shape.hpp"
#include "ngraph/type/element_type.hpp"
#include "onnx_import/core/null_node.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "ov_models/ov_builders/split.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
@ -55,7 +55,7 @@ struct LSTMNgInputMap {
// Packed input sequences.
// ONNX Shape: [seq_length, batch_size, input_size]
// OpenVino Shape: [batch_size, seq_length, input_size]
m_input_map[LSTMInput::LSTM_INPUT_X] = builder::opset1::reorder_axes(ng_inputs.at(0), {1, 0, 2});
m_input_map[LSTMInput::LSTM_INPUT_X] = ov::op::util::reorder_axes(ng_inputs.at(0), {1, 0, 2});
// Weight tensor for the gates.
// Shape: [num_directions, 4*hidden_size, input_size]
@ -101,7 +101,7 @@ struct LSTMNgInputMap {
// OpenVino Shape: [num_directions, 4*hidden_size]
if (ng_inputs.size() > 3 && !ngraph::op::is_null(ng_inputs.at(3))) {
auto bias = ng_inputs.at(3);
auto split_bias = builder::opset1::split(bias, 2, 1);
auto split_bias = ov::op::util::split(bias, 2, 1);
m_input_map[LSTMInput::LSTM_INPUT_B] =
std::make_shared<default_opset::Add>(split_bias.at(0), split_bias.at(1));
m_input_map[LSTMInput::LSTM_INPUT_B] =
@ -132,7 +132,7 @@ struct LSTMNgInputMap {
// ONNX Shape: [num_directions, batch_size, hidden_size]
// OpenVino Shape: [batch_size, num_directions, hidden_size]
if (ng_inputs.size() > 5 && !ngraph::op::is_null(ng_inputs.at(5))) {
m_input_map[LSTMInput::LSTM_INPUT_INIT_H] = builder::opset1::reorder_axes(ng_inputs.at(5), {1, 0, 2});
m_input_map[LSTMInput::LSTM_INPUT_INIT_H] = ov::op::util::reorder_axes(ng_inputs.at(5), {1, 0, 2});
} else {
auto init_h_shape = std::make_shared<default_opset::Concat>(
OutputVector{batch_size_node, num_directions_node, hidden_size_node},
@ -145,7 +145,7 @@ struct LSTMNgInputMap {
// ONNX Shape: [num_directions, batch_size, hidden_size]
// OpenVino Shape: [batch_size, num_directions, hidden_size]
if (ng_inputs.size() > 6 && !ngraph::op::is_null(ng_inputs.at(6))) {
m_input_map[LSTMInput::LSTM_INPUT_INIT_C] = builder::opset1::reorder_axes(ng_inputs.at(6), {1, 0, 2});
m_input_map[LSTMInput::LSTM_INPUT_INIT_C] = ov::op::util::reorder_axes(ng_inputs.at(6), {1, 0, 2});
} else {
auto init_c_shape = std::make_shared<default_opset::Concat>(
OutputVector{batch_size_node, num_directions_node, hidden_size_node},
@ -258,9 +258,9 @@ OutputVector lstm(const Node& node) {
const auto Y_h = lstm_sequence->output(1);
const auto Y_c = lstm_sequence->output(2);
return {builder::opset1::reorder_axes(Y, {2, 1, 0, 3}),
builder::opset1::reorder_axes(Y_h, {1, 0, 2}),
builder::opset1::reorder_axes(Y_c, {1, 0, 2})};
return {ov::op::util::reorder_axes(Y, {2, 1, 0, 3}),
ov::op::util::reorder_axes(Y_h, {1, 0, 2}),
ov::op::util::reorder_axes(Y_c, {1, 0, 2})};
}
} // namespace set_1

View File

@ -10,7 +10,6 @@ OPENVINO_SUPPRESS_DEPRECATED_START
#include <memory>
#include "default_opset.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/node.hpp"
#include "ngraph/op/broadcast.hpp"
#include "ngraph/op/multiply.hpp"

View File

@ -5,8 +5,6 @@
#include "op/org.openvinotoolkit/group_norm.hpp"
#include "default_opset.hpp"
#include "ngraph/builder/reduce_ops.hpp"
#include "ngraph/builder/split.hpp"
#include "ngraph/node.hpp"
#include "ngraph/opsets/opset5.hpp"
#include "onnx_import/core/node.hpp"

View File

@ -8,7 +8,6 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/split.hpp"
#include "ngraph/coordinate_diff.hpp"
#include "ngraph/op/constant.hpp"
#include "ngraph/op/convert.hpp"
@ -16,6 +15,7 @@
#include "ngraph/shape.hpp"
#include "onnx_import/core/null_node.hpp"
#include "op/pad.hpp"
#include "ov_models/ov_builders/split.hpp"
#include "utils/convpool.hpp"
#include "utils/reshape.hpp"
@ -91,7 +91,7 @@ OutputVector pad(const Node& node) {
padding_begin = default_opset::Constant::create(element::i64, ngraph::Shape{half_size}, padding_begin_values);
padding_end = default_opset::Constant::create(element::i64, ngraph::Shape{half_size}, padding_end_values);
} else {
OutputVector padding = builder::opset1::split(pads, 2, 0);
OutputVector padding = ov::op::util::split(pads, 2, 0);
padding_begin = padding.at(0);
padding_end = padding.at(1);

View File

@ -12,10 +12,10 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/axis_set.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/shape.hpp"
#include "ngraph/type/element_type.hpp"
#include "ngraph/validation_util.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "utils/reshape.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
@ -196,7 +196,7 @@ OutputVector quantize_linear(Output<ngraph::Node> x,
Shape target_shape(x_shape.rank().get_length(), 1);
target_shape[axis] = static_cast<size_t>(x_shape[axis].get_length());
y_scale = builder::opset1::reshape(y_scale, target_shape);
y_scale = ov::op::util::reshape(y_scale, target_shape);
}
if (y_zero_point_shape.rank().is_static() && y_zero_point_shape.rank().get_length() == 1 &&
@ -211,7 +211,7 @@ OutputVector quantize_linear(Output<ngraph::Node> x,
Shape target_shape(x_shape.rank().get_length(), 1);
target_shape[axis] = static_cast<size_t>(x_shape[axis].get_length());
y_zero_point = builder::opset1::reshape(y_zero_point, target_shape);
y_zero_point = ov::op::util::reshape(y_zero_point, target_shape);
}
return {detail::make_fake_quantize(y_scale, y_zero_point, x)};

View File

@ -9,7 +9,6 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/norm.hpp"
#include "ngraph/node.hpp"
#include "op/identity.hpp"
#include "utils/common.hpp"

View File

@ -7,7 +7,7 @@
#include <memory>
#include "default_opset.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "utils/recurrent.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
@ -50,7 +50,7 @@ OutputVector rnn(const Node& node) {
const auto Y = rnn_sequence->output(0);
const auto Y_h = rnn_sequence->output(1);
return {builder::opset1::reorder_axes(Y, {2, 1, 0, 3}), builder::opset1::reorder_axes(Y_h, {1, 0, 2})};
return {ov::op::util::reorder_axes(Y, {2, 1, 0, 3}), ov::op::util::reorder_axes(Y_h, {1, 0, 2})};
}
} // namespace set_1
} // namespace op

View File

@ -7,15 +7,15 @@
#include <memory>
#include "default_opset.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/validation_util.hpp"
#include "ov_models/ov_builders/reshape.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
namespace onnx_import {
namespace {
std::shared_ptr<ngraph::Node> onnx_softmax(const Output<ngraph::Node> data, const int64_t axis) {
const auto coerced_data = ngraph::builder::opset1::flatten(data, static_cast<int>(axis));
const auto coerced_data = ov::op::util::flatten(data, static_cast<int>(axis));
const auto result = std::make_shared<default_opset::Softmax>(coerced_data, 1);
const auto data_shape = std::make_shared<default_opset::ShapeOf>(data);
const bool special_zero = false;

View File

@ -2,12 +2,12 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/builder/split.hpp"
#include "op/split.hpp"
#include <vector>
#include "default_opset.hpp"
#include "op/split.hpp"
#include "ov_models/ov_builders/split.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
@ -20,10 +20,10 @@ OutputVector split(const Node& node) {
if (node.has_attribute("split")) {
const auto splits = node.get_attribute_value<std::vector<int64_t>>("split");
return ngraph::builder::opset1::split(input, splits, axis);
return ov::op::util::split(input, splits, axis);
} else {
const auto outputs_number = node.get_output_names().size();
return ngraph::builder::opset1::split(input, outputs_number, axis);
return ov::op::util::split(input, outputs_number, axis);
}
}
@ -36,7 +36,7 @@ OutputVector split(const Node& node) {
if (inputs.size() < 2) {
const auto outputs_number = node.get_output_names().size();
return ngraph::builder::opset1::split(inputs.at(0), outputs_number, axis);
return ov::op::util::split(inputs.at(0), outputs_number, axis);
} else {
const auto axis_node = default_opset::Constant::create(element::Type_t::i64, Shape{}, {axis});
return {std::make_shared<default_opset::VariadicSplit>(inputs.at(0), axis_node, inputs.at(1))->outputs()};
@ -49,4 +49,4 @@ OutputVector split(const Node& node) {
} // namespace onnx_import
} // namespace ngraph
OPENVINO_SUPPRESS_DEPRECATED_END
OPENVINO_SUPPRESS_DEPRECATED_END

View File

@ -8,7 +8,6 @@
OPENVINO_SUPPRESS_DEPRECATED_START
#include "default_opset.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/node.hpp"
#include "onnx_import/core/node.hpp"

View File

@ -7,8 +7,8 @@
#include <memory>
#include <vector>
#include "ngraph/builder/reshape.hpp"
#include "ngraph/node.hpp"
#include "ov_models/ov_builders/reshape.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
@ -20,8 +20,7 @@ OutputVector transpose(const Node& node) {
auto permute_axes = node.get_attribute_value<std::vector<std::size_t>>("perm", {});
return {(permute_axes.empty()) ? ngraph::builder::opset1::transpose(data)
: ngraph::builder::opset1::reorder_axes(data, permute_axes)};
return {(permute_axes.empty()) ? ov::op::util::transpose(data) : ov::op::util::reorder_axes(data, permute_axes)};
}
} // namespace set_1

View File

@ -6,10 +6,10 @@
#include "default_opset.hpp"
#include "exceptions.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/op/group_conv.hpp"
#include "ngraph/op/util/attr_types.hpp"
#include "onnx_import/core/null_node.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "utils/conv_factory.hpp"
#include "utils/convpool.hpp"
#include "utils/reshape.hpp"

View File

@ -9,12 +9,11 @@
#include <vector>
#include "default_opset.hpp"
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/builder/split.hpp"
#include "ngraph/check.hpp"
#include "ngraph/enum_names.hpp"
#include "onnx_import/core/null_node.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include "ov_models/ov_builders/split.hpp"
OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
@ -23,7 +22,7 @@ namespace recurrent {
OpInputMap::OpInputMap(const onnx_import::Node& node, std::size_t gates_count) {
const auto& ng_inputs = node.get_ng_inputs();
m_map[OpInput::X] = builder::opset1::reorder_axes(ng_inputs.at(0), {1, 0, 2});
m_map[OpInput::X] = ov::op::util::reorder_axes(ng_inputs.at(0), {1, 0, 2});
m_map[OpInput::W] = ng_inputs.at(1);
m_map[OpInput::R] = ng_inputs.at(2);
@ -56,7 +55,7 @@ OpInputMap::OpInputMap(const onnx_import::Node& node, std::size_t gates_count) {
// ------ Optional inputs ------
if (ng_inputs.size() > 3 && !ngraph::op::is_null(ng_inputs.at(3))) {
auto bias = ng_inputs.at(3);
auto split_bias = builder::opset1::split(bias, 2, 1);
auto split_bias = ov::op::util::split(bias, 2, 1);
m_map[OpInput::B] = std::make_shared<default_opset::Add>(split_bias.at(0), split_bias.at(1));
} else {
auto b_shape = std::make_shared<default_opset::Concat>(
@ -76,7 +75,7 @@ OpInputMap::OpInputMap(const onnx_import::Node& node, std::size_t gates_count) {
}
// The initial value of the hidden.
if (ng_inputs.size() > 5 && !ngraph::op::is_null(ng_inputs.at(5))) {
m_map[OpInput::INIT_H] = builder::opset1::reorder_axes(ng_inputs.at(5), {1, 0, 2});
m_map[OpInput::INIT_H] = ov::op::util::reorder_axes(ng_inputs.at(5), {1, 0, 2});
} else {
auto init_h_shape = std::make_shared<default_opset::Concat>(
OutputVector{batch_size_node, num_directions_node, hidden_size_node},

View File

@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/builder/reshape.hpp"
#include "ov_models/ov_builders/reshape.hpp"
#include <algorithm>
#include <functional>
@ -10,7 +10,6 @@
#include <numeric>
#include "default_opset.hpp"
#include "ngraph/builder/make_constant.hpp"
#include "ngraph/op/util/op_types.hpp"
#include "ngraph/shape.hpp"
#include "utils/reshape.hpp"
@ -79,7 +78,7 @@ Output<ngraph::Node> interpret_as_scalar(const Output<ngraph::Node>& node) {
return std::make_shared<default_opset::Constant>(node.get_element_type(), ngraph::Shape{}, value);
}
return builder::opset1::reshape(node, Shape{});
return ov::op::util::reshape(node, Shape{});
}
Output<ngraph::Node> reshape_channel_shaped_node_to_nchw(const Output<ngraph::Node>& node,

View File

@ -13,8 +13,8 @@ set (SRC
backend.hpp
executable.cpp
executable.hpp
int_backend.cpp
int_executable.cpp
int_backend.cpp
int_executable.cpp
evaluates_map.cpp
)
@ -39,7 +39,7 @@ target_compile_definitions(${TARGET_NAME}
SHARED_LIB_PREFIX="${CMAKE_SHARED_LIBRARY_PREFIX}"
SHARED_LIB_SUFFIX="${OV_BUILD_POSTFIX}${CMAKE_SHARED_LIBRARY_SUFFIX}"
)
target_link_libraries(${TARGET_NAME} PRIVATE openvino::builders openvino::reference openvino::util openvino::runtime::dev openvino::shape_inference)
target_link_libraries(${TARGET_NAME} PRIVATE openvino::reference openvino::util openvino::runtime::dev openvino::shape_inference)
target_include_directories(${TARGET_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ops/>

View File

@ -23,6 +23,8 @@ ov_add_target(
openvino::runtime::dev
common_test_utils
ADD_CLANG_FORMAT
EXCLUDED_SOURCE_PATHS
"${CMAKE_CURRENT_SOURCE_DIR}/ov_builders"
)
ov_build_target_faster(${TARGET_NAME}
@ -34,3 +36,5 @@ ov_build_target_faster(${TARGET_NAME}
ov_developer_package_export_targets(TARGET ${TARGET_NAME}
INSTALL_INCLUDE_DIRECTORIES "${PUBLIC_HEADERS_DIR}/")
add_subdirectory(ov_builders)

View File

@ -1,8 +1,8 @@
# Copyright (C) 2018-2023 Intel Corporation
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
set(TARGET_NAME "openvino_builders")
set(TARGET_NAME ov_builders)
set(BUILDER_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include/)
@ -21,9 +21,7 @@ add_library(${TARGET_NAME} STATIC ${LIBRARY_SRC} ${PUBLIC_HEADERS})
add_library(openvino::builders ALIAS ${TARGET_NAME})
set_target_properties(${TARGET_NAME} PROPERTIES EXPORT_NAME builders)
ov_build_target_faster(${TARGET_NAME}
UNITY
PCH PRIVATE "src/precomp.hpp")
ov_build_target_faster(${TARGET_NAME} UNITY)
target_include_directories(${TARGET_NAME} PUBLIC
$<BUILD_INTERFACE:${BUILDER_INCLUDE_DIR}>
@ -33,11 +31,13 @@ if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(${TARGET_NAME} PUBLIC OPENVINO_STATIC_LIBRARY)
endif()
target_link_libraries(${TARGET_NAME} PRIVATE openvino::runtime)
ov_add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})
# install & export
ov_install_static_lib(openvino_builders ${OV_CPACK_COMP_CORE})
ov_install_static_lib(${TARGET_NAME} ${OV_CPACK_COMP_CORE})
ov_developer_package_export_targets(TARGET openvino::builders
INSTALL_INCLUDE_DIRECTORIES "${BUILDER_INCLUDE_DIR}/")
ov_developer_package_export_targets(TARGET ${TARGET_NAME}
INSTALL_INCLUDE_DIRECTORIES "${BUILDER_INCLUDE_DIR}/")

View File

@ -0,0 +1,17 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "openvino/core/node.hpp"
namespace ov {
namespace op {
namespace util {
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, const AxisSet& broadcast_axes);
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, std::size_t start_match_axis);
} // namespace util
} // namespace op
} // namespace ov

View File

@ -0,0 +1,30 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <memory>
#include "openvino/core/node.hpp"
namespace ov {
namespace op {
namespace util {
/// \brief Creates node which calculates L-p norm on input tensor.
///
/// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] p_norm The p norm to calculate.
/// \param[in] bias The bias added to the calculated sum.
/// \param[in] keep_dims The flag indicates if axes will be removed or kept.
///
/// \return L-p norm of value. The output sub-graph is composed of v1 ops.
///
std::shared_ptr<Node> lp_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
std::size_t p_norm = 2,
float bias = 0.f,
bool keep_dims = false);
} // namespace util
} // namespace op
} // namespace ov

View File

@ -0,0 +1,45 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "openvino/core/node.hpp"
namespace ov {
namespace op {
namespace util {
/// \brief Change shape of a value
///
/// \param[in] value The value to be reshaped.
/// \param[in] shape The new shape.
///
/// \return Reshape:v1 op.
std::shared_ptr<Node> reshape(const Output<Node>& value, const Shape& shape);
/// \brief Permute axes according to specified axes_order parameter.
///
/// \param The vlaue whose axes we want to permute.
/// \param axes_order The permutation of axes.
///
/// \return Transpose:v1 op.
std::shared_ptr<Node> reorder_axes(const Output<Node>& value, std::vector<size_t> axes_order = {});
/// \brief Return transposed value (with axes in reversed order).
///
/// \param Value to transpose.
///
/// \return Transpose:v1 op.
std::shared_ptr<Node> transpose(const Output<Node>& value);
/// \brief Flatten a value into a 2D matrix, with a static dividing axis.
///
/// \param The tensor to be flattened.
/// \param The axis dividing shape.
///
/// \return The new value will be a 2D matrix representing the flattened input
/// node.
std::shared_ptr<Node> flatten(const Output<Node>& value, int axis);
} // namespace util
} // namespace op
} // namespace ov

View File

@ -1,43 +1,13 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <memory>
#include "ngraph/node.hpp"
#include "openvino/core/node.hpp"
namespace ngraph {
namespace builder {
/// \brief Split value on specified axis into multiple parts.
///
/// \param value The value to be split.
/// \param length_parts The vector defining the lengths of each split part.
/// \param axis The axis we split input node on. Default value is zero axis.
///
/// \return The vector containing multiple nodes we split input node into.
///
NGRAPH_DEPRECATED("This builder was deprecated.")
OutputVector split(const Output<Node>& value, const std::vector<int64_t>& length_parts, int64_t axis = 0);
/// \brief Split node on specified axis into multiple parts.
///
/// \param value The value to split.
/// \param split_parts The number of parts we want to split output at given
/// axis. The length of the axis to split must be divisible by
/// this value.
/// \param axis The axis we split input node on. Default value is zero axis.
///
/// \note This implementation supports negative `axis` values (similar to NumPy
/// indexing). This means that the axis to split on will be counted from
/// the back of the tensor (negative values are subtracted from its rank).
///
/// \return The vector containing multiple outputs we split input node into.
///
NGRAPH_DEPRECATED("This builder was deprecated.")
OutputVector split(const Output<Node>& value, int64_t split_parts, int axis = 0);
namespace opset1 {
namespace ov {
namespace op {
namespace util {
/// \brief Split value on specified axis into multiple parts.
///
/// \param value The value to be split.
@ -70,6 +40,6 @@ OutputVector split(const Output<Node>& value, const std::vector<int64_t>& split_
/// The vector is output of VariadicSplit:v1 op
///
OutputVector split(const Output<Node>& value, int64_t num_splits, int64_t axis = 0);
} // namespace opset1
} // namespace builder
} // namespace ngraph
} // namespace util
} // namespace op
} // namespace ov

View File

@ -0,0 +1,89 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ov_models/ov_builders/broadcast.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/range.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
#include "ov_models/ov_builders/reshape.hpp"
namespace ov {
namespace op {
namespace util {
namespace {
///
/// \brief Reconstructs axes mapping vector for Broadcast:v1 operation.
///
/// \param[in] output_shape The output shape of Broadcast operation.
/// \param[in] broadcast_axes The broadcast axes used for Broadcast:v0 operator.
///
/// \return The vector with axes indexes mapping .
///
std::vector<size_t> get_axes_mapping(const Shape& output_shape, const AxisSet& broadcast_axes) {
NGRAPH_CHECK((broadcast_axes.size() <= output_shape.size()));
std::vector<size_t> axes_mapping(output_shape.size());
iota(axes_mapping.begin(), axes_mapping.end(), 0);
for (auto i = broadcast_axes.rbegin(); i != broadcast_axes.rend(); ++i) {
axes_mapping.erase(axes_mapping.begin() + *i);
}
return axes_mapping;
}
///
/// \brief Creates Node returning the axes mapping for Broadcast:v1 operation.
///
/// \param[in] output_shape The output shape of Broadcast operation.
/// \param[in] broadcast_axes The broadcast axes used for Broadcast:v0 operator.
///
/// \return The Output object with Node returning axes mapping.
///
Output<Node> get_axes_mapping_output(const Shape& output_shape, const AxisSet& broadcast_axes) {
std::vector<size_t> axes_mapping{get_axes_mapping(output_shape, broadcast_axes)};
return ov::op::v0::Constant::create(element::i64, Shape{axes_mapping.size()}, axes_mapping);
}
static Output<Node> get_axes_mapping_output(const PartialShape& output_shape,
const Output<Node>& input_shape,
std::size_t start_match_axis) {
const auto one_node = ov::op::v0::Constant::create(element::i64, Shape{}, {1});
const auto zero_node = ov::op::v0::Constant::create(element::i64, Shape{}, {0});
const auto start_match_axis_node = ov::op::v0::Constant::create(element::i64, Shape{}, {start_match_axis});
const auto target_shape_rank_node =
ov::op::util::reshape(std::make_shared<ov::op::v3::ShapeOf>(input_shape), Shape{});
const auto range_node =
std::make_shared<ov::op::v4::Range>(zero_node, target_shape_rank_node, one_node, element::i64);
// workaround for GPU plugin type incompatibility
const auto range_node_converted =
std::make_shared<ov::op::v0::Convert>(range_node, start_match_axis_node->get_element_type());
// end of workaround
const auto result = std::make_shared<ov::op::v1::Add>(range_node_converted, start_match_axis_node);
return result;
}
} // namespace
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, const AxisSet& broadcast_axes) {
return std::make_shared<ov::op::v1::Broadcast>(
node,
ov::op::v0::Constant::create(element::i64, Shape{target_shape.size()}, target_shape),
get_axes_mapping_output(target_shape, broadcast_axes));
}
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, size_t start_match_axis) {
const auto node_shape = std::make_shared<ov::op::v3::ShapeOf>(node);
return std::make_shared<ov::op::v1::Broadcast>(
node,
ov::op::v0::Constant::create(element::i64, Shape{target_shape.size()}, target_shape),
get_axes_mapping_output(target_shape, node_shape, start_match_axis));
}
} // namespace util
} // namespace op
} // namespace ov

View File

@ -0,0 +1,164 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ov_models/ov_builders/norm.hpp"
#include "openvino/op/abs.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/maximum.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/not_equal.hpp"
#include "openvino/op/power.hpp"
#include "openvino/op/reduce_sum.hpp"
#include "openvino/op/sqrt.hpp"
namespace ov {
namespace op {
namespace util {
namespace {
/// \brief Specifies method of bias application to avoid numerical problems
enum class BiasMode {
// Add bias to intermediate result
ADD,
// Calculate max of intermediate result and bias
MAX
};
std::shared_ptr<Node> lp_norm(const Output<Node>& value,
size_t p_norm,
const Output<Node>& reduction_axes,
float bias,
bool keep_dims) {
// In general "entrywise" lp-norm for matrix `A` is defined as following double
// sum:
// ||A||_p = ||vec(A)||_p = [sum_{i=1}^m sum_{j=1}^n abs(a_{i,j})^p]^{1/p}
std::shared_ptr<Node> abs_values{std::make_shared<ov::op::v0::Abs>(value)};
std::shared_ptr<Node> p_node = ov::op::v0::Constant::create(value.get_element_type(), Shape{}, {p_norm});
// Get inner part of equation: abs_values^p_node, then sum over reduction_axes.
std::shared_ptr<Node> values{std::make_shared<ov::op::v1::Power>(abs_values, p_node)};
values = std::make_shared<ov::op::v1::ReduceSum>(values, reduction_axes, keep_dims);
std::shared_ptr<Node> bias_node{ov::op::v0::Constant::create(values->get_element_type(), Shape{}, {bias})};
values = std::make_shared<ov::op::v1::Add>(values, bias_node);
// Get outer part of equation: raise values to 1/p_norm exponent.
std::shared_ptr<Node> inv_p_node =
ov::op::v0::Constant::create(values->get_element_type(), Shape{}, {1.f / p_norm});
return {std::make_shared<ov::op::v1::Power>(values, inv_p_node)};
}
/// \brief Calculates L-0 norm of input tensor.
///
/// \note The L-0 norm represents the cardinality of elements different
/// from zero. This actually is not a "true" norm.
///
/// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] keep_dims The flag indicates if axes will be removed or kept.
///
/// \return L-0 norm of value. The output sub-graph is composed of v1 ops.
///
std::shared_ptr<Node> l0_norm(const Output<Node>& value, const Output<Node>& reduction_axes, bool keep_dims) {
// L0 norm returns number of elements different from zero.
const std::shared_ptr<Node> zero_node{ov::op::v0::Constant::create(value.get_element_type(), Shape{}, {0.f})};
// Convert bool values to input node data type.
const std::shared_ptr<Node> non_zero_values =
std::make_shared<ov::op::v0::Convert>(std::make_shared<ov::op::v1::NotEqual>(value, zero_node),
value.get_element_type());
return std::make_shared<ov::op::v1::ReduceSum>(non_zero_values, reduction_axes, keep_dims);
}
/// \brief Calculates L-1 norm of a value.
///
/// \note The L-1 norm represents the sum of absolute values.
///
/// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] bias The bias added to the calculated sum.
/// \param[in] keep_dims The flag indicates if axes will be removed or kept.
///
/// \return L-1 norm of value. The output sub-graph is composed of v1 ops.
///
std::shared_ptr<Node> l1_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
float bias,
bool keep_dims) {
const std::shared_ptr<Node> values{
std::make_shared<ov::op::v1::ReduceSum>(std::make_shared<ov::op::v0::Abs>(value), reduction_axes, keep_dims)};
const std::shared_ptr<Node> bias_node{ov::op::v0::Constant::create(values->get_element_type(), Shape{}, {bias})};
return std::make_shared<ov::op::v1::Add>(values, bias_node);
}
/// \brief Calculates L-2 norm of input tensor.
///
/// \note The L-2 norm represents the square root of sum of squares of each
/// individual element.
///
/// \param[in] value The input tensor.
/// \param[in] reduction_axes The axes along which we calculate norm.
/// \param[in] bias The bias combined with calculated sum.
/// \param[in] bias_mode The method of bias application.
/// \param[in] keep_dims The flag indicates if axes will be removed or kept.
///
/// \return L-2 norm of value. The output sub-graph is composed of v1 ops.
///
std::shared_ptr<Node> l2_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
float bias,
BiasMode bias_mode,
bool keep_dims) {
std::shared_ptr<Node> pow = std::make_shared<ov::op::v1::Power>(
value,
std::make_shared<ov::op::v0::Constant>(value.get_element_type(), Shape{}, 2));
std::shared_ptr<Node> values{std::make_shared<ov::op::v1::ReduceSum>(pow, reduction_axes, keep_dims)};
std::shared_ptr<Node> bias_node{ov::op::v0::Constant::create(values->get_element_type(), Shape{}, {bias})};
std::shared_ptr<Node> result;
switch (bias_mode) {
case BiasMode::MAX: {
result = std::make_shared<ov::op::v0::Sqrt>(std::make_shared<ov::op::v1::Maximum>(values, bias_node));
break;
}
case BiasMode::ADD:
default:
result = std::make_shared<ov::op::v0::Sqrt>(std::make_shared<ov::op::v1::Add>(values, bias_node));
}
return result;
}
} // namespace
std::shared_ptr<Node> lp_norm(const Output<Node>& value,
const Output<Node>& reduction_axes,
size_t p_norm,
float bias,
bool keep_dims) {
// The number of non-zero elements
if (p_norm == 0) {
return l0_norm(value, reduction_axes, keep_dims);
}
// sum of absolute values.
else if (p_norm == 1) {
return l1_norm(value, reduction_axes, bias, keep_dims);
}
// sqrt of sum of squares - Euclidean norm
else if (p_norm == 2) {
return l2_norm(value, reduction_axes, bias, BiasMode::ADD, keep_dims);
}
// generic case
else {
return lp_norm(value, p_norm, reduction_axes, bias, keep_dims);
}
}
} // namespace util
} // namespace op
} // namespace ov

View File

@ -0,0 +1,126 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ov_models/ov_builders/reshape.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/range.hpp"
#include "openvino/op/reduce_prod.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/squeeze.hpp"
#include "openvino/op/strided_slice.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/transpose.hpp"
#include "openvino/op/variadic_split.hpp"
namespace ov {
namespace op {
namespace util {
std::shared_ptr<Node> reshape(const Output<Node>& value, const Shape& shape) {
if (value.get_partial_shape().same_scheme(shape)) {
return value.get_node_shared_ptr();
} else if (is_scalar(shape)) {
auto value_rank = value.get_shape().size();
AxisVector axes_vector(value_rank);
std::iota(axes_vector.begin(), axes_vector.end(), 0);
auto axes = ov::op::v0::Constant::create(element::i64, Shape{value_rank}, axes_vector);
return std::make_shared<ov::op::v0::Squeeze>(value, axes);
} else {
auto out_pattern = ov::op::v0::Constant::create(element::i64,
Shape{shape.size()},
std::vector<int64_t>(shape.begin(), shape.end()));
return std::make_shared<ov::op::v1::Reshape>(value, out_pattern, false);
}
}
std::shared_ptr<Node> reorder_axes(const Output<Node>& value, std::vector<size_t> axes_order) {
const auto axes_order_const =
ov::op::v0::Constant::create(element::i64,
Shape{axes_order.size()},
std::vector<int64_t>(axes_order.begin(), axes_order.end()));
return std::make_shared<ov::op::v1::Transpose>(value, axes_order_const);
}
std::shared_ptr<Node> transpose(const Output<Node>& value) {
// This part is left to preserve backward compatibility and ensure passing ONNX tests.
if (value.get_partial_shape().is_static()) {
std::vector<size_t> axes_order(value.get_shape().size());
std::iota(begin(axes_order), end(axes_order), 0);
std::reverse(begin(axes_order), end(axes_order));
return reorder_axes(value, axes_order);
}
const auto input_rank = std::make_shared<ov::op::v0::ShapeOf>(std::make_shared<ov::op::v0::ShapeOf>(value));
const auto neg_one = ov::op::v0::Constant::create(element::i64, Shape{}, {-1});
const auto start_node = std::make_shared<ov::op::v1::Add>(input_rank, neg_one);
const auto reverse_axes_order = std::make_shared<ov::op::v0::Range>(reshape(start_node, Shape{}), // start
neg_one, // stop (exclusive)
neg_one); // step
return std::make_shared<ov::op::v1::Transpose>(value, reverse_axes_order);
}
namespace {
///
/// \brief Return the node representing normalized axis with respect to
/// provided rank.
///
/// \param[in] node_rank The node representing rank used for normalization.
/// \param[in] axis The axis value to be normalized.
///
/// \return The new Constant node representing normalized axis value.
///
std::shared_ptr<Node> get_normalized_axis_node(const std::shared_ptr<Node> node_rank, int64_t axis) {
auto axis_node = ov::op::v0::Constant::create(element::i64, Shape{1}, {axis});
// shortcut for already positive value
if (axis >= 0) {
return axis_node;
}
// TODO: What if axis value is beyond acceptable values? [-node_rank,
// node_rank-1]
return std::make_shared<ov::op::v1::Add>(node_rank, axis_node);
}
} // namespace
std::shared_ptr<Node> flatten(const Output<Node>& value, int axis) {
// First dimension of output tensor is the product of [d_0, ... d_{axis-1}] dimensions of
// input tensor. The last dimension is the product of the rest of input tensor dimensions:
// [d_{axis}, ..., d_n]
std::shared_ptr<Node> output_shape;
if (axis == 0) {
output_shape = ov::op::v0::Constant::create(element::i64, Shape{2}, {1, -1});
} else if (axis == 1) {
output_shape = ov::op::v0::Constant::create(element::i64, Shape{2}, {0, -1});
} else {
const auto value_shape = std::make_shared<ov::op::v0::ShapeOf>(value);
const auto value_rank = std::make_shared<ov::op::v0::ShapeOf>(value_shape);
const auto axis_node = get_normalized_axis_node(value_rank, axis);
const auto first_part_dims =
std::make_shared<ov::op::v1::StridedSlice>(value_shape,
ov::op::v0::Constant::create(element::i64, {1}, {0}),
axis_node,
std::vector<int64_t>{0},
std::vector<int64_t>{0});
const auto first_part_dims_length =
std::make_shared<ov::op::v1::ReduceProd>(first_part_dims,
ov::op::v0::Constant::create(element::i64, {}, {0}),
true);
const auto remaining_part_length = ov::op::v0::Constant::create(element::i64, {1}, {-1});
output_shape =
std::make_shared<ov::op::v0::Concat>(OutputVector{first_part_dims_length, remaining_part_length}, 0);
}
return std::make_shared<ov::op::v1::Reshape>(value, output_shape, true);
}
} // namespace util
} // namespace op
} // namespace ov

View File

@ -0,0 +1,31 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ov_models/ov_builders/split.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/split.hpp"
#include "openvino/op/variadic_split.hpp"
namespace ov {
namespace op {
namespace util {
OutputVector split(const Output<Node>& value, const std::vector<int64_t>& split_lengths, int64_t axis) {
const auto axis_node = ov::op::v0::Constant::create(element::i64, Shape{}, {axis});
const auto split_lengths_node =
ov::op::v0::Constant::create(element::i64, Shape{split_lengths.size()}, split_lengths);
const auto variadic_split = std::make_shared<ov::op::v1::VariadicSplit>(value, axis_node, split_lengths_node);
return variadic_split->outputs();
}
OutputVector split(const Output<Node>& value, int64_t num_splits, int64_t axis) {
const auto axis_node = ov::op::v0::Constant::create(element::i64, Shape{}, {axis});
const auto split = std::make_shared<ov::op::v1::Split>(value, axis_node, num_splits);
return split->outputs();
}
} // namespace util
} // namespace op
} // namespace ov

View File

@ -0,0 +1,89 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ov_models/ov_builders/broadcast.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/range.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
#include "ov_models/ov_builders/reshape.hpp"
namespace ov {
namespace op {
namespace util {
namespace {
///
/// \brief Reconstructs axes mapping vector for Broadcast:v1 operation.
///
/// \param[in] output_shape The output shape of Broadcast operation.
/// \param[in] broadcast_axes The broadcast axes used for Broadcast:v0 operator.
///
/// \return The vector with axes indexes mapping .
///
std::vector<size_t> get_axes_mapping(const Shape& output_shape, const AxisSet& broadcast_axes) {
NGRAPH_CHECK((broadcast_axes.size() <= output_shape.size()));
std::vector<size_t> axes_mapping(output_shape.size());
iota(axes_mapping.begin(), axes_mapping.end(), 0);
for (auto i = broadcast_axes.rbegin(); i != broadcast_axes.rend(); ++i) {
axes_mapping.erase(axes_mapping.begin() + *i);
}
return axes_mapping;
}
///
/// \brief Creates Node returning the axes mapping for Broadcast:v1 operation.
///
/// \param[in] output_shape The output shape of Broadcast operation.
/// \param[in] broadcast_axes The broadcast axes used for Broadcast:v0 operator.
///
/// \return The Output object with Node returning axes mapping.
///
Output<Node> get_axes_mapping_output(const Shape& output_shape, const AxisSet& broadcast_axes) {
std::vector<size_t> axes_mapping{get_axes_mapping(output_shape, broadcast_axes)};
return ov::op::v0::Constant::create(element::i64, Shape{axes_mapping.size()}, axes_mapping);
}
static Output<Node> get_axes_mapping_output(const PartialShape& output_shape,
const Output<Node>& input_shape,
std::size_t start_match_axis) {
const auto one_node = ov::op::v0::Constant::create(element::i64, Shape{}, {1});
const auto zero_node = ov::op::v0::Constant::create(element::i64, Shape{}, {0});
const auto start_match_axis_node = ov::op::v0::Constant::create(element::i64, Shape{}, {start_match_axis});
const auto target_shape_rank_node =
ov::op::util::reshape(std::make_shared<ov::op::v3::ShapeOf>(input_shape), Shape{});
const auto range_node =
std::make_shared<ov::op::v4::Range>(zero_node, target_shape_rank_node, one_node, element::i64);
// workaround for GPU plugin type incompatibility
const auto range_node_converted =
std::make_shared<ov::op::v0::Convert>(range_node, start_match_axis_node->get_element_type());
// end of workaround
const auto result = std::make_shared<ov::op::v1::Add>(range_node_converted, start_match_axis_node);
return result;
}
} // namespace
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, const AxisSet& broadcast_axes) {
return std::make_shared<ov::op::v1::Broadcast>(
node,
ov::op::v0::Constant::create(element::i64, Shape{target_shape.size()}, target_shape),
get_axes_mapping_output(target_shape, broadcast_axes));
}
Output<Node> make_broadcast(const Output<Node>& node, const Shape& target_shape, size_t start_match_axis) {
const auto node_shape = std::make_shared<ov::op::v3::ShapeOf>(node);
return std::make_shared<ov::op::v1::Broadcast>(
node,
ov::op::v0::Constant::create(element::i64, Shape{target_shape.size()}, target_shape),
get_axes_mapping_output(target_shape, node_shape, start_match_axis));
}
} // namespace util
} // namespace op
} // namespace ov

View File

@ -0,0 +1,31 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ov_models/ov_builders/split.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/split.hpp"
#include "openvino/op/variadic_split.hpp"
namespace ov {
namespace op {
namespace util {
OutputVector split(const Output<Node>& value, const std::vector<int64_t>& split_lengths, int64_t axis) {
const auto axis_node = ov::op::v0::Constant::create(element::i64, Shape{}, {axis});
const auto split_lengths_node =
ov::op::v0::Constant::create(element::i64, Shape{split_lengths.size()}, split_lengths);
const auto variadic_split = std::make_shared<ov::op::v1::VariadicSplit>(value, axis_node, split_lengths_node);
return variadic_split->outputs();
}
OutputVector split(const Output<Node>& value, int64_t num_splits, int64_t axis) {
const auto axis_node = ov::op::v0::Constant::create(element::i64, Shape{}, {axis});
const auto split = std::make_shared<ov::op::v1::Split>(value, axis_node, num_splits);
return split->outputs();
}
} // namespace util
} // namespace op
} // namespace ov

View File

@ -31,6 +31,7 @@ function(add_common_utils ADD_TARGET_NAME)
ov_models
openvino::runtime
openvino::runtime::dev
openvino::builders
PRIVATE
openvino::util
openvino::shape_inference

View File

@ -15,7 +15,7 @@ add_library(${TARGET_FE_NAME} SHARED ${LIBRARY_SRC} ${LIBRARY_HEADERS})
target_include_directories(${TARGET_FE_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${TARGET_FE_NAME} PUBLIC openvino::runtime PRIVATE openvino::builders)
target_link_libraries(${TARGET_FE_NAME} PUBLIC openvino::runtime)
ov_add_clang_format_target(${TARGET_FE_NAME}_clang FOR_TARGETS ${TARGET_FE_NAME})