• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 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"""Record profiler information"""
16import json
17import os
18import stat
19
20from mindspore.version import __version__ as ms_version
21from mindspore.profiler.common.validator.validate_path import validate_and_normalize_path
22
23
24class ProfilerInfo:
25    """
26    This class is used to record profiler information.
27    it contains context_mode, rank_id, rank_size, parallel_mode, pipeline_stage_num, pipeline_stage_id,
28    profiling_start_time, profiling_stop_time, analyse_start_time, analyse_end_time
29    """
30
31    _file_name = "profiler_info_{}.json"
32    _file_path = ""
33    _profiler_info_dict = dict()
34
35    @staticmethod
36    def init_info(context_mode, rank_id):
37        """Profiler info initialization must include context_mode, rank_id and output_path."""
38        ProfilerInfo._profiler_info_dict["context_mode"] = context_mode
39        ProfilerInfo._profiler_info_dict["rank_id"] = rank_id
40        ProfilerInfo._profiler_info_dict["ms_version"] = ms_version
41        ProfilerInfo._file_name = ProfilerInfo._file_name.format(rank_id)
42
43    @staticmethod
44    def set_parallel_info(parallel_mode="", stage_num=1):
45        """Set parallel info include parallel_mode, pipeline_stage_num and pipeline_stage_id."""
46        info = dict()
47        info["parallel_mode"] = parallel_mode
48        info["stage_num"] = stage_num
49        ProfilerInfo._profiler_info_dict.update(info)
50
51    @staticmethod
52    def set_profiling_start_time(start_time):
53        """Set the profiling start time."""
54        info = dict()
55        info["profiling_start_time"] = start_time
56        ProfilerInfo._profiler_info_dict.update(info)
57
58    @staticmethod
59    def set_profiling_stop_time(stop_time):
60        """Set the profiling stop time."""
61        info = dict()
62        info["profiling_stop_time"] = stop_time
63        ProfilerInfo._profiler_info_dict.update(info)
64
65    @staticmethod
66    def set_analyse_start_time(start_time):
67        """Set the analyse start time."""
68        info = dict()
69        info["analyse_start_time"] = start_time
70        ProfilerInfo._profiler_info_dict.update(info)
71
72    @staticmethod
73    def set_analyse_end_time(end_time):
74        """Set the analyse end time."""
75        info = dict()
76        info["analyse_end_time"] = end_time
77        ProfilerInfo._profiler_info_dict.update(info)
78
79    @staticmethod
80    def set_export_start_time(start_time):
81        """Set the export start time."""
82        info = dict()
83        info["export_start_time"] = start_time
84        ProfilerInfo._profiler_info_dict.update(info)
85
86    @staticmethod
87    def set_export_end_time(end_time):
88        """Set the export end time."""
89        info = dict()
90        info["export_end_time"] = end_time
91        ProfilerInfo._profiler_info_dict.update(info)
92
93    @staticmethod
94    def set_export_flag(flag):
95        """Set whether all-export or not."""
96        ProfilerInfo._profiler_info_dict["all_export"] = flag
97
98    @staticmethod
99    def set_system_time(sys_time):
100        """Set system time."""
101        ProfilerInfo._profiler_info_dict["system_time"] = sys_time
102
103    @staticmethod
104    def set_system_cnt(sys_cnt):
105        """Set system cnt."""
106        ProfilerInfo._profiler_info_dict["system_cnt"] = sys_cnt
107
108    @staticmethod
109    def set_diff_time(diff_time):
110        """synchronize timestamps between different devices"""
111        ProfilerInfo._profiler_info_dict["diff_time"] = diff_time
112
113    @staticmethod
114    def set_graph_ids(graph_ids):
115        """Set the graph id list."""
116        ProfilerInfo._profiler_info_dict["graph_ids"] = graph_ids
117
118    @staticmethod
119    def set_rank_size(rank_size):
120        """Set the rank size."""
121        ProfilerInfo._profiler_info_dict["rank_size"] = rank_size
122
123    @staticmethod
124    def set_heterogeneous(is_heterogeneous):
125        """Set is it heterogeneous."""
126        ProfilerInfo._profiler_info_dict["is_heterogeneous"] = is_heterogeneous
127
128    @staticmethod
129    def get_profiler_info():
130        """Get the profiler info."""
131        return ProfilerInfo._profiler_info_dict
132
133    @staticmethod
134    def save(output_path):
135        """Save the profiler info to file."""
136        ProfilerInfo._file_path = os.path.join(output_path, ProfilerInfo._file_name)
137        ProfilerInfo._file_path = validate_and_normalize_path(ProfilerInfo._file_path)
138        with os.fdopen(os.open(ProfilerInfo._file_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600),
139                       'w') as json_file:
140            json.dump(ProfilerInfo._profiler_info_dict, json_file)
141        os.chmod(ProfilerInfo._file_path, stat.S_IREAD | stat.S_IWRITE)
142