-
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
Merged
valentina-kustikova
merged 20 commits into
itlab-vision:master
from
ismukhin:tvm_inference
Nov 7, 2023
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
974ebc2
raw tvm inference
ismukhin b1cbed7
pytorch, onnx and time measurements
ismukhin d017099
Merge branch 'master' into tvm_inference
ismukhin e769268
template for tvm benchmark
ismukhin 9d48c08
some features
ismukhin 2006dd9
benchmark support
ismukhin 0ba6146
examination of None type for framework
ismukhin 72938be
fix
ismukhin d5afb7c
fix1
ismukhin f53df5d
Merge branch 'master' into tvm_inference
ismukhin 9fa26d0
support of onnx and pytorch(torchvision)
ismukhin 92e97b1
pytorch and codestyle fixes
ismukhin 5689413
fixes
ismukhin 02f6f21
fixes1
ismukhin 1ade071
fixes2
ismukhin 429b730
fixes3
ismukhin 581199a
fixes4
ismukhin d44ba48
fixes
ismukhin 463ec43
fixes1
ismukhin d68d22b
readme update
ismukhin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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_OPTIMIZATION_LEVEL = 'OptimizationLevel' | ||
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 | ||
_optimization_level = dep_parameters_tag.getElementsByTagName( | ||
CONFIG_FRAMEWORK_DEPENDENT_OPTIMIZATION_LEVEL)[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, | ||
optimization_level=_optimization_level.data if _optimization_level else None, | ||
) | ||
|
||
|
||
class TVMParameters(FrameworkParameters): | ||
def __init__(self, framework, input_name, input_shape, | ||
normalize, mean, std, channel_swap, optimization_level): | ||
self.framework = None | ||
self.input_name = None | ||
self.input_shape = None | ||
self.normalize = None | ||
self.mean = None | ||
self.std = None | ||
self.channel_swap = None | ||
self.optimization_level = 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 | ||
if self._parameter_is_not_none(optimization_level): | ||
self.optimization_level = optimization_level | ||
|
||
@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)}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} -is {input_shape} -b {batch_size} ' | ||
f'-ni {iteration} --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) | ||
|
||
opt_level = self._test.dep_parameters.optimization_level | ||
common_params = TVMProcess._add_optional_argument_to_cmd_line( | ||
common_params, '--opt_level', opt_level) | ||
|
||
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_script = Path.joinpath(self.inference_script_root, '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_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_script = Path.joinpath(self.inference_script_root, '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_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_script = Path.joinpath(self.inference_script_root, '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_script} {common_params}' | ||
|
||
return command_line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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}) | ||
parameters.update({'Optimization level': self.dep_parameters.optimization_level}) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
В основном readme в корне репозитория тоже надо добавить такую строчку.