From 6e9fedba5d8b9614e297fed0b0211761eb7edb70 Mon Sep 17 00:00:00 2001 From: chenchao99 Date: Wed, 10 Jun 2020 11:30:01 +0800 Subject: [PATCH] add function of check whether the op name exists in framework --- mindinsight/profiler/analyser/analyser.py | 1 + .../profiler/analyser/base_analyser.py | 5 +++- .../profiler/common/exceptions/error_code.py | 2 ++ .../profiler/common/exceptions/exceptions.py | 11 ++++++++ .../profiler/parser/framework_parser.py | 25 ++++++++++++++++++- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/mindinsight/profiler/analyser/analyser.py b/mindinsight/profiler/analyser/analyser.py index 8dc8838..f805e76 100644 --- a/mindinsight/profiler/analyser/analyser.py +++ b/mindinsight/profiler/analyser/analyser.py @@ -93,6 +93,7 @@ class AicoreDetailAnalyser(BaseAnalyser): self._none_filter_condition_key = [ 'is_display_detail', 'is_display_full_op_name' ] + self._none_sort_col_names = ['op_info'] def query_and_sort_by_op_type(self, filter_condition, op_type_order: list): """ diff --git a/mindinsight/profiler/analyser/base_analyser.py b/mindinsight/profiler/analyser/base_analyser.py index bdc14df..c24b250 100644 --- a/mindinsight/profiler/analyser/base_analyser.py +++ b/mindinsight/profiler/analyser/base_analyser.py @@ -20,7 +20,7 @@ from marshmallow import ValidationError from mindinsight.profiler.common.exceptions.exceptions import \ ProfilerColumnNotExistException, ProfilerPathErrorException, \ - ProfilerIOException + ProfilerIOException, ProfilerColumnNotSupportSortException from mindinsight.profiler.common.log import logger from mindinsight.profiler.common.validator.validate_path import \ validate_and_normalize_path @@ -50,6 +50,7 @@ class BaseAnalyser(ABC): self._display_col_names = None self._size = 0 self._none_filter_condition_key = [] + self._none_sort_col_names = [] try: self._load() @@ -150,6 +151,8 @@ class BaseAnalyser(ABC): index = self.__col_names__.index(sort_name) except ValueError: raise ProfilerColumnNotExistException(sort_name) + if self._none_sort_col_names and sort_name in self._none_sort_col_names: + raise ProfilerColumnNotSupportSortException(sort_name) self._result.sort(key=functools.cmp_to_key(_cmp), reverse=reverse) def _group(self, group_condition: dict): diff --git a/mindinsight/profiler/common/exceptions/error_code.py b/mindinsight/profiler/common/exceptions/error_code.py index a74ca52..64360c2 100644 --- a/mindinsight/profiler/common/exceptions/error_code.py +++ b/mindinsight/profiler/common/exceptions/error_code.py @@ -46,6 +46,7 @@ class ProfilerErrors(ProfilerMgrErrors): GROUP_CONDITION_ERROR = 4 | _ANALYSER_MASK SORT_CONDITION_ERROR = 5 | _ANALYSER_MASK FILTER_CONDITION_ERROR = 6 | _ANALYSER_MASK + COLUMN_NOT_SUPPORT_SORT_ERROR = 7 | _ANALYSER_MASK @unique @@ -71,3 +72,4 @@ class ProfilerErrorMsg(Enum): OP_TYPE_ERROR = 'The op_type in search_condition error, {}' GROUP_CONDITION_ERROR = 'The group_condition in search_condition error, {}' SORT_CONDITION_ERROR = 'The sort_condition in search_condition error, {}' + COLUMN_NOT_SUPPORT_SORT_ERROR = 'The column {} does not support to sort.' diff --git a/mindinsight/profiler/common/exceptions/exceptions.py b/mindinsight/profiler/common/exceptions/exceptions.py index 86dced5..8502442 100644 --- a/mindinsight/profiler/common/exceptions/exceptions.py +++ b/mindinsight/profiler/common/exceptions/exceptions.py @@ -181,3 +181,14 @@ class ProfilerGroupConditionException(MindInsightException): message=ProfilerErrorMsg.GROUP_CONDITION_ERROR.value.format(msg), http_code=400 ) + + +class ProfilerColumnNotSupportSortException(MindInsightException): + """The column does not support to sort error in profiler module.""" + + def __init__(self, msg): + super(ProfilerColumnNotSupportSortException, self).__init__( + error=ProfilerErrors.COLUMN_NOT_SUPPORT_SORT_ERROR, + message=ProfilerErrorMsg.COLUMN_NOT_SUPPORT_SORT_ERROR.value.format(msg), + http_code=400 + ) diff --git a/mindinsight/profiler/parser/framework_parser.py b/mindinsight/profiler/parser/framework_parser.py index eb080de..d8331fa 100644 --- a/mindinsight/profiler/parser/framework_parser.py +++ b/mindinsight/profiler/parser/framework_parser.py @@ -24,7 +24,7 @@ from marshmallow import ValidationError from mindinsight.profiler.common.exceptions.exceptions import \ ProfilerPathErrorException, ProfilerDirNotFoundException, \ ProfilerFileNotFoundException, ProfilerDeviceIdMismatchException, \ - ProfilerRawFileException + ProfilerRawFileException, ProfilerParamValueErrorException from mindinsight.profiler.common.validator.validate_path import \ validate_and_normalize_path @@ -223,6 +223,29 @@ class FrameworkParser: self._parse_graph_files_and_save(self._task_cache) del self._task_cache + def check_op_name(self, op_name, is_prefix=True): + """ + Check whether the operator name exists. + + Args: + op_name (str): The operator name or operator name prefix. + is_prefix (bool): `True` if the op_name is prefix, else `False`. + Default: True. + + Returns: + bool, `True` if the operator name does exist in framework file, else + `False`. + """ + if not op_name: + raise ProfilerParamValueErrorException('The op_name should exist.') + for full_op_name in self._task_id_full_op_name_dict.values(): + if full_op_name: + if is_prefix and full_op_name.startswith(op_name): + return True + if not is_prefix and op_name == full_op_name: + return True + return False + def _get_raw_profiling_path(self, profiling_id): """ Get raw profiling path. -- GitLab