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

[Opset14][CORE] ConvertPromoteTypes op core implementation #22566

Merged
merged 22 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1f5216d
[op] ConvertAlignTypes core implementation and tests
mmikolajcz Jan 31, 2024
36d1cfe
Fix ConvertAlignTypes tests
mmikolajcz Jan 31, 2024
7b300b4
Merge branch 'master' of https://github.com/openvinotoolkit/openvino …
mmikolajcz Feb 2, 2024
465d8af
Add ConvertAlignTypes to opset14
mmikolajcz Feb 2, 2024
e4827af
ConvertAlignTypes missing v14
mmikolajcz Feb 2, 2024
ad534d7
ConvertAlignTypes missing conformance test
mmikolajcz Feb 2, 2024
e86d846
FIx ConvertAlignTypes test
mmikolajcz Feb 2, 2024
51c40e1
ConvertAlignTypes decomposition transformation
mmikolajcz Feb 5, 2024
e237deb
Merge branch 'master' of https://github.com/openvinotoolkit/openvino …
mmikolajcz Feb 5, 2024
116a105
Merge branch 'mateuszm/ops/align/core' of https://github.com/mmikolaj…
mmikolajcz Feb 5, 2024
1fe3df9
Fix ConvertAlignTypes test name
mmikolajcz Feb 5, 2024
146a3f8
Fix ConvertAlignTYpes license header
mmikolajcz Feb 6, 2024
ba5fd18
Apply first batch of requested changes to ConvertAlignTypes
mmikolajcz Feb 6, 2024
a78a263
Implement second batch of requested changes
mmikolajcz Feb 7, 2024
7723a90
Rename ConvertAlignTypes to ConvertPromoteTypes
mmikolajcz Feb 7, 2024
d2f7e7a
Rename ConvertAlignTypes to ConvertPromoteTypes
mmikolajcz Feb 7, 2024
132a4ba
Improve ConvertPromoteTypes docstring
mmikolajcz Feb 7, 2024
0ec423b
ConvertPromoteTypes fix formatting
mmikolajcz Feb 7, 2024
9f059e0
Add docstring and prefix for friendly name
mmikolajcz Feb 9, 2024
3ad5bec
AddConvertPromotionTypes requested changes
mmikolajcz Feb 9, 2024
ca94b1e
Remove unused include
mmikolajcz Feb 9, 2024
5b5c0d5
Merge branch 'master' into mateuszm/ops/align/core
mmikolajcz Feb 9, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "openvino/pass/graph_rewrite.hpp"
#include "transformations_visibility.hpp"

namespace ov {
namespace pass {

class TRANSFORMATIONS_API ConvertConvertAlignTypes;

} // namespace pass
} // namespace ov

class ov::pass::ConvertConvertAlignTypes : public ov::pass::MatcherPass {
public:
OPENVINO_RTTI("ConvertConvertAlignTypes", "0");
ConvertConvertAlignTypes();
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#include "transformations/op_conversions/bidirectional_sequences_decomposition.hpp"
#include "transformations/op_conversions/convert_bitwise_to_logical_bool.hpp"
#include "transformations/op_conversions/convert_broadcast_to_tiles.hpp"
#include "transformations/op_conversions/convert_convertaligntypes.hpp"
#include "transformations/op_conversions/convert_convertlike.hpp"
#include "transformations/op_conversions/convert_deformable_conv_v8_to_v1.hpp"
#include "transformations/op_conversions/convert_depth_to_space.hpp"
Expand Down Expand Up @@ -164,6 +165,7 @@ bool ov::pass::CommonOptimizations::run_on_model(const std::shared_ptr<ov::Model
ADD_MATCHER(decomp, ConvertDivide)
ADD_MATCHER(decomp, ConvertDepthToSpace)
ADD_MATCHER(decomp, ConvertSpaceToDepth)
ADD_MATCHER(decomp, ConvertConvertAlignTypes)
ADD_MATCHER(decomp, ConvertConvertLike)
ADD_MATCHER(decomp, BatchNormDecomposition)
ADD_MATCHER(decomp, GroupNormalizationDecomposition)
Expand Down
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "transformations/op_conversions/convert_convertaligntypes.hpp"

#include "itt.hpp"
#include "openvino/core/rt_info.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/convert_align_types.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"

using namespace ov;
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved

ov::pass::ConvertConvertAlignTypes::ConvertConvertAlignTypes() {
MATCHER_SCOPE(ConvertConvertAlignTypes);

auto convert_align_types = pattern::wrap_type<ov::op::v14::ConvertAlignTypes>();
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved

matcher_pass_callback callback = [](pattern::Matcher& m) {
auto convert = std::dynamic_pointer_cast<ov::op::v14::ConvertAlignTypes>(m.get_match_root());
if (!convert) {
return false;
}
const element::Type& dest_type = convert->get_output_element_type(0);
if (dest_type == element::dynamic || dest_type == element::undefined) {
return false;
}
auto out0 = std::make_shared<ov::op::v0::Convert>(convert->input_value(0), dest_type);
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
auto out1 = std::make_shared<ov::op::v0::Convert>(convert->input_value(1), dest_type);
out0->set_friendly_name(convert->get_friendly_name() + ".0");
out1->set_friendly_name(convert->get_friendly_name() + ".1");
copy_runtime_info(convert, {out0, out1});
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
replace_node(convert, {out0, out1});
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
return true;
};

auto m = std::make_shared<pattern::Matcher>(convert_align_types, matcher_name);
this->register_matcher(m, callback);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "transformations/op_conversions/convert_convertaligntypes.hpp"

#include "common_test_utils/ov_test_utils.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/convert_align_types.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/pass/manager.hpp"

using namespace ov;
using namespace ov::op;
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved

namespace {

struct ConvertAlignTypesTestParams {
PartialShape lhs_shape;
element::Type lhs_type;
PartialShape rhs_shape;
element::Type rhs_type;
element::Type expected_type;
};

class ConvertConvertAlignTypesTest : public TransformationTestsF,
public testing::WithParamInterface<ConvertAlignTypesTestParams> {
public:
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
void SetUp() override {
TransformationTestsF::SetUp();
const auto& parameters = GetParam();
const auto& lhsType = parameters.lhs_type;
const auto& lhsShape = parameters.lhs_shape;
const auto& rhsType = parameters.rhs_type;
const auto& rhsShape = parameters.rhs_shape;
const auto& alignType = parameters.expected_type;
model = transform(lhsShape, lhsType, rhsShape, rhsType);
model_ref = reference(lhsShape, lhsType, rhsShape, rhsType, alignType);
manager.register_pass<pass::ConvertConvertAlignTypes>();
}

protected:
static std::shared_ptr<Model> transform(const PartialShape& lhsShape,
const element::Type& lhsType,
const PartialShape& rhsShape,
const element::Type& rhsType) {
const auto lhs = std::make_shared<v0::Parameter>(lhsType, lhsShape);
const auto rhs = std::make_shared<v0::Parameter>(rhsType, rhsShape);
const auto convert_align_types = std::make_shared<v14::ConvertAlignTypes>(lhs, rhs, true);
return std::make_shared<Model>(convert_align_types->outputs(), ParameterVector{lhs, rhs}, "Actual");
}

static std::shared_ptr<Model> reference(const PartialShape& lhsShape,
const element::Type& lhsType,
const PartialShape& rhsShape,
const element::Type& rhsType,
const element::Type& alignType) {
const auto lhs = std::make_shared<v0::Parameter>(lhsType, lhsShape);
const auto rhs = std::make_shared<v0::Parameter>(rhsType, rhsShape);
const auto lhs_converted = std::make_shared<v0::Convert>(lhs, alignType);
const auto rhs_converted = std::make_shared<v0::Convert>(rhs, alignType);
return std::make_shared<Model>(NodeVector{lhs_converted, rhs_converted},
ParameterVector{lhs, rhs},
"Reference");
}
};
INSTANTIATE_TEST_SUITE_P(
ConvertAlignTypesDecomposition,
ConvertConvertAlignTypesTest,
testing::Values(
ConvertAlignTypesTestParams{PartialShape::dynamic(),
element::f32,
PartialShape::dynamic(),
element::f32,
element::f32},
ConvertAlignTypesTestParams{PartialShape::dynamic(), element::u16, {5, 6, 7}, element::i16, element::i32},
ConvertAlignTypesTestParams{{1, 2, 3, 4}, element::u16, PartialShape::dynamic(), element::f32, element::f32},
ConvertAlignTypesTestParams{{1, {3, 7}, -1, 4}, element::f8e4m3, {0, 6, 7}, element::f8e5m2, element::f16},
ConvertAlignTypesTestParams{{}, element::bf16, {5, 6, 7}, element::f16, element::f32},
ConvertAlignTypesTestParams{{1, 2, 3, 4}, element::u16, {}, element::boolean, element::u16},
ConvertAlignTypesTestParams{{}, element::u16, {}, element::u1, element::u16},
ConvertAlignTypesTestParams{{-1}, element::u64, {1}, element::i16, element::f32},
ConvertAlignTypesTestParams{{1, 2, 3, 4}, element::boolean, {5, 6, 7}, element::boolean, element::boolean},
ConvertAlignTypesTestParams{{1, 2, 3, 4}, element::i64, {5, 6, 7}, element::f16, element::f16}));
TEST_P(ConvertConvertAlignTypesTest, CompareFunctions) {}
} // namespace
69 changes: 69 additions & 0 deletions src/core/include/openvino/op/convert_align_types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "openvino/op/op.hpp"

namespace ov {
namespace op {
namespace v14 {
/// \brief Elementwise type alignment conversion operation.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is any specification for this operator?
If not provide some basic description in doxy comment and rules about type conversion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Specification is planned to be added in separate PR. I will update docstring to explain it better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added more description for op

/// \ingroup ov_ops_cpp_api
class OPENVINO_API ConvertAlignTypes : public Op {
public:
OPENVINO_OP("ConvertAlignTypes", "opset14", op::Op);

/// \brief Constructs a type alignment and conversion operation.
ConvertAlignTypes() = default;
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
/// \brief Constructs a type alignment and conversion operation.
/// \param lhs Node with datatype to be aligned.
/// \param rhs Node with datatype to be aligned.
/// \param promote_unsafe Bool attribute wether to allow for promotions that might result in bit-widening,
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
/// precision loss and undefined behaviors.
/// \param pytorch_scalar_align Bool attribute wether to align scalars using PyTorch-like rules.
Copy link
Contributor

Choose a reason for hiding this comment

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

PyTorch-like rule it would be nice to explain these rules

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Full explanation is planned to be provided in specification, but I will try to summarize it a bit in docstring

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extended docstring with some explanation

mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
/// \param u64_integer_promotion_target Element type attribute to select alignment target for u64 and signed
/// integers.
ConvertAlignTypes(const Output<Node>& lhs,
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
const Output<Node>& rhs,
const bool promote_unsafe = false,
const bool pytorch_scalar_align = false,
const element::Type& u64_integer_promotion_target = element::f32);
void validate_and_infer_types() override;
bool visit_attributes(AttributeVisitor& visitor) override;

std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;

bool get_pytorch_scalar_align() const {
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
return m_pytorch_scalar_align;
}

void set_pytorch_scalar_align(bool pytorch_scalar_align) {
m_pytorch_scalar_align = pytorch_scalar_align;
}

bool get_promote_unsafe() const {
return m_promote_unsafe;
}

void set_promote_unsafe(bool promote_unsafe) {
m_promote_unsafe = promote_unsafe;
}

element::Type get_u64_integer_promotion_target() const {
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
return m_u64_integer_promotion_target;
}

void set_u64_integer_promotion_target(element::Type u64_integer_promotion_target) {
mmikolajcz marked this conversation as resolved.
Show resolved Hide resolved
m_u64_integer_promotion_target = u64_integer_promotion_target;
}

private:
bool m_promote_unsafe = false;
bool m_pytorch_scalar_align = false;
element::Type m_u64_integer_promotion_target = element::f32;
};
} // namespace v14
} // namespace op
} // namespace ov
1 change: 1 addition & 0 deletions src/core/include/openvino/op/ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/convert_align_types.hpp"
#include "openvino/op/convert_like.hpp"
#include "openvino/op/convolution.hpp"
#include "openvino/op/cos.hpp"
Expand Down
3 changes: 2 additions & 1 deletion src/core/include/openvino/opsets/opset14_tbl.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2018-2023 Intel Corporation
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

Expand Down Expand Up @@ -219,3 +219,4 @@ _OPENVINO_OP_REG(ScaledDotProductAttention, ov::op::v13)
_OPENVINO_OP_REG(FakeConvert, ov::op::v13)

// New operations added in opset14
_OPENVINO_OP_REG(ConvertAlignTypes, ov::op::v14)
Loading
Loading