Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Python string formatting (#21136)
Browse files Browse the repository at this point in the history
* Improve string formatting

* Another formatting fix

* Minor fixes

* Review suggestion

* Review suggestions

* Review suggestions once again

* Review additional suggestions

* A few more fixes

* Remove f-strings from logging functions

* Fix lack of 'f'

* Minor fixes

* Fix lint errors
  • Loading branch information
hankaj committed Sep 16, 2022
1 parent bd6405b commit c8922fe
Show file tree
Hide file tree
Showing 138 changed files with 835 additions and 928 deletions.
6 changes: 3 additions & 3 deletions benchmark/opperf/opperf.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ def main():
'Valid Inputs - positive integers')

args = parser.parse_args()
logging.info("Running MXNet operator benchmarks with the following options: {args}".format(args=args))
logging.info(f"Running MXNet operator benchmarks with the following options: {args}")
assert not os.path.isfile(args.output_file),\
"Output file {output_file} already exists.".format(output_file=args.output_file)
f"Output file {args.output_file} already exists."

# 2. RUN BENCHMARKS
ctx = _parse_mxnet_context(args.ctx)
Expand All @@ -218,7 +218,7 @@ def main():
# 4. Generate list of MXNet operators not covered in benchmarks
ops_not_covered = get_operators_with_no_benchmark(final_benchmark_results.keys())
for idx, op in enumerate(ops_not_covered):
print("{idx}. {op}".format(idx=idx, op=op))
print(f"{idx}. {op}")

return 0

Expand Down
6 changes: 3 additions & 3 deletions benchmark/opperf/utils/benchmark_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _run_operator_performance_test(op, inputs, run_backward, warmup, runs, kwarg

# Run Benchmarks
op_benchmark_result = {op.__name__: []}
logging.info("Begin Benchmark - {name}".format(name=op.__name__))
logging.info(f"Begin Benchmark - {op.__name__}")

for idx, kwargs in enumerate(kwargs_list):
_, profiler_output = benchmark_helper_func(op, runs, **kwargs)
Expand All @@ -199,7 +199,7 @@ def _run_operator_performance_test(op, inputs, run_backward, warmup, runs, kwarg
new_inp = parse_input_ndarray(inputs[idx])
profiler_output = merge_map_list([{"inputs": new_inp}] + [profiler_output])
op_benchmark_result[op.__name__].append(profiler_output)
logging.info("Complete Benchmark - {name}".format(name=op.__name__))
logging.info(f"Complete Benchmark - {op.__name__}")
return op_benchmark_result


Expand Down Expand Up @@ -250,7 +250,7 @@ def run_performance_test(ops, inputs, run_backward=True,
kwargs_list = _prepare_op_inputs(inputs, run_backward, dtype, ctx, op.__module__)
benchmark_result = _run_operator_performance_test(op, inputs, run_backward, warmup, runs, kwargs_list, profiler)
else:
raise ValueError("Unknown {0} operator provided to benchmark. - {1}".format(op.__module__, op.__name__))
raise ValueError(f"Unknown {op.__module__} operator provided to benchmark. - {op.__name__}")
op_benchmark_result.append(benchmark_result)
return op_benchmark_result

Expand Down
9 changes: 4 additions & 5 deletions benchmark/opperf/utils/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def save_to_file(inp_dict, out_filepath, out_format='json', runtime_features=Non
with open(out_filepath, "w") as result_file:
result_file.write(_prepare_markdown(inp_dict, runtime_features, profiler))
else:
raise ValueError("Invalid output file format provided - '{}'. Supported - json, md".format(format))
raise ValueError(f"Invalid output file format provided - '{out_format}'. Supported - json, md")


def get_json(inp_dict):
Expand Down Expand Up @@ -126,10 +126,9 @@ def _prepare_op_benchmark_result(op, op_bench_result, profiler):

result = ""
if profiler == "native":
result = "| {} | {} | {} | {} | {} |".format(operator_name,
inputs, max_mem_usage, avg_forward_time, avg_backward_time)
result = f"| {operator_name} | {inputs} | {max_mem_usage} | {avg_forward_time} | {avg_backward_time} |"
elif profiler == "python":
result = "| {} | {} | {} | {} | {} | {} |".format(operator_name, avg_time, p50_time, p90_time, p99_time, inputs)
result = f"| {operator_name} | {avg_time} | {p50_time} | {p90_time} | {p99_time} | {inputs} |"
return result


Expand All @@ -139,7 +138,7 @@ def _prepare_markdown(results, runtime_features=None, profiler='native'):
results_markdown.append("# Runtime Features")
idx = 0
for key, value in runtime_features['runtime_features'].items():
results_markdown.append('{}. {} : {}'.format(idx, key, value))
results_markdown.append(f'{idx}. {key} : {value}')

results_markdown.append("# Benchmark Results")
if profiler == 'native':
Expand Down
8 changes: 4 additions & 4 deletions benchmark/python/control_flow/rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def run_benchmark(cell_type, ctx, seq_len, batch_size, hidden_dim):
layer.initialize(ctx=ctx)
if is_hyb_layer:
layer.hybridize(static_alloc=True)
print("is_train = %r, hybridize_cell = %r, hybridize_layer = %r" % (is_train, is_hyb_cell, is_hyb_layer))
print(
f"is_train = {repr(is_train)}, hybridize_cell = {repr(is_hyb_cell)}, hybridize_layer = {repr(is_hyb_layer)}")
times = []
for _ in range(args.warmup_rounds + args.test_rounds):
tick = time()
Expand All @@ -112,7 +113,7 @@ def run_benchmark(cell_type, ctx, seq_len, batch_size, hidden_dim):
tock = time()
times.append((tock - tick) * 1000.0)
times = times[args.warmup_rounds: ]
print("Time used: mean = %.3f ms, std = %.3f ms" % (onp.mean(times), onp.std(times)))
print(f"Time used: mean = {onp.mean(times):.3f} ms, std = {onp.std(times):.3f} ms")


def main():
Expand All @@ -131,8 +132,7 @@ def main():
for cell_type, ctx, seq_len, batch_size, hidden_dim in product( \
cell_types, ctxs, seq_lens, batch_sizes, hidden_dims):
print("--------------------------------------")
print("cell: %s ctx: %s length: %d batch size: %d dim: %d" % \
(cell_type.__name__, str(ctx), seq_len, batch_size, hidden_dim))
print(f"cell: {cell_type.__name__} ctx: {str(ctx)} length: {seq_len} batch size: {batch_size} dim: {hidden_dim}")
run_benchmark(cell_type, ctx, seq_len, batch_size, hidden_dim)


Expand Down
4 changes: 2 additions & 2 deletions benchmark/python/dnnl/fc_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def print_header(header):

def print_value(shape, hidden, mean):
if table_left_colums:
print("| ({:4},{:4}) | {:6} | {:9.3f} |".format(shape[0], shape[1], hidden, mean))
print(f"| ({shape[0]:4},{shape[1]:4}) | {hidden:6} | {mean:9.3f} |")
else:
print(" {:9.3f} |".format(mean))
print(f" {mean:9.3f} |")


def measure(net, data0, data1, data2, shape, nhid):
Expand Down
20 changes: 10 additions & 10 deletions benchmark/python/einsum/benchmark_einsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,48 @@ def test_np_einsum():
a = np.ones(64).reshape(2,4,8)
args = ['ijk,ilm,njm,nlk,abc->', a, a, a, a, a]
cost = measure_cost(500, np.einsum, *args)
print("Basic einsum: {} ms".format(cost * 1000))
print(f"Basic einsum: {cost * 1000} ms")

# Sub-optimal einsum
# cost = measure_cost(500, np.einsum, *args, optimize='optimal')
# print("Optimal einsum: {} ms".format(cost * 1000))

# Greedy einsum
cost = measure_cost(500, np.einsum, *args, optimize=True)
print("Greedy einsum: {} ms".format(cost * 1000))
print(f"Greedy einsum: {cost * 1000} ms")

print("RNN Use Case:")
a = np.random.uniform(0, 1, size=(64, 128, 512))
b = np.random.uniform(0, 1, size=(128, 512, 2, 2))
args = ['bij, ijkl->bkl', a, b]
cost = measure_cost(2, np.einsum, *args, optimize=True)
print('Greedy einsum: {} ms'.format(cost * 1000))
print(f'Greedy einsum: {cost * 1000} ms')
cost = measure_cost(2, np.einsum, *args)
print('Basic einsum: {} ms'.format(cost * 1000))
print(f'Basic einsum: {cost * 1000} ms')

print('Inner Product:')
a = np.ones(6000000)
b = np.ones(6000000)
args = [a, b]
cost = measure_cost(50, np.tensordot, *args, axes=([0],[0]))
print('Tensordot: {} ms'.format(cost * 1000))
print(f'Tensordot: {cost * 1000} ms')
args = ['i, i', a, b]
cost = measure_cost(50, np.einsum, *args, optimize=True)
print('Greedy einsum: {} ms'.format(cost * 1000))
print(f'Greedy einsum: {cost * 1000} ms')
cost = measure_cost(50, np.einsum, *args)
print('Basic einsum: {} ms'.format(cost * 1000))
print(f'Basic einsum: {cost * 1000} ms')

print('Matrix Product:')
a = np.ones(600000).reshape(200, 3000)
b = np.ones(600000).reshape(3000, 200)
args = [a, b]
cost = measure_cost(50, np.tensordot, *args, axes=([1],[0]))
print('Tensordot: {} ms'.format(cost * 1000))
print(f'Tensordot: {cost * 1000} ms')
args = ['ij, jk', a, b]
cost = measure_cost(50, np.einsum, *args, optimize=True)
print('Greedy einsum: {} ms'.format(cost * 1000))
print(f'Greedy einsum: {cost * 1000} ms')
cost = measure_cost(50, np.einsum, *args)
print('Basic einsum: {} ms'.format(cost * 1000))
print(f'Basic einsum: {cost * 1000} ms')


if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions benchmark/python/ffi/benchmark_ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def add_workload(funcname, *args, **kwargs):
_specifier = kwargs["_specififer"]
del kwargs["_specififer"]
if _specifier in OpArgMngr.args:
raise ValueError("duplicate {}".format(_specifier))
raise ValueError(f"duplicate {_specifier}")
OpArgMngr.args[_specifier] = {'args': args, 'kwargs': kwargs, 'funcname': funcname}


Expand All @@ -43,7 +43,7 @@ def generate_workloads():
for shape in shapes:
name = 'x'.join(str(i) for i in shape)
if name in array_pool:
raise ValueError("duplicate array {}".format(name))
raise ValueError(f"duplicate array {name}")
array_pool[name] = dnp.ones(shape)
return array_pool

Expand Down Expand Up @@ -229,7 +229,7 @@ def run_benchmark(packages):
for (k, v) in OpArgMngr.args.items():
result = {}
for (name, package) in packages.items():
print('{}.{} running...'.format(name, k))
print(f'{name}.{k} running...')
op = get_op(package["module"], v["funcname"])
args = [package["data"](arg) for arg in v["args"]]
kwargs = {k: package["data"](v) for (k, v) in v["kwargs"].items()}
Expand All @@ -240,10 +240,10 @@ def run_benchmark(packages):


def show_results(results):
print("{:>24}{:>24}{:>24}".format("name", "package", "time(us)"))
print(f'{"name":>24}{"package":>24}{"time(us)":>24}')
for (specifier, d) in results.items():
for (k, v) in d.items():
print("{:>24}{:>24}{:>24}".format(specifier, k, v * 10 ** 6))
print(f"{specifier:>24}{k:>24}{v * 10 ** 6:>24}")


if __name__ == "__main__":
Expand Down
16 changes: 6 additions & 10 deletions benchmark/python/metric/benchmark_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ def run_metric(name, data_gen_cls, i, n, c, pred_ctx, label_ctx, **kwargs):
metric.update([label] * i, [pred] * i)
mx.nd.waitall()
elapsed = time.time() - before
elapsed_str = "{:<.5}".format(elapsed)
elapsed_str = f"{elapsed:<.5}"
except mx.MXNetError:
elapsed_str = "FAILED"
print("{metric:<15}{pctx:<10}{lctx:<12}{niter:<12}{bs:<15}{out_dim:<15}{elapsed:<}".format(
metric=name, pctx=str(pred_ctx), lctx=str(label_ctx), niter=i * n, bs=data_gen.batch_size,
out_dim=data_gen.output_dim, elapsed=elapsed_str), file=sys.stderr)
print(f"{name:<15}{pred_ctx:<10}{label_ctx:<12}{i * n:<12}{data_gen.batch_size:<15}{data_gen.output_dim:<15}{elapsed_str:<}", file=sys.stderr)


def test_metric_performance():
Expand All @@ -107,14 +105,12 @@ def test_metric_performance():

print("\nmx.gluon.metric benchmarks", file=sys.stderr)
print(
"{:15}{:10}{:12}{:12}{:15}{:15}{}".format(
'Metric', 'Data-Ctx', 'Label-Ctx', 'Data Size', 'Batch Size', 'Output Dim', 'Elapsed Time'),
f"{'Metric':15}{'Data-Ctx':10}{'Label-Ctx':12}{'Data Size':12}{'Batch Size':15}{'Output Dim':15}{'Elapsed Time'}",
file=sys.stderr)
print("{:-^90}".format(''), file=sys.stderr)
print(f"{'':-^90}", file=sys.stderr)
for k, v in metrics:
for c in output_dims:
for n in batch_sizes:
for pred_ctx, label_ctx in itertools.product(ctxs, ctxs):
run_metric(k, v[1], (data_size * 128)//(n * c), n, c, pred_ctx, label_ctx, **v[0])
print("{:-^90}".format(''), file=sys.stderr)

run_metric(k, v[1], (data_size * 128), (n * c), n, c, pred_ctx, label_ctx, **v[0])
print(f"{'':-^90}", file=sys.stderr)
9 changes: 4 additions & 5 deletions benchmark/python/quantization/benchmark_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ def benchmark_convolution(data_shape, kernel, num_filter, pad, stride, no_bias=T
grad_req='null', typ='forward') * 1000

print('==================================================================================================')
print('data=%s, kernel=%s, num_filter=%s, pad=%s, stride=%s, no_bias=%s, layout=%s, repeats=%s'
% (data_shape, kernel, num_filter, pad, stride, no_bias, layout, repeats))
print('%s , ctx=%s, time=%.2f ms' % (conv_cudnn.name + '-FP32', ctx_gpu, conv_cudnn_time))
print('%s, ctx=%s, time=%.2f ms' % (quantized_conv2d.name, ctx_gpu, qconv_time))
print('quantization speedup: %.1fX' % (conv_cudnn_time / qconv_time))
print(f'data={data_shape}, kernel={kernel}, num_filter={num_filter}, pad={pad}, stride={stride}, no_bias={no_bias}, layout={layout}, repeats={repeats}')
print(f'{conv_cudnn.name}-FP32 , ctx={ctx_gpu}, time={conv_cudnn_time:.2f} ms')
print(f'{quantized_conv2d.name}, ctx={ctx_gpu}, time={qconv_time:.2f} ms')
print(f'quantization speedup: {conv_cudnn_time / qconv_time:.1f}X')
print('\n')


Expand Down
6 changes: 3 additions & 3 deletions benchmark/python/sparse/cast_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def dense_to_sparse(m, n, density, ctx, repeat, stype):

# start benchmarking
cost = measure_cost(repeat, mx.nd.cast_storage, dns_data, stype)
results = '{:10.1f} {:>10} {:8d} {:8d} {:10.2f}'.format(density*100, str(ctx), m, n, cost*1000)
results = f'{density*100:10.1f} {str(ctx):>10} {m:8d} {n:8d} {cost * 1000:10.2f}'
print(results)

check_call(_LIB.MXSetNumOMPThreads(ctypes.c_int(args.num_omp_threads)))
Expand Down Expand Up @@ -82,10 +82,10 @@ def dense_to_sparse(m, n, density, ctx, repeat, stype):
stype = 'row_sparse'
print(" cast_storage benchmark: dense to rsp, size m x n ")
else:
print("invalid benchmark: %s" %b)
print(f"invalid benchmark: {b}")
continue
print("==================================================")
headline = '{:>10} {:>10} {:>8} {:>8} {:>10}'.format('density(%)', 'context', 'm', 'n', 'time(ms)')
headline = f"{'density(%)':>10} {'context':>10} {'m':>8} {'n':>8} {'time(ms)':>10}"
print(headline)
for i in range(len(n)):
for ctx in contexts:
Expand Down
35 changes: 16 additions & 19 deletions benchmark/python/sparse/dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
'data_mini': 'criteo.t.mini',
'data_name': 'criteo.t',
'data_origin_name': 'criteo.t.bz2',
'url' : "https://s3-us-west-2.amazonaws.com/sparse-dataset/criteo.t.bz2",
'url': "https://s3-us-west-2.amazonaws.com/sparse-dataset/criteo.t.bz2",
'feature_dim': 8388621,
'm': [1, 8, 16, 32, 64],
'batch_size': [64, 128],
Expand Down Expand Up @@ -148,11 +148,10 @@ def create_mini_path(mini_path, path, num_batches):
last = _line_count(path) - num_batches * batch_size
last = last if last >= 1 else 1
start = int(rnd.uniform(1, last))
os.system("sed -n '%d,%dp' %r > %r"
%(start, start + num_batches * batch_size, path, mini_path))
os.system("sed -n '{},{}p' {} > {}".format(
start, start + num_batches * batch_size, repr(path), repr(mini_path)))
assert os.path.exists(mini_path)


def run_benchmark(mini_path):
"""Run benchmarks
"""
Expand Down Expand Up @@ -181,7 +180,6 @@ def run_benchmark(mini_path):
average_cost["dense"] = total_cost["dense"] / count
return (average_cost["sparse"], average_cost["dense"])


def print_result(average_cost_sparse, average_cost_dense):
"""Print result of comparison between sparse and dense
"""
Expand Down Expand Up @@ -224,17 +222,16 @@ def test_dot_real(data_dict):
assert default_batch_size_index < len(batch_size_list)
assert default_output_index < len(m)
if ARGS.verbose:
print("Running Benchmarking on %r data") % data_dict['data_mini']
print(f"Running Benchmarking on {repr(data_dict['data_mini'])} data")
print('{:>15} {:>10} {:>10} {:>10} {:>20} {:>15} {:>15} {:>10} {:>10}'.format('density(%)',
'n',
'm',
'k',
't_dense/t_sparse',
't_dense(ms)',
't_sparse(ms)',
'is_transpose',
'rhs_rsp'))

'n',
'm',
'k',
't_dense/t_sparse',
't_dense(ms)',
't_sparse(ms)',
'is_transpose',
'rhs_rsp'))

for output_dim in m:
_compare_sparse_dense(data_dir, data_dict['data_name'], data_dict['data_mini'],
Expand Down Expand Up @@ -324,8 +321,9 @@ def bench_dot(lhs_shape, rhs_shape, lhs_stype, rhs_stype,
def print_benchmark_info(lhs, rhs, lhs_trans, fw):
trans_str = "^T" if lhs_trans else ""
print("========================================================")
print(" %s sparse dot benchmark: dot(%s, %s) = %s ") % (fw, lhs, rhs, rhs)
print(" (matrix multiplication: (m x k)%s * (k x n) = m x n) ") % (trans_str)
print(f" {fw} sparse dot benchmark: dot({lhs}, {rhs}) = {rhs} ")
print(
f" (matrix multiplication: (m x k){trans_str} * (k x n) = m x n) ")
print("========================================================")
headline_pattern = '{:>15} {:>15} {:>10} {:>8} {:>8} {:>8} {:>13} {:>13} {:>8}'
headline = headline_pattern.format('lhs_density(%)',
Expand All @@ -337,7 +335,6 @@ def print_benchmark_info(lhs, rhs, lhs_trans, fw):
'speedup')
print(headline)


def run_benchmark(ctx=None, lhs="csr", lhs_trans=False, rhs="dns", fw="mxnet", rhs_density=1,
distribution="uniform"):

Expand Down Expand Up @@ -463,4 +460,4 @@ def run_benchmark(ctx=None, lhs="csr", lhs_trans=False, rhs="dns", fw="mxnet", r
test_dot_synthetic(SYNTHETIC1)
test_dot_synthetic(SYNTHETIC2)
total_time = time.time() - begin_time
print("total time is %f") % total_time
print(f"total time is {total_time}")
Loading

0 comments on commit c8922fe

Please sign in to comment.