Upate OpenVINO to PyTorch 2.6 and fix mismatched shapes error on too many resolution changes

This commit is contained in:
Disty0
2025-02-09 01:17:06 +03:00
parent e578afc1f5
commit 1acbabb276
6 changed files with 34 additions and 20 deletions
@@ -42,7 +42,7 @@ def load_model(device, model_path, model_type="dpt_large_384", optimize=True, he
network input
"""
if "openvino" in model_type:
from openvino.runtime import Core
from openvino import Core
keep_aspect_ratio = not square
+18 -8
View File
@@ -3,10 +3,10 @@ import sys
import torch
import nncf
from openvino.frontend import FrontEndManager
from openvino.frontend.pytorch.fx_decoder import TorchFXPythonDecoder
from openvino.frontend.pytorch.torchdynamo.partition import Partitioner
from openvino.runtime import Core, Type, PartialShape, serialize
from openvino.frontend.pytorch.fx_decoder import TorchFXPythonDecoder
from openvino.frontend import FrontEndManager
from openvino import Core, Type, PartialShape, serialize
from openvino.properties import hint as ov_hints
from torch._dynamo.backends.common import fake_tensor_unsupported
@@ -23,6 +23,11 @@ import functools
from modules import shared, devices, sd_models
torch._dynamo.eval_frame.check_if_dynamo_supported = lambda: True # pylint: disable=protected-access
if hasattr(torch._dynamo.config, "inline_inbuilt_nn_modules"):
torch._dynamo.config.inline_inbuilt_nn_modules = False # pylint: disable=protected-access
DEFAULT_OPENVINO_PYTHON_CONFIG = MappingProxyType(
{
"use_python_fusion_cache": True,
@@ -114,9 +119,9 @@ def cached_model_name(model_hash_str, device, args, cache_root, reversed = False
for input_data in args:
if isinstance(input_data, torch.SymInt):
if reversed:
inputs_str = "_" + "torch.SymInt1" + inputs_str
inputs_str = "_" + "torch.SymInt[]" + inputs_str
else:
inputs_str += "_" + "torch.SymInt1"
inputs_str += "_" + "torch.SymInt[]"
elif isinstance(input_data, int):
pass
else:
@@ -176,7 +181,7 @@ def openvino_compile(gm: GraphModule, *example_inputs, model_hash_str: str = Non
for input_data in example_inputs:
if isinstance(input_data, torch.SymInt):
input_types.append(torch.SymInt)
input_shapes.append(torch.Size([1]))
input_shapes.append(torch.Size([]))
elif isinstance(input_data, int):
pass
else:
@@ -426,9 +431,8 @@ def get_subgraph_type(tensor):
return tensor
@register_backend
@fake_tensor_unsupported
def openvino_fx(subgraph, example_inputs):
def openvino_fx(subgraph, example_inputs, options=None):
global dont_use_4bit_nncf
global dont_use_nncf
global dont_use_quant
@@ -528,6 +532,8 @@ def openvino_fx(subgraph, example_inputs):
for node in model.graph.nodes:
if node.target == torch.ops.aten.mul_.Tensor:
node.target = torch.ops.aten.mul.Tensor
elif node.target == torch.ops.aten._unsafe_index.Tensor:
node.target = torch.ops.aten.index.Tensor
with devices.inference_context():
model.eval()
partitioner = Partitioner(options=None)
@@ -543,3 +549,7 @@ def openvino_fx(subgraph, example_inputs):
res = execute(compiled_model, *args, executor="openvino", executor_parameters=executor_parameters, file_name=maybe_fs_cached_name)
return res
return _call
if "openvino_fx" not in torch.compiler.list_backends():
register_backend(compiler_fn=openvino_fx, name="openvino_fx")
+4 -3
View File
@@ -63,7 +63,6 @@ def ipex_optimize(sd_model):
def optimize_openvino(sd_model):
try:
from modules.intel.openvino import openvino_fx # pylint: disable=unused-import
torch._dynamo.eval_frame.check_if_dynamo_supported = lambda: True # pylint: disable=protected-access
if shared.compiled_model_state is not None:
shared.compiled_model_state.compiled_cache.clear()
shared.compiled_model_state.req_cache.clear()
@@ -164,13 +163,15 @@ def compile_torch(sd_model):
model = torch.compile(model.to(devices.device),
mode=shared.opts.cuda_compile_mode,
backend=shared.opts.cuda_compile_backend,
fullgraph=shared.opts.cuda_compile_fullgraph
fullgraph=shared.opts.cuda_compile_fullgraph,
dynamic=None if shared.opts.cuda_compile_backend != "openvino_fx" else False,
).to(return_device)
else:
model = torch.compile(model,
mode=shared.opts.cuda_compile_mode,
backend=shared.opts.cuda_compile_backend,
fullgraph=shared.opts.cuda_compile_fullgraph
fullgraph=shared.opts.cuda_compile_fullgraph,
dynamic=None if shared.opts.cuda_compile_backend != "openvino_fx" else False,
)
devices.torch_gc()
return model
+6 -2
View File
@@ -187,7 +187,6 @@ def compile_upscaler(model):
if shared.opts.cuda_compile_backend == "openvino_fx":
from modules.intel.openvino import openvino_fx # pylint: disable=unused-import
torch._dynamo.eval_frame.check_if_dynamo_supported = lambda: True # pylint: disable=protected-access
log_level = logging.WARNING if shared.opts.cuda_compile_verbose else logging.CRITICAL # pylint: disable=protected-access
if hasattr(torch, '_logging'):
@@ -206,7 +205,12 @@ def compile_upscaler(model):
shared.log.error(f"Torch inductor config error: {e}")
t0 = time.time()
model = torch.compile(model, mode=shared.opts.cuda_compile_mode, backend=shared.opts.cuda_compile_backend, fullgraph=shared.opts.cuda_compile_fullgraph) # pylint: disable=attribute-defined-outside-init
model = torch.compile(model,
mode=shared.opts.cuda_compile_mode,
backend=shared.opts.cuda_compile_backend,
fullgraph=shared.opts.cuda_compile_fullgraph,
dynamic=None if shared.opts.cuda_compile_backend != "openvino_fx" else False,
) # pylint: disable=attribute-defined-outside-init
setup_logging() # compile messes with logging so reset is needed
t1 = time.time()
shared.log.info(f"Upscaler compile: time={t1-t0:.2f}")