Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PT FE] [ONNX FE] Partially upcast random_normal to f32 #21400

Merged
merged 28 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
57108a0
upcast randn to fp32
pavel-esir Nov 30, 2023
e306867
style fix
pavel-esir Nov 30, 2023
c604e68
corrected tests
pavel-esir Dec 1, 2023
66bc3e2
add layer tests with statistics
pavel-esir Dec 1, 2023
eee5558
style-fix
pavel-esir Dec 1, 2023
053873b
move make_random_normal to cmmmon
pavel-esir Dec 4, 2023
76951f7
style-fix
pavel-esir Dec 4, 2023
605112d
added randn layer tests; updated CMakeLists.txt
pavel-esir Dec 4, 2023
10993da
moved to inline
pavel-esir Dec 4, 2023
3808dda
fix problem with _USE_MATH_DEFINES on Win
pavel-esir Dec 4, 2023
85c925a
Merge branch 'master' into upcast_randn_to_f32
pavel-esir Dec 4, 2023
c788e20
pass NodeRegistry as reference; some other minor corrections
pavel-esir Dec 4, 2023
930d0ef
adjust thresholds to avoid sporadicity
pavel-esir Dec 4, 2023
51852fe
move random_normal_helper and hide from public api
pavel-esir Dec 5, 2023
cb6fbd4
fix install
pavel-esir Dec 5, 2023
1f64344
fix install: 2nd try
pavel-esir Dec 5, 2023
c3f4be9
Frontend common
ilya-lavrenov Dec 5, 2023
2fe4933
Merge pull request #123 from ilya-lavrenov/frontend-common
pavel-esir Dec 5, 2023
d09625d
remove last frontend_common::static
pavel-esir Dec 5, 2023
58e4af1
build fix
pavel-esir Dec 5, 2023
d25fdb5
try to fix mock1 build: 2nd attempt
pavel-esir Dec 6, 2023
24d7941
try to fix mock1 build: 3rd attempt
pavel-esir Dec 6, 2023
1bd8b24
Update src/core/tests/CMakeLists.txt
ilya-lavrenov Dec 6, 2023
02530c2
Fixed build: attemp 2
ilya-lavrenov Dec 6, 2023
e0aa4f8
Merge remote-tracking branch 'pavel-esir/upcast_randn_to_f32' into up…
ilya-lavrenov Dec 6, 2023
5fa00ce
Merge pull request #124 from ilya-lavrenov/upcast_randn_to_f32
pavel-esir Dec 6, 2023
d088ad3
Update src/plugins/intel_cpu/tests/unit/CMakeLists.txt
ilya-lavrenov Dec 6, 2023
4fee468
Update CMakeLists.txt
ilya-lavrenov Dec 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmake/developer_package/frontends/frontends.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ function(ov_generate_frontends_hpp)
# for some reason dependency on source files does not work
# so, we have to use explicit target and make it dependency for frontend_common
add_custom_target(_ov_frontends_hpp DEPENDS ${ov_frontends_hpp})
add_dependencies(frontend_common_obj _ov_frontends_hpp)
add_dependencies(openvino_frontend_common_obj _ov_frontends_hpp)

# add dependency for object files
get_target_property(sources frontend_common_obj SOURCES)
get_target_property(sources openvino_frontend_common_obj SOURCES)
foreach(source IN LISTS sources)
if("${source}" MATCHES "\\$\\<TARGET_OBJECTS\\:([A-Za-z0-9_]*)\\>")
# object library
Expand Down Expand Up @@ -226,7 +226,7 @@ macro(ov_add_frontend)
ov_add_vs_version_file(NAME ${TARGET_NAME}
FILEDESCRIPTION ${OV_FRONTEND_FILEDESCRIPTION})

target_link_libraries(${TARGET_NAME} PRIVATE ${OV_FRONTEND_LINK_LIBRARIES} PUBLIC openvino::runtime)
target_link_libraries(${TARGET_NAME} PRIVATE ${OV_FRONTEND_LINK_LIBRARIES} openvino::frontend::common PUBLIC openvino::runtime)
ov_add_library_version(${TARGET_NAME})

if(OV_FRONTEND_PROTOBUF_REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion src/cmake/openvino.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ endif()
add_library(${TARGET_NAME}
$<TARGET_OBJECTS:ngraph_obj>
$<TARGET_OBJECTS:ngraph_obj_version>
$<TARGET_OBJECTS:frontend_common_obj>
$<TARGET_OBJECTS:openvino_frontend_common_obj>
$<TARGET_OBJECTS:inference_engine_obj>
$<TARGET_OBJECTS:inference_engine_obj_version>
$<TARGET_OBJECTS:inference_engine_transformations_obj>
Expand Down
1 change: 1 addition & 0 deletions src/core/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ov_add_test_target(
Threads::Threads
openvino::conditional_compilation
openvino::runtime::dev
openvino_frontend_common
ilya-lavrenov marked this conversation as resolved.
Show resolved Hide resolved
ADD_CLANG_FORMAT
LABELS
OV UNIT CORE
Expand Down
4 changes: 2 additions & 2 deletions src/core/tests/frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ target_compile_definitions(${MOCK1_FE_NAME} PRIVATE "-DMOCK_VARIANT=\"1\"")

target_include_directories(${MOCK1_FE_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(${MOCK1_FE_NAME} PRIVATE frontend_common)
add_dependencies(ov_core_unit_tests ${MOCK1_FE_NAME})
target_link_libraries(${MOCK1_FE_NAME} PRIVATE openvino_frontend_common)
add_dependencies(ov_core_unit_tests ${MOCK1_FE_NAME} openvino_frontend_common)

ov_add_clang_format_target(${MOCK1_FE_NAME}_clang FOR_TARGETS ${MOCK1_FE_NAME})

Expand Down
33 changes: 18 additions & 15 deletions src/frontends/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@
# SPDX-License-Identifier: Apache-2.0
#

set(TARGET_NAME "frontend_common")
set(TARGET_NAME "openvino_frontend_common")

set(FRONTEND_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(FRONTEND_DEV_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dev_api)

file(GLOB_RECURSE LIBRARY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE LIBRARY_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp)
file(GLOB_RECURSE LIBRARY_PUBLIC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp)

set(FRONTEND_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)

source_group("src" FILES ${LIBRARY_SRC})
source_group("include" FILES ${LIBRARY_HEADERS})
source_group("public include" FILES ${LIBRARY_PUBLIC_HEADERS})

# create frontend common library

add_library(${TARGET_NAME} INTERFACE)

target_include_directories(${TARGET_NAME} INTERFACE
$<BUILD_INTERFACE:${FRONTEND_DEV_INCLUDE_DIR}>
$<BUILD_INTERFACE:${FRONTEND_INCLUDE_DIR}>
$<TARGET_PROPERTY:openvino::runtime,INTERFACE_INCLUDE_DIRECTORIES>)

add_library(openvino::frontend::common ALIAS ${TARGET_NAME})

ov_install_static_lib(${TARGET_NAME} ${OV_CPACK_COMP_CORE})

# create library

add_library(${TARGET_NAME}_obj OBJECT ${LIBRARY_SRC} ${LIBRARY_HEADERS} ${LIBRARY_PUBLIC_HEADERS})
Expand All @@ -33,7 +47,7 @@ if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(${TARGET_NAME}_obj PUBLIC OPENVINO_STATIC_LIBRARY)
endif()

target_link_libraries(${TARGET_NAME}_obj PRIVATE openvino::util openvino::core::dev)
target_link_libraries(${TARGET_NAME}_obj PRIVATE openvino::util openvino::core::dev openvino::frontend::common)

# TODO: fix lto
set_target_properties(${TARGET_NAME}_obj PROPERTIES
Expand All @@ -60,17 +74,6 @@ ov_ncc_naming_style(FOR_TARGET ${TARGET_NAME}_obj
ADDITIONAL_INCLUDE_DIRECTORIES
$<TARGET_PROPERTY:ngraph,INTERFACE_INCLUDE_DIRECTORIES>)

# INTERFACE library for BW compatibility

add_library(${TARGET_NAME} INTERFACE)
target_link_libraries(${TARGET_NAME} INTERFACE openvino::runtime)
target_include_directories(${TARGET_NAME} INTERFACE $<BUILD_INTERFACE:${FRONTEND_INCLUDE_DIR}>
$<BUILD_INTERFACE:$<TARGET_PROPERTY:ngraph,INTERFACE_INCLUDE_DIRECTORIES>>)

add_library(ngraph::${TARGET_NAME} ALIAS ${TARGET_NAME})
add_library(openvino::frontend::common ALIAS ${TARGET_NAME})
add_library(${TARGET_NAME}::static ALIAS ${TARGET_NAME})

# Installation rules header files

install(DIRECTORY ${FRONTEND_INCLUDE_DIR}/openvino
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "ngraph/output_vector.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, avoid use of legacy API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will fix this in a separate PR

#include "openvino/frontend/visibility.hpp"
#include "openvino/pass/graph_rewrite.hpp"

namespace ov {
namespace frontend {

/// \brief Creates a random normal tensor with the given shape and type.
/// \details Uses Box-Mueller algorithm to generate random numbers from a Gauassian distribution
/// \param sizes Shape of the output tensor
/// \param target_type Type of the output tensor
/// \param mean Mean of the distribution
/// \param scale Standard deviation of the distribution
/// \param seed Seed for the random number generator
FRONTEND_API OutputVector make_random_normal(pass::NodeRegistry& registry,
const Output<Node>& sizes,
element::Type target_type,
const Output<Node>& mean,
const Output<Node>& scale,
float seed);

/// \brief Creates a random normal tensor with the given shape and type.
/// \details Uses Box-Mueller algorithm to generate random numbers from a Gauassian distribution
/// \param sizes Shape of the output tensor
/// \param target_type Type of the output tensor
/// \param mean Mean of the distribution
/// \param scale Standard deviation of the distribution
/// \param seed Seed for the random number generator
FRONTEND_API std::pair<OutputVector, pass::NodeRegistry> make_random_normal(const Output<Node>& sizes,
element::Type target_type,
const Output<Node>& mean,
const Output<Node>& scale,
float seed);

} // namespace frontend
} // namespace ov
77 changes: 77 additions & 0 deletions src/frontends/common/src/random_normal_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/frontend/common/random_normal_helper.hpp"

#include "ngraph/output_vector.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/opsets/opset12.hpp"
#include "openvino/pass/graph_rewrite.hpp"
#include "transformations/rt_info/disable_fp16_compression.hpp"
#define _USE_MATH_DEFINES
#include <math.h>

namespace ov {
namespace frontend {

OutputVector make_random_normal(pass::NodeRegistry& registry,
const Output<Node>& sizes,
element::Type target_type,
const Output<Node>& mean,
const Output<Node>& scale,
float seed) {
// We start by generating two random series from a uniform distribution
const uint64_t global_seed = 0;

// ONNX specifies the seed as a float, but OpenVINO uses uint64_t
const auto op_seed = static_cast<uint64_t>(seed * 1000);

// We need to use two op_seeds to make sure we get different results for two RandomUniform series
// But we also have to keep original logic and pass "0" (auto-generated seed) to RandomUniform
const uint64_t seed_1 = op_seed;
const uint64_t seed_2 = (op_seed == 0 ? op_seed : op_seed + 10000);

auto min_val = registry.make<op::v0::Constant>(target_type, Shape{1}, std::numeric_limits<float>::min());
auto max_val = registry.make<op::v0::Constant>(target_type, Shape{1}, 1);

auto uniform_1 = registry.make<op::v8::RandomUniform>(sizes, min_val, max_val, target_type, global_seed, seed_1);
auto uniform_2 = registry.make<op::v8::RandomUniform>(sizes, min_val, max_val, target_type, global_seed, seed_2);

// Compute Box–Muller transform
// random_normal = scale * sqrt(-2.0 * log(uniform_1)) * cos(2.0 * pi * uniform_2) + mean
auto pi = registry.make<op::v0::Constant>(target_type, Shape{1}, M_PI);
auto minus_two = registry.make<op::v0::Constant>(target_type, Shape{1}, -2.0);
auto two = registry.make<op::v0::Constant>(target_type, Shape{1}, 2.0);

auto log = registry.make<op::v0::Log>(uniform_1);
auto multiply_minus_two_log = registry.make<op::v1::Multiply>(log, minus_two);
auto sqrt = registry.make<op::v0::Sqrt>(multiply_minus_two_log);

auto multiply_2pi = registry.make<op::v1::Multiply>(two, pi);
auto multiply_2pi_uniform_2 = registry.make<op::v1::Multiply>(multiply_2pi, uniform_2);
auto cos = registry.make<op::v0::Cos>(multiply_2pi_uniform_2);

auto sqrt_x_cos = registry.make<op::v1::Multiply>(sqrt, cos);
auto product = registry.make<op::v1::Multiply>(scale, sqrt_x_cos);
auto sum = registry.make<op::v1::Add>(product, mean);

// if we don't disable down-casting then log(float32_min) gives -inf
disable_fp16_compression(uniform_1);
disable_fp16_compression(log);
mvafin marked this conversation as resolved.
Show resolved Hide resolved

return {sum};
}

std::pair<OutputVector, pass::NodeRegistry> make_random_normal(const Output<Node>& sizes,
element::Type target_type,
const Output<Node>& mean,
const Output<Node>& scale,
float seed) {
pass::NodeRegistry registry;
OutputVector res = make_random_normal(registry, sizes, target_type, mean, scale, seed);
return std::make_pair(res, registry);
}

} // namespace frontend
} // namespace ov
4 changes: 1 addition & 3 deletions src/frontends/onnx/frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ target_compile_definitions(${TARGET_NAME} PRIVATE ONNX_OPSET_VERSION=${ONNX_OPSE
ov_ncc_naming_style(FOR_TARGET ${TARGET_NAME}
SOURCE_DIRECTORIES "${${TARGET_NAME}_INCLUDE_DIR}"
DEFINITIONS
$<TARGET_PROPERTY:onnx,INTERFACE_COMPILE_DEFINITIONS>
ADDITIONAL_INCLUDE_DIRECTORIES
$<TARGET_PROPERTY:frontend_common::static,INTERFACE_INCLUDE_DIRECTORIES>)
$<TARGET_PROPERTY:onnx,INTERFACE_COMPILE_DEFINITIONS>)

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/onnx_import
DESTINATION ${FRONTEND_INSTALL_INCLUDE}/ngraph/frontend
Expand Down
12 changes: 7 additions & 5 deletions src/frontends/onnx/frontend/src/op/random_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "utils/random_normal.hpp"

#include "exceptions.hpp"
#include "ngraph/shape.hpp"
#include "openvino/frontend/common/random_normal_helper.hpp"
#include "openvino/op/constant.hpp"
#include "utils/common.hpp"

OPENVINO_SUPPRESS_DEPRECATED_START
Expand All @@ -23,11 +23,13 @@ OutputVector random_normal(const Node& node) {

const auto mean = node.get_attribute_value<float>("mean", 0.0f);
const auto scale = node.get_attribute_value<float>("scale", 1.0f);
const auto seed = node.get_attribute_value<float>("seed", 0);
auto scale_node = ov::op::v0::Constant::create(target_type, Shape{1}, {scale});
auto mean_node = ov::op::v0::Constant::create(target_type, Shape{1}, {mean});

const auto seed = node.get_attribute_value<float>("seed", 0);
const auto shape = node.get_attribute_as_constant<std::vector<int64_t>>("shape");

return detail::make_random_normal(shape, target_type, mean, scale, seed);
auto res = ov::frontend::make_random_normal(shape, target_type, mean_node, scale_node, seed);
return res.first;
}

} // namespace set_1
Expand Down
10 changes: 7 additions & 3 deletions src/frontends/onnx/frontend/src/op/random_normal_like.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "ngraph/shape.hpp"
#include "op/random_uniform_like.hpp"
#include "openvino/frontend/common/random_normal_helper.hpp"
#include "utils/common.hpp"
#include "utils/random_normal.hpp"

OPENVINO_SUPPRESS_DEPRECATED_START
namespace ngraph {
Expand All @@ -25,11 +25,15 @@ OutputVector random_normal_like(const Node& node) {
}

const auto shape = std::make_shared<default_opset::ShapeOf>(input);
const auto seed = node.get_attribute_value<float>("seed", 0.0f);

const auto mean = node.get_attribute_value<float>("mean", 0.0f);
const auto scale = node.get_attribute_value<float>("scale", 1.0f);
const auto seed = node.get_attribute_value<float>("seed", 0.0f);
auto scale_node = ov::op::v0::Constant::create(target_type, Shape{1}, {scale});
auto mean_node = ov::op::v0::Constant::create(target_type, Shape{1}, {mean});

return detail::make_random_normal(shape, target_type, mean, scale, seed);
auto res = ov::frontend::make_random_normal(shape, target_type, mean_node, scale_node, seed);
return res.first;
}

} // namespace set_1
Expand Down
63 changes: 0 additions & 63 deletions src/frontends/onnx/frontend/src/utils/random_normal.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions src/frontends/onnx/frontend/src/utils/random_normal.hpp

This file was deleted.

Loading
Loading