-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[CPU]remove useless convert pair(Convert[a->b] -> Convert[b->a]) #27868
base: master
Are you sure you want to change the base?
Changes from all commits
1fc6161
b075968
bc407d5
a47dd6c
2b758b3
786302e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -443,16 +443,54 @@ pass::EliminateConvert::EliminateConvert() { | |
if (!convert) { | ||
return false; | ||
} | ||
|
||
if (convert->get_input_element_type(0) == convert->get_element_type()) { | ||
return replace_output_update_name(convert->output(0), convert->input_value(0)); | ||
} | ||
|
||
// Only for some test. | ||
if (convert->get_rt_info().count("DisableEliminateUselessConvert") && | ||
convert->get_rt_info()["DisableEliminateUselessConvert"].as<std::string>() == std::string("1")) { | ||
return false; | ||
} | ||
|
||
// Eliminate useless convert pattern, for example: | ||
// Convert[fp16->fp32] -> Convert[fp32->f16] | ||
if (convert && convert->get_output_size() > 0u && convert->get_input_size() > 0u) { | ||
auto convert2 = ov::as_type_ptr<ov::op::v0::Convert>( | ||
convert->get_output_target_inputs(0).begin()->get_node()->shared_from_this()); | ||
if (convert2 && convert->get_input_element_type(0) == convert2->get_output_element_type(0) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general such optimization is not safe. Cases like Convert[fp32->fp16] -> Convert[fp16->f32] still needs to be executed explicitly as long as its elemintation will affect model accuracy. However we still can adjust the pass to the patterns which are safe to remove (then src/dst precisions can be converted to intermediate precision w/o any loss). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @dmitry-gorokhov for the clarification. |
||
convert->get_output_partial_shape(0) == convert2->get_output_partial_shape(0)) { | ||
replace_output_update_name(convert2->output(0), convert->input_value(0)); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
auto m = make_shared<pattern::Matcher>(convert_pattern, matcher_name); | ||
this->register_matcher(m, callback); | ||
} | ||
|
||
pass::DisableEliminateUselessConvert::DisableEliminateUselessConvert() { | ||
MATCHER_SCOPE(DisableEliminateUselessConvert); | ||
auto convert_pattern = pattern::wrap_type<ov::op::v0::Convert>(); | ||
|
||
matcher_pass_callback callback = [](pattern::Matcher& m) { | ||
auto convert = dynamic_pointer_cast<ov::op::v0::Convert>(m.get_match_root()); | ||
if (!convert) { | ||
return false; | ||
} | ||
convert->get_rt_info()["DisableEliminateUselessConvert"] = "1"; | ||
|
||
return true; | ||
}; | ||
|
||
auto m = make_shared<pattern::Matcher>(convert_pattern, matcher_name); | ||
this->register_matcher(m, callback); | ||
} | ||
|
||
pass::EliminateConvertNonZero::EliminateConvertNonZero() { | ||
MATCHER_SCOPE(EliminateConvertNonZero); | ||
auto convert_pattern = pattern::wrap_type<ov::op::v0::Convert>(pattern::consumers_count(1)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definetelly incorrect way to solve test failures. Test exptected behavior should be updated instead.