• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Minddata aicpu parser."""
16import os
17
18from mindspore.profiler.common.util import get_file_join_name, fwrite_format
19from mindspore import log as logger
20from mindspore.profiler.common.validator.validate_path import \
21    validate_and_normalize_path
22
23
24class MinddataParser:
25    """Minddata Aicpu Parser."""
26
27    @staticmethod
28    def parse_step_minddata_aicpu_data(one_step, result):
29        """
30        Parse step mind_data ai_cpu data.
31
32        Args:
33            one_step (str): The mind_data step info text, it is one of two structures.
34
35                Type queue: node_name,queue_size,run_start,run_end
36                Type run: node_name,run_start,run_end,queue_size
37
38            result ([[node_name, node_start, node_end, queue_size]]): Step info list.
39        """
40
41        if not one_step:
42            return
43        node_info = one_step.split(", ")
44        node_name, node_start, node_end, queue_size = "", 0, 0, 0
45        if node_info:
46            node_name = node_info[0].replace("Node:", "")
47
48        if len(node_info) > 3:
49            if "queue" in node_info[1]:
50                queue_size = node_info[1].replace("queue size:", "")
51                node_start = node_info[2].replace("Run start:", "")
52                node_end = node_info[3].replace("Run end:", "")
53            elif "Run" in node_info[1]:
54                queue_size = node_info[3].replace("queue size:", "")
55                node_start = node_info[1].replace("Run start:", "")
56                node_end = node_info[2].replace("Run end:", "")
57            queue_size = int(queue_size) if queue_size.isdigit() else queue_size
58            node_start = int(node_start) if node_start.isdigit() else node_start
59            node_end = int(node_end) if node_end.isdigit() else node_end
60
61        one_step_list = [node_name, node_start, node_end, queue_size]
62        result.append(one_step_list)
63
64    @staticmethod
65    def parse_minddata_aicpu_data(minddata_aicpu_source_path):
66        """
67        Parse minddata get_next info which contains queue size and execute time.
68
69        Args:
70            minddata_aicpu_source_path (str): the source file path.
71
72        Returns:
73            list[Union[str, float]], the converted data.
74        """
75        result = list()
76        try:
77            minddata_aicpu_source_path = validate_and_normalize_path(minddata_aicpu_source_path)
78            with open(minddata_aicpu_source_path) as source_data_file:
79                source_data = source_data_file.read()
80                step_data = source_data.split("\x00")
81                for one_step in step_data:
82                    MinddataParser.parse_step_minddata_aicpu_data(one_step, result)
83        except OSError:
84            logger.error("Open get_next profiling file error.")
85
86        return result
87
88    @staticmethod
89    def execute(source_path, output_path, device_id):
90        """
91        Execute the parser.
92
93        Args:
94            source_path (str): the source file path.
95            output_path (str): the output file path.
96            device_id (str): the device id.
97        """
98        col_names = ["node_name", "start_time", "end_time", "queue_size"]
99        source_path = validate_and_normalize_path(source_path)
100        minddata_aicpu_source_path = get_file_join_name(
101            input_path=source_path, file_name='DATA_PREPROCESS.AICPUMI')
102        if not minddata_aicpu_source_path:
103            minddata_aicpu_source_path = get_file_join_name(
104                input_path=source_path, file_name='DATA_PREPROCESS.dev.AICPUMI')
105            if not minddata_aicpu_source_path:
106                minddata_aicpu_source_path = get_file_join_name(
107                    input_path=os.path.join(source_path, "data"), file_name='DATA_PREPROCESS.AICPUMI')
108                if not minddata_aicpu_source_path:
109                    minddata_aicpu_source_path = get_file_join_name(
110                        input_path=os.path.join(source_path, "data"), file_name='DATA_PREPROCESS.dev.AICPUMI')
111                    if not minddata_aicpu_source_path:
112                        return
113        minddata_aicpu_output_path = os.path.join(output_path, "minddata_aicpu_" + device_id + ".txt")
114        minddata_aicpu_data = MinddataParser.parse_minddata_aicpu_data(minddata_aicpu_source_path)
115        if minddata_aicpu_data:
116            fwrite_format(minddata_aicpu_output_path, " ".join(col_names), is_start=True)
117            fwrite_format(minddata_aicpu_output_path, minddata_aicpu_data, is_start=True)
118