-
Notifications
You must be signed in to change notification settings - Fork 38
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
[TVM] inference script #412
Changes from 17 commits
974ebc2
b1cbed7
d017099
e769268
9d48c08
2006dd9
0ba6146
72938be
d5afb7c
f53df5d
9fa26d0
92e97b1
5689413
02f6f21
1ade071
429b730
581199a
d44ba48
463ec43
d68d22b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
openvino-dev[caffe,mxnet,tensorflow2,pytorch,onnx]==2022.3.0 | ||
gluoncv | ||
torchvision | ||
onnxruntime | ||
onnxruntime | ||
apache-tvm==0.14.dev170 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from ..config_parser.dependent_parameters_parser import DependentParametersParser | ||
from ..config_parser.framework_parameters_parser import FrameworkParameters | ||
|
||
|
||
class TVMParametersParser(DependentParametersParser): | ||
def parse_parameters(self, curr_test): | ||
CONFIG_FRAMEWORK_DEPENDENT_TAG = 'FrameworkDependent' | ||
CONFIG_FRAMEWORK_DEPENDENT_NAME_OF_FRAMEWORK_TAG = 'Framework' | ||
CONFIG_FRAMEWORK_DEPENDENT_INPUT_NAME_TAG = 'InputName' | ||
CONFIG_FRAMEWORK_DEPENDENT_INPUT_SHAPE_TAG = 'InputShape' | ||
CONFIG_FRAMEWORK_DEPENDENT_NORMALIZE_TAG = 'Normalize' | ||
CONFIG_FRAMEWORK_DEPENDENT_MEAN_TAG = 'Mean' | ||
CONFIG_FRAMEWORK_DEPENDENT_STD_TAG = 'Std' | ||
CONFIG_FRAMEWORK_DEPENDENT_CHANNEL_SWAP_TAG = 'ChannelSwap' | ||
|
||
dep_parameters_tag = curr_test.getElementsByTagName(CONFIG_FRAMEWORK_DEPENDENT_TAG)[0] | ||
|
||
_framework = dep_parameters_tag.getElementsByTagName( | ||
CONFIG_FRAMEWORK_DEPENDENT_NAME_OF_FRAMEWORK_TAG)[0].firstChild | ||
_input_name = dep_parameters_tag.getElementsByTagName( | ||
CONFIG_FRAMEWORK_DEPENDENT_INPUT_NAME_TAG)[0].firstChild | ||
_input_shape = dep_parameters_tag.getElementsByTagName( | ||
CONFIG_FRAMEWORK_DEPENDENT_INPUT_SHAPE_TAG)[0].firstChild | ||
_normalize = dep_parameters_tag.getElementsByTagName( | ||
CONFIG_FRAMEWORK_DEPENDENT_NORMALIZE_TAG)[0].firstChild | ||
_mean = dep_parameters_tag.getElementsByTagName( | ||
CONFIG_FRAMEWORK_DEPENDENT_MEAN_TAG)[0].firstChild | ||
_std = dep_parameters_tag.getElementsByTagName( | ||
CONFIG_FRAMEWORK_DEPENDENT_STD_TAG)[0].firstChild | ||
_channel_swap = dep_parameters_tag.getElementsByTagName( | ||
CONFIG_FRAMEWORK_DEPENDENT_CHANNEL_SWAP_TAG)[0].firstChild | ||
|
||
return TVMParameters( | ||
framework=_framework.data if _framework else None, | ||
input_name=_input_name.data if _input_name else None, | ||
input_shape=_input_shape.data if _input_shape else None, | ||
normalize=_normalize.data if _normalize else None, | ||
mean=_mean.data if _mean else None, | ||
std=_std.data if _std else None, | ||
channel_swap=_channel_swap.data if _channel_swap else None, | ||
) | ||
|
||
|
||
class TVMParameters(FrameworkParameters): | ||
def __init__(self, framework, input_name, input_shape, | ||
normalize, mean, std, channel_swap): | ||
self.framework = None | ||
self.input_name = None | ||
self.input_shape = None | ||
self.normalize = None | ||
self.mean = None | ||
self.std = None | ||
self.channel_swap = None | ||
|
||
if self._framework_is_correct(framework): | ||
self.framework = framework | ||
if self._parameter_is_not_none(input_name): | ||
self.input_name = input_name | ||
if self._parameter_is_not_none(input_shape): | ||
self.input_shape = input_shape | ||
if self._parameter_is_not_none(normalize): | ||
self.normalize = normalize | ||
if self._parameter_is_not_none(mean): | ||
self.mean = mean | ||
if self._parameter_is_not_none(std): | ||
self.std = std | ||
if self._parameter_is_not_none(channel_swap): | ||
self.channel_swap = channel_swap | ||
|
||
@staticmethod | ||
def _framework_is_correct(framework): | ||
correct_frameworks = ['mxnet', 'onnx', 'tvm', | ||
'tf', 'tflite', 'pytorch'] | ||
if framework.lower() in correct_frameworks: | ||
return True | ||
else: | ||
raise ValueError(f'Framework is required parameter. TVM support: {", ".join(correct_frameworks)}') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from pathlib import Path | ||
|
||
from ..processes import ProcessHandler | ||
|
||
|
||
class TVMProcess(ProcessHandler): | ||
benchmark_app_name = 'tvm_python_benchmark' | ||
launcher_latency_units = 'seconds' | ||
|
||
def __init__(self, test, executor, log): | ||
super().__init__(test, executor, log) | ||
|
||
@staticmethod | ||
def create_process(test, executor, log): | ||
framework = test.dep_parameters.framework | ||
if framework is None: | ||
framework = 'TVM' | ||
return TVMProcessMXNetFormat(test, executor, log) | ||
else: | ||
framework = test.dep_parameters.framework.lower() | ||
if framework == 'mxnet': | ||
return TVMProcessMXNetFormat(test, executor, log) | ||
elif framework == 'pytorch': | ||
return TVMProcessPyTorchFormat(test, executor, log) | ||
elif framework == 'onnx': | ||
return TVMProcessONNXFormat(test, executor, log) | ||
else: | ||
raise AssertionError(f'Unknown framework {framework}') | ||
|
||
def get_performance_metrics(self): | ||
return self.get_performance_metrics_from_json_report() | ||
|
||
def _fill_command_line(self): | ||
dataset = self._test.dataset.path | ||
input_shape = self._test.dep_parameters.input_shape | ||
batch_size = self._test.indep_parameters.batch_size | ||
iteration = self._test.indep_parameters.iteration | ||
|
||
common_params = (f'-i {dataset} ' | ||
f'-is {input_shape} -b {batch_size} -ni {iteration} ' | ||
f'--report_path {self.report_path}') | ||
|
||
input_name = self._test.dep_parameters.input_name | ||
common_params = TVMProcess._add_optional_argument_to_cmd_line( | ||
common_params, '--input_name', input_name) | ||
|
||
normalize = self._test.dep_parameters.normalize | ||
if normalize == 'True': | ||
common_params = TVMProcess._add_flag_to_cmd_line( | ||
common_params, '--norm') | ||
|
||
mean = self._test.dep_parameters.mean | ||
common_params = TVMProcess._add_optional_argument_to_cmd_line( | ||
common_params, '--mean', mean) | ||
|
||
std = self._test.dep_parameters.std | ||
common_params = TVMProcess._add_optional_argument_to_cmd_line( | ||
common_params, '--std', std) | ||
|
||
channel_swap = self._test.dep_parameters.channel_swap | ||
common_params = TVMProcess._add_optional_argument_to_cmd_line( | ||
common_params, '--channel_swap', channel_swap) | ||
|
||
device = self._test.indep_parameters.device | ||
common_params = TVMProcess._add_optional_argument_to_cmd_line( | ||
common_params, '--device', device) | ||
|
||
return f'{common_params}' | ||
|
||
|
||
class TVMProcessMXNetFormat(TVMProcess): | ||
def __init__(self, test, executor, log): | ||
super().__init__(test, executor, log) | ||
|
||
def get_performance_metrics(self): | ||
return self.get_performance_metrics_from_json_report() | ||
|
||
def _fill_command_line(self): | ||
name = self._test.model.name | ||
model_json = self._test.model.model | ||
model_params = self._test.model.weight | ||
if ((name is not None) | ||
and (model_json is None or model_json == '') | ||
and (model_params is None or model_params == '')): | ||
common_params = (f'-mn {name} ') | ||
elif (model_json is not None) and (model_params is not None): | ||
common_params = (f'-m {model_json} -w {model_params} ') | ||
else: | ||
raise Exception('Incorrect model parameters. Set model name or file names.') | ||
path_to_sync_script = Path.joinpath(self.inference_script_root, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше переименовать в |
||
'inference_tvm_mxnet.py') | ||
python = ProcessHandler.get_cmd_python_version() | ||
time_limit = self._test.indep_parameters.test_time_limit | ||
common_params += super()._fill_command_line() | ||
common_params += f' --time {time_limit}' | ||
command_line = f'{python} {path_to_sync_script} {common_params}' | ||
|
||
return command_line | ||
|
||
|
||
class TVMProcessPyTorchFormat(TVMProcess): | ||
def __init__(self, test, executor, log): | ||
super().__init__(test, executor, log) | ||
|
||
def get_performance_metrics(self): | ||
return self.get_performance_metrics_from_json_report() | ||
|
||
def _fill_command_line(self): | ||
name = self._test.model.name | ||
model_json = self._test.model.model | ||
model_params = self._test.model.weight | ||
if ((name is not None) | ||
and (model_json is None or model_json == '') | ||
and (model_params is None or model_params == '')): | ||
common_params = (f'-mn {name} ') | ||
elif (model_json is not None) and (model_params is not None): | ||
common_params = (f'-m {model_json} -w {model_params} ') | ||
else: | ||
raise Exception('Incorrect model parameters. Set model name or file names.') | ||
path_to_sync_script = Path.joinpath(self.inference_script_root, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше переименовать в path_to_script, поскольку здесь явно режимов нет. |
||
'inference_tvm_pytorch.py') | ||
python = ProcessHandler.get_cmd_python_version() | ||
time_limit = self._test.indep_parameters.test_time_limit | ||
common_params += super()._fill_command_line() | ||
common_params += f' --time {time_limit}' | ||
command_line = f'{python} {path_to_sync_script} {common_params}' | ||
|
||
return command_line | ||
|
||
|
||
class TVMProcessONNXFormat(TVMProcess): | ||
def __init__(self, test, executor, log): | ||
super().__init__(test, executor, log) | ||
|
||
def get_performance_metrics(self): | ||
return self.get_performance_metrics_from_json_report() | ||
|
||
def _fill_command_line(self): | ||
model = self._test.model.model | ||
common_params = f'-m {model} ' | ||
path_to_sync_script = Path.joinpath(self.inference_script_root, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше переименовать в path_to_script, поскольку здесь явно режимов нет. |
||
'inference_tvm_onnx.py') | ||
python = ProcessHandler.get_cmd_python_version() | ||
time_limit = self._test.indep_parameters.test_time_limit | ||
common_params += super()._fill_command_line() | ||
common_params += f' --time {time_limit}' | ||
command_line = f'{python} {path_to_sync_script} {common_params}' | ||
|
||
return command_line |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from collections import OrderedDict | ||
|
||
from ..config_parser.test_reporter import Test | ||
|
||
|
||
class TVMTest(Test): | ||
def __init__(self, model, dataset, indep_parameters, dep_parameters): | ||
super().__init__(model, dataset, indep_parameters, dep_parameters) | ||
|
||
def get_report(self, process): | ||
parameters = OrderedDict() | ||
parameters.update({'Device': self.indep_parameters.device}) | ||
parameters.update({'Iteration count': self.indep_parameters.iteration}) | ||
parameters.update({'Framework': self.dep_parameters.framework}) | ||
other_param = self._get_optional_parameters_string(parameters) | ||
|
||
report_res = { | ||
'task': self.model.task, | ||
'model': self.model.name, | ||
'dataset': self.dataset.name, | ||
'source_framework': self.model.source_framework, | ||
'inference_framework': self.indep_parameters.inference_framework, | ||
'precision': self.model.precision, | ||
'batch_size': self.indep_parameters.batch_size, | ||
'mode': 'Sync', | ||
'framework_params': other_param, | ||
} | ||
|
||
return report_res |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from .tvm_process import TVMProcess | ||
from .tvm_test import TVMTest | ||
from ..framework_wrapper import FrameworkWrapper | ||
from ..known_frameworks import KnownFrameworks | ||
|
||
|
||
class TVMWrapper(FrameworkWrapper): | ||
framework_name = KnownFrameworks.tvm | ||
|
||
@staticmethod | ||
def create_process(test, executor, log, **kwargs): | ||
return TVMProcess.create_process(test, executor, log) | ||
|
||
@staticmethod | ||
def create_test(model, dataset, indep_parameters, dep_parameters): | ||
return TVMTest(model, dataset, indep_parameters, dep_parameters) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше в 2 строки написать, а не 3.