Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Expand Up @@ -14,18 +14,25 @@ using namespace ov::pass::pattern;
((in_ps.size() == 3 && out_ps.size() == 2) || (in_ps.size() == 4 && out_ps.size() == 3));\
};\
\
auto compressed_weights_m = wrap_type<ov::op::v0::Constant>(compressed_constant);\
auto weights_const_m = wrap_type<ov::op::v0::Constant>(compressed_constant);\
auto weights_param_m = wrap_type<ov::op::v0::Parameter>(compressed_constant);\
auto weights_initial_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{weights_const_m, weights_param_m});\
auto weights_reshape_m = wrap_type<ov::op::v1::Reshape>({weights_initial_m, any_input()});\
auto compressed_weights_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{weights_const_m, weights_param_m, weights_reshape_m});\
auto convert_m = wrap_type<ov::op::v0::Convert>({compressed_weights_m});\
auto weights_param_convert_m = wrap_type<ov::op::v0::Convert>({weights_param_m});\
auto weights_convert_reshape_m = wrap_type<ov::op::v1::Reshape>({weights_param_convert_m, any_input()});\
auto decompressed_weights_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{convert_m, weights_convert_reshape_m});\
\
auto sub_const_m = wrap_type<ov::op::v0::Constant>();\
auto sub_convert_const_m = wrap_type<ov::op::v0::Convert>({sub_const_m});\
auto sub_with_convert_m = wrap_type<ov::op::v1::Subtract>({convert_m, sub_convert_const_m});\
auto sub_no_convert_m = wrap_type<ov::op::v1::Subtract>({convert_m, sub_const_m});\
auto sub_with_convert_m = wrap_type<ov::op::v1::Subtract>({decompressed_weights_m, sub_convert_const_m});\
auto sub_no_convert_m = wrap_type<ov::op::v1::Subtract>({decompressed_weights_m, sub_const_m});\
auto subtract_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{sub_with_convert_m, sub_no_convert_m});\
\
auto mul_const_m = wrap_type<ov::op::v0::Constant>();\
auto mul_with_sub_m = wrap_type<ov::op::v1::Multiply>({subtract_m, mul_const_m});\
auto mul_no_sub_m = wrap_type<ov::op::v1::Multiply>({convert_m, mul_const_m});\
auto mul_no_sub_m = wrap_type<ov::op::v1::Multiply>({decompressed_weights_m, mul_const_m});\
auto mul_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{mul_with_sub_m, mul_no_sub_m});\
\
auto reshape_const_m = wrap_type<ov::op::v0::Constant>();\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
const auto& pattern_map = m.get_pattern_value_map();
OPENVINO_ASSERT(pattern_map.count(fully_connected_m));
OPENVINO_ASSERT(pattern_map.count(mul_const_m));
OPENVINO_ASSERT(pattern_map.count(compressed_weights_m));
OPENVINO_ASSERT(pattern_map.count(decompressed_weights_m));
OPENVINO_ASSERT(pattern_map.count(bias_m));
OPENVINO_ASSERT(pattern_map.count(convert_m));
auto fc = ov::as_type_ptr<op::FullyConnected>(pattern_map.at(fully_connected_m).get_node_shared_ptr());
if (!fc || transformation_callback(fc)) {
return false;
Expand All @@ -55,8 +54,9 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
auto weight_shape = fc->get_input_shape(1);
bool is_weight_3d = (std::count_if(weight_shape.begin(), weight_shape.end(), [](size_t d) { return d > 1; }) == 3);

auto weight_ptr = ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(compressed_weights_m).get_node_shared_ptr());
bool weight_u8 = false;
std::shared_ptr<ov::Node> weight_ptr =
pattern_map.count(weights_const_m) ? pattern_map.at(weights_const_m).get_node_shared_ptr() : pattern_map.at(weights_param_m).get_node_shared_ptr();
if (weight_ptr->get_element_type() == ov::element::u8 || weight_ptr->get_element_type() == ov::element::i8)
weight_u8 = true;

Expand Down Expand Up @@ -112,12 +112,28 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
optional_zero_point = convert_const_to_u8(reshape_const(pattern_map.at(sub_const_m).get_node_shared_ptr()));
}

std::shared_ptr<ov::Node> fc_input_b = reshape_const(pattern_map.at(compressed_weights_m).get_node_shared_ptr());
std::shared_ptr<ov::Node> fc_input_b =
pattern_map.count(weights_const_m) ? reshape_const(pattern_map.at(weights_const_m).get_node_shared_ptr())
: (pattern_map.count(weights_reshape_m) ? pattern_map.at(weights_reshape_m).get_node_shared_ptr()
: pattern_map.at(weights_param_m).get_node_shared_ptr());
std::shared_ptr<ov::Node> fc_input_scale = scale;
std::shared_ptr<ov::Node> fc_input_zp = optional_zero_point;
std::shared_ptr<ov::Node> fc_input_bias = pattern_map.at(bias_m).get_node_shared_ptr();
std::vector<std::shared_ptr<ov::Node>> result_nodes = {};

if (fc_input_b->get_output_partial_shape(0).size() != weight_shape.size()) {
OPENVINO_ASSERT(weight_shape.size() < 3);
if (has_transpose) {
OPENVINO_ASSERT(weight_shape.size() == 2);
std::swap(weight_shape[0], weight_shape[1]);
}
std::shared_ptr<ov::Node> weight_shape_const =
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{weight_shape.size()}, weight_shape);
fc_input_b = std::make_shared<ov::op::v1::Reshape>(fc_input_b, weight_shape_const, false);
result_nodes.push_back(weight_shape_const);
result_nodes.push_back(fc_input_b);
}

if (has_transpose) {
const auto& transpose = pattern_map.at(transpose_m).get_node_shared_ptr();
std::shared_ptr<ov::Node> transpose_const = pattern_map.at(transpose_const_m).get_node_shared_ptr();
Expand All @@ -128,16 +144,16 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
transpose_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{new_order.size()}, new_order);
}

fc_input_b = transpose->clone_with_new_inputs({ fc_input_b->output(0), transpose_const });
fc_input_b = transpose->clone_with_new_inputs({fc_input_b->output(0), transpose_const});
result_nodes.push_back(fc_input_b);

if (ov::shape_size(scale->output(0).get_shape()) > 1) {
fc_input_scale = transpose->clone_with_new_inputs({ scale->output(0), transpose_const });
fc_input_scale = transpose->clone_with_new_inputs({scale->output(0), transpose_const});
result_nodes.push_back(fc_input_scale);
}

if (with_zero_point && ov::shape_size(optional_zero_point->output(0).get_shape()) > 1) {
fc_input_zp = transpose->clone_with_new_inputs({ optional_zero_point->output(0), transpose_const });
fc_input_zp = transpose->clone_with_new_inputs({optional_zero_point->output(0), transpose_const});
result_nodes.push_back(fc_input_zp);
}
}
Expand All @@ -149,18 +165,10 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon

std::shared_ptr<ov::Node> new_fc = nullptr;
if (with_zero_point) {
new_fc = std::make_shared<op::FullyConnectedCompressed>(fc_input_a,
fc_input_b,
fc_input_bias,
fc_input_scale,
fc_input_zp,
fc->get_output_type());
new_fc =
std::make_shared<op::FullyConnectedCompressed>(fc_input_a, fc_input_b, fc_input_bias, fc_input_scale, fc_input_zp, fc->get_output_type());
} else {
new_fc = std::make_shared<op::FullyConnectedCompressed>(fc_input_a,
fc_input_b,
fc_input_bias,
fc_input_scale,
fc->get_output_type());
new_fc = std::make_shared<op::FullyConnectedCompressed>(fc_input_a, fc_input_b, fc_input_bias, fc_input_scale, fc->get_output_type());
}

result_nodes.push_back(new_fc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected(bool supports_immad
};
auto weights_path = [&static_rank_gt_1](const ov::Output<ov::Node>& output) {
const auto& pshape = output.get_partial_shape();
return ov::op::util::is_on_path<ov::op::v0::Constant>(output) &&
return ov::op::util::is_on_path<ov::op::v0::Constant, ov::op::v0::Parameter>(output) &&
static_rank_gt_1(output) &&
pshape.is_static();
};
Expand Down
Loading
Loading