Skip to content

Commit f094ea8

Browse files
authored
Enable torch compile openvino backend in accuracy checker (#3952)
* Enable openvino backend with pytorch_launcher.py * Update pytorch_launcher documentation * Add missing BoolField definition * Avoid W0611 pylint warning for openvino.torch import
1 parent e0d756e commit f094ea8

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

tools/accuracy_checker/accuracy_checker/launcher/pytorch_launcher.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from collections import OrderedDict
2121

2222
import numpy as np
23-
from ..config import PathField, StringField, DictField, NumberField, ListField
23+
from ..config import PathField, StringField, DictField, NumberField, ListField, BoolField
2424
from .launcher import Launcher
2525

2626
MODULE_REGEX = r'(?:\w+)(?:(?:.\w+)*)'
@@ -51,7 +51,9 @@ def parameters(cls):
5151
'batch': NumberField(value_type=int, min_value=1, optional=True, description="Batch size.", default=1),
5252
'output_names': ListField(
5353
optional=True, value_type=str, description='output tensor names'
54-
)
54+
),
55+
'use_openvino_backend': BoolField(
56+
optional=True, default=False, description='use torch.compile feature with openvino backend')
5557
})
5658
return parameters
5759

@@ -66,6 +68,13 @@ def __init__(self, config_entry: dict, *args, **kwargs):
6668
import_error.msg)) from import_error
6769
self._torch = torch
6870
self.validate_config(config_entry)
71+
self.is_openvino_backend = config_entry.get('use_openvino_backend')
72+
if self.is_openvino_backend:
73+
try:
74+
import openvino.torch # pylint: disable=C0415, W0611
75+
except ImportError as import_error:
76+
raise ValueError("torch.compile is supported from OpenVINO 2023.1\n{}".format(
77+
import_error.msg)) from import_error
6978
module_args = config_entry.get("module_args", ())
7079
module_kwargs = config_entry.get("module_kwargs", {})
7180
self.device = self.get_value_from_config('device')
@@ -123,6 +132,9 @@ def load_module(self, model_cls, module_args, module_kwargs, checkpoint=None, st
123132
module.load_state_dict(state, strict=False)
124133
module.to(self.device)
125134
module.eval()
135+
if self.is_openvino_backend:
136+
opts = {"device" : f"{self.device}"}
137+
module = self._torch.compile(module, backend="openvino", options=opts)
126138
return module
127139

128140
def fit_to_input(self, data, layer_name, layout, precision, template=None):

tools/accuracy_checker/accuracy_checker/launcher/pytorch_launcher_readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ For enabling PyTorch launcher you need to add `framework: pytorch` in launchers
1212
* `module_kwargs` - dictionary (`key`: `value` where `key` is argument name, `value` is argument value) which represent network module keyword arguments.
1313
* `adapter` - approach how raw output will be converted to representation of dataset problem, some adapters can be specific to framework. You can find detailed instruction how to use adapters [here](../adapters/README.md).
1414
* `batch` - batch size for running model (Optional, default 1).
15+
* `use_openvino_backend` - use torch.compile feature with `openvino` backend (Optional, default `False`)
1516

1617
In turn if you model has several inputs you need to specify them in config, using specific parameter: `inputs`.
1718
Each input description should has following info:

0 commit comments

Comments
 (0)