Skip to content

Commit 0b6770d

Browse files
authored
[CodeStyle][UP031] Use f-string instead of percent format in some uts (#64857)
1 parent e09bb80 commit 0b6770d

15 files changed

+55
-53
lines changed

cmake/make_resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"const unsigned char "
2525
+ var
2626
+ "[] = {"
27-
+ ",".join(["0x%02x" % ord(c) for c in open(res).read()])
27+
+ ",".join([f"0x{ord(c):02x}" for c in open(res).read()])
2828
+ ",0};\n"
2929
+ "const unsigned "
3030
+ var

test/ir/inference/quant_dequant_test.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222

2323
import paddle
2424
from paddle import base
25-
from paddle.base import Program, Variable, core
25+
from paddle.base import core
2626
from paddle.base.core import AnalysisConfig, create_paddle_predictor
2727
from paddle.base.framework import IrGraph
28+
from paddle.static import Variable
2829
from paddle.static.io import append_fetch_ops, prepend_feed_ops
2930
from paddle.static.quantization import (
3031
AddQuantDequantPass,
@@ -39,10 +40,10 @@ class QuantDequantTest(unittest.TestCase):
3940
def __init__(self, methodName='runTest'):
4041
super().__init__(methodName)
4142
paddle.enable_static()
42-
self.main_program = base.Program()
43-
self.startup_program = base.Program()
44-
self.test_main_program = base.Program()
45-
self.test_startup_program = base.Program()
43+
self.main_program = paddle.static.Program()
44+
self.startup_program = paddle.static.Program()
45+
self.test_main_program = paddle.static.Program()
46+
self.test_startup_program = paddle.static.Program()
4647
self.feeds = None
4748
self.fetch_list = None
4849
self.enable_mkldnn = False
@@ -62,10 +63,9 @@ def __init__(self, methodName='runTest'):
6263

6364
# from Paddle release2.1
6465
def _normalize_program(self, program, feed_vars, fetch_vars):
65-
if not isinstance(program, Program):
66+
if not isinstance(program, paddle.static.Program):
6667
raise TypeError(
67-
"program type must be `base.Program`, but received `%s`"
68-
% type(program)
68+
f"program type must be `paddle.static.Program`, but received `{type(program)}`"
6969
)
7070
if not isinstance(feed_vars, list):
7171
feed_vars = [feed_vars]
@@ -127,7 +127,7 @@ def _save_models(
127127
if var.name in feeded_var_names:
128128
feeded_vars.append(var)
129129

130-
with base.scope_guard(scope):
130+
with paddle.static.scope_guard(scope):
131131
paddle.static.io.save_inference_model(
132132
dirname,
133133
feeded_vars,
@@ -155,7 +155,7 @@ def _get_paddle_outs(self, feed, fetch_list, executor, program, scope):
155155
'''
156156
Return PaddlePaddle outputs.
157157
'''
158-
with base.scope_guard(scope):
158+
with paddle.static.scope_guard(scope):
159159
outs = executor.run(
160160
program=program,
161161
feed=feed,
@@ -245,12 +245,12 @@ def check_output_with_option(
245245
or disable TensorRT, enable MKLDNN or disable MKLDNN
246246
are all the same.
247247
'''
248-
place = base.CUDAPlace(0) if use_gpu else base.CPUPlace()
249-
executor = base.Executor(place)
250-
scope = base.Scope()
248+
place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace()
249+
executor = paddle.static.Executor(place)
250+
scope = paddle.static.Scope()
251251
device = "GPU" if use_gpu else "CPU"
252252

253-
with base.scope_guard(scope):
253+
with paddle.static.scope_guard(scope):
254254
executor.run(self.startup_program)
255255
executor.run(self.test_startup_program)
256256
main_graph = IrGraph(core.Graph(self.main_program.desc), for_test=False)
@@ -274,11 +274,11 @@ def check_output_with_option(
274274
scale_training_pass = OutScaleForTrainingPass(scope=scope, place=place)
275275
scale_training_pass.apply(main_graph)
276276

277-
build_strategy = base.BuildStrategy()
277+
build_strategy = paddle.static.BuildStrategy()
278278
build_strategy.memory_optimize = False
279279
build_strategy.enable_inplace = False
280280
build_strategy.fuse_all_reduce_ops = False
281-
binary = base.CompiledProgram(main_graph.graph)
281+
binary = paddle.static.CompiledProgram(main_graph.graph)
282282

283283
iters = 10
284284
batch_size = 1
@@ -287,7 +287,7 @@ def check_output_with_option(
287287
batch_size=batch_size,
288288
)
289289
feeder = base.DataFeeder(feed_list=[self.data, self.label], place=place)
290-
with base.scope_guard(scope):
290+
with paddle.static.scope_guard(scope):
291291
for _ in range(iters):
292292
data = next(train_reader())
293293
loss_v = executor.run(
@@ -307,7 +307,7 @@ def check_output_with_option(
307307

308308
self.main_program = test_graph.to_program()
309309

310-
with base.scope_guard(scope):
310+
with paddle.static.scope_guard(scope):
311311
self.main_program = self._normalize_program(
312312
self.main_program, self.data, self.fetch_list
313313
)
@@ -450,6 +450,6 @@ def __init__(
450450
self.disable_trt_plugin_fp16 = disable_trt_plugin_fp16
451451

452452
def quant_dequant(self):
453-
place = base.CPUPlace()
454-
exe = base.Executor(place)
455-
scope = base.Scope()
453+
place = paddle.CPUPlace()
454+
exe = paddle.static.Executor(place)
455+
scope = paddle.static.Scope()

test/ir/test_ir_fusion_group_pass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _feed_random_data(self, feed_vars):
7272
elif var.dtype == paddle.float16:
7373
dtype = "float16"
7474
else:
75-
raise ValueError("Unsupported dtype %s" % var.dtype)
75+
raise ValueError(f"Unsupported dtype {var.dtype}")
7676
feeds[var.name] = np.random.random(shape).astype(dtype)
7777
return feeds
7878

test/quantization/test_imperative_qat_lsq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def func_qat(self):
213213
print('eval_acc_top1', eval_acc_top1)
214214
self.assertTrue(
215215
eval_acc_top1 > 0.9,
216-
msg="The test acc {%f} is less than 0.9." % eval_acc_top1,
216+
msg=f"The test acc {{{eval_acc_top1:f}}} is less than 0.9.",
217217
)
218218

219219
def test_qat(self):

test/standalone_executor/test_standalone_measure_real_op_cost.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _run_op_profiling(self, place, run_profiling=True):
112112
return loss_data
113113

114114
def _compare_loss_between(self, loss_run1, loss_run2):
115-
s1, s2 = '%.6f' % loss_run1, '%.6f' % loss_run2
115+
s1, s2 = f'{loss_run1:.6f}', f'{loss_run2:.6f}'
116116
return s1 == s2
117117

118118
def test_op_profiling_cuda0(self):

test/xpu/test_collective_api_base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def setUp(self):
202202
self._trainers = 2
203203
self._ps_endpoints = f"127.0.0.1:{self._find_free_port()},127.0.0.1:{self._find_free_port()}"
204204
self._python_interp = sys.executable
205-
self._master_endpoints = "127.0.0.1:%s" % (self._find_free_port())
205+
self._master_endpoints = f"127.0.0.1:{self._find_free_port()}"
206206

207207
self.temp_dir = tempfile.TemporaryDirectory()
208208

@@ -300,15 +300,15 @@ def _run_cluster(self, model_file, envs):
300300

301301
tr0_out, tr0_err = tr0_proc.communicate()
302302
tr1_out, tr1_err = tr1_proc.communicate()
303-
sys.stderr.write('trainer 0 stderr: %s\n' % tr0_err)
304-
sys.stderr.write('trainer 1 stderr: %s\n' % tr1_err)
303+
sys.stderr.write(f'trainer 0 stderr: {tr0_err}\n')
304+
sys.stderr.write(f'trainer 1 stderr: {tr1_err}\n')
305305
# close trainer file
306306
tr0_pipe.close()
307307
tr1_pipe.close()
308308
with open(path0, "r") as f:
309-
sys.stderr.write('trainer 0 stderr file: %s\n' % f.read())
309+
sys.stderr.write(f'trainer 0 stderr file: {f.read()}\n')
310310
with open(path1, "r") as f:
311-
sys.stderr.write('trainer 1 stderr file: %s\n' % f.read())
311+
sys.stderr.write(f'trainer 1 stderr file: {f.read()}\n')
312312

313313
def load_and_remove(path):
314314
with open(path, 'rb') as f:

test/xpu/test_collective_base_xpu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ def _run_cluster(self, model_file, envs):
244244

245245
tr0_out, tr0_err = tr0_proc.communicate()
246246
tr1_out, tr1_err = tr1_proc.communicate()
247-
sys.stderr.write('trainer 0 stderr: %s\n' % tr0_err)
248-
sys.stderr.write('trainer 1 stderr: %s\n' % tr1_err)
247+
sys.stderr.write(f'trainer 0 stderr: {tr0_err}\n')
248+
sys.stderr.write(f'trainer 1 stderr: {tr1_err}\n')
249249
# close trainer file
250250
tr0_pipe.close()
251251
tr1_pipe.close()

test/xpu/test_conv2d_op_xpu.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ def conv2d_forward_naive(
3636
):
3737
if padding_algorithm not in ["SAME", "VALID", "EXPLICIT"]:
3838
raise ValueError(
39-
"Unknown Attr(padding_algorithm): '%s'. "
40-
"It can only be 'SAME' or 'VALID'." % str(padding_algorithm)
39+
f"Unknown Attr(padding_algorithm): '{str(padding_algorithm)}'. "
40+
"It can only be 'SAME' or 'VALID'."
4141
)
4242

4343
if data_format not in ["NCHW", "NHWC"]:
4444
raise ValueError(
45-
"Unknown Attr(data_format): '%s' ."
46-
"It can only be 'NCHW' or 'NHWC'." % str(data_format)
45+
f"Unknown Attr(data_format): '{str(data_format)}' ."
46+
"It can only be 'NCHW' or 'NHWC'."
4747
)
4848

4949
channel_last = data_format == "NHWC"

test/xpu/test_conv2d_transpose_op_xpu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def conv2dtranspose_forward_naive(input_, filter_, attrs):
3131
padding_algorithm = attrs['padding_algorithm']
3232
if padding_algorithm not in ["SAME", "VALID", "EXPLICIT"]:
3333
raise ValueError(
34-
"Unknown Attr(padding_algorithm): '%s'. "
35-
"It can only be 'SAME' or 'VALID'." % str(padding_algorithm)
34+
f"Unknown Attr(padding_algorithm): '{str(padding_algorithm)}'. "
35+
"It can only be 'SAME' or 'VALID'."
3636
)
3737

3838
if attrs['data_format'] == 'NHWC':

test/xpu/test_conv3d_op_xpu.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ def conv3d_forward_naive(
3131
):
3232
if padding_algorithm not in ["SAME", "VALID", "EXPLICIT"]:
3333
raise ValueError(
34-
"Unknown Attr(padding_algorithm): '%s'. "
35-
"It can only be 'SAME' or 'VALID'." % str(padding_algorithm)
34+
f"Unknown Attr(padding_algorithm): '{str(padding_algorithm)}'. "
35+
"It can only be 'SAME' or 'VALID'."
3636
)
3737

3838
if data_format not in ["NCDHW", "NDHWC"]:
3939
raise ValueError(
40-
"Unknown Attr(data_format): '%s' ."
41-
"It can only be 'NCDHW' or 'NDHWC'." % str(data_format)
40+
f"Unknown Attr(data_format): '{str(data_format)}' ."
41+
"It can only be 'NCDHW' or 'NDHWC'."
4242
)
4343

4444
channel_last = data_format == "NDHWC"

0 commit comments

Comments
 (0)