forked from pytorch/xla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe_parser_util.py
executable file
·75 lines (63 loc) · 1.86 KB
/
frame_parser_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import collections
import logging
import os
import re
import sys
logging.basicConfig(format='%(name)s: %(message)s')
logger = logging.getLogger('pt-xla-profiler')
def parse_frame_content(line):
# Python Frames:
m = re.match(r'Python Frames:', line)
if m:
return line
# train_loop_fn (test/test_train_mp_imagenet.py:216)
m = re.match(r'.*\s\(.*:\d*\)', line)
if m:
return line
# [Unlowered Op _local_scalar_dense from Device TPU:0]
m = re.match(r'\[TAG\s(.*)\sFrom Thread\s\d*\]', line)
if m:
return f'Unlowered Op: "{m.group(1)}"\n'
def create_report(frames):
mrkeys = sorted(frames.keys(), key=lambda x: frames[x], reverse=True)
report = []
report.append('=' * 80)
report.append(
'Unlowered Op usage summary (more of these ops, lower performance)')
report.append(
'Note: _local_scalar_dense typically indicates CPU context access')
report.append('-' * 80)
for key in mrkeys:
report.append('FRAME (count={}):'.format(frames[key]))
report.append(f'{key}')
report.append('')
report.append('=' * 80)
if os.environ.get('PT_XLA_DEBUG_FILE'):
with open(os.environ.get('PT_XLA_DEBUG_FILE'), 'a') as f:
f.write('\n'.join(report))
else:
for line in '\n'.join(report).split('\n'):
logger.warning(line)
def parse_frames(lines):
frames = collections.defaultdict(int)
frame, skip_frames = [], False
for line in lines:
if re.match(r'C\+\+ Frames:', line):
skip_frames = True
continue
elif re.match(r'\*{3}\sEnd stack trace\s\*{3}', line):
skip_frames = False
continue
if skip_frames:
continue
content = parse_frame_content(line)
if content:
frame.append(content)
continue
if frame:
frames[''.join(frame)] += 1
frame = []
return frames
def process_frames(fname):
frames = parse_frames(open(fname, 'r'))
create_report(frames)