20
20
from __future__ import print_function
21
21
22
22
import inspect
23
- import time
23
+ import logging
24
24
import json
25
+ import os
25
26
import re
27
+ import sys
28
+ import time
26
29
import uuid
27
30
28
31
from tags import *
29
32
33
+ # Set by imagenet_main.py
34
+ ROOT_DIR_RESNET = None
30
35
31
36
PATTERN = re .compile ('[a-zA-Z0-9]+' )
32
37
38
+ LOG_FILE = os .getenv ("COMPLIANCE_FILE" )
39
+ # create logger with 'spam_application'
40
+ LOGGER = logging .getLogger ('mlperf_compliance' )
41
+ LOGGER .setLevel (logging .DEBUG )
42
+
43
+ _STREAM_HANDLER = logging .StreamHandler (stream = sys .stdout )
44
+ _STREAM_HANDLER .setLevel (logging .INFO )
45
+ LOGGER .addHandler (_STREAM_HANDLER )
46
+
47
+ if LOG_FILE :
48
+ _FILE_HANDLER = logging .FileHandler (LOG_FILE )
49
+ _FILE_HANDLER .setLevel (logging .DEBUG )
50
+ LOGGER .addHandler (_FILE_HANDLER )
51
+ else :
52
+ _STREAM_HANDLER .setLevel (logging .DEBUG )
53
+
33
54
34
- def get_caller (stack_index = 2 ):
55
+
56
+ def get_caller (stack_index = 2 , root_dir = None ):
35
57
''' Returns file.py:lineno of your caller. A stack_index of 2 will provide
36
58
the caller of the function calling this function. Notice that stack_index
37
59
of 2 or more will fail if called from global scope. '''
38
60
caller = inspect .getframeinfo (inspect .stack ()[stack_index ][0 ])
39
61
40
62
# Trim the filenames for readability.
41
- return "%s:%d" % (caller .filename , caller .lineno )
63
+ filename = caller .filename
64
+ if root_dir is not None :
65
+ filename = re .sub ("^" + root_dir + "/" , "" , filename )
66
+ return "%s:%d" % (filename , caller .lineno )
42
67
43
68
44
69
def _mlperf_print (key , value = None , benchmark = None , stack_offset = 0 ,
45
- tag_set = None , deferred = False ):
70
+ tag_set = None , deferred = False , root_dir = None ,
71
+ extra_print = False ):
46
72
''' Prints out an MLPerf Log Line.
47
73
48
74
key: The MLPerf log key such as 'CLOCK' or 'QUALITY'. See the list of log keys in the spec.
@@ -56,6 +82,9 @@ def _mlperf_print(key, value=None, benchmark=None, stack_offset=0,
56
82
be assigned as the value of this call and will be returned. The
57
83
caller can then include said unique ID when the value is known
58
84
later.
85
+ root_dir: Directory prefix which will be trimmed when reporting calling file
86
+ for compliance logging.
87
+ extra_print: Print a blank line before logging to clear any text in the line.
59
88
60
89
Example output:
61
90
:::MLP-1537375353 MINGO[17] (eval.py:42) QUALITY: 43.7
@@ -79,24 +108,37 @@ def _mlperf_print(key, value=None, benchmark=None, stack_offset=0,
79
108
str_json = json .dumps (value )
80
109
tag = '{key}: {value}' .format (key = key , value = str_json )
81
110
82
- callsite = get_caller (2 + stack_offset )
111
+ callsite = get_caller (2 + stack_offset , root_dir = root_dir )
83
112
now = int (time .time ())
84
113
85
114
message = ':::MLPv0.5.0 {benchmark} {secs} ({callsite}) {tag}' .format (
86
115
secs = now , benchmark = benchmark , callsite = callsite , tag = tag )
87
116
88
- # And log to tensorflow too
89
- print () # There could be prior text on a line
90
- print (message )
117
+ if extra_print :
118
+ print () # There could be prior text on a line
119
+
120
+ if tag in STDOUT_TAG_SET :
121
+ LOGGER .info (message )
122
+ else :
123
+ LOGGER .debug (message )
91
124
92
125
return return_value
93
126
94
127
95
128
NCF_TAG_SET = set (NCF_TAGS )
96
- def ncf_print (key , value = None , stack_offset = 2 , deferred = False ):
129
+ def ncf_print (key , value = None , stack_offset = 2 , deferred = False ,
130
+ extra_print = True ):
131
+ # Extra print is needed for the reference NCF because of tqdm.
97
132
return _mlperf_print (key = key , value = value , benchmark = NCF ,
98
133
stack_offset = stack_offset , tag_set = NCF_TAG_SET ,
99
- deferred = deferred )
134
+ deferred = deferred , extra_print = extra_print )
135
+
136
+
137
+ RESNET_TAG_SET = set (RESNET_TAGS )
138
+ def resnet_print (key , value = None , stack_offset = 2 , deferred = False ):
139
+ return _mlperf_print (key = key , value = value , benchmark = RESNET ,
140
+ stack_offset = stack_offset , tag_set = RESNET_TAG_SET ,
141
+ deferred = deferred , root_dir = ROOT_DIR_RESNET )
100
142
101
143
102
144
if __name__ == '__main__' :
0 commit comments