Fix TypeError: Object of type int64 is not JSON serializable (#24340)

* Fix TypeError: Object of type int64 is not JSON serializable

* Convert numpy.float64 and numpy.int64 to float and int for json serialization

* Black reformatted examples/pytorch/token-classification/run_ner_no_trainer.py

* * make style
This commit is contained in:
Xiaoli Wang 2023-06-27 19:15:49 +08:00 committed by GitHub
parent ac19871ce2
commit 239ace152b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
44 changed files with 74 additions and 71 deletions

View File

@ -28,6 +28,7 @@ from pathlib import Path
import datasets import datasets
import evaluate import evaluate
import numpy as np
import torch import torch
from accelerate import Accelerator from accelerate import Accelerator
from accelerate.logging import get_logger from accelerate.logging import get_logger
@ -777,6 +778,12 @@ def main():
if args.with_tracking: if args.with_tracking:
all_results.update({"train_loss": total_loss.item() / len(train_dataloader)}) all_results.update({"train_loss": total_loss.item() / len(train_dataloader)})
with open(os.path.join(args.output_dir, "all_results.json"), "w") as f: with open(os.path.join(args.output_dir, "all_results.json"), "w") as f:
# Convert all float64 & int64 type numbers to float & int for json serialization
for key, value in all_results.items():
if isinstance(value, np.float64):
all_results[key] = float(value)
elif isinstance(value, np.int64):
all_results[key] = int(value)
json.dump(all_results, f) json.dump(all_results, f)

View File

@ -60,7 +60,7 @@ class EndOfFunctionCriteria(StoppingCriteria):
decoded_generations = self.tokenizer.batch_decode(input_ids[:, self.start_length :]) decoded_generations = self.tokenizer.batch_decode(input_ids[:, self.start_length :])
done = [] done = []
for decoded_generation in decoded_generations: for decoded_generation in decoded_generations:
done.append(any([stop_string in decoded_generation for stop_string in self.eof_strings])) done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings))
return all(done) return all(done)

View File

@ -17,7 +17,7 @@ class FSNERTokenizerUtils(object):
`transformers.tokenization_utils_base.BatchEncoding` dict with additional keys and values for start_token_id, end_token_id and sizes of example lists for each entity type `transformers.tokenization_utils_base.BatchEncoding` dict with additional keys and values for start_token_id, end_token_id and sizes of example lists for each entity type
""" """
if isinstance(x, list) and all([isinstance(_x, list) for _x in x]): if isinstance(x, list) and all(isinstance(_x, list) for _x in x):
d = None d = None
for l in x: for l in x:
t = self.tokenizer( t = self.tokenizer(
@ -37,7 +37,7 @@ class FSNERTokenizerUtils(object):
d["start_token_id"] = torch.tensor(self.tokenizer.convert_tokens_to_ids("[E]")) d["start_token_id"] = torch.tensor(self.tokenizer.convert_tokens_to_ids("[E]"))
d["end_token_id"] = torch.tensor(self.tokenizer.convert_tokens_to_ids("[/E]")) d["end_token_id"] = torch.tensor(self.tokenizer.convert_tokens_to_ids("[/E]"))
elif isinstance(x, list) and all([isinstance(_x, str) for _x in x]): elif isinstance(x, list) and all(isinstance(_x, str) for _x in x):
d = self.tokenizer( d = self.tokenizer(
x, x,
padding="max_length", padding="max_length",

View File

@ -50,7 +50,7 @@ def _get_single_answer(example):
answer["remove_it"] = False answer["remove_it"] = False
cols = ["start_token", "end_token", "start_byte", "end_byte", "text"] cols = ["start_token", "end_token", "start_byte", "end_byte", "text"]
if not all([isinstance(answer[k], list) for k in cols]): if not all(isinstance(answer[k], list) for k in cols):
raise ValueError("Issue in ID", example["id"]) raise ValueError("Issue in ID", example["id"])
return answer return answer

View File

@ -610,7 +610,7 @@ def main():
predicted_sequence = [label_list[0]] * len(true_tags) predicted_sequence = [label_list[0]] * len(true_tags)
for _, span, label in sorted(predictions, key=lambda o: o[0], reverse=True): for _, span, label in sorted(predictions, key=lambda o: o[0], reverse=True):
if all([o == label_list[0] for o in predicted_sequence[span[0] : span[1]]]): if all(o == label_list[0] for o in predicted_sequence[span[0] : span[1]]):
predicted_sequence[span[0]] = label predicted_sequence[span[0]] = label
if span[1] - span[0] > 1: if span[1] - span[0] > 1:
predicted_sequence[span[0] + 1 : span[1]] = [label] * (span[1] - span[0] - 1) predicted_sequence[span[0] + 1 : span[1]] = [label] * (span[1] - span[0] - 1)

View File

@ -554,8 +554,8 @@ class Matcher(object):
assert thresholds[0] > 0 assert thresholds[0] > 0
thresholds.insert(0, -float("inf")) thresholds.insert(0, -float("inf"))
thresholds.append(float("inf")) thresholds.append(float("inf"))
assert all([low <= high for (low, high) in zip(thresholds[:-1], thresholds[1:])]) assert all(low <= high for (low, high) in zip(thresholds[:-1], thresholds[1:]))
assert all([label_i in [-1, 0, 1] for label_i in labels]) assert all(label_i in [-1, 0, 1] for label_i in labels)
assert len(labels) == len(thresholds) - 1 assert len(labels) == len(thresholds) - 1
self.thresholds = thresholds self.thresholds = thresholds
self.labels = labels self.labels = labels

View File

@ -554,8 +554,8 @@ class Matcher(object):
assert thresholds[0] > 0 assert thresholds[0] > 0
thresholds.insert(0, -float("inf")) thresholds.insert(0, -float("inf"))
thresholds.append(float("inf")) thresholds.append(float("inf"))
assert all([low <= high for (low, high) in zip(thresholds[:-1], thresholds[1:])]) assert all(low <= high for (low, high) in zip(thresholds[:-1], thresholds[1:]))
assert all([label_i in [-1, 0, 1] for label_i in labels]) assert all(label_i in [-1, 0, 1] for label_i in labels)
assert len(labels) == len(thresholds) - 1 assert len(labels) == len(thresholds) - 1
self.thresholds = thresholds self.thresholds = thresholds
self.labels = labels self.labels = labels

View File

@ -110,7 +110,7 @@ class MinLengthLogitsProcessor(LogitsProcessor):
if isinstance(eos_token_id, int): if isinstance(eos_token_id, int):
eos_token_id = [eos_token_id] eos_token_id = [eos_token_id]
if not all([isinstance(i, int) for i in eos_token_id]) or any([i < 0 for i in eos_token_id]): if not all(isinstance(i, int) for i in eos_token_id) or any(i < 0 for i in eos_token_id):
logger.warning(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}") logger.warning(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}")
self.min_length = min_length self.min_length = min_length
@ -147,7 +147,7 @@ class MinNewTokensLengthLogitsProcessor(LogitsProcessor):
if isinstance(eos_token_id, int): if isinstance(eos_token_id, int):
eos_token_id = [eos_token_id] eos_token_id = [eos_token_id]
if not all([isinstance(i, int) for i in eos_token_id]) or any([i < 0 for i in eos_token_id]): if not all(isinstance(i, int) for i in eos_token_id) or any(i < 0 for i in eos_token_id):
logger.warning(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}") logger.warning(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}")
self.prompt_length_to_skip = prompt_length_to_skip self.prompt_length_to_skip = prompt_length_to_skip
@ -731,7 +731,7 @@ class NoBadWordsLogitsProcessor(SequenceBiasLogitsProcessor):
if isinstance(eos_token_id, int): if isinstance(eos_token_id, int):
eos_token_id = [eos_token_id] eos_token_id = [eos_token_id]
bad_words_ids = list( bad_words_ids = list(
filter(lambda bad_token_seq: all([bad_token_seq != [i] for i in eos_token_id]), bad_words_ids) filter(lambda bad_token_seq: all(bad_token_seq != [i] for i in eos_token_id), bad_words_ids)
) )
# Forbidding a sequence is equivalent to setting its bias to -inf # Forbidding a sequence is equivalent to setting its bias to -inf

View File

@ -318,7 +318,7 @@ class TFNoBadWordsLogitsProcessor(TFLogitsProcessor):
self.bad_word_seqs_ids = tf.ragged.constant(bad_words_ids).to_tensor(default_value=-1) self.bad_word_seqs_ids = tf.ragged.constant(bad_words_ids).to_tensor(default_value=-1)
# 2. a tensor with the unpadded length of each forbidden sequence, for quick length comparisons # 2. a tensor with the unpadded length of each forbidden sequence, for quick length comparisons
bad_word_seqs_len = [len(bad_words) for bad_words in bad_words_ids] bad_word_seqs_len = [len(bad_words) for bad_words in bad_words_ids]
if any([word_len == 0 for word_len in bad_word_seqs_len]): if any(word_len == 0 for word_len in bad_word_seqs_len):
raise ValueError(f"Banned words token sequences {bad_words_ids} cannot have an empty list") raise ValueError(f"Banned words token sequences {bad_words_ids} cannot have an empty list")
self.bad_word_seqs_len = tf.convert_to_tensor(bad_word_seqs_len, dtype=tf.int32) self.bad_word_seqs_len = tf.convert_to_tensor(bad_word_seqs_len, dtype=tf.int32)
# 3. a tensor containing the last token for each sequence, for easy access to the tokens that may be banned # 3. a tensor containing the last token for each sequence, for easy access to the tokens that may be banned

View File

@ -1638,7 +1638,7 @@ class TFGenerationMixin:
# TODO (Joao): fix cache format or find programatic way to detect cache index # TODO (Joao): fix cache format or find programatic way to detect cache index
# GPT2 and other models has a slightly different cache structure, with a different batch axis # GPT2 and other models has a slightly different cache structure, with a different batch axis
model_name = str(self.decoder) if "EncoderDecoder" in str(self) else str(self) model_name = str(self.decoder) if "EncoderDecoder" in str(self) else str(self)
cache_batch_axis = 1 if any([model_prefix in model_name for model_prefix in ("TFGPT2", "TFCTRL")]) else 0 cache_batch_axis = 1 if any(model_prefix in model_name for model_prefix in ("TFGPT2", "TFCTRL")) else 0
# some models, like XLNet, need more than the last token in the presence of past_key_values # some models, like XLNet, need more than the last token in the presence of past_key_values
needs_full_input = "use_mems" in set(inspect.signature(self.prepare_inputs_for_generation).parameters.keys()) needs_full_input = "use_mems" in set(inspect.signature(self.prepare_inputs_for_generation).parameters.keys())
@ -1922,7 +1922,7 @@ class TFGenerationMixin:
# TODO (Joao): fix cache format or find programatic way to detect cache index # TODO (Joao): fix cache format or find programatic way to detect cache index
# GPT2 and other models has a slightly different cache structure, with a different batch axis # GPT2 and other models has a slightly different cache structure, with a different batch axis
model_name = str(self.decoder) if "EncoderDecoder" in str(self) else str(self) model_name = str(self.decoder) if "EncoderDecoder" in str(self) else str(self)
cache_batch_axis = 1 if any([model_prefix in model_name for model_prefix in ("TFGPT2", "TFCTRL")]) else 0 cache_batch_axis = 1 if any(model_prefix in model_name for model_prefix in ("TFGPT2", "TFCTRL")) else 0
# some models, like XLNet, need more than the last token in the presence of past_key_values # some models, like XLNet, need more than the last token in the presence of past_key_values
needs_full_input = "use_mems" in set(inspect.signature(self.prepare_inputs_for_generation).parameters.keys()) needs_full_input = "use_mems" in set(inspect.signature(self.prepare_inputs_for_generation).parameters.keys())
@ -2265,7 +2265,7 @@ class TFGenerationMixin:
# TODO (Joao): fix cache format or find programatic way to detect cache index # TODO (Joao): fix cache format or find programatic way to detect cache index
# GPT2 and other models has a slightly different cache structure, with a different batch axis # GPT2 and other models has a slightly different cache structure, with a different batch axis
model_name = str(self.decoder) if "EncoderDecoder" in str(self) else str(self) model_name = str(self.decoder) if "EncoderDecoder" in str(self) else str(self)
cache_batch_axis = 1 if any([model_prefix in model_name for model_prefix in ("TFGPT2", "TFCTRL")]) else 0 cache_batch_axis = 1 if any(model_prefix in model_name for model_prefix in ("TFGPT2", "TFCTRL")) else 0
# some models, like XLNet, need more than the last token in the presence of past_key_values # some models, like XLNet, need more than the last token in the presence of past_key_values
needs_full_input = "use_mems" in set(inspect.signature(self.prepare_inputs_for_generation).parameters.keys()) needs_full_input = "use_mems" in set(inspect.signature(self.prepare_inputs_for_generation).parameters.keys())
@ -2779,7 +2779,7 @@ class TFGenerationMixin:
# TODO (Joao): fix cache format or find programatic way to detect cache index # TODO (Joao): fix cache format or find programatic way to detect cache index
# GPT2 and other models has a slightly different cache structure, with a different batch axis # GPT2 and other models has a slightly different cache structure, with a different batch axis
model_name = str(self.decoder) if "EncoderDecoder" in str(self) else str(self) model_name = str(self.decoder) if "EncoderDecoder" in str(self) else str(self)
cache_batch_axis = 1 if any([model_prefix in model_name for model_prefix in ("TFGPT2", "TFCTRL")]) else 0 cache_batch_axis = 1 if any(model_prefix in model_name for model_prefix in ("TFGPT2", "TFCTRL")) else 0
# 2. init `attentions`, `hidden_states`, and `scores` tuples # 2. init `attentions`, `hidden_states`, and `scores` tuples
scores = [] if (return_dict_in_generate and output_scores) else None scores = [] if (return_dict_in_generate and output_scores) else None

View File

@ -144,7 +144,7 @@ class KerasMetricCallback(Callback):
@staticmethod @staticmethod
def _concatenate_batches(batches, padding_index=-100): def _concatenate_batches(batches, padding_index=-100):
# If all batches are unidimensional or same length, do a simple concatenation # If all batches are unidimensional or same length, do a simple concatenation
if batches[0].ndim == 1 or all([batch.shape[1] == batches[0].shape[1] for batch in batches]): if batches[0].ndim == 1 or all(batch.shape[1] == batches[0].shape[1] for batch in batches):
return np.concatenate(batches, axis=0) return np.concatenate(batches, axis=0)
# Welp, they're not the same length. Let's do some padding # Welp, they're not the same length. Let's do some padding

View File

@ -78,7 +78,7 @@ def convert_pytorch_checkpoint_to_tf(model: BertModel, ckpt_dir: str, model_name
for var_name in state_dict: for var_name in state_dict:
tf_name = to_tf_var_name(var_name) tf_name = to_tf_var_name(var_name)
torch_tensor = state_dict[var_name].numpy() torch_tensor = state_dict[var_name].numpy()
if any([x in var_name for x in tensors_to_transpose]): if any(x in var_name for x in tensors_to_transpose):
torch_tensor = torch_tensor.T torch_tensor = torch_tensor.T
tf_var = create_tf_var(tensor=torch_tensor, name=tf_name, session=session) tf_var = create_tf_var(tensor=torch_tensor, name=tf_name, session=session)
tf.keras.backend.set_value(tf_var, torch_tensor) tf.keras.backend.set_value(tf_var, torch_tensor)

View File

@ -104,7 +104,7 @@ def convert_bigbird_pegasus(tf_weights: dict, config_update: dict) -> BigBirdPeg
new_k = rename_state_dict_key(k, patterns) new_k = rename_state_dict_key(k, patterns)
if new_k not in state_dict: if new_k not in state_dict:
raise ValueError(f"could not find new key {new_k} in state dict. (converted from {k})") raise ValueError(f"could not find new key {new_k} in state dict. (converted from {k})")
if any([True if i in k else False for i in ["dense", "query", "key", "value"]]): if any(True if i in k else False for i in ["dense", "query", "key", "value"]):
v = v.T v = v.T
mapping[new_k] = torch.from_numpy(v) mapping[new_k] = torch.from_numpy(v)
assert v.shape == state_dict[new_k].shape, f"{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}" assert v.shape == state_dict[new_k].shape, f"{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}"
@ -117,7 +117,7 @@ def convert_bigbird_pegasus(tf_weights: dict, config_update: dict) -> BigBirdPeg
new_k = rename_state_dict_key(k, patterns) new_k = rename_state_dict_key(k, patterns)
if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings":
raise ValueError(f"could not find new key {new_k} in state dict. (converted from {k})") raise ValueError(f"could not find new key {new_k} in state dict. (converted from {k})")
if any([True if i in k else False for i in ["dense", "query", "key", "value"]]): if any(True if i in k else False for i in ["dense", "query", "key", "value"]):
v = v.T v = v.T
mapping[new_k] = torch.from_numpy(v) mapping[new_k] = torch.from_numpy(v)
if k != "pegasus/embeddings/position_embeddings": if k != "pegasus/embeddings/position_embeddings":
@ -147,7 +147,7 @@ def get_tf_weights_as_numpy(path) -> Dict:
tf_weights = {} tf_weights = {}
ignore_name = ["global_step"] ignore_name = ["global_step"]
for name, shape in tqdm(init_vars, desc="converting tf checkpoint to dict"): for name, shape in tqdm(init_vars, desc="converting tf checkpoint to dict"):
skip_key = any([pat in name for pat in ignore_name]) skip_key = any(pat in name for pat in ignore_name)
if skip_key: if skip_key:
continue continue
array = tf.train.load_variable(path, name) array = tf.train.load_variable(path, name)

View File

@ -2485,9 +2485,9 @@ class DetaMatcher(object):
thresholds.insert(0, -float("inf")) thresholds.insert(0, -float("inf"))
thresholds.append(float("inf")) thresholds.append(float("inf"))
# Currently torchscript does not support all + generator # Currently torchscript does not support all + generator
if not all([low <= high for (low, high) in zip(thresholds[:-1], thresholds[1:])]): if not all(low <= high for (low, high) in zip(thresholds[:-1], thresholds[1:])):
raise ValueError("Thresholds should be sorted.") raise ValueError("Thresholds should be sorted.")
if not all([l in [-1, 0, 1] for l in labels]): if not all(l in [-1, 0, 1] for l in labels):
raise ValueError("All labels should be either -1, 0 or 1") raise ValueError("All labels should be either -1, 0 or 1")
if len(labels) != len(thresholds) - 1: if len(labels) != len(thresholds) - 1:
raise ValueError("Number of labels should be equal to number of thresholds - 1") raise ValueError("Number of labels should be equal to number of thresholds - 1")

View File

@ -379,11 +379,9 @@ class CustomDPRReaderTokenizerMixin:
if length > max_answer_length: if length > max_answer_length:
raise ValueError(f"Span is too long: {length} > {max_answer_length}") raise ValueError(f"Span is too long: {length} > {max_answer_length}")
if any( if any(
[ start_index <= prev_start_index <= prev_end_index <= end_index
start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index
or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals
for (prev_start_index, prev_end_index) in chosen_span_intervals
]
): ):
continue continue
chosen_span_intervals.append((start_index, end_index)) chosen_span_intervals.append((start_index, end_index))

View File

@ -377,11 +377,9 @@ class CustomDPRReaderTokenizerMixin:
length = end_index - start_index + 1 length = end_index - start_index + 1
assert length <= max_answer_length, f"Span is too long: {length} > {max_answer_length}" assert length <= max_answer_length, f"Span is too long: {length} > {max_answer_length}"
if any( if any(
[ start_index <= prev_start_index <= prev_end_index <= end_index
start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index
or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals
for (prev_start_index, prev_end_index) in chosen_span_intervals
]
): ):
continue continue
chosen_span_intervals.append((start_index, end_index)) chosen_span_intervals.append((start_index, end_index))

View File

@ -90,7 +90,7 @@ def get_tf_weights_as_numpy(path="./ckpt/aeslc/model.ckpt-32000") -> Dict:
tf_weights = {} tf_weights = {}
ignore_name = ["Adafactor", "global_step"] ignore_name = ["Adafactor", "global_step"]
for name, shape in tqdm(init_vars, desc="converting tf checkpoint to dict"): for name, shape in tqdm(init_vars, desc="converting tf checkpoint to dict"):
skip_key = any([pat in name for pat in ignore_name]) skip_key = any(pat in name for pat in ignore_name)
if skip_key: if skip_key:
continue continue
array = tf.train.load_variable(path, name) array = tf.train.load_variable(path, name)

View File

@ -115,7 +115,7 @@ class SamProcessor(ProcessorMixin):
for point, original_size in zip(input_points, original_sizes) for point, original_size in zip(input_points, original_sizes)
] ]
# check that all arrays have the same shape # check that all arrays have the same shape
if not all([point.shape == input_points[0].shape for point in input_points]): if not all(point.shape == input_points[0].shape for point in input_points):
if input_labels is not None: if input_labels is not None:
input_points, input_labels = self._pad_points_and_labels(input_points, input_labels) input_points, input_labels = self._pad_points_and_labels(input_points, input_labels)

View File

@ -647,7 +647,7 @@ class GenerationIntegrationTestsMixin:
generated_tokens = model.generate(**tokens, eos_token_id=eos_token_id, **generation_kwargs) generated_tokens = model.generate(**tokens, eos_token_id=eos_token_id, **generation_kwargs)
unpadded_correct_condition = expectation == len(generated_tokens[0]) unpadded_correct_condition = expectation == len(generated_tokens[0])
padded_correct_condition = expectation < len(generated_tokens[0]) and all( padded_correct_condition = expectation < len(generated_tokens[0]) and all(
[token == model.config.pad_token_id for token in generated_tokens[0][expectation:]] token == model.config.pad_token_id for token in generated_tokens[0][expectation:]
) )
self.assertTrue(unpadded_correct_condition or padded_correct_condition) self.assertTrue(unpadded_correct_condition or padded_correct_condition)
@ -655,7 +655,7 @@ class GenerationIntegrationTestsMixin:
generated_tokens = model.generate(**tokens, eos_token_id=eos_token_id, **generation_kwargs) generated_tokens = model.generate(**tokens, eos_token_id=eos_token_id, **generation_kwargs)
unpadded_correct_condition = expectation == len(generated_tokens[0]) unpadded_correct_condition = expectation == len(generated_tokens[0])
padded_correct_condition = expectation < len(generated_tokens[0]) and all( padded_correct_condition = expectation < len(generated_tokens[0]) and all(
[token == model.config.pad_token_id for token in generated_tokens[0][expectation:]] token == model.config.pad_token_id for token in generated_tokens[0][expectation:]
) )
self.assertTrue(unpadded_correct_condition or padded_correct_condition) self.assertTrue(unpadded_correct_condition or padded_correct_condition)

View File

@ -521,7 +521,7 @@ class CodeGenModelLanguageGenerationTest(unittest.TestCase):
self.assertEqual(output_str, EXPECTED_OUTPUT_STR) self.assertEqual(output_str, EXPECTED_OUTPUT_STR)
self.assertTrue( self.assertTrue(
all([output_seq_strs[idx] != output_seq_tt_strs[idx] for idx in range(len(output_seq_tt_strs))]) all(output_seq_strs[idx] != output_seq_tt_strs[idx] for idx in range(len(output_seq_tt_strs)))
) # token_type_ids should change output ) # token_type_ids should change output
@is_flaky(max_attempts=3, description="measure of timing is somehow flaky.") @is_flaky(max_attempts=3, description="measure of timing is somehow flaky.")

View File

@ -516,7 +516,7 @@ class Data2VecAudioModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.Tes
"objective.weight", "objective.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -373,12 +373,12 @@ class EncodecModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase)
uniform_init_parms = ["conv"] uniform_init_parms = ["conv"]
ignore_init = ["lstm"] ignore_init = ["lstm"]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",
) )
elif not any([x in name for x in ignore_init]): elif not any(x in name for x in ignore_init):
self.assertIn( self.assertIn(
((param.data.mean() * 1e9).round() / 1e9).item(), ((param.data.mean() * 1e9).round() / 1e9).item(),
[0.0, 1.0], [0.0, 1.0],

View File

@ -768,7 +768,7 @@ class GPT2ModelLanguageGenerationTest(unittest.TestCase):
) )
self.assertEqual(output_str, EXPECTED_OUTPUT_STR) self.assertEqual(output_str, EXPECTED_OUTPUT_STR)
self.assertTrue( self.assertTrue(
all([output_seq_strs[idx] != output_seq_tt_strs[idx] for idx in range(len(output_seq_tt_strs))]) all(output_seq_strs[idx] != output_seq_tt_strs[idx] for idx in range(len(output_seq_tt_strs)))
) # token_type_ids should change output ) # token_type_ids should change output
@slow @slow

View File

@ -571,7 +571,7 @@ class GPTJModelLanguageGenerationTest(unittest.TestCase):
self.assertEqual(output_str, EXPECTED_OUTPUT_STR) self.assertEqual(output_str, EXPECTED_OUTPUT_STR)
self.assertTrue( self.assertTrue(
all([output_seq_strs[idx] != output_seq_tt_strs[idx] for idx in range(len(output_seq_tt_strs))]) all(output_seq_strs[idx] != output_seq_tt_strs[idx] for idx in range(len(output_seq_tt_strs)))
) # token_type_ids should change output ) # token_type_ids should change output
@slow @slow

View File

@ -423,7 +423,7 @@ class HubertModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
"quantizer.weight_proj.weight", "quantizer.weight_proj.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",
@ -684,7 +684,7 @@ class HubertRobustModelTest(ModelTesterMixin, unittest.TestCase):
"quantizer.weight_proj.weight", "quantizer.weight_proj.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -386,7 +386,7 @@ class MCTCTModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
"objective.weight", "objective.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",
@ -533,7 +533,7 @@ class MCTCTRobustModelTest(ModelTesterMixin, unittest.TestCase):
"objective.weight", "objective.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -334,7 +334,7 @@ class RwkvModelTest(ModelTesterMixin, GenerationTesterMixin, PipelineTesterMixin
if param.requires_grad: if param.requires_grad:
# check if it's a ones like # check if it's a ones like
self.assertTrue(torch.allclose(param.data, torch.ones_like(param.data), atol=1e-5, rtol=1e-5)) self.assertTrue(torch.allclose(param.data, torch.ones_like(param.data), atol=1e-5, rtol=1e-5))
elif any([x in name for x in ["time_mix_key", "time_mix_receptance"]]): elif any(x in name for x in ["time_mix_key", "time_mix_receptance"]):
if param.requires_grad: if param.requires_grad:
self.assertInterval( self.assertInterval(
param.data, param.data,

View File

@ -417,7 +417,7 @@ class SEWModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
"quantizer.weight_proj.weight", "quantizer.weight_proj.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -431,7 +431,7 @@ class SEWDModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
"quantizer.weight_proj.weight", "quantizer.weight_proj.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -583,7 +583,7 @@ class SpeechT5ForSpeechToTextTest(ModelTesterMixin, unittest.TestCase):
"feature_projection.projection.bias", "feature_projection.projection.bias",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",
@ -927,7 +927,7 @@ class SpeechT5ForTextToSpeechTest(ModelTesterMixin, unittest.TestCase):
"conv.weight", "conv.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",
@ -1337,7 +1337,7 @@ class SpeechT5ForSpeechToSpeechTest(ModelTesterMixin, unittest.TestCase):
"feature_projection.projection.bias", "feature_projection.projection.bias",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -432,7 +432,7 @@ class UniSpeechRobustModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.T
"feature_projection.projection.bias", "feature_projection.projection.bias",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -484,7 +484,7 @@ class UniSpeechSatModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.Test
"objective.weight", "objective.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",
@ -695,7 +695,7 @@ class UniSpeechSatRobustModelTest(ModelTesterMixin, unittest.TestCase):
"objective.weight", "objective.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -464,7 +464,7 @@ class FlaxWav2Vec2UtilsTest(unittest.TestCase):
negative_indices = _sample_negative_indices(features.shape, num_negatives, attention_mask=attention_mask) negative_indices = _sample_negative_indices(features.shape, num_negatives, attention_mask=attention_mask)
# make sure that no padding tokens are sampled # make sure that no padding tokens are sampled
self.assertTrue(all([idx not in negative_indices for idx in forbidden_indices])) self.assertTrue(all(idx not in negative_indices for idx in forbidden_indices))
features = features.reshape(-1, hidden_size) # BTC => (BxT)C features = features.reshape(-1, hidden_size) # BTC => (BxT)C
# take negative vectors from sampled indices # take negative vectors from sampled indices

View File

@ -637,7 +637,7 @@ class Wav2Vec2ModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase
"objective.weight", "objective.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",
@ -971,7 +971,7 @@ class Wav2Vec2RobustModelTest(ModelTesterMixin, unittest.TestCase):
"objective.weight", "objective.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -569,7 +569,7 @@ class Wav2Vec2ConformerModelTest(ModelTesterMixin, PipelineTesterMixin, unittest
"objective.weight", "objective.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -438,7 +438,7 @@ class WavLMModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
"objective.weight", "objective.weight",
] ]
if param.requires_grad: if param.requires_grad:
if any([x in name for x in uniform_init_parms]): if any(x in name for x in uniform_init_parms):
self.assertTrue( self.assertTrue(
-1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0, -1.0 <= ((param.data.mean() * 1e9).round() / 1e9).item() <= 1.0,
msg=f"Parameter {name} of model {model_class} seems not properly initialized", msg=f"Parameter {name} of model {model_class} seems not properly initialized",

View File

@ -1535,7 +1535,7 @@ class WhisperModelIntegrationTests(unittest.TestCase):
text = processor.decode(output[0]) text = processor.decode(output[0])
self.assertTrue(prompt in text) self.assertTrue(prompt in text)
self.assertTrue(all([token in text for token in expected_tokens])) self.assertTrue(all(token in text for token in expected_tokens))
@slow @slow
def test_generate_with_prompt_ids_and_no_non_prompt_forced_decoder_ids(self): def test_generate_with_prompt_ids_and_no_non_prompt_forced_decoder_ids(self):

View File

@ -145,7 +145,7 @@ class OnnxExportTestCase(unittest.TestCase):
# Assert all variables are present # Assert all variables are present
self.assertEqual(len(shapes), len(variable_names)) self.assertEqual(len(shapes), len(variable_names))
self.assertTrue(all([var_name in shapes for var_name in variable_names])) self.assertTrue(all(var_name in shapes for var_name in variable_names))
self.assertSequenceEqual(variable_names[:3], input_vars) self.assertSequenceEqual(variable_names[:3], input_vars)
self.assertSequenceEqual(variable_names[3:], output_vars) self.assertSequenceEqual(variable_names[3:], output_vars)

View File

@ -1566,7 +1566,7 @@ class TFModelTesterMixin:
return_labels=True if "labels" in inspect.signature(model_class.call).parameters.keys() else False, return_labels=True if "labels" in inspect.signature(model_class.call).parameters.keys() else False,
) )
if not any( if not any(
[tensor.dtype.is_integer for tensor in prepared_for_class.values() if isinstance(tensor, tf.Tensor)] tensor.dtype.is_integer for tensor in prepared_for_class.values() if isinstance(tensor, tf.Tensor)
): ):
return # No integer inputs means no need for this test return # No integer inputs means no need for this test

View File

@ -79,7 +79,7 @@ SMALL_TRAINING_CORPUS = [
def filter_non_english(_, pretrained_name: str): def filter_non_english(_, pretrained_name: str):
"""Filter all the model for non-english language""" """Filter all the model for non-english language"""
return not any([lang in pretrained_name for lang in NON_ENGLISH_TAGS]) return not any(lang in pretrained_name for lang in NON_ENGLISH_TAGS)
def filter_roberta_detectors(_, pretrained_name: str): def filter_roberta_detectors(_, pretrained_name: str):

View File

@ -56,8 +56,8 @@ class Seq2seqTrainerTester(TestCasePlus):
] ]
batch["decoder_attention_mask"] = outputs.attention_mask batch["decoder_attention_mask"] = outputs.attention_mask
assert all([len(x) == 512 for x in inputs.input_ids]) assert all(len(x) == 512 for x in inputs.input_ids)
assert all([len(x) == 128 for x in outputs.input_ids]) assert all(len(x) == 128 for x in outputs.input_ids)
return batch return batch

View File

@ -362,7 +362,7 @@ def convert_to_localized_md(model_list, localized_model_list, format_str):
model_keys = [re.search(r"\*\*\[([^\]]*)", line).groups()[0] for line in model_list.strip().split("\n")] model_keys = [re.search(r"\*\*\[([^\]]*)", line).groups()[0] for line in model_list.strip().split("\n")]
# We exclude keys in localized README not in the main one. # We exclude keys in localized README not in the main one.
readmes_match = not any([k not in model_keys for k in localized_model_index]) readmes_match = not any(k not in model_keys for k in localized_model_index)
localized_model_index = {k: v for k, v in localized_model_index.items() if k in model_keys} localized_model_index = {k: v for k, v in localized_model_index.items() if k in model_keys}
for model in model_list.strip().split("\n"): for model in model_list.strip().split("\n"):

View File

@ -735,7 +735,7 @@ def build_model(model_arch, tiny_config, output_dir):
tiny_config = copy.deepcopy(tiny_config) tiny_config = copy.deepcopy(tiny_config)
if any([model_arch.__name__.endswith(x) for x in ["ForCausalLM", "LMHeadModel"]]): if any(model_arch.__name__.endswith(x) for x in ["ForCausalLM", "LMHeadModel"]):
tiny_config.is_encoder_decoder = False tiny_config.is_encoder_decoder = False
tiny_config.is_decoder = True tiny_config.is_decoder = True

View File

@ -428,7 +428,7 @@ def get_module_dependencies(module_fname, cache=None):
# So we get the imports from that init then try to find where our objects come from. # So we get the imports from that init then try to find where our objects come from.
new_imported_modules = extract_imports(module, cache=cache) new_imported_modules = extract_imports(module, cache=cache)
for new_module, new_imports in new_imported_modules: for new_module, new_imports in new_imported_modules:
if any([i in new_imports for i in imports]): if any(i in new_imports for i in imports):
if new_module not in dependencies: if new_module not in dependencies:
new_modules.append((new_module, [i for i in new_imports if i in imports])) new_modules.append((new_module, [i for i in new_imports if i in imports]))
imports = [i for i in imports if i not in new_imports] imports = [i for i in imports if i not in new_imports]