Skip to content

Commit 8595506

Browse files
Taylor Robienvpaulius
authored andcommitted
Feat/accelerate compliance merge (pytorch#153)
* remove pycache * bring updated logging util to head
1 parent d83bfe5 commit 8595506

File tree

6 files changed

+376
-16
lines changed

6 files changed

+376
-16
lines changed

compliance/_resnet_tags.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2018 MLBenchmark Group. All Rights Reserved.
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+
"""Keys which only appear in ResNet.
16+
"""
17+
18+
from __future__ import absolute_import
19+
from __future__ import division
20+
from __future__ import print_function
21+
22+
23+
BOTTLENECK_BLOCK = "bottleneck_block"
24+
25+
# ==============================================================================
26+
# == Topology ==================================================================
27+
# ==============================================================================
28+
29+
MODEL_HP_INITIAL_MAX_POOL = "model_hp_initial_max_pool"
30+
MODEL_HP_BEGIN_BLOCK = "model_hp_begin_block"
31+
MODEL_HP_END_BLOCK = "model_hp_end_block"
32+
MODEL_HP_BLOCK_TYPE = "model_hp_block_type"
33+
MODEL_HP_PROJECTION_SHORTCUT = "model_hp_projection_shortcut"
34+
MODEL_HP_SHORTCUT_ADD = "model_hp_shorcut_add"
35+
36+
MODEL_HP_RESNET_TOPOLOGY = "model_hp_resnet_topology"

compliance/mlperf_log.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,55 @@
2020
from __future__ import print_function
2121

2222
import inspect
23-
import time
23+
import logging
2424
import json
25+
import os
2526
import re
27+
import sys
28+
import time
2629
import uuid
2730

2831
from tags import *
2932

33+
# Set by imagenet_main.py
34+
ROOT_DIR_RESNET = None
3035

3136
PATTERN = re.compile('[a-zA-Z0-9]+')
3237

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+
3354

34-
def get_caller(stack_index=2):
55+
56+
def get_caller(stack_index=2, root_dir=None):
3557
''' Returns file.py:lineno of your caller. A stack_index of 2 will provide
3658
the caller of the function calling this function. Notice that stack_index
3759
of 2 or more will fail if called from global scope. '''
3860
caller = inspect.getframeinfo(inspect.stack()[stack_index][0])
3961

4062
# 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)
4267

4368

4469
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):
4672
''' Prints out an MLPerf Log Line.
4773
4874
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,
5682
be assigned as the value of this call and will be returned. The
5783
caller can then include said unique ID when the value is known
5884
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.
5988
6089
Example output:
6190
:::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,
79108
str_json = json.dumps(value)
80109
tag = '{key}: {value}'.format(key=key, value=str_json)
81110

82-
callsite = get_caller(2 + stack_offset)
111+
callsite = get_caller(2 + stack_offset, root_dir=root_dir)
83112
now = int(time.time())
84113

85114
message = ':::MLPv0.5.0 {benchmark} {secs} ({callsite}) {tag}'.format(
86115
secs=now, benchmark=benchmark, callsite=callsite, tag=tag)
87116

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)
91124

92125
return return_value
93126

94127

95128
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.
97132
return _mlperf_print(key=key, value=value, benchmark=NCF,
98133
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)
100142

101143

102144
if __name__ == '__main__':

compliance/resnet_log_helper.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2018 MLBenchmark Group. All Rights Reserved.
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+
"""Convenience functions for logging ResNet topology.
16+
"""
17+
18+
from __future__ import absolute_import
19+
from __future__ import division
20+
from __future__ import print_function
21+
22+
import mlperf_log
23+
24+
25+
def _get_shape(input_tensor):
26+
return "({})".format(", ".join(
27+
[str(i) for i in input_tensor.shape.as_list()[1:]]))
28+
29+
30+
def _in_out_shape(input_tensor, output_tensor):
31+
return "{} -> {}".format( _get_shape(input_tensor), _get_shape(output_tensor))
32+
33+
34+
def log_max_pool(input_tensor, output_tensor):
35+
mlperf_log.resnet_print(
36+
key=mlperf_log.MODEL_HP_INITIAL_MAX_POOL, value=_in_out_shape(
37+
input_tensor=input_tensor, output_tensor=output_tensor))
38+
39+
40+
def log_begin_block(input_tensor, block_type):
41+
mlperf_log.resnet_print(key=mlperf_log.MODEL_HP_BEGIN_BLOCK,
42+
value={"block_type": block_type})
43+
mlperf_log.resnet_print(
44+
key=mlperf_log.MODEL_HP_RESNET_TOPOLOGY,
45+
value=" Block Input: {}".format(_get_shape(input_tensor)))
46+
47+
48+
def log_end_block(output_tensor):
49+
mlperf_log.resnet_print(
50+
key=mlperf_log.MODEL_HP_END_BLOCK,
51+
value=" Block Output: {}".format(_get_shape(output_tensor)))
52+
53+
54+
def log_projection(input_tensor, output_tensor):
55+
mlperf_log.resnet_print(
56+
key=mlperf_log.MODEL_HP_PROJECTION_SHORTCUT,
57+
value=_in_out_shape(input_tensor, output_tensor))
58+
59+
60+
def log_conv2d(input_tensor, output_tensor, stride, filters, initializer):
61+
mlperf_log.resnet_print(key=mlperf_log.MODEL_HP_CONV2D_FIXED_PADDING,
62+
value=_in_out_shape(input_tensor, output_tensor))
63+
mlperf_log.resnet_print(
64+
key=mlperf_log.MODEL_HP_CONV2D_FIXED_PADDING,
65+
value={"stride": stride, "filters": filters, "initializer": initializer})
66+
67+
68+
def log_batch_norm(input_tensor, output_tensor, momentum, epsilon, center,
69+
scale, training):
70+
assert _get_shape(input_tensor) == _get_shape(output_tensor)
71+
mlperf_log.resnet_print(key=mlperf_log.MODEL_HP_BATCH_NORM, value={
72+
"shape": _get_shape(input_tensor), "momentum": momentum, "epsilon": epsilon,
73+
"center": center, "scale": scale, "training": training})

0 commit comments

Comments
 (0)