Skip to content

Commit acc3bd9

Browse files
sguggerLysandreJik
andauthored
Enforce string-formatting with f-strings (#10980)
* First third * Styling and fix mistake * Quality * All the rest * Treat %s and %d * typo * Missing ) * Apply suggestions from code review Co-authored-by: Lysandre Debut <[email protected]> Co-authored-by: Lysandre Debut <[email protected]>
1 parent d0b3797 commit acc3bd9

File tree

224 files changed

+984
-1312
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

224 files changed

+984
-1312
lines changed

examples/language-modeling/run_clm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def main():
213213
transformers.utils.logging.set_verbosity_info()
214214
transformers.utils.logging.enable_default_handler()
215215
transformers.utils.logging.enable_explicit_format()
216-
logger.info("Training/evaluation parameters %s", training_args)
216+
logger.info(f"Training/evaluation parameters {training_args}")
217217

218218
# Set seed before initializing model.
219219
set_seed(training_args.seed)

examples/language-modeling/run_mlm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def main():
223223
transformers.utils.logging.set_verbosity_info()
224224
transformers.utils.logging.enable_default_handler()
225225
transformers.utils.logging.enable_explicit_format()
226-
logger.info("Training/evaluation parameters %s", training_args)
226+
logger.info(f"Training/evaluation parameters {training_args}")
227227

228228
# Set seed before initializing model.
229229
set_seed(training_args.seed)

examples/language-modeling/run_mlm_flax.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def step_fn(step):
307307
progress = jnp.maximum(0.0, (step - warmup_steps) / float(steps_per_cycle))
308308
ret *= jnp.maximum(0.0, 0.5 * (1.0 + jnp.cos(jnp.pi * (progress % 1.0))))
309309
else:
310-
raise ValueError("Unknown factor %s." % name)
310+
raise ValueError(f"Unknown factor {name}.")
311311
return jnp.asarray(ret, dtype=jnp.float32)
312312

313313
return step_fn
@@ -332,9 +332,7 @@ def accuracy(logits, targets, weights=None):
332332
Tuple of scalar loss and batch normalizing factor.
333333
"""
334334
if logits.ndim != targets.ndim + 1:
335-
raise ValueError(
336-
"Incorrect shapes. Got shape %s logits and %s targets" % (str(logits.shape), str(targets.shape))
337-
)
335+
raise ValueError(f"Incorrect shapes. Got shape {logits.shape} logits and {targets.shape} targets")
338336

339337
loss = jnp.equal(jnp.argmax(logits, axis=-1), targets)
340338
loss *= weights
@@ -353,9 +351,7 @@ def cross_entropy(logits, targets, weights=None, label_smoothing=0.0):
353351
Tuple of scalar loss and batch normalizing factor.
354352
"""
355353
if logits.ndim != targets.ndim + 1:
356-
raise ValueError(
357-
"Incorrect shapes. Got shape %s logits and %s targets" % (str(logits.shape), str(targets.shape))
358-
)
354+
raise ValueError(f"Incorrect shapes. Got shape {logits.shape} logits and {targets.shape} targets")
359355

360356
vocab_size = logits.shape[-1]
361357
confidence = 1.0 - label_smoothing
@@ -463,7 +459,7 @@ def generate_batch_splits(samples_idx: jnp.ndarray, batch_size: int) -> jnp.ndar
463459
)
464460

465461
# Set the verbosity to info of the Transformers logger (on main process only):
466-
logger.info("Training/evaluation parameters %s", training_args)
462+
logger.info(f"Training/evaluation parameters {training_args}")
467463

468464
# Set seed before initializing model.
469465
set_seed(training_args.seed)

examples/language-modeling/run_plm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def main():
220220
transformers.utils.logging.set_verbosity_info()
221221
transformers.utils.logging.enable_default_handler()
222222
transformers.utils.logging.enable_explicit_format()
223-
logger.info("Training/evaluation parameters %s", training_args)
223+
logger.info(f"Training/evaluation parameters {training_args}")
224224

225225
# Set seed before initializing model.
226226
set_seed(training_args.seed)

examples/multiple-choice/run_swag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def main():
247247
transformers.utils.logging.set_verbosity_info()
248248
transformers.utils.logging.enable_default_handler()
249249
transformers.utils.logging.enable_explicit_format()
250-
logger.info("Training/evaluation parameters %s", training_args)
250+
logger.info(f"Training/evaluation parameters {training_args}")
251251

252252
# Set seed before initializing model.
253253
set_seed(training_args.seed)

examples/multiple-choice/run_tf_multiple_choice.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,10 @@ def main():
116116
level=logging.INFO,
117117
)
118118
logger.warning(
119-
"device: %s, n_replicas: %s, 16-bits training: %s",
120-
training_args.device,
121-
training_args.n_replicas,
122-
training_args.fp16,
119+
f"device: {training_args.device}, n_replicas: {training_args.n_replicas}, "
120+
f"16-bits training: {training_args.fp16}"
123121
)
124-
logger.info("Training/evaluation parameters %s", training_args)
122+
logger.info(f"Training/evaluation parameters {training_args}")
125123

126124
# Set seed
127125
set_seed(training_args.seed)
@@ -131,7 +129,7 @@ def main():
131129
label_list = processor.get_labels()
132130
num_labels = len(label_list)
133131
except KeyError:
134-
raise ValueError("Task not found: %s" % (data_args.task_name))
132+
raise ValueError(f"Task not found: {data_args.task_name}")
135133

136134
# Load pretrained model and tokenizer
137135
#
@@ -210,8 +208,8 @@ def compute_metrics(p: EvalPrediction) -> Dict:
210208
with open(output_eval_file, "w") as writer:
211209
logger.info("***** Eval results *****")
212210
for key, value in result.items():
213-
logger.info(" %s = %s", key, value)
214-
writer.write("%s = %s\n" % (key, value))
211+
logger.info(f" {key} = {value}")
212+
writer.write(f"{key} = {value}\n")
215213

216214
results.update(result)
217215

examples/multiple-choice/utils_multiple_choice.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,7 @@ def __init__(
9999
processor = processors[task]()
100100

101101
cached_features_file = os.path.join(
102-
data_dir,
103-
"cached_{}_{}_{}_{}".format(
104-
mode.value,
105-
tokenizer.__class__.__name__,
106-
str(max_seq_length),
107-
task,
108-
),
102+
data_dir, f"cached_{mode.value}_{tokenizer.__class__.__name__}_{max_seq_length}_{task}"
109103
)
110104

111105
# Make sure only the first process in distributed training processes the dataset,
@@ -125,14 +119,14 @@ def __init__(
125119
examples = processor.get_test_examples(data_dir)
126120
else:
127121
examples = processor.get_train_examples(data_dir)
128-
logger.info("Training examples: %s", len(examples))
122+
logger.info(f"Training examples: {len(examples)}")
129123
self.features = convert_examples_to_features(
130124
examples,
131125
label_list,
132126
max_seq_length,
133127
tokenizer,
134128
)
135-
logger.info("Saving features into cached file %s", cached_features_file)
129+
logger.info(f"Saving features into cached file {cached_features_file}")
136130
torch.save(self.features, cached_features_file)
137131

138132
def __len__(self):
@@ -172,7 +166,7 @@ def __init__(
172166
examples = processor.get_test_examples(data_dir)
173167
else:
174168
examples = processor.get_train_examples(data_dir)
175-
logger.info("Training examples: %s", len(examples))
169+
logger.info(f"Training examples: {len(examples)}")
176170

177171
self.features = convert_examples_to_features(
178172
examples,
@@ -184,7 +178,7 @@ def __init__(
184178
def gen():
185179
for (ex_index, ex) in tqdm.tqdm(enumerate(self.features), desc="convert examples to features"):
186180
if ex_index % 10000 == 0:
187-
logger.info("Writing example %d of %d" % (ex_index, len(examples)))
181+
logger.info(f"Writing example {ex_index} of {len(examples)}")
188182

189183
yield (
190184
{
@@ -255,7 +249,7 @@ class RaceProcessor(DataProcessor):
255249

256250
def get_train_examples(self, data_dir):
257251
"""See base class."""
258-
logger.info("LOOKING AT {} train".format(data_dir))
252+
logger.info(f"LOOKING AT {data_dir} train")
259253
high = os.path.join(data_dir, "train/high")
260254
middle = os.path.join(data_dir, "train/middle")
261255
high = self._read_txt(high)
@@ -264,7 +258,7 @@ def get_train_examples(self, data_dir):
264258

265259
def get_dev_examples(self, data_dir):
266260
"""See base class."""
267-
logger.info("LOOKING AT {} dev".format(data_dir))
261+
logger.info(f"LOOKING AT {data_dir} dev")
268262
high = os.path.join(data_dir, "dev/high")
269263
middle = os.path.join(data_dir, "dev/middle")
270264
high = self._read_txt(high)
@@ -273,7 +267,7 @@ def get_dev_examples(self, data_dir):
273267

274268
def get_test_examples(self, data_dir):
275269
"""See base class."""
276-
logger.info("LOOKING AT {} test".format(data_dir))
270+
logger.info(f"LOOKING AT {data_dir} test")
277271
high = os.path.join(data_dir, "test/high")
278272
middle = os.path.join(data_dir, "test/middle")
279273
high = self._read_txt(high)
@@ -298,7 +292,7 @@ def _create_examples(self, lines, set_type):
298292
"""Creates examples for the training and dev sets."""
299293
examples = []
300294
for (_, data_raw) in enumerate(lines):
301-
race_id = "%s-%s" % (set_type, data_raw["race_id"])
295+
race_id = f"{set_type}-{data_raw['race_id']}"
302296
article = data_raw["article"]
303297
for i in range(len(data_raw["answers"])):
304298
truth = str(ord(data_raw["answers"][i]) - ord("A"))
@@ -322,17 +316,17 @@ class SynonymProcessor(DataProcessor):
322316

323317
def get_train_examples(self, data_dir):
324318
"""See base class."""
325-
logger.info("LOOKING AT {} train".format(data_dir))
319+
logger.info(f"LOOKING AT {data_dir} train")
326320
return self._create_examples(self._read_csv(os.path.join(data_dir, "mctrain.csv")), "train")
327321

328322
def get_dev_examples(self, data_dir):
329323
"""See base class."""
330-
logger.info("LOOKING AT {} dev".format(data_dir))
324+
logger.info(f"LOOKING AT {data_dir} dev")
331325
return self._create_examples(self._read_csv(os.path.join(data_dir, "mchp.csv")), "dev")
332326

333327
def get_test_examples(self, data_dir):
334328
"""See base class."""
335-
logger.info("LOOKING AT {} dev".format(data_dir))
329+
logger.info(f"LOOKING AT {data_dir} dev")
336330

337331
return self._create_examples(self._read_csv(os.path.join(data_dir, "mctest.csv")), "test")
338332

@@ -368,17 +362,17 @@ class SwagProcessor(DataProcessor):
368362

369363
def get_train_examples(self, data_dir):
370364
"""See base class."""
371-
logger.info("LOOKING AT {} train".format(data_dir))
365+
logger.info(f"LOOKING AT {data_dir} train")
372366
return self._create_examples(self._read_csv(os.path.join(data_dir, "train.csv")), "train")
373367

374368
def get_dev_examples(self, data_dir):
375369
"""See base class."""
376-
logger.info("LOOKING AT {} dev".format(data_dir))
370+
logger.info(f"LOOKING AT {data_dir} dev")
377371
return self._create_examples(self._read_csv(os.path.join(data_dir, "val.csv")), "dev")
378372

379373
def get_test_examples(self, data_dir):
380374
"""See base class."""
381-
logger.info("LOOKING AT {} dev".format(data_dir))
375+
logger.info(f"LOOKING AT {data_dir} dev")
382376
raise ValueError(
383377
"For swag testing, the input file does not contain a label column. It can not be tested in current code"
384378
"setting!"
@@ -419,16 +413,16 @@ class ArcProcessor(DataProcessor):
419413

420414
def get_train_examples(self, data_dir):
421415
"""See base class."""
422-
logger.info("LOOKING AT {} train".format(data_dir))
416+
logger.info(f"LOOKING AT {data_dir} train")
423417
return self._create_examples(self._read_json(os.path.join(data_dir, "train.jsonl")), "train")
424418

425419
def get_dev_examples(self, data_dir):
426420
"""See base class."""
427-
logger.info("LOOKING AT {} dev".format(data_dir))
421+
logger.info(f"LOOKING AT {data_dir} dev")
428422
return self._create_examples(self._read_json(os.path.join(data_dir, "dev.jsonl")), "dev")
429423

430424
def get_test_examples(self, data_dir):
431-
logger.info("LOOKING AT {} test".format(data_dir))
425+
logger.info(f"LOOKING AT {data_dir} test")
432426
return self._create_examples(self._read_json(os.path.join(data_dir, "test.jsonl")), "test")
433427

434428
def get_labels(self):
@@ -450,7 +444,7 @@ def normalize(truth):
450444
elif truth in "1234":
451445
return int(truth) - 1
452446
else:
453-
logger.info("truth ERROR! %s", str(truth))
447+
logger.info(f"truth ERROR! {truth}")
454448
return None
455449

456450
examples = []
@@ -496,11 +490,11 @@ def normalize(truth):
496490
if type == "train":
497491
assert len(examples) > 1
498492
assert examples[0].label is not None
499-
logger.info("len examples: %s}", str(len(examples)))
500-
logger.info("Three choices: %s", str(three_choice))
501-
logger.info("Five choices: %s", str(five_choice))
502-
logger.info("Other choices: %s", str(other_choices))
503-
logger.info("four choices: %s", str(four_choice))
493+
logger.info(f"len examples: {len(examples)}")
494+
logger.info(f"Three choices: {three_choice}")
495+
logger.info(f"Five choices: {five_choice}")
496+
logger.info(f"Other choices: {other_choices}")
497+
logger.info(f"four choices: {four_choice}")
504498

505499
return examples
506500

@@ -520,7 +514,7 @@ def convert_examples_to_features(
520514
features = []
521515
for (ex_index, example) in tqdm.tqdm(enumerate(examples), desc="convert examples to features"):
522516
if ex_index % 10000 == 0:
523-
logger.info("Writing example %d of %d" % (ex_index, len(examples)))
517+
logger.info(f"Writing example {ex_index} of {len(examples)}")
524518
choices_inputs = []
525519
for ending_idx, (context, ending) in enumerate(zip(example.contexts, example.endings)):
526520
text_a = context
@@ -570,7 +564,7 @@ def convert_examples_to_features(
570564

571565
for f in features[:2]:
572566
logger.info("*** Example ***")
573-
logger.info("feature: %s" % f)
567+
logger.info("feature: {f}")
574568

575569
return features
576570

examples/question-answering/run_qa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def main():
240240
transformers.utils.logging.set_verbosity_info()
241241
transformers.utils.logging.enable_default_handler()
242242
transformers.utils.logging.enable_explicit_format()
243-
logger.info("Training/evaluation parameters %s", training_args)
243+
logger.info(f"Training/evaluation parameters {training_args}")
244244

245245
# Set seed before initializing model.
246246
set_seed(training_args.seed)

examples/question-answering/run_qa_beam_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def main():
239239
transformers.utils.logging.set_verbosity_info()
240240
transformers.utils.logging.enable_default_handler()
241241
transformers.utils.logging.enable_explicit_format()
242-
logger.info("Training/evaluation parameters %s", training_args)
242+
logger.info(f"Training/evaluation parameters {training_args}")
243243

244244
# Set seed before initializing model.
245245
set_seed(training_args.seed)

examples/question-answering/run_tf_squad.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,10 @@ def main():
148148
level=logging.INFO,
149149
)
150150
logger.info(
151-
"n_replicas: %s, distributed training: %s, 16-bits training: %s",
152-
training_args.n_replicas,
153-
bool(training_args.n_replicas > 1),
154-
training_args.fp16,
151+
f"n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1)}, "
152+
f"16-bits training: {training_args.fp16}"
155153
)
156-
logger.info("Training/evaluation parameters %s", training_args)
154+
logger.info(f"Training/evaluation parameters {training_args}")
157155

158156
# Prepare Question-Answering task
159157
# Load pretrained model and tokenizer

0 commit comments

Comments
 (0)