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
Fix torch.compile guard failures (#39480)
Surya Siddharth Pemmaraju77915ab63487
diff --git a/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/backend.py b/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/backend.pyindex fba2f3c5055..b2500830985 100644--- a/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/backend.py+++ b/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/backend.py@@ -47,8 +47,6 @@ logger.setLevel(logging.WARNING)2) model = torch.compile(model, backend="openvino")"""-openvino_options = {}-# Disable regional compilation which was enabled by default from Torch 2.5.0if hasattr(torch._dynamo.config, "inline_inbuilt_nn_modules"):torch._dynamo.config.inline_inbuilt_nn_modules=False@@ -56,10 +54,9 @@ if hasattr(torch._dynamo.config, "inline_inbuilt_nn_modules"):@fake_tensor_unsupporteddef openvino(subgraph, example_inputs, options=None):if _get_aot_autograd(options):- global openvino_options- openvino_options = optionsdecompositions = _get_decompositions(options) + get_inf_decomposition_list() + get_aot_decomposition_list()- return aot_autograd(fw_compiler=fx_openvino, bw_compiler=fx_openvino, decompositions=get_decompositions(decompositions))(subgraph, example_inputs)+ compiler = partial(fx_openvino, options=options)+ return aot_autograd(fw_compiler=compiler, bw_compiler=compiler, decompositions=get_decompositions(decompositions))(subgraph, example_inputs)return fx_openvino(subgraph, example_inputs, options)if "openvino" not in torch.compiler.list_backends():@@ -67,8 +64,6 @@ if "openvino" not in torch.compiler.list_backends():def fx_openvino(subgraph, example_inputs, options=None):try:- if len(openvino_options) != 0:- options = openvino_optionsexecutor_parameters = Noneinputs_reversed = Falseopenvino_model_caching = _get_model_caching(options)@@ -103,12 +98,15 @@ def fx_openvino(subgraph, example_inputs, options=None):model = subgraphelse:decompositions = _get_decompositions(options) + get_inf_decomposition_list()- model = make_fx(- subgraph,- decomposition_table=get_decompositions(decompositions),- tracing_mode="fake",- _allow_non_fake_inputs=True,- )(*example_inputs)+ # Trace outside Dynamo's TracingContext so make_fx creates its own fake mode;+ # otherwise its shape guards leak into Dynamo's ShapeEnv with make_fx input names.+ with torch._guards.tracing(None):+ model = make_fx(+ subgraph,+ decomposition_table=get_decompositions(decompositions),+ tracing_mode="fake",+ _allow_non_fake_inputs=True,+ )(*example_inputs)with torch.no_grad():model.eval()partitioner = Partitioner(options)diff --git a/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py b/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.pyindex cbb7da04e2e..b9c2a943f46 100644--- a/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py+++ b/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py@@ -515,7 +515,7 @@ class PytorchLayerTest:return omdef torch_compile_backend_test(self, model, inputs, **kwargs):- torch._dynamo.config.suppress_errors = True+ torch._dynamo.config.suppress_errors = Falsetorch._dynamo.reset()model.eval()