OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
GPTQ pattern updates to support aot_autograd (#26124)
Mustafa Cavusecb08d4895b5
diff --git a/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/partition.py b/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/partition.pyindex b433630fb6b..a99fdb4ebc6 100644--- a/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/partition.py+++ b/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/partition.py@@ -25,6 +25,13 @@ logger = logging.getLogger(__name__)logger.setLevel(logging.WARNING)+class PatternNode:+ op_types = {}++ def __init__(self):+ self.op_types = {}++class Partitioner:def __init__(self, options):self.supported_ops = OperatorSupport(options)@@ -56,55 +63,56 @@ class Partitioner:return Truereturn False- def capture_gptq_patterns(self, graph_module: GraphModule) -> bool:+ def check_pattern(self, node: torch.fx.Node, pattern: PatternNode, enabled_ops: list) -> bool:+ if node.op == "call_function":+ if ("call_function" + ":" + str(node.target)) in pattern.op_types:+ pt_input_nodes = node.all_input_nodes+ pattern_input_ops = pattern.op_types["call_function" + ":" + str(node.target)]+ if pattern_input_ops is None:+ enabled_ops.append(node)+ return True+ if len(pt_input_nodes) != len(pattern_input_ops):+ return False+ for i in range(len(pt_input_nodes)):+ if not self.check_pattern(pt_input_nodes[i], pattern_input_ops[i], enabled_ops):+ return False+ enabled_ops.append(node)+ return True+ elif node.op == "get_attr":+ if "get_attr" in pattern.op_types:+ return True+ else:+ return False+ return False++ def capture_gptq_patterns(self, graph_module: GraphModule):+ const_0_node = PatternNode+ const_0_node.op_types["get_attr"] = None+ unsqueeze_0_node = PatternNode+ unsqueeze_0_node.op_types["call_function:aten.unsqueeze.default"] = [const_0_node,]+ expand_node = PatternNode+ expand_node.op_types["call_function:aten.expand.default"] = [unsqueeze_0_node,]+ const_1_node = PatternNode+ const_1_node.op_types["get_attr"] = None+ unsqueeze_1_node = PatternNode+ unsqueeze_1_node.op_types["call_function:aten.unsqueeze.default"] = [const_1_node,]+ bitwise_right_shift_node = PatternNode+ bitwise_right_shift_node.op_types["call_function:aten.bitwise_right_shift.Tensor"] = [expand_node, unsqueeze_1_node]+ to_copy_node = PatternNode+ to_copy_node.op_types["call_function:aten._to_copy.default"] = [bitwise_right_shift_node,]+ add_or_to_copy_node = PatternNode+ add_or_to_copy_node.op_types["call_function:aten._to_copy.default"] = [bitwise_right_shift_node,]+ add_or_to_copy_node.op_types["call_function:aten.add.Tensor"] = [to_copy_node,]+ bitwise_and_node = PatternNode+ bitwise_and_node.op_types["call_function:aten.bitwise_and.Scalar"] = [add_or_to_copy_node,]+for node in graph_module.graph.nodes:if str(node.op) == "call_function" and str(node.target) == "aten.bitwise_and.Scalar":- bitwise_and_in_nodes = node.all_input_nodes- if len(bitwise_and_in_nodes) != 1:- continue- to_copy_node = bitwise_and_in_nodes[0]- if str(to_copy_node.op) != "call_function" or str(to_copy_node.target) != "aten._to_copy.default":- continue- to_copy_in_nodes = to_copy_node.all_input_nodes- if len(to_copy_in_nodes) != 1:- continue- bitwise_right_shift_node = to_copy_in_nodes[0]- if str(bitwise_right_shift_node.op) != "call_function" or str(bitwise_right_shift_node.target) != "aten.bitwise_right_shift.Tensor":- continue- bitwise_right_shift_in_nodes = bitwise_right_shift_node.all_input_nodes- if len(bitwise_right_shift_in_nodes) != 2:- continue- expand_node = bitwise_right_shift_in_nodes[0]- if str(expand_node.op) != "call_function" or str(expand_node.target) != "aten.expand.default":- continue- expand_in_nodes = expand_node.all_input_nodes- if len(expand_in_nodes) != 1:- continue- unsqueeze_0_node = expand_in_nodes[0]- if str(unsqueeze_0_node.op) != "call_function" or str(unsqueeze_0_node.target) != "aten.unsqueeze.default":- continue- unsqueeze_0_in_nodes = unsqueeze_0_node.all_input_nodes- if len(unsqueeze_0_in_nodes) != 1:- continue- const_0_node = unsqueeze_0_in_nodes[0]- if str(const_0_node.op) != "get_attr":- continue- unsqueeze_1_node = bitwise_right_shift_in_nodes[1]- if str(unsqueeze_1_node.op) != "call_function" or str(unsqueeze_1_node.target) != "aten.unsqueeze.default":- continue- unsqueeze_1_in_nodes = unsqueeze_1_node.all_input_nodes- if len(unsqueeze_1_in_nodes) != 1:- continue- const_1_node = unsqueeze_1_in_nodes[0]- if str(const_1_node.op) != "get_attr":- continue-- self.supported_ops.enable_by_name(node)- self.supported_ops.enable_by_name(to_copy_node)- self.supported_ops.enable_by_name(bitwise_right_shift_node)- self.supported_ops.enable_by_name(expand_node)- self.supported_ops.enable_by_name(unsqueeze_0_node)- self.supported_ops.enable_by_name(unsqueeze_1_node)+ enabled_ops = []+ pattern_match = self.check_pattern(node, bitwise_and_node, enabled_ops)+ if pattern_match:+ for pattern_op in enabled_ops:+ self.supported_ops.enable_by_name(pattern_op)def make_partitions(self, graph_module: GraphModule, options) -> GraphModule:allow_single_node_partition = _is_testing(options)diff --git a/src/frontends/pytorch/src/transforms/torchfx_gptq_pattern_replacer.cpp b/src/frontends/pytorch/src/transforms/torchfx_gptq_pattern_replacer.cppindex 3108765b21e..a533739b16f 100644--- a/src/frontends/pytorch/src/transforms/torchfx_gptq_pattern_replacer.cpp+++ b/src/frontends/pytorch/src/transforms/torchfx_gptq_pattern_replacer.cpp@@ -17,6 +17,7 @@#include "openvino/op/unsqueeze.hpp"#include "openvino/op/util/framework_node.hpp"#include "openvino/pass/pattern/matcher.hpp"+#include "openvino/pass/pattern/op/or.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"#include "utils.hpp"#include "utils_quantize.hpp"@@ -57,15 +58,20 @@ GPTQDecompressionReplacer::GPTQDecompressionReplacer() {const auto& unsqueeze_1 = wrap_type<v0::Unsqueeze>({const_1, const_2});const auto& const_abs = wrap_type<v0::Constant>();const auto& abs = wrap_type<v0::Abs>({const_abs});- const auto& broadcast_3 = wrap_type<ov::op::v3::Broadcast>({unsqueeze_1, abs});- const auto& const_6 = wrap_type<v0::Constant>();- const auto& const_7 = wrap_type<v0::Constant>();- const auto& unsqueeze_2 = wrap_type<v0::Unsqueeze>({const_6, const_7});- const auto& bitwise_right_shift = wrap_type<ov::op::util::FrameworkNode>({broadcast_3, unsqueeze_2});+ const auto& broadcast = wrap_type<ov::op::v3::Broadcast>({unsqueeze_1, abs});+ const auto& const_3 = wrap_type<v0::Constant>();+ const auto& const_4 = wrap_type<v0::Constant>();+ const auto& unsqueeze_2 = wrap_type<v0::Unsqueeze>({const_3, const_4});+ const auto& bitwise_right_shift = wrap_type<ov::op::util::FrameworkNode>({broadcast, unsqueeze_2});const auto& convert_1 = wrap_type<v0::Convert>({bitwise_right_shift});- const auto& const_8 = wrap_type<v0::Constant>();- const auto& convert_2 = wrap_type<v0::Convert>({const_8});- const auto& bitwise_and = wrap_type<ov::op::v13::BitwiseAnd>({convert_1, convert_2});+ const auto& const_5 = wrap_type<v0::Constant>();+ const auto& convert_3 = wrap_type<v0::Convert>({bitwise_right_shift});+ const auto& convert_4 = wrap_type<v0::Convert>({const_5});+ const auto& add = wrap_type<v1::Add>({convert_3, convert_4});+ const auto& add_or_convert = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{add, convert_1});+ const auto& const_6 = wrap_type<v0::Constant>();+ const auto& convert_2 = wrap_type<v0::Convert>({const_6});+ const auto& bitwise_and = wrap_type<ov::op::v13::BitwiseAnd>({add_or_convert, convert_2});ov::matcher_pass_callback callback = [unsqueeze_1](Matcher& m) {auto bitwise_and = m.get_match_root();@@ -131,22 +137,23 @@ GPTQDecompressionReplacer::GPTQDecompressionReplacer() {};GPTQMultPatternReplacer::GPTQMultPatternReplacer() {- const auto& const_1_1 = wrap_type<v0::Constant>();- const auto& convert_1 = wrap_type<v0::Convert>({const_1_1});- const auto& const_1_2 = wrap_type<v0::Constant>();- const auto& convert_2 = wrap_type<v0::Convert>({const_1_2});- const auto& add = wrap_type<v1::Add>({convert_1, convert_2});+ const auto& const_1 = wrap_type<v0::Constant>();+ const auto& convert_1 = wrap_type<v0::Convert>({const_1});const auto& const_2 = wrap_type<v0::Constant>();- const auto& reshape_1 = wrap_type<v1::Reshape>({add, const_2});- const auto& const_3_1 = wrap_type<v0::Constant>();- const auto& convert_4 = wrap_type<v0::Convert>({const_3_1});- const auto& const_3_2 = wrap_type<v0::Constant>();- const auto& reshape_2 = wrap_type<v1::Reshape>({convert_4, const_3_2});+ const auto& convert_2 = wrap_type<v0::Convert>({const_2});+ const auto& add = wrap_type<v1::Add>({convert_1, convert_2});+ const auto& const_3 = wrap_type<v0::Constant>();+ const auto& add_or_convert = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{add, convert_1});+ const auto& reshape_1 = wrap_type<v1::Reshape>({add_or_convert, const_3});+ const auto& const_4 = wrap_type<v0::Constant>();+ const auto& convert_4 = wrap_type<v0::Convert>({const_4});+ const auto& const_5 = wrap_type<v0::Constant>();+ const auto& reshape_2 = wrap_type<v1::Reshape>({convert_4, const_5});const auto& subtract = wrap_type<v1::Subtract>({reshape_2, reshape_1});const auto& convert_3 = wrap_type<v0::Convert>({subtract});- const auto& const_4_1 = wrap_type<v0::Constant>();- const auto& const_4_2 = wrap_type<v0::Constant>();- const auto& reshape_3 = wrap_type<v1::Reshape>({const_4_1, const_4_2});+ const auto& const_6 = wrap_type<v0::Constant>();+ const auto& const_7 = wrap_type<v0::Constant>();+ const auto& reshape_3 = wrap_type<v1::Reshape>({const_6, const_7});auto mult = wrap_type<v1::Multiply>({reshape_3, convert_3});@@ -156,7 +163,10 @@ GPTQMultPatternReplacer::GPTQMultPatternReplacer() {return false;}const auto& pattern_map = m.get_pattern_value_map();- auto convert_2_node = pattern_map.at(convert_2).get_node_shared_ptr();+ std::shared_ptr<ov::Node> convert_2_node = nullptr;+ if (pattern_map.find(convert_2) != pattern_map.end()) {+ convert_2_node = pattern_map.at(convert_2).get_node_shared_ptr();+ }auto convert_1_node = pattern_map.at(convert_1).get_node_shared_ptr();auto convert_4_node = pattern_map.at(convert_4).get_node_shared_ptr();auto reshape_node = pattern_map.at(reshape_1).get_node_shared_ptr();@@ -169,17 +179,20 @@ GPTQMultPatternReplacer::GPTQMultPatternReplacer() {return false;}auto add_in0_ptr = add_input0_const->get_data_ptr<uint8_t>();- auto convert_2_input_const =- std::dynamic_pointer_cast<v0::Constant>(convert_2_node->get_input_node_shared_ptr(0));- auto add_in1_ptr = convert_2_input_const->get_data_ptr<uint8_t>();- if (!add_in1_ptr)- return false;+ uint32_t add_val = 0;+ if (convert_2_node) {+ auto convert_2_input_const =+ std::dynamic_pointer_cast<v0::Constant>(convert_2_node->get_input_node_shared_ptr(0));+ auto add_in1_ptr = convert_2_input_const->get_data_ptr<uint8_t>();+ if (!add_in1_ptr)+ return false;+ add_val = (uint32_t)(add_in1_ptr[0] & 0x0F);+ }const auto& add_in0_shape = add_input0_const->get_shape();const auto& static_shape_1 = reshape_node->get_shape();size_t add_in0_size = shape_size(add_in0_shape);auto add_replace_const = std::make_shared<v0::Constant>(element::f32, static_shape_1);auto add_replace_ptr = const_cast<float*>(reinterpret_cast<const float*>(add_replace_const->get_data_ptr()));- uint32_t add_val = (uint32_t)(add_in1_ptr[0] & 0x0F);if (!add_replace_ptr || (add_in0_size != shape_size(add_replace_const->get_shape()))) {return false;