Skip to content

Commit

Permalink
[CodeStyle][UP031] fix `/test/{deprecated, dygraph_to_static, ir, mkl…
Browse files Browse the repository at this point in the history
…dnn, quantization}*` - part 16 (PaddlePaddle#65577)
  • Loading branch information
gouzil authored Jun 30, 2024
1 parent 8ff2ac1 commit 78632fa
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 38 deletions.
6 changes: 3 additions & 3 deletions test/deprecated/legacy_test/auto_parallel_op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def check_eager_auto_parallel(self):
# check eager auto parallel forward
if len(actual_ret) != len(self.eager_forward_desire):
msg = (
f"The eager auto parallel out tensor nums is different with eager out tensor nums on {str(self.place)}."
f"The eager auto parallel out tensor nums is different with eager out tensor nums on {self.place}."
f'eager auto parallel out tensor nums = {len(actual_ret)}, eager out tensor nums = {len(self.eager_forward_desire)}. \n'
)
raise RuntimeError(msg)
Expand Down Expand Up @@ -715,7 +715,7 @@ def check_eager_auto_parallel(self):
# check eager auto parallel forward
if len(actual_forward_res) != len(self.eager_forward_desire):
msg = (
f"The eager auto parallel out tensor nums is different with eager out tensor nums on {str(self.place)}."
f"The eager auto parallel out tensor nums is different with eager out tensor nums on {self.place}."
f'eager auto parallel out tensor nums = {len(actual_forward_res)}, eager out tensor nums = {len(self.eager_forward_desire)}. \n'
)
raise RuntimeError(msg)
Expand All @@ -741,7 +741,7 @@ def check_eager_auto_parallel(self):
# check eager auto parallel grad
if len(actual_grad_res) != len(self.eager_grad_desire):
msg = (
f"The eager auto parallel grad out tensor nums is different with eager grad out tensor nums on {str(self.place)}."
f"The eager auto parallel grad out tensor nums is different with eager grad out tensor nums on {self.place}."
f'eager auto parallel grad out tensor nums = {len(actual_grad_res)}, eager grad out tensor nums = {len(self.eager_grad_desire)}. \n'
)
raise RuntimeError(msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,22 @@ def _train(use_program_cache, max_iters=1):
run_time_with_cache = _train(
use_program_cache=True, max_iters=max_iters
)
print("run time with program cache: %f" % run_time_with_cache)
print(f"run time with program cache: {run_time_with_cache:f}")

run_time_without_cache = _train(
use_program_cache=False, max_iters=max_iters
)
print("run time without program cache: %f" % run_time_without_cache)
print(f"run time without program cache: {run_time_without_cache:f}")

run_time_with_cache = _train(
use_program_cache=True, max_iters=max_iters
)
print("run time with program cache: %f" % run_time_with_cache)
print(f"run time with program cache: {run_time_with_cache:f}")

run_time_with_cache = _train(
use_program_cache=True, max_iters=max_iters
)
print("run time with program cache: %f" % run_time_with_cache)
print(f"run time with program cache: {run_time_with_cache:f}")


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_fit_line_inference_model(self):

self.assertEqual(model.feed_var_names, ["x", "y"])
self.assertEqual(len(model.fetch_vars), 1)
print("fetch %s" % str(model.fetch_vars[0]))
print(f"fetch {model.fetch_vars[0]}")
self.assertEqual(expected, actual)

root_path.cleanup()
Expand Down
4 changes: 2 additions & 2 deletions test/deprecated/legacy_test/test_learning_rate_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def check_decay_with_place(
python_decayed_lr,
lr_val[0],
places=6,
msg=f'Failed lr scheduler is {python_decay_fn.__name__}, step {str(step)}, Python result is {str(python_decayed_lr)}, Fluid result is {str(lr_val[0])}',
msg=f'Failed lr scheduler is {python_decay_fn.__name__}, step {step}, Python result is {python_decayed_lr}, Fluid result is {lr_val[0]}',
)

def test_decay(self):
Expand Down Expand Up @@ -541,7 +541,7 @@ def check_decay_with_place(
self.assertAlmostEqual(
python_decayed_lr,
lr_val[0],
msg=f'Test {python_decay_fn.__name__} Failed, step {str(step)}, Python result is {str(python_decayed_lr)}, Fluid result is {str(lr_val[0])}',
msg=f'Test {python_decay_fn.__name__} Failed, step {step}, Python result is {python_decayed_lr}, Fluid result is {lr_val[0]}',
)


Expand Down
2 changes: 1 addition & 1 deletion test/deprecated/legacy_test/test_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def normalize(S, norm=np.inf, axis=0, threshold=None, fill=None):
return S

else:
raise Exception(f"Unsupported norm: {repr(norm)}")
raise Exception(f"Unsupported norm: {norm!r}")

# indices where norm is below the threshold
small_idx = length < threshold
Expand Down
4 changes: 2 additions & 2 deletions test/deprecated/quantization/test_imperative_qat.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def test_qat(self):
print('eval_acc_top1', eval_acc_top1)
self.assertTrue(
eval_acc_top1 > 0.9,
msg="The test acc {%f} is less than 0.9." % eval_acc_top1,
msg=f"The test acc {{{eval_acc_top1:f}}} is less than 0.9.",
)

# test the correctness of `paddle.jit.save`
Expand Down Expand Up @@ -207,7 +207,7 @@ def test_qat(self):
)
],
)
print('Quantized model saved in %s' % tmpdir)
print(f'Quantized model saved in {tmpdir}')

if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ def setUp(self):
try:
os.system("mkdir -p " + self.int8_model_path)
except Exception as e:
print(f"Failed to create {self.int8_model_path} due to {str(e)}")
print(f"Failed to create {self.int8_model_path} due to {e}")
sys.exit(-1)

def tearDown(self):
try:
os.system(f"rm -rf {self.int8_model_path}")
except Exception as e:
print(f"Failed to delete {self.int8_model_path} due to {str(e)}")
print(f"Failed to delete {self.int8_model_path} due to {e}")

def cache_unzipping(self, target_folder, zip_path):
cmd = f'tar xf {zip_path} -C {target_folder}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def quantize_to_int(
try:
os.system(f"rm -rf {save_model_dir}")
except Exception as e:
print(f"Failed to delete {save_model_dir} due to {str(e)}")
print(f"Failed to delete {save_model_dir} due to {e}")

def convert_to_fp16(
self,
Expand Down Expand Up @@ -166,7 +166,7 @@ def convert_to_fp16(
try:
os.system(f"rm -rf {save_model_dir}")
except Exception as e:
print(f"Failed to delete {save_model_dir} due to {str(e)}")
print(f"Failed to delete {save_model_dir} due to {e}")

def run_models(
self,
Expand Down
4 changes: 2 additions & 2 deletions test/dygraph_to_static/simnet_dygraph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def ops(self):
"""
fc = FC(
size=self.fc_dim,
param_attr=paddle.ParamAttr(name="%s.w" % self.name),
bias_attr=paddle.ParamAttr(name="%s.b" % self.name),
param_attr=paddle.ParamAttr(name=f"{self.name}.w"),
bias_attr=paddle.ParamAttr(name=f"{self.name}.b"),
act=self.act,
)
return fc
Expand Down
2 changes: 1 addition & 1 deletion test/dygraph_to_static/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def predict_dygraph(args, batch_generator):
if step_idx == STEP_NUM:
break
logging.info(
"Dygraph Predict: avg_speed: %.4f steps/s" % (np.mean(speed_list))
f"Dygraph Predict: avg_speed: {np.mean(speed_list):.4f} steps/s"
)
return seq_ids, seq_scores

Expand Down
6 changes: 3 additions & 3 deletions test/ir/inference/auto_scan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def assert_tensors_near(
for key, arr in tensor.items():
self.assertTrue(
baseline[key].shape == arr.shape,
f"The output shapes are not equal, the baseline shape is {baseline[key].shape}, but got {str(arr.shape)}",
f"The output shapes are not equal, the baseline shape is {baseline[key].shape}, but got {arr.shape}",
)
diff = abs(baseline[key] - arr)
np.testing.assert_allclose(
Expand Down Expand Up @@ -699,7 +699,7 @@ def assert_tensors_near(
self.assertEqual(
baseline[key].shape,
arr.shape,
f"The output shapes are not equal, the baseline shape is {baseline[key].shape}, but got {str(arr.shape)}",
f"The output shapes are not equal, the baseline shape is {baseline[key].shape}, but got {arr.shape}",
)
np.testing.assert_allclose(arr, baseline[key], rtol=rtol, atol=atol)

Expand Down Expand Up @@ -953,7 +953,7 @@ def run_test(self, quant=False, *args, **kwargs):
except Exception as e:
self.fail_log(
self.inference_config_str(pred_config)
+ f'\033[1;31m \nERROR INFO: {str(e)}\033[0m'
+ f'\033[1;31m \nERROR INFO: {e}\033[0m'
)
if not ignore_flag:
status = False
Expand Down
4 changes: 2 additions & 2 deletions test/mkldnn/test_pool2d_bf16_mkldnn_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def _get_padding_with_SAME(input_shape, pool_size, pool_stride):
padding_algorithm = padding_algorithm.upper()
if padding_algorithm not in ["SAME", "VALID", "EXPLICIT"]:
raise ValueError(
"Unknown Attr(padding_algorithm): '%s'. "
"It can only be 'SAME' or 'VALID'." % str(padding_algorithm)
f"Unknown Attr(padding_algorithm): '{padding_algorithm}'. "
"It can only be 'SAME' or 'VALID'."
)

if padding_algorithm == "VALID":
Expand Down
16 changes: 8 additions & 8 deletions test/quantization/test_imperative_ptq.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def func_ptq(self):
self.ptq.save_quantized_model(
model=quant_model, path=save_path, input_spec=input_spec
)
print('Quantized model saved in {%s}' % save_path)
print(f'Quantized model saved in {{{save_path}}}')

after_acc_top1 = self.model_test(
quant_model, self.batch_num, self.batch_size
Expand All @@ -242,9 +242,9 @@ def func_ptq(self):
paddle.disable_static()

# Check
print('Before converted acc_top1: %s' % before_acc_top1)
print('After converted acc_top1: %s' % after_acc_top1)
print('Infer acc_top1: %s' % infer_acc_top1)
print(f'Before converted acc_top1: {before_acc_top1}')
print(f'After converted acc_top1: {after_acc_top1}')
print(f'Infer acc_top1: {infer_acc_top1}')

self.assertTrue(
after_acc_top1 >= self.eval_acc_top1,
Expand Down Expand Up @@ -295,7 +295,7 @@ def func_ptq(self):
self.ptq.save_quantized_model(
model=quant_model, path=save_path, input_spec=input_spec
)
print('Quantized model saved in {%s}' % save_path)
print(f'Quantized model saved in {{{save_path}}}')

after_acc_top1 = self.model_test(
quant_model, self.batch_num, self.batch_size
Expand All @@ -308,9 +308,9 @@ def func_ptq(self):
paddle.disable_static()

# Check
print('Before converted acc_top1: %s' % before_acc_top1)
print('After converted acc_top1: %s' % after_acc_top1)
print('Infer acc_top1: %s' % infer_acc_top1)
print(f'Before converted acc_top1: {before_acc_top1}')
print(f'After converted acc_top1: {after_acc_top1}')
print(f'Infer acc_top1: {infer_acc_top1}')

# Check whether the quant_model is correct after converting.
# The acc of quantized model should be higher than 0.95.
Expand Down
2 changes: 1 addition & 1 deletion test/quantization/test_imperative_qat_amp.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def test_ptq(self):
paddle.static.InputSpec(shape=[None, 1, 28, 28], dtype='float32')
]
paddle.jit.save(layer=model, path=self.save_path, input_spec=input_spec)
print('Quantized model saved in {%s}' % self.save_path)
print(f'Quantized model saved in {{{self.save_path}}}')

end_time = time.time()
print("total time: %ss" % (end_time - start_time))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setUp(self):
try:
os.system("mkdir -p " + self.int8_model_path)
except Exception as e:
print(f"Failed to create {self.int8_model_path} due to {str(e)}")
print(f"Failed to create {self.int8_model_path} due to {e}")
sys.exit(-1)

def tearDown(self):
Expand Down
2 changes: 1 addition & 1 deletion test/quantization/test_post_training_quantization_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def setUp(self):
os.system("mkdir -p " + self.int8_model_path)
os.system("mkdir -p " + self.cache_folder)
except Exception as e:
print(f"Failed to create {self.int8_model_path} due to {str(e)}")
print(f"Failed to create {self.int8_model_path} due to {e}")
sys.exit(-1)

def tearDown(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def generate_quantized_model(
try:
os.system("mkdir " + self.int8_model)
except Exception as e:
_logger.info(f"Failed to create {self.int8_model} due to {str(e)}")
_logger.info(f"Failed to create {self.int8_model} due to {e}")
sys.exit(-1)

place = paddle.CPUPlace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def generate_quantized_model(
try:
os.system("mkdir " + self.int8_model)
except Exception as e:
print(f"Failed to create {self.int8_model} due to {str(e)}")
print(f"Failed to create {self.int8_model} due to {e}")
sys.exit(-1)

place = paddle.CPUPlace()
Expand Down

0 comments on commit 78632fa

Please sign in to comment.