Skip to content

Commit f5c9bc9

Browse files
author
Andrew Reusch
authored
Customize SI prefix in logging (#5411)
* Customize SI prefix in logging * Include unit test
1 parent 8f9796b commit f5c9bc9

4 files changed

Lines changed: 71 additions & 8 deletions

File tree

python/tvm/autotvm/tuner/callback.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import numpy as np
2424

2525
from .. import record
26+
from ..util import format_si_prefix
2627

2728
logger = logging.getLogger('autotvm')
2829

@@ -105,7 +106,7 @@ def trial_timestamps(self):
105106
return np.array(self.timestamps)
106107

107108

108-
def progress_bar(total, prefix=''):
109+
def progress_bar(total, prefix='', si_prefix='G'):
109110
"""Display progress bar for tuning
110111
111112
Parameters
@@ -114,6 +115,8 @@ def progress_bar(total, prefix=''):
114115
The total number of trials
115116
prefix: str
116117
The prefix of output message
118+
si_prefix: str
119+
SI prefix for flops
117120
"""
118121
class _Context(object):
119122
"""Context to store local variables"""
@@ -130,6 +133,9 @@ def __del__(self):
130133
ctx = _Context()
131134
tic = time.time()
132135

136+
# Validate si_prefix argument
137+
format_si_prefix(0, si_prefix)
138+
133139
if logger.level < logging.DEBUG: # only print progress bar in non-debug mode
134140
sys.stdout.write('\r%s Current/Best: %7.2f/%7.2f GFLOPS | Progress: (%d/%d) '
135141
'| %.2f s' % (prefix, 0, 0, 0, total, time.time() - tic))
@@ -147,10 +153,11 @@ def _callback(tuner, inputs, results):
147153
ctx.cur_flops = flops
148154
ctx.best_flops = tuner.best_flops
149155

150-
sys.stdout.write('\r%s Current/Best: %7.2f/%7.2f GFLOPS | Progress: (%d/%d) '
156+
sys.stdout.write('\r%s Current/Best: %7.2f/%7.2f %sFLOPS | Progress: (%d/%d) '
151157
'| %.2f s' %
152-
(prefix, ctx.cur_flops/1e9, ctx.best_flops/1e9, ctx.ct, ctx.total,
153-
time.time() - tic))
158+
(prefix, format_si_prefix(ctx.cur_flops, si_prefix),
159+
format_si_prefix(ctx.best_flops, si_prefix), si_prefix,
160+
ctx.ct, ctx.total, time.time() - tic))
154161
sys.stdout.flush()
155162

156163
return _callback

python/tvm/autotvm/tuner/tuner.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import numpy as np
2222

2323
from ..measure import MeasureInput, create_measure_batch
24+
from ..util import format_si_prefix
2425

2526
from ..env import GLOBAL_SCOPE
2627

@@ -87,7 +88,7 @@ def update(self, inputs, results):
8788
"""
8889

8990

90-
def tune(self, n_trial, measure_option, early_stopping=None, callbacks=()):
91+
def tune(self, n_trial, measure_option, early_stopping=None, callbacks=(), si_prefix='G'):
9192
"""Begin tuning
9293
9394
Parameters
@@ -104,13 +105,18 @@ def tune(self, n_trial, measure_option, early_stopping=None, callbacks=()):
104105
(Tuner, List of MeasureInput, List of MeasureResult)
105106
with no return value. These callback functions will be called on
106107
every measurement pair. See autotvm/tuner/callback.py for some examples.
108+
si_prefix: str
109+
One of tvm.autotvm.util.SI_PREFIXES. The SI prefix to use when reporting FLOPS.
107110
"""
108111
measure_batch = create_measure_batch(self.task, measure_option)
109112
n_parallel = getattr(measure_batch, 'n_parallel', 1)
110113
early_stopping = early_stopping or 1e9
111114
self.n_trial = n_trial
112115
self.early_stopping = early_stopping
113116

117+
# Validate si_prefix arg
118+
format_si_prefix(0, si_prefix)
119+
114120
old_level = logger.level
115121

116122
GLOBAL_SCOPE.in_tuning = True
@@ -140,9 +146,9 @@ def tune(self, n_trial, measure_option, early_stopping=None, callbacks=()):
140146
self.best_measure_pair = (inp, res)
141147
self.best_iter = i + k
142148

143-
logger.debug("No: %d\tGFLOPS: %.2f/%.2f\tresult: %s\t%s",
144-
i + k + 1, flops / 1e9, self.best_flops / 1e9,
145-
res, config)
149+
logger.debug("No: %d\t%sFLOPS: %.2f/%.2f\tresult: %s\t%s",
150+
i + k + 1, si_prefix, format_si_prefix(flops, si_prefix),
151+
format_si_prefix(self.best_flops, si_prefix), res, config)
146152

147153
i += len(results)
148154
self.ttl = min(early_stopping + self.best_iter, n_trial) - i

python/tvm/autotvm/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,12 @@ def get_const_tuple(in_tuple):
188188
else:
189189
ret.append(get_const_int(elem))
190190
return tuple(ret)
191+
192+
193+
SI_PREFIXES = 'yzafpn\xb5m kMGTPEZY'
194+
YOCTO_EXP10 = -24
195+
196+
197+
def format_si_prefix(x, si_prefix):
198+
exp10 = 10 ** (SI_PREFIXES.index(si_prefix) * 3 + YOCTO_EXP10)
199+
return float(x) / exp10
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from numpy import isclose
19+
import random
20+
from tvm.autotvm import util
21+
22+
23+
SI_PREFIXES = 'yzafpn\xb5m kMGTPEZY'
24+
25+
26+
def test_format_si_prefix():
27+
# test float conversion
28+
assert util.format_si_prefix(1024, 'k') == 1.024
29+
30+
for i, prefix in enumerate(SI_PREFIXES):
31+
integer, decimal = random.randint(0, 1000), random.randint(0, 1000)
32+
exp = -24 + 3 * i # 0th prefix (yocto) is 10^-24
33+
number = integer * (10 ** exp) + decimal * (10 ** (exp - 3))
34+
expected = (integer + decimal / 1000)
35+
assert isclose(util.format_si_prefix(number, prefix), expected)
36+
37+
assert util.format_si_prefix(0, 'y') == 0
38+
39+
40+
if __name__ == '__main__':
41+
test_format_si_prefix()

0 commit comments

Comments
 (0)