mindstudio-probe 1.2.1__py3-none-any.whl → 1.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.2.2.dist-info}/METADATA +1 -1
- {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.2.2.dist-info}/RECORD +85 -66
- msprobe/README.md +2 -2
- msprobe/core/common/const.py +34 -9
- msprobe/core/common/inplace_ops.yaml +1 -0
- msprobe/core/common/utils.py +14 -0
- msprobe/core/compare/layer_mapping/data_scope_parser.py +1 -1
- msprobe/core/compare/merge_result/merge_result.py +8 -7
- msprobe/core/compare/merge_result/utils.py +81 -0
- msprobe/core/compare/utils.py +10 -0
- msprobe/core/data_dump/data_collector.py +58 -13
- msprobe/core/data_dump/data_processor/base.py +92 -8
- msprobe/core/data_dump/data_processor/factory.py +3 -0
- msprobe/core/data_dump/data_processor/mindspore_processor.py +17 -4
- msprobe/core/data_dump/data_processor/pytorch_processor.py +58 -7
- msprobe/core/data_dump/json_writer.py +26 -8
- msprobe/docs/01.installation.md +25 -0
- msprobe/docs/02.config_introduction.md +14 -12
- msprobe/docs/03.config_examples.md +24 -0
- msprobe/docs/05.data_dump_PyTorch.md +34 -15
- msprobe/docs/06.data_dump_MindSpore.md +45 -22
- msprobe/docs/09.accuracy_checker_MindSpore.md +4 -2
- msprobe/docs/19.monitor.md +257 -260
- msprobe/docs/21.visualization_PyTorch.md +10 -0
- msprobe/docs/22.visualization_MindSpore.md +11 -0
- msprobe/docs/27.dump_json_instruction.md +24 -20
- msprobe/docs/28.debugger_save_instruction.md +94 -0
- msprobe/docs/28.kernel_dump_MindSpore.md +69 -0
- msprobe/docs/img/monitor/step_count_per_record.png +0 -0
- msprobe/mindspore/__init__.py +1 -0
- msprobe/mindspore/api_accuracy_checker/api_accuracy_checker.py +26 -6
- msprobe/mindspore/api_accuracy_checker/api_runner.py +54 -16
- msprobe/mindspore/api_accuracy_checker/compute_element.py +47 -1
- msprobe/mindspore/api_accuracy_checker/torch_mindtorch_importer.py +129 -0
- msprobe/mindspore/api_accuracy_checker/type_mapping.py +24 -1
- msprobe/mindspore/api_accuracy_checker/utils.py +6 -1
- msprobe/mindspore/common/utils.py +20 -2
- msprobe/mindspore/debugger/debugger_config.py +25 -2
- msprobe/mindspore/debugger/precision_debugger.py +25 -6
- msprobe/mindspore/dump/hook_cell/api_registry.py +2 -0
- msprobe/mindspore/dump/jit_dump.py +7 -6
- msprobe/mindspore/monitor/anomaly_detect.py +404 -0
- msprobe/mindspore/monitor/distributed/__init__.py +0 -0
- msprobe/mindspore/monitor/distributed/distributed_ops.yaml +15 -0
- msprobe/mindspore/monitor/distributed/stack_blacklist.yaml +5 -0
- msprobe/mindspore/monitor/distributed/wrap_distributed.py +300 -0
- msprobe/mindspore/monitor/features.py +63 -0
- msprobe/mindspore/monitor/module_hook.py +821 -0
- msprobe/mindspore/monitor/module_spec_verifier.py +94 -0
- msprobe/mindspore/monitor/utils.py +267 -0
- msprobe/mindspore/ms_config.py +8 -2
- msprobe/mindspore/service.py +95 -21
- msprobe/pytorch/__init__.py +0 -1
- msprobe/pytorch/api_accuracy_checker/generate_op_script/op_generator.py +1 -1
- msprobe/pytorch/bench_functions/apply_adam.py +215 -0
- msprobe/pytorch/bench_functions/group_norm_silu.py +27 -0
- msprobe/pytorch/bench_functions/mish.py +21 -0
- msprobe/pytorch/bench_functions/moe_gating_top_k_softmax.py +44 -0
- msprobe/pytorch/bench_functions/sort_v2.py +21 -0
- msprobe/pytorch/common/utils.py +71 -0
- msprobe/pytorch/debugger/debugger_config.py +19 -9
- msprobe/pytorch/debugger/precision_debugger.py +14 -0
- msprobe/pytorch/dump/module_dump/module_processer.py +10 -30
- msprobe/pytorch/function_factory.py +7 -1
- msprobe/pytorch/hook_module/support_wrap_ops.yaml +2 -1
- msprobe/pytorch/hook_module/wrap_distributed.py +4 -0
- msprobe/pytorch/monitor/anomaly_detect.py +14 -29
- msprobe/pytorch/monitor/csv2tb.py +10 -12
- msprobe/pytorch/monitor/module_hook.py +123 -104
- msprobe/pytorch/monitor/module_metric.py +6 -6
- msprobe/pytorch/monitor/optimizer_collect.py +45 -63
- msprobe/pytorch/monitor/utils.py +8 -43
- msprobe/pytorch/pt_config.py +19 -22
- msprobe/pytorch/service.py +103 -24
- msprobe/visualization/builder/graph_builder.py +31 -5
- msprobe/visualization/builder/msprobe_adapter.py +7 -5
- msprobe/visualization/graph/base_node.py +3 -2
- msprobe/visualization/graph/distributed_analyzer.py +80 -3
- msprobe/visualization/graph/node_op.py +4 -2
- msprobe/visualization/graph_service.py +3 -4
- msprobe/visualization/utils.py +10 -2
- {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.2.2.dist-info}/LICENSE +0 -0
- {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.2.2.dist-info}/WHEEL +0 -0
- {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.2.2.dist-info}/entry_points.txt +0 -0
- {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.2.2.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
# Copyright (c) 2024-2025, Huawei Technologies Co., Ltd.
|
|
2
|
+
# All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
import itertools
|
|
17
|
+
import os
|
|
18
|
+
import sys
|
|
19
|
+
import statistics as st
|
|
20
|
+
from abc import ABC
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
from typing import List
|
|
23
|
+
from collections import defaultdict
|
|
24
|
+
|
|
25
|
+
import pandas as pd
|
|
26
|
+
|
|
27
|
+
from mindspore import ops
|
|
28
|
+
from mindspore import _no_grad
|
|
29
|
+
from msprobe.core.common.log import logger
|
|
30
|
+
from msprobe.core.common.file_utils import change_mode, create_directory, write_df_to_csv
|
|
31
|
+
from msprobe.core.common.const import FileCheckConst, MonitorConst
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ScanRule(ABC):
|
|
35
|
+
name = "ScanRule"
|
|
36
|
+
|
|
37
|
+
def apply(self, history, cur):
|
|
38
|
+
raise NotImplementedError("abstract method apply is not implemented")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class AnomalyTurbulence(ScanRule):
|
|
42
|
+
name = "AnomalyTurbulence"
|
|
43
|
+
|
|
44
|
+
def __init__(self, threshold) -> None:
|
|
45
|
+
self.threshold = threshold
|
|
46
|
+
|
|
47
|
+
def apply(self, history, cur):
|
|
48
|
+
baseline = st.mean(history) if isinstance(history, list) else history
|
|
49
|
+
|
|
50
|
+
up_bound = baseline + baseline * self.threshold
|
|
51
|
+
if baseline > 0:
|
|
52
|
+
return cur > up_bound
|
|
53
|
+
else:
|
|
54
|
+
return cur < up_bound
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class AnomalyScanner:
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def load_rules(specs: List[dict]):
|
|
61
|
+
"""
|
|
62
|
+
specs: [{"rule_name": "AnomalyTurbulence", "args": {"threshold": 0.5}}]
|
|
63
|
+
"""
|
|
64
|
+
if specs is None:
|
|
65
|
+
return []
|
|
66
|
+
alert_rules = []
|
|
67
|
+
for spec in specs:
|
|
68
|
+
# 使用get方法获取键值,如果键不存在则返回None
|
|
69
|
+
rule_cls_name = spec.get("rule_name")
|
|
70
|
+
rule_args = spec.get("args")
|
|
71
|
+
|
|
72
|
+
# 检查必要的键是否存在
|
|
73
|
+
if rule_cls_name is None or rule_args is None:
|
|
74
|
+
logger.warning(f"Spec is missing required keys: {spec}")
|
|
75
|
+
continue
|
|
76
|
+
|
|
77
|
+
cur_module = sys.modules.get(__name__)
|
|
78
|
+
try:
|
|
79
|
+
rule_cls = getattr(cur_module, rule_cls_name)
|
|
80
|
+
except AttributeError:
|
|
81
|
+
logger.error(f"Rule class '{rule_cls_name}' not found in the current module.")
|
|
82
|
+
continue
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
rule_instance = rule_cls(**rule_args)
|
|
86
|
+
alert_rules.append(rule_instance)
|
|
87
|
+
except Exception as e:
|
|
88
|
+
logger.error(f"Error creating instance of rule '{rule_cls_name}': {e}")
|
|
89
|
+
continue
|
|
90
|
+
|
|
91
|
+
return alert_rules
|
|
92
|
+
|
|
93
|
+
@staticmethod
|
|
94
|
+
def scan(scan_rules: List[ScanRule], history, cur):
|
|
95
|
+
anomaly = False
|
|
96
|
+
for rule in scan_rules:
|
|
97
|
+
anomaly = rule.apply(history, cur)
|
|
98
|
+
if anomaly:
|
|
99
|
+
return anomaly, rule.name
|
|
100
|
+
return anomaly, None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class BCOLORS:
|
|
104
|
+
HEADER = '\033[95m'
|
|
105
|
+
OKBLUE = '\033[94m'
|
|
106
|
+
OKCYAN = '\033[96m'
|
|
107
|
+
OKGREEN = '\033[92m'
|
|
108
|
+
WARNING = '\033[93m'
|
|
109
|
+
FAIL = '\033[91m'
|
|
110
|
+
ENDC = '\033[0m'
|
|
111
|
+
BOLD = '\033[1m'
|
|
112
|
+
UNDERLINE = '\033[4m'
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class AnomalyDataFactory(ABC):
|
|
116
|
+
def __init__(self, rank, pp_stage, group_mates):
|
|
117
|
+
super().__init__()
|
|
118
|
+
self.rank = rank
|
|
119
|
+
self.pp_stage = pp_stage
|
|
120
|
+
self.group_mates = group_mates
|
|
121
|
+
self.micro_step = 0
|
|
122
|
+
self.name2callid = {}
|
|
123
|
+
|
|
124
|
+
def set_call_id(self, name2callid):
|
|
125
|
+
"""根据当前GradContext信息更新call_id vpp_stage等信息
|
|
126
|
+
"""
|
|
127
|
+
self.name2callid = name2callid
|
|
128
|
+
|
|
129
|
+
def create(self, tag, message, step):
|
|
130
|
+
"""如果检查出异常, 调用当前接口生成GradAnomalyData实例
|
|
131
|
+
tag (tuple): metric tag ('0:1.post_attention_norm.weight/rank0/pre_grad', 'min')
|
|
132
|
+
message (str): anomaly detect message
|
|
133
|
+
step (int): training step
|
|
134
|
+
"""
|
|
135
|
+
if not isinstance(tag, tuple) or len(tag) != 2:
|
|
136
|
+
raise ValueError("tag must be a tuple with length 2")
|
|
137
|
+
tag_name = tag[0]
|
|
138
|
+
param_name = tag_name.split('/')[0]
|
|
139
|
+
call_id = self.name2callid.get(tag_name, -1)
|
|
140
|
+
if MonitorConst.NAME_SEP in param_name:
|
|
141
|
+
vpp_stage = int(param_name.split(MonitorConst.NAME_SEP)[0])
|
|
142
|
+
else:
|
|
143
|
+
vpp_stage = 0
|
|
144
|
+
|
|
145
|
+
return GradAnomalyData(
|
|
146
|
+
self.rank,
|
|
147
|
+
step,
|
|
148
|
+
self.micro_step,
|
|
149
|
+
self.pp_stage,
|
|
150
|
+
vpp_stage,
|
|
151
|
+
call_id,
|
|
152
|
+
tag_name,
|
|
153
|
+
message,
|
|
154
|
+
self.group_mates
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
class TrainStage:
|
|
159
|
+
DEFAULT_STAGE = -1
|
|
160
|
+
FORWARD_STAGE = 0
|
|
161
|
+
BACKWARD_STAGE = 1
|
|
162
|
+
OPTIMIZER_STAGE = 2
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
FORWARD_KEY = [MonitorConst.ACTV_IN, MonitorConst.ACTV_OUT]
|
|
166
|
+
BACKWARD_KEY = [MonitorConst.ACTVGRAD_IN, MonitorConst.ACTVGRAD_OUT,
|
|
167
|
+
MonitorConst.PRE_GRAD, MonitorConst.POST_GRAD, MonitorConst.ACC_GRAD]
|
|
168
|
+
OPTIMIZER_KEY = [MonitorConst.EXP_AVG, MonitorConst.EXP_AVG_SQ]
|
|
169
|
+
TRAIN_STAGE = {
|
|
170
|
+
**{key_: TrainStage.FORWARD_STAGE for key_ in FORWARD_KEY},
|
|
171
|
+
**{key_: TrainStage.BACKWARD_STAGE for key_ in BACKWARD_KEY},
|
|
172
|
+
**{key_: TrainStage.OPTIMIZER_STAGE for key_ in OPTIMIZER_KEY}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@dataclass(eq=True)
|
|
177
|
+
class GradAnomalyData:
|
|
178
|
+
rank: int = 0
|
|
179
|
+
step: int = 0
|
|
180
|
+
micro_step: int = 0
|
|
181
|
+
pp_stage: int = 0
|
|
182
|
+
vpp_stage: int = 0
|
|
183
|
+
call_id: int = 0
|
|
184
|
+
tag_name: str = field(default=None, compare=False)
|
|
185
|
+
message: str = field(default="", compare=False)
|
|
186
|
+
group_mates: list = field(default=None, compare=False)
|
|
187
|
+
|
|
188
|
+
def __lt__(self, other):
|
|
189
|
+
"""
|
|
190
|
+
自定义比较函数,用于确定 GradAnomalyData 实例之间的顺序。
|
|
191
|
+
比较规则为:
|
|
192
|
+
step 和 micro_step 值越小优先级越高;
|
|
193
|
+
vpp 和 pp 在前向阶段值越小优先级越高,在非前向阶段值越大优先级越高;
|
|
194
|
+
call_id 值越小优先级越高。
|
|
195
|
+
"""
|
|
196
|
+
if not isinstance(other, GradAnomalyData):
|
|
197
|
+
return NotImplemented
|
|
198
|
+
|
|
199
|
+
self_train_stage = self.get_train_stage(self.tag_name)
|
|
200
|
+
other_train_stage = self.get_train_stage(other.tag_name)
|
|
201
|
+
|
|
202
|
+
def vpp_pp_comparator(anomaly):
|
|
203
|
+
"""
|
|
204
|
+
Determine the priority rule for vpp and pp based on train stage
|
|
205
|
+
Forward stage prefers smaller vpp and pp
|
|
206
|
+
Other stages prefer larger vpp and pp
|
|
207
|
+
"""
|
|
208
|
+
if self_train_stage == TrainStage.FORWARD_STAGE:
|
|
209
|
+
return anomaly.vpp_stage, anomaly.pp_stage
|
|
210
|
+
else:
|
|
211
|
+
return -anomaly.vpp_stage, -anomaly.pp_stage
|
|
212
|
+
|
|
213
|
+
self_cmp = [self.step, self.micro_step, self_train_stage, *vpp_pp_comparator(self), self.call_id]
|
|
214
|
+
other_cmp = [other.step, other.micro_step, other_train_stage, *vpp_pp_comparator(other), other.call_id]
|
|
215
|
+
return self_cmp < other_cmp
|
|
216
|
+
|
|
217
|
+
def __le__(self, other):
|
|
218
|
+
if not isinstance(other, GradAnomalyData):
|
|
219
|
+
return NotImplemented
|
|
220
|
+
return self == other or self < other
|
|
221
|
+
|
|
222
|
+
@staticmethod
|
|
223
|
+
def get_train_stage(tag_name):
|
|
224
|
+
"""
|
|
225
|
+
:param tag_name: "0:fc2_0/rank0/input", "0:fc1.weight/rank0/post_grad", "0:fc2.weight/rank0/exp_avg_sq"
|
|
226
|
+
:return: int, if forward return 0; if backward return 1; if optimizer return 2
|
|
227
|
+
"""
|
|
228
|
+
key_ = tag_name.split("/")[-1]
|
|
229
|
+
return TRAIN_STAGE.get(key_, TrainStage.DEFAULT_STAGE)
|
|
230
|
+
|
|
231
|
+
def to_dict(self):
|
|
232
|
+
return self.__dict__
|
|
233
|
+
|
|
234
|
+
def get_key(self):
|
|
235
|
+
# 0:1.self_attention.core_attention_flash_0/rank0/input_grad
|
|
236
|
+
return ''.join([str(self.tag_name), "_step_", str(self.step), "_call_", str(self.call_id)])
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
@dataclass
|
|
240
|
+
class WriterInput:
|
|
241
|
+
path: str
|
|
242
|
+
ad_rules: list
|
|
243
|
+
job_id: str
|
|
244
|
+
anomaly_factory: AnomalyDataFactory = None
|
|
245
|
+
ndigits: int = 6
|
|
246
|
+
step_count_per_record: int = 1
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
class BaseWriterWithAD:
|
|
250
|
+
def __init__(self, writer_input: WriterInput):
|
|
251
|
+
self.tag2scalars = {}
|
|
252
|
+
self.ad_rules = writer_input.ad_rules
|
|
253
|
+
self.job_id = writer_input.job_id
|
|
254
|
+
self.anomaly_factory = writer_input.anomaly_factory
|
|
255
|
+
self.anomalies = []
|
|
256
|
+
self.ndigits = writer_input.ndigits
|
|
257
|
+
|
|
258
|
+
def get_anomalies(self):
|
|
259
|
+
"""返回已检测到的异常列表
|
|
260
|
+
"""
|
|
261
|
+
return self.anomalies
|
|
262
|
+
|
|
263
|
+
def clear_anomalies(self):
|
|
264
|
+
self.anomalies.clear()
|
|
265
|
+
|
|
266
|
+
def add_scalar(self, tag, scalar_value, global_step=None, need_explain=False):
|
|
267
|
+
"""If an anomaly is detected, the anomaly information is recorded and added to self.anomalies.
|
|
268
|
+
Args:
|
|
269
|
+
tag (tuple): tuple of tag_name and tag like ('0:1.post_attention_norm.weight/rank0/pre_grad', 'min').
|
|
270
|
+
scalar_value (float): scalar_value.
|
|
271
|
+
global_step (int): global_step.
|
|
272
|
+
Returns:
|
|
273
|
+
None
|
|
274
|
+
"""
|
|
275
|
+
detected = False
|
|
276
|
+
if self.ad_rules:
|
|
277
|
+
avg = self._update_tag2scalars(tag, scalar_value)
|
|
278
|
+
detected, rule_name = self._ad(scalar_value, history=avg)
|
|
279
|
+
if detected:
|
|
280
|
+
exception_message = f"Rule {rule_name} reports anomaly signal in {tag} at step {global_step}."
|
|
281
|
+
logger.info(f"{BCOLORS.WARNING}> {exception_message}{BCOLORS.ENDC}")
|
|
282
|
+
# append to self.anomalies for dump
|
|
283
|
+
if self.anomaly_factory:
|
|
284
|
+
self.anomalies.append(self.anomaly_factory.create(tag, exception_message, global_step))
|
|
285
|
+
|
|
286
|
+
def write_metrics(self, op_list, metric_value, step, prefix='', need_explain=False):
|
|
287
|
+
if not metric_value:
|
|
288
|
+
return
|
|
289
|
+
tensors = []
|
|
290
|
+
tags = list(itertools.product(metric_value.keys(), op_list))
|
|
291
|
+
for op2tensor in metric_value.values():
|
|
292
|
+
tensors.extend(op2tensor.values())
|
|
293
|
+
with _no_grad():
|
|
294
|
+
metric_list = ops.stack(tensors).tolist() if tensors else []
|
|
295
|
+
for tag, metric in zip(tags, metric_list):
|
|
296
|
+
self.add_scalar(tag, metric, step, need_explain)
|
|
297
|
+
|
|
298
|
+
def _ad(self, scalar_value, history):
|
|
299
|
+
return AnomalyScanner.scan(self.ad_rules, history, cur=scalar_value)
|
|
300
|
+
|
|
301
|
+
def _update_tag2scalars(self, tag, scalar_value):
|
|
302
|
+
"""Update the average and count of a scalar value associated with a tag.
|
|
303
|
+
|
|
304
|
+
This method is used to maintain a running average of scalar values for each tag.
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
Args:
|
|
308
|
+
tag (str): The tag identifier.
|
|
309
|
+
scalar_value (float): The scalar value to be added.
|
|
310
|
+
|
|
311
|
+
Returns:
|
|
312
|
+
float: The average value before update.
|
|
313
|
+
"""
|
|
314
|
+
if tag not in self.tag2scalars:
|
|
315
|
+
self.tag2scalars[tag] = {'avg': scalar_value, 'count': 0}
|
|
316
|
+
avg = self.tag2scalars[tag]['avg']
|
|
317
|
+
new_avg = (avg * self.tag2scalars[tag]['count'] + scalar_value) / (self.tag2scalars[tag]['count'] + 1)
|
|
318
|
+
self.tag2scalars[tag]['avg'] = new_avg
|
|
319
|
+
self.tag2scalars[tag]['count'] += 1
|
|
320
|
+
return avg
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
class CSVWriterWithAD(BaseWriterWithAD):
|
|
324
|
+
def __init__(self, writer_input: WriterInput):
|
|
325
|
+
super().__init__(writer_input)
|
|
326
|
+
|
|
327
|
+
path = writer_input.path
|
|
328
|
+
self.log_dir = path
|
|
329
|
+
create_directory(path)
|
|
330
|
+
change_mode(path, FileCheckConst.DATA_DIR_AUTHORITY)
|
|
331
|
+
self.context_dict = defaultdict(list)
|
|
332
|
+
self.header = []
|
|
333
|
+
self.step_count_per_record = writer_input.step_count_per_record
|
|
334
|
+
|
|
335
|
+
def get_step_interval(self, step):
|
|
336
|
+
count = step // self.step_count_per_record
|
|
337
|
+
return count * self.step_count_per_record, (count + 1) * self.step_count_per_record - 1
|
|
338
|
+
|
|
339
|
+
def write_csv(self, prefix, step):
|
|
340
|
+
"""
|
|
341
|
+
Args:
|
|
342
|
+
prefix[str]: prefix of output csv file e.g. grad_unreduced
|
|
343
|
+
step[int]
|
|
344
|
+
"""
|
|
345
|
+
if len(self.context_dict) == 0:
|
|
346
|
+
return
|
|
347
|
+
|
|
348
|
+
ster_start, step_end = self.get_step_interval(step)
|
|
349
|
+
filepath = os.path.join(self.log_dir, f'{prefix}_{ster_start}-{step_end}.csv')
|
|
350
|
+
if not os.path.exists(filepath):
|
|
351
|
+
data_frame = pd.DataFrame(columns=self.header)
|
|
352
|
+
write_df_to_csv(data_frame, filepath)
|
|
353
|
+
|
|
354
|
+
new_data = []
|
|
355
|
+
for name, metric_value in self.context_dict.items():
|
|
356
|
+
if MonitorConst.NAME_SEP not in name:
|
|
357
|
+
new_data.append([name] + [step] + metric_value)
|
|
358
|
+
else:
|
|
359
|
+
new_data.append(name.split(MonitorConst.NAME_SEP) + [step] + metric_value)
|
|
360
|
+
new_data = pd.DataFrame(new_data).round(self.ndigits)
|
|
361
|
+
write_df_to_csv(new_data, filepath, mode='a+', header=False)
|
|
362
|
+
self.context_dict = defaultdict(list)
|
|
363
|
+
|
|
364
|
+
def add_scalar(self, tag, scalar_value, global_step, need_explain=False):
|
|
365
|
+
"""
|
|
366
|
+
('0:1.post_attention_norm.weight/rank0/pre_grad', 'min')
|
|
367
|
+
"""
|
|
368
|
+
super().add_scalar(tag, scalar_value, global_step, need_explain=False)
|
|
369
|
+
split_name = tag[0].split('/')
|
|
370
|
+
name = split_name[0]
|
|
371
|
+
if need_explain:
|
|
372
|
+
if 'pre' in split_name[-1]:
|
|
373
|
+
name += '.input'
|
|
374
|
+
if 'post' in split_name[-1]:
|
|
375
|
+
name += '.output'
|
|
376
|
+
self.context_dict[name].append(scalar_value)
|
|
377
|
+
|
|
378
|
+
def write_metrics(self, op_list, metric_value, step, prefix='', need_explain=False):
|
|
379
|
+
need_explain = prefix == 'other'
|
|
380
|
+
super().write_metrics(op_list, metric_value, step, prefix='', need_explain=need_explain)
|
|
381
|
+
|
|
382
|
+
# generate csv headers
|
|
383
|
+
# set hashmap to reduce the number of headers generated.
|
|
384
|
+
# 前向的norm用input.ops_和output.ops_,反向的用input_grad.ops_和output_grad.ops_
|
|
385
|
+
if prefix in {"actv", "actv_grad"}:
|
|
386
|
+
if prefix == "actv":
|
|
387
|
+
input_and_output = [MonitorConst.ACTV_IN, MonitorConst.ACTV_OUT]
|
|
388
|
+
else:
|
|
389
|
+
input_and_output = [MonitorConst.ACTVGRAD_IN, MonitorConst.ACTVGRAD_OUT]
|
|
390
|
+
ops_ = [MonitorConst.DOT.join(i) for i in itertools.product(input_and_output, op_list)]
|
|
391
|
+
csv_header = ["module_name", "step", *ops_]
|
|
392
|
+
else:
|
|
393
|
+
csv_header = ["param_name", "step", *op_list]
|
|
394
|
+
|
|
395
|
+
keys = list(metric_value.keys())
|
|
396
|
+
if keys and MonitorConst.NAME_SEP in keys[0]:
|
|
397
|
+
csv_header.insert(0, "vpp_stage")
|
|
398
|
+
|
|
399
|
+
self.header = csv_header
|
|
400
|
+
self.write_csv(prefix, step)
|
|
401
|
+
self.header = []
|
|
402
|
+
|
|
403
|
+
def close(self):
|
|
404
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
communication.comm_func:
|
|
2
|
+
- all_reduce
|
|
3
|
+
- all_gather_into_tensor
|
|
4
|
+
- reduce
|
|
5
|
+
- reduce_scatter_tensor
|
|
6
|
+
- all_to_all_single_with_output_shape
|
|
7
|
+
- all_to_all_with_output_shape
|
|
8
|
+
- batch_isend_irecv
|
|
9
|
+
- broadcast
|
|
10
|
+
- gather_into_tensor
|
|
11
|
+
- scatter_tensor
|
|
12
|
+
- send
|
|
13
|
+
- recv
|
|
14
|
+
- isend
|
|
15
|
+
- irecv
|