-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PT FE] [ONNX FE] Partially upcast random_normal to f32 (#21400)
* upcast randn to fp32 * style fix * corrected tests * add layer tests with statistics * style-fix * move make_random_normal to cmmmon * style-fix * added randn layer tests; updated CMakeLists.txt * moved to inline * fix problem with _USE_MATH_DEFINES on Win * pass NodeRegistry as reference; some other minor corrections * adjust thresholds to avoid sporadicity * move random_normal_helper and hide from public api * fix install * fix install: 2nd try * Frontend common * remove last frontend_common::static * build fix * try to fix mock1 build: 2nd attempt * try to fix mock1 build: 3rd attempt * Update src/core/tests/CMakeLists.txt * Fixed build: attemp 2 * Update src/plugins/intel_cpu/tests/unit/CMakeLists.txt * Update CMakeLists.txt --------- Co-authored-by: Ilya Lavrenov <[email protected]>
- Loading branch information
1 parent
c61de14
commit b71906c
Showing
19 changed files
with
229 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/frontends/common/dev_api/openvino/frontend/common/random_normal_helper.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.