Skip to content

Commit

Permalink
[CodeStyle][UP031] fix `/python/paddle/{hapi, geometric, jit, quantiz…
Browse files Browse the repository at this point in the history
…ation, utils}*` - part 15 (PaddlePaddle#65576)
  • Loading branch information
gouzil authored Jun 30, 2024
1 parent d7bb2a6 commit 16e38c7
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def SubstituteTemplate(template, values):
while changed:
changed = False
for key, value in values.items():
regex = "\\{%s\\}" % key
regex = f"\\{{{key}\\}}"
newtext = re.sub(regex, value, text)
if newtext != text:
changed = True
Expand Down Expand Up @@ -224,7 +224,7 @@ def generate_source_cu(
header_name = "autogen_tmp/arch_define.h"
if archs:
for arch in archs:
define_line = "#define USE_FPAINTB_GEMM_WITH_SM%s\n" % str(arch)
define_line = f"#define USE_FPAINTB_GEMM_WITH_SM{arch}\n"
header_all += define_line
with open(header_name, "w") as f:
f.write(header_all)
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/fusion/cutlass/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def SubstituteTemplate(template, values):
while changed:
changed = False
for key, value in values.items():
regex = "\\$\\{%s\\}" % key
regex = f"\\$\\{{{key}\\}}"
newtext = re.sub(regex, value, text)
if newtext != text:
changed = True
Expand Down
12 changes: 4 additions & 8 deletions python/paddle/geometric/message_passing/send_recv.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ def send_u_recv(

if reduce_op not in ["sum", "mean", "max", "min"]:
raise ValueError(
"reduce_op should be `sum`, `mean`, `max` or `min`, but received %s"
% reduce_op
f"reduce_op should be `sum`, `mean`, `max` or `min`, but received {reduce_op}"
)

# TODO(daisiming): Should we add judgement for out_size: max(dst_index) + 1.
Expand Down Expand Up @@ -291,14 +290,12 @@ def send_ue_recv(

if message_op not in ["add", "sub", "mul", "div"]:
raise ValueError(
"message_op should be `add`, `sub`, `mul`, `div`, but received %s"
% message_op
f"message_op should be `add`, `sub`, `mul`, `div`, but received {message_op}"
)

if reduce_op not in ["sum", "mean", "max", "min"]:
raise ValueError(
"reduce_op should be `sum`, `mean`, `max` or `min`, but received %s"
% reduce_op
f"reduce_op should be `sum`, `mean`, `max` or `min`, but received {reduce_op}"
)

x, y = reshape_lhs_rhs(x, y)
Expand Down Expand Up @@ -459,8 +456,7 @@ def send_uv(x, y, src_index, dst_index, message_op="add", name=None):

if message_op not in ['add', 'sub', 'mul', 'div']:
raise ValueError(
"message_op should be `add`, `sub`, `mul`, `div`, but received %s"
% message_op
f"message_op should be `add`, `sub`, `mul`, `div`, but received {message_op}"
)

x, y = reshape_lhs_rhs(x, y)
Expand Down
29 changes: 14 additions & 15 deletions python/paddle/hapi/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,16 @@ def on_epoch_begin(self, epoch=None, logs=None):

def _updates(self, logs, mode):
values = []
metrics = getattr(self, '%s_metrics' % (mode))
progbar = getattr(self, '%s_progbar' % (mode))
steps = getattr(self, '%s_step' % (mode))
metrics = getattr(self, f'{mode}_metrics')
progbar = getattr(self, f'{mode}_progbar')
steps = getattr(self, f'{mode}_step')

for k in metrics:
if k in logs:
values.append((k, logs[k]))

if self.verbose == 3 and hasattr(self, '_%s_timer' % (mode)):
timer = getattr(self, '_%s_timer' % (mode))
if self.verbose == 3 and hasattr(self, f'_{mode}_timer'):
timer = getattr(self, f'_{mode}_timer')
cnt = timer['count'] if timer['count'] > 0 else 1.0
samples = timer['samples'] if timer['samples'] > 0 else 1.0
values.append(
Expand Down Expand Up @@ -814,8 +814,8 @@ def __init__(
self.save_dir = None
if mode not in ['auto', 'min', 'max']:
warnings.warn(
'EarlyStopping mode %s is unknown, '
'fallback to auto mode.' % mode
f'EarlyStopping mode {mode} is unknown, '
'fallback to auto mode.'
)
mode = 'auto'
if mode == 'min':
Expand Down Expand Up @@ -870,8 +870,7 @@ def on_eval_end(self, logs=None):
print('Epoch %d: Early stopping.' % (self.stopped_epoch + 1))
if self.save_best_model and self.save_dir is not None:
print(
'Best checkpoint has been saved at %s'
% (
'Best checkpoint has been saved at {}'.format(
os.path.abspath(
os.path.join(self.save_dir, 'best_model')
)
Expand Down Expand Up @@ -946,8 +945,8 @@ def _updates(self, logs, mode):
visualdl = try_import('visualdl')
self.writer = visualdl.LogWriter(self.log_dir)

metrics = getattr(self, '%s_metrics' % (mode))
current_step = getattr(self, '%s_step' % (mode))
metrics = getattr(self, f'{mode}_metrics')
current_step = getattr(self, f'{mode}_step')

if mode == 'train':
total_step = current_step
Expand Down Expand Up @@ -1118,8 +1117,8 @@ def _updates(self, logs, mode):
if not self._is_write():
return

metrics = getattr(self, '%s_metrics' % (mode))
current_step = getattr(self, '%s_step' % (mode))
metrics = getattr(self, f'{mode}_metrics')
current_step = getattr(self, f'{mode}_step')

_metrics = {}

Expand Down Expand Up @@ -1271,8 +1270,8 @@ def _reset(self):
"""Resets wait counter and cooldown counter."""
if self.mode not in ['auto', 'min', 'max']:
warnings.warn(
'Learning rate reduction mode %s is unknown, '
'fallback to auto mode.' % self.mode
f'Learning rate reduction mode {self.mode} is unknown, '
'fallback to auto mode.'
)
self.mode = 'auto'
if self.mode == 'min' or (
Expand Down
3 changes: 1 addition & 2 deletions python/paddle/hapi/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2242,8 +2242,7 @@ def _save_inference_model(self, path):
)
if self._is_shape_inferred:
warnings.warn(
"'inputs' was not specified when Model initialization, so the input shape to be saved will be the shape derived from the user's actual inputs. The input shape to be saved is %s. For saving correct input shapes, please provide 'inputs' for Model initialization."
% self._input_info[0]
f"'inputs' was not specified when Model initialization, so the input shape to be saved will be the shape derived from the user's actual inputs. The input shape to be saved is {self._input_info[0]}. For saving correct input shapes, please provide 'inputs' for Model initialization."
)

paddle.jit.save(layer, path, input_spec=self._inputs)
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/hapi/model_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,12 @@ def _get_input_size(input_size, size):
f"Non-trainable params: {total_params - trainable_params:,}" + "\n"
)
summary_str += "-" * table_width['table_width'] + "\n"
summary_str += "Input size (MB): %0.2f" % total_input_size + "\n"
summary_str += f"Input size (MB): {total_input_size:0.2f}" + "\n"
summary_str += (
"Forward/backward pass size (MB): %0.2f" % total_output_size + "\n"
f"Forward/backward pass size (MB): {total_output_size:0.2f}" + "\n"
)
summary_str += "Params size (MB): %0.2f" % total_params_size + "\n"
summary_str += "Estimated Total Size (MB): %0.2f" % total_size + "\n"
summary_str += f"Params size (MB): {total_params_size:0.2f}" + "\n"
summary_str += f"Estimated Total Size (MB): {total_size:0.2f}" + "\n"
summary_str += "-" * table_width['table_width'] + "\n"

# return summary
Expand Down
22 changes: 11 additions & 11 deletions python/paddle/hapi/progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,16 @@ def convert_uint16_to_float(in_list):
sys.stdout.write(bar_chars)

for k, val in values:
info += ' - %s:' % k
info += f' - {k}:'
val = val if isinstance(val, list) else [val]
for i, v in enumerate(val):
if isinstance(v, (float, np.float32, np.float64)):
if abs(v) > 1e-3:
info += ' %.4f' % v
info += f' {v:.4f}'
else:
info += ' %.4e' % v
info += f' {v:.4e}'
else:
info += ' %s' % v
info += f' {v}'

if self._num is not None and current_num < self._num:
eta = time_per_unit * (self._num - current_num)
Expand All @@ -159,7 +159,7 @@ def convert_uint16_to_float(in_list):
else:
eta_format = '%ds' % eta

info += ' - ETA: %s' % eta_format
info += f' - ETA: {eta_format}'

info += fps
self._total_width += len(info)
Expand Down Expand Up @@ -187,25 +187,25 @@ def convert_uint16_to_float(in_list):
info = count + info

for k, val in values:
info += ' - %s:' % k
info += f' - {k}:'
val = val if isinstance(val, list) else [val]
for v in val:
if isinstance(v, (float, np.float32, np.float64)):
if abs(v) > 1e-3:
info += ' %.4f' % v
info += f' {v:.4f}'
else:
info += ' %.4e' % v
info += f' {v:.4e}'
elif (
isinstance(v, np.ndarray)
and v.size == 1
and v.dtype in [np.float32, np.float64]
):
if abs(v.item()) > 1e-3:
info += ' %.4f' % v.item()
info += f' {v.item():.4f}'
else:
info += ' %.4e' % v.item()
info += f' {v.item():.4e}'
else:
info += ' %s' % v
info += f' {v}'

info += fps
info += '\n'
Expand Down
6 changes: 3 additions & 3 deletions python/paddle/jit/dy2static/convert_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,11 @@ def new_false_fn():
"Unsupported return type of true_fn and false_fn in cond", str(e)
):
raise Dygraph2StaticException(
f"Your if/else have different return type. TODO: add link to modifty. {str(e)}"
f"Your if/else have different return type. TODO: add link to modifty. {e}"
)
if re.search("Incompatible return values of", str(e)):
raise Dygraph2StaticException(
f"Your if/else have different number of return value. TODO: add link to modifty. {str(e)}"
f"Your if/else have different number of return value. TODO: add link to modifty. {e}"
)
raise e
get_args = lambda: helper.get(union_name)
Expand Down Expand Up @@ -644,7 +644,7 @@ def convert_zip(*args):
if isinstance(arg, (Variable, Value)) and arg.shape[0] == -1:
raise RuntimeError(
"Not support zip(tensor, ...) when tensor.shape[0] == -1, "
f"but found args[{str(i)}].shape[0] == -1 in 'zip'"
f"but found args[{i}].shape[0] == -1 in 'zip'"
)
return zip(*args)

Expand Down
2 changes: 1 addition & 1 deletion python/paddle/jit/dy2static/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def _create_revise_suggestion(self, bottom_error_message):
for suggestion in self.suggestion_dict[keywords]:
suggestion_msg = (
' ' * BLANK_COUNT_BEFORE_FILE_STR * 2
+ f'{str(len(revise_suggestions) - 1)}. {suggestion}'
+ f'{len(revise_suggestions) - 1}. {suggestion}'
)
revise_suggestions.append(suggestion_msg)
return revise_suggestions if len(revise_suggestions) > 2 else []
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/jit/dy2static/export_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,5 @@ def pir_exporter(pp_layer, program, role, shared_inputs=None, inter_outs=None):
)
except Exception as e:
_logger.error(
f"Export subgraph failed: {e}\n. Received original program: {str(program)}"
f"Export subgraph failed: {e}\n. Received original program: {program}"
)
2 changes: 1 addition & 1 deletion python/paddle/jit/dy2static/function_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def _replace_to_input_spec_with_new_name(args, arg_names):
paddle.base.framework.Variable,
),
):
input_var.name = f"_jst.{str(order).zfill(order_digit)}.{name_prefix}.{str(index)}"
input_var.name = f"_jst.{str(order).zfill(order_digit)}.{name_prefix}.{index}"
index += 1
args_with_spec.append(input_var)
args_with_spec = paddle.utils.pack_sequence_as(args, args_with_spec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,10 +1002,10 @@ def make_stringify_guard(self) -> list[StringifyExpression]:
frame_value_tracer = self.tracker.trace_value_from_frame()

def format_dtype(dtype: np.dtype):
return f"np.{str(dtype)}"
return f"np.{dtype}"

def format_number(number: np.number):
return f"{format_dtype(number.dtype)}({str(number.item())})"
return f"{format_dtype(number.dtype)}({number.item()})"

return [
StringifyExpression(
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/jit/sot/symbolic/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def create_layer(self):
if inp in self.SIR.non_param_symbol:
meta = self.SIR.symbol_meta_map[inp]
forward_definition.append(
f" {self.name_gener(inp)}, # {str(meta)}"
f" {self.name_gener(inp)}, # {meta}"
)
forward_definition.append("):")

Expand Down
15 changes: 7 additions & 8 deletions python/paddle/quantization/imperative/qat.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def __init__(
assert (
not isinstance(layer, str)
and layer in self.fake_quant_input_layers
), ("%s is unsupported to be quantized." % layer)
), f"{layer} is unsupported to be quantized."

quantize_type = {
'abs_max',
Expand All @@ -352,14 +352,13 @@ def __init__(
weight_quantize_type != 'moving_average_abs_max'
and weight_quantize_type in quantize_type
), (
"Unsupported weight_quantize_type: %s. It can only "
"be abs_max or channel_wise_abs_max." % weight_quantize_type
f"Unsupported weight_quantize_type: {weight_quantize_type}. It can only "
"be abs_max or channel_wise_abs_max."
)
# TODO (jc): activation_quantize_type supports range_abs_max
assert activation_quantize_type in act_quantize_type, (
"Unsupported activation_quantize_type: %s. It can "
f"Unsupported activation_quantize_type: {activation_quantize_type}. It can "
"only be moving_average_abs_max or lsq_act now."
% activation_quantize_type
)

bits_check = (
Expand Down Expand Up @@ -436,9 +435,9 @@ def _get_input_quantized_layer(self, layer):
if isinstance(layer, value):
quant_layer_name = 'Quantized' + key
break
assert quant_layer_name is not None, (
"The layer %s is unsupported to be quantized." % layer.full_name()
)
assert (
quant_layer_name is not None
), f"The layer {layer.full_name()} is unsupported to be quantized."

return quant_layers.__dict__[quant_layer_name](layer, **self._kwargs)

Expand Down
2 changes: 1 addition & 1 deletion python/paddle/utils/cpp_extension/cpp_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ def run(self, *args, **kwargs):
if will_rename:
new_so_path = filename + "_pd_" + ext
if not os.path.exists(new_so_path):
os.rename(r'%s' % egg_file, r'%s' % new_so_path)
os.rename(rf'{egg_file}', rf'{new_so_path}')
assert os.path.exists(new_so_path)


Expand Down
26 changes: 15 additions & 11 deletions python/paddle/utils/cpp_extension/extension_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,21 +1209,25 @@ def _get_api_inputs_str(op_name):
# input name by `@`, and only use first substr as argument
params_list = ','.join([p.split("@")[0].lower() for p in param_names])
# e.g: {'X': x, 'Y': y, 'Z': z}
ins_map = "{%s}" % ','.join(
[
"'{}' : {}".format(in_name, in_name.split("@")[0].lower())
for in_name in in_names
]
ins_map = "{{{}}}".format(
','.join(
[
"'{}' : {}".format(in_name, in_name.split("@")[0].lower())
for in_name in in_names
]
)
)
# e.g: {'num': n}
attrs_map = "{%s}" % ",".join(
[
"'{}' : {}".format(attr_name, attr_name.split("@")[0].lower())
for attr_name in attr_names
]
attrs_map = "{{{}}}".format(
",".join(
[
"'{}' : {}".format(attr_name, attr_name.split("@")[0].lower())
for attr_name in attr_names
]
)
)
# e.g: ['Out', 'Index']
outs_list = "[%s]" % ','.join([f"'{name}'" for name in out_names])
outs_list = "[{}]".format(','.join([f"'{name}'" for name in out_names]))

inplace_reverse_idx = core.eager._get_custom_operator_inplace_map(op_name)

Expand Down
4 changes: 1 addition & 3 deletions python/paddle/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ def _get_download(url, fullname):
return fullname

except Exception as e: # requests.exceptions.ConnectionError
logger.info(
f"Downloading {fname} from {url} failed with exception {str(e)}"
)
logger.info(f"Downloading {fname} from {url} failed with exception {e}")
return False


Expand Down

0 comments on commit 16e38c7

Please sign in to comment.