Skip to content

Enable torch compile openvino backend in accuracy checker #3952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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 @@ -20,7 +20,7 @@
from collections import OrderedDict

import numpy as np
from ..config import PathField, StringField, DictField, NumberField, ListField
from ..config import PathField, StringField, DictField, NumberField, ListField, BoolField
from .launcher import Launcher

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

Expand All @@ -66,6 +68,13 @@ def __init__(self, config_entry: dict, *args, **kwargs):
import_error.msg)) from import_error
self._torch = torch
self.validate_config(config_entry)
self.is_openvino_backend = config_entry.get('use_openvino_backend')
if self.is_openvino_backend:
try:
import openvino.torch # pylint: disable=C0415
except ImportError as import_error:
raise ValueError("torch.compile is supported from OpenVINO 2023.1\n{}".format(
import_error.msg)) from import_error
module_args = config_entry.get("module_args", ())
module_kwargs = config_entry.get("module_kwargs", {})
self.device = self.get_value_from_config('device')
Expand Down Expand Up @@ -123,6 +132,9 @@ def load_module(self, model_cls, module_args, module_kwargs, checkpoint=None, st
module.load_state_dict(state, strict=False)
module.to(self.device)
module.eval()
if self.is_openvino_backend:
opts = {"device" : f"{self.device}"}
module = self._torch.compile(module, backend="openvino", options=opts)
return module

def fit_to_input(self, data, layer_name, layout, precision, template=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For enabling PyTorch launcher you need to add `framework: pytorch` in launchers
* `module_kwargs` - dictionary (`key`: `value` where `key` is argument name, `value` is argument value) which represent network module keyword arguments.
* `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).
* `batch` - batch size for running model (Optional, default 1).
* `use_openvino_backend` - use torch.compile feature with `openvino` backend (Optional, default `False`)

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