From 678ad8535eac9970dfee978b111c6a35b2e266cc Mon Sep 17 00:00:00 2001 From: Kashif Rasul Date: Thu, 12 Oct 2023 09:34:47 +0200 Subject: [PATCH 1/5] use isinstance --- .../flax/image-captioning/run_image_captioning_flax.py | 6 +++--- examples/flax/text-classification/run_flax_glue.py | 2 +- examples/legacy/pytorch-lightning/run_glue.py | 2 +- examples/legacy/pytorch-lightning/run_ner.py | 2 +- .../deebert/src/modeling_highway_bert.py | 2 +- .../quantization-qdqbert/quant_trainer.py | 2 +- setup.py | 2 +- src/transformers/data/data_collator.py | 4 ++-- src/transformers/dependency_versions_table.py | 2 +- src/transformers/models/blip/modeling_blip_text.py | 4 ++-- src/transformers/models/blip/modeling_tf_blip_text.py | 4 ++-- src/transformers/models/blip_2/modeling_blip_2.py | 4 ++-- src/transformers/models/deberta/configuration_deberta.py | 2 +- .../models/deberta_v2/configuration_deberta_v2.py | 2 +- src/transformers/models/esm/modeling_esmfold.py | 8 ++++---- .../models/gptsan_japanese/modeling_gptsan_japanese.py | 2 +- .../gptsan_japanese/tokenization_gptsan_japanese.py | 2 +- .../models/instructblip/modeling_instructblip.py | 4 ++-- .../models/nougat/tokenization_nougat_fast.py | 2 +- .../models/regnet/convert_regnet_to_pytorch.py | 2 +- .../models/transfo_xl/tokenization_transfo_xl.py | 2 +- src/transformers/models/yoso/modeling_yoso.py | 2 +- src/transformers/trainer_pt_utils.py | 2 +- .../encoder_decoder/test_modeling_tf_encoder_decoder.py | 2 +- tests/models/lxmert/test_modeling_lxmert.py | 2 +- tests/models/lxmert/test_modeling_tf_lxmert.py | 2 +- .../test_modeling_tf_vision_encoder_decoder.py | 2 +- tests/test_modeling_common.py | 2 +- tests/test_modeling_tf_common.py | 2 +- utils/notification_service.py | 6 +++--- utils/notification_service_doc_tests.py | 4 ++-- 31 files changed, 44 insertions(+), 44 deletions(-) diff --git a/examples/flax/image-captioning/run_image_captioning_flax.py b/examples/flax/image-captioning/run_image_captioning_flax.py index d8c89c1a242f14..45bd5546fa2176 100644 --- a/examples/flax/image-captioning/run_image_captioning_flax.py +++ b/examples/flax/image-captioning/run_image_captioning_flax.py @@ -1174,7 +1174,7 @@ def evaluation_loop( with open(output_file, "w", encoding="UTF-8") as fp: json.dump(generations, fp, ensure_ascii=False, indent=4) - def evaluate(rng: jax.random.PRNGKey, dataset: Dataset, ckpt_dir: str = ""): + def evaluation(rng: jax.random.PRNGKey, dataset: Dataset, ckpt_dir: str = ""): evaluation_loop(rng, dataset, metric_key_prefix="eval", ckpt_dir=ckpt_dir) def predict(rng: jax.random.PRNGKey, dataset: Dataset): @@ -1247,7 +1247,7 @@ def predict(rng: jax.random.PRNGKey, dataset: Dataset): ): ckpt_dir = f"ckpt_epoch_{epoch + 1}_step_{cur_step}" commit_msg = f"Saving weights and logs of epoch {epoch + 1} - step {cur_step}" - evaluate(input_rng, eval_dataset, ckpt_dir) + evaluation(input_rng, eval_dataset, ckpt_dir) save_ckpt(ckpt_dir=ckpt_dir, commit_msg=commit_msg) # ======================== Epoch End ============================== @@ -1270,7 +1270,7 @@ def predict(rng: jax.random.PRNGKey, dataset: Dataset): if training_args.do_eval and (training_args.eval_steps is None or training_args.eval_steps <= 0): ckpt_dir = f"ckpt_epoch_{epoch + 1}_step_{cur_step}" commit_msg = f"Saving weights and logs of epoch {epoch + 1} - step {cur_step}" - evaluate(input_rng, eval_dataset, ckpt_dir) + evaluation(input_rng, eval_dataset, ckpt_dir) save_ckpt(ckpt_dir=ckpt_dir, commit_msg=commit_msg) # ======================== Evaluating | Predicting ============================== diff --git a/examples/flax/text-classification/run_flax_glue.py b/examples/flax/text-classification/run_flax_glue.py index 1535ff8492781b..14dc54d6c48dd7 100755 --- a/examples/flax/text-classification/run_flax_glue.py +++ b/examples/flax/text-classification/run_flax_glue.py @@ -212,7 +212,7 @@ def __post_init__(self): if self.validation_file is not None: extension = self.validation_file.split(".")[-1] assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." - self.task_name = self.task_name.lower() if type(self.task_name) == str else self.task_name + self.task_name = self.task_name.lower() if isinstance(self.task_name, str) else self.task_name def create_train_state( diff --git a/examples/legacy/pytorch-lightning/run_glue.py b/examples/legacy/pytorch-lightning/run_glue.py index 5f22e2fc7a1311..681f633fcd6d2b 100644 --- a/examples/legacy/pytorch-lightning/run_glue.py +++ b/examples/legacy/pytorch-lightning/run_glue.py @@ -23,7 +23,7 @@ class GLUETransformer(BaseTransformer): mode = "sequence-classification" def __init__(self, hparams): - if type(hparams) == dict: + if isinstance(hparams, dict): hparams = Namespace(**hparams) hparams.glue_output_mode = glue_output_modes[hparams.task] num_labels = glue_tasks_num_labels[hparams.task] diff --git a/examples/legacy/pytorch-lightning/run_ner.py b/examples/legacy/pytorch-lightning/run_ner.py index 7f6b00854d99cc..fc6f812275ea2c 100644 --- a/examples/legacy/pytorch-lightning/run_ner.py +++ b/examples/legacy/pytorch-lightning/run_ner.py @@ -25,7 +25,7 @@ class NERTransformer(BaseTransformer): mode = "token-classification" def __init__(self, hparams): - if type(hparams) == dict: + if isinstance(hparams, dict): hparams = Namespace(**hparams) module = import_module("tasks") try: diff --git a/examples/research_projects/deebert/src/modeling_highway_bert.py b/examples/research_projects/deebert/src/modeling_highway_bert.py index 2a881decbbd529..5dc43734be4e78 100644 --- a/examples/research_projects/deebert/src/modeling_highway_bert.py +++ b/examples/research_projects/deebert/src/modeling_highway_bert.py @@ -32,7 +32,7 @@ def __init__(self, config): self.early_exit_entropy = [-1 for _ in range(config.num_hidden_layers)] def set_early_exit_entropy(self, x): - if (type(x) is float) or (type(x) is int): + if isinstance(x, (float, int)): for i in range(len(self.early_exit_entropy)): self.early_exit_entropy[i] = x else: diff --git a/examples/research_projects/quantization-qdqbert/quant_trainer.py b/examples/research_projects/quantization-qdqbert/quant_trainer.py index 73e880ebf67636..09bac19e921a89 100755 --- a/examples/research_projects/quantization-qdqbert/quant_trainer.py +++ b/examples/research_projects/quantization-qdqbert/quant_trainer.py @@ -239,7 +239,7 @@ def print_model_summary(model, name_width=25, line_width=180, ignore=None): continue if type(mod) in ignore: continue - if [True for s in ignore if type(s) is str and s in name]: + if [True for s in ignore if isinstance(s, str) and s in name]: continue act_str = f"Act:{input_q.extra_repr()}" wgt_str = f"Wgt:{weight_q.extra_repr()}" diff --git a/setup.py b/setup.py index 444efbf268d1b6..b963e80164471c 100644 --- a/setup.py +++ b/setup.py @@ -153,7 +153,7 @@ "rhoknp>=1.1.0,<1.3.1", "rjieba", "rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1", - "ruff>=0.0.241,<=0.0.259", + "ruff>=0.0.241", "sacrebleu>=1.4.12,<2.0.0", "sacremoses", "safetensors>=0.3.1", diff --git a/src/transformers/data/data_collator.py b/src/transformers/data/data_collator.py index 57879ec3e2d831..7954c828fee9da 100644 --- a/src/transformers/data/data_collator.py +++ b/src/transformers/data/data_collator.py @@ -121,7 +121,7 @@ def torch_default_data_collator(features: List[InputDataClass]) -> Dict[str, Any if isinstance(first["label_ids"], torch.Tensor): batch["labels"] = torch.stack([f["label_ids"] for f in features]) else: - dtype = torch.long if type(first["label_ids"][0]) is int else torch.float + dtype = torch.long if isinstance(first["label_ids"][0], int) else torch.float batch["labels"] = torch.tensor([f["label_ids"] for f in features], dtype=dtype) # Handling of all other possible keys. @@ -196,7 +196,7 @@ def numpy_default_data_collator(features: List[InputDataClass]) -> Dict[str, Any if isinstance(first["label_ids"], np.ndarray): batch["labels"] = np.stack([f["label_ids"] for f in features]) else: - dtype = np.int64 if type(first["label_ids"][0]) is int else np.float32 + dtype = np.int64 if isinstance(first["label_ids"][0], int) else np.float32 batch["labels"] = np.array([f["label_ids"] for f in features], dtype=dtype) # Handling of all other possible keys. diff --git a/src/transformers/dependency_versions_table.py b/src/transformers/dependency_versions_table.py index 20dacb3cf0d4e1..b02b9774055f73 100644 --- a/src/transformers/dependency_versions_table.py +++ b/src/transformers/dependency_versions_table.py @@ -60,7 +60,7 @@ "rhoknp": "rhoknp>=1.1.0,<1.3.1", "rjieba": "rjieba", "rouge-score": "rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1", - "ruff": "ruff>=0.0.241,<=0.0.259", + "ruff": "ruff>=0.0.241", "sacrebleu": "sacrebleu>=1.4.12,<2.0.0", "sacremoses": "sacremoses", "safetensors": "safetensors>=0.3.1", diff --git a/src/transformers/models/blip/modeling_blip_text.py b/src/transformers/models/blip/modeling_blip_text.py index 49b958afc2ebae..a8fe56db9bbfc7 100644 --- a/src/transformers/models/blip/modeling_blip_text.py +++ b/src/transformers/models/blip/modeling_blip_text.py @@ -747,13 +747,13 @@ def forward( # If a 2D or 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if encoder_hidden_states is not None: - if type(encoder_hidden_states) == list: + if isinstance(encoder_hidden_states, list): encoder_batch_size, encoder_sequence_length, _ = encoder_hidden_states[0].size() else: encoder_batch_size, encoder_sequence_length, _ = encoder_hidden_states.size() encoder_hidden_shape = (encoder_batch_size, encoder_sequence_length) - if type(encoder_attention_mask) == list: + if isinstance(encoder_attention_mask, list): encoder_extended_attention_mask = [self.invert_attention_mask(mask) for mask in encoder_attention_mask] elif encoder_attention_mask is None: encoder_attention_mask = torch.ones(encoder_hidden_shape, device=device) diff --git a/src/transformers/models/blip/modeling_tf_blip_text.py b/src/transformers/models/blip/modeling_tf_blip_text.py index 9873c292b7af34..b7307c062f7911 100644 --- a/src/transformers/models/blip/modeling_tf_blip_text.py +++ b/src/transformers/models/blip/modeling_tf_blip_text.py @@ -741,13 +741,13 @@ def call( # If a 2D or 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if encoder_hidden_states is not None: - if type(encoder_hidden_states) == list: + if isinstance(encoder_hidden_states, list): encoder_batch_size, encoder_sequence_length, _ = shape_list(encoder_hidden_states[0]) else: encoder_batch_size, encoder_sequence_length, _ = shape_list(encoder_hidden_states) encoder_hidden_shape = (encoder_batch_size, encoder_sequence_length) - if type(encoder_attention_mask) == list: + if isinstance(encoder_attention_mask, list): encoder_extended_attention_mask = [invert_attention_mask(mask) for mask in encoder_attention_mask] elif encoder_attention_mask is None: encoder_attention_mask = tf.ones(encoder_hidden_shape) diff --git a/src/transformers/models/blip_2/modeling_blip_2.py b/src/transformers/models/blip_2/modeling_blip_2.py index 87c8132ff4fd86..3ec1e323774f25 100644 --- a/src/transformers/models/blip_2/modeling_blip_2.py +++ b/src/transformers/models/blip_2/modeling_blip_2.py @@ -1140,13 +1140,13 @@ def forward( # If a 2D or 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if encoder_hidden_states is not None: - if type(encoder_hidden_states) == list: + if isinstance(encoder_hidden_states, list): encoder_batch_size, encoder_sequence_length, _ = encoder_hidden_states[0].size() else: encoder_batch_size, encoder_sequence_length, _ = encoder_hidden_states.size() encoder_hidden_shape = (encoder_batch_size, encoder_sequence_length) - if type(encoder_attention_mask) == list: + if isinstance(encoder_attention_mask, list): encoder_extended_attention_mask = [self.invert_attention_mask(mask) for mask in encoder_attention_mask] elif encoder_attention_mask is None: encoder_attention_mask = torch.ones(encoder_hidden_shape, device=device) diff --git a/src/transformers/models/deberta/configuration_deberta.py b/src/transformers/models/deberta/configuration_deberta.py index 94ea91cd3a0888..795195478b5503 100644 --- a/src/transformers/models/deberta/configuration_deberta.py +++ b/src/transformers/models/deberta/configuration_deberta.py @@ -148,7 +148,7 @@ def __init__( self.position_biased_input = position_biased_input # Backwards compatibility - if type(pos_att_type) == str: + if isinstance(pos_att_type, str): pos_att_type = [x.strip() for x in pos_att_type.lower().split("|")] self.pos_att_type = pos_att_type diff --git a/src/transformers/models/deberta_v2/configuration_deberta_v2.py b/src/transformers/models/deberta_v2/configuration_deberta_v2.py index d55486cd563381..04fa7228fd7581 100644 --- a/src/transformers/models/deberta_v2/configuration_deberta_v2.py +++ b/src/transformers/models/deberta_v2/configuration_deberta_v2.py @@ -150,7 +150,7 @@ def __init__( self.position_biased_input = position_biased_input # Backwards compatibility - if type(pos_att_type) == str: + if isinstance(pos_att_type, str): pos_att_type = [x.strip() for x in pos_att_type.lower().split("|")] self.pos_att_type = pos_att_type diff --git a/src/transformers/models/esm/modeling_esmfold.py b/src/transformers/models/esm/modeling_esmfold.py index 9bdb5b4eb74f1c..c6e5dd090d5eb3 100644 --- a/src/transformers/models/esm/modeling_esmfold.py +++ b/src/transformers/models/esm/modeling_esmfold.py @@ -229,7 +229,7 @@ def dict_multimap(fn, dicts): new_dict = {} for k, v in first.items(): all_v = [d[k] for d in dicts] - if type(v) is dict: + if isinstance(v, dict): new_dict[k] = dict_multimap(fn, all_v) else: new_dict[k] = fn(all_v) @@ -1060,7 +1060,7 @@ def __init__(self, r: float, batch_dim: Union[int, List[int]]): super().__init__() self.r = r - if type(batch_dim) == int: + if isinstance(batch_dim, int): batch_dim = [batch_dim] self.batch_dim = batch_dim self.dropout = nn.Dropout(self.r) @@ -2254,7 +2254,7 @@ def infer( seqs: Union[str, List[str]], position_ids=None, ): - if type(seqs) is str: + if isinstance(seqs, str): lst = [seqs] else: lst = seqs @@ -2312,7 +2312,7 @@ def output_to_pdb(output: Dict) -> List[str]: def infer_pdb(self, seqs, *args, **kwargs) -> str: """Returns the pdb (file) string from the model given an input sequence.""" - assert type(seqs) is str + assert isinstance(seqs, str) output = self.infer(seqs, *args, **kwargs) return self.output_to_pdb(output)[0] diff --git a/src/transformers/models/gptsan_japanese/modeling_gptsan_japanese.py b/src/transformers/models/gptsan_japanese/modeling_gptsan_japanese.py index 0d9301406da8e9..e569783ba60d64 100644 --- a/src/transformers/models/gptsan_japanese/modeling_gptsan_japanese.py +++ b/src/transformers/models/gptsan_japanese/modeling_gptsan_japanese.py @@ -1288,7 +1288,7 @@ def prepare_inputs_for_generation( past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, **kwargs, ): - if type(spout) is list: + if isinstance(spout, list): spout = torch.tensor(spout).float() if input_ids is not None: spout = spout.to(input_ids.device) diff --git a/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py b/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py index e805acf3c74bca..42544f59584724 100644 --- a/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py +++ b/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py @@ -369,7 +369,7 @@ def _batch_encode_plus( verbose: bool = True, ) -> BatchEncoding: # This tokenizer converts input text pairs into Prefix input and subsequent input - if type(batch_text_or_text_pairs[0]) is tuple or type(batch_text_or_text_pairs[0]) is list: + if type(batch_text_or_text_pairs[0]) is tuple or isinstance(batch_text_or_text_pairs[0], list): # As a single text with an explicit un-prefix position batch_prefix_texts = [] for pref, txt in batch_text_or_text_pairs: diff --git a/src/transformers/models/instructblip/modeling_instructblip.py b/src/transformers/models/instructblip/modeling_instructblip.py index 082900a6652f80..a73204978da540 100644 --- a/src/transformers/models/instructblip/modeling_instructblip.py +++ b/src/transformers/models/instructblip/modeling_instructblip.py @@ -1193,13 +1193,13 @@ def forward( # If a 2D or 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if encoder_hidden_states is not None: - if type(encoder_hidden_states) == list: + if isinstance(encoder_hidden_states, list): encoder_batch_size, encoder_sequence_length, _ = encoder_hidden_states[0].size() else: encoder_batch_size, encoder_sequence_length, _ = encoder_hidden_states.size() encoder_hidden_shape = (encoder_batch_size, encoder_sequence_length) - if type(encoder_attention_mask) == list: + if isinstance(encoder_attention_mask, list): encoder_extended_attention_mask = [self.invert_attention_mask(mask) for mask in encoder_attention_mask] elif encoder_attention_mask is None: encoder_attention_mask = torch.ones(encoder_hidden_shape, device=device) diff --git a/src/transformers/models/nougat/tokenization_nougat_fast.py b/src/transformers/models/nougat/tokenization_nougat_fast.py index 9d95940875e155..d02aec75752123 100644 --- a/src/transformers/models/nougat/tokenization_nougat_fast.py +++ b/src/transformers/models/nougat/tokenization_nougat_fast.py @@ -251,7 +251,7 @@ def remove_numbers(lines): def _clean(s): return re.sub(r"(?:[\d_]|\*\*)", "", s).strip() - if type(lines) is str: + if isinstance(lines, str): return _clean(lines) out = [] for l in lines: diff --git a/src/transformers/models/regnet/convert_regnet_to_pytorch.py b/src/transformers/models/regnet/convert_regnet_to_pytorch.py index 14d01ae4452584..d29077c1a729ba 100644 --- a/src/transformers/models/regnet/convert_regnet_to_pytorch.py +++ b/src/transformers/models/regnet/convert_regnet_to_pytorch.py @@ -192,7 +192,7 @@ def convert_weight_and_push( ) from_output = from_model(x) - from_output = from_output[-1] if type(from_output) is list else from_output + from_output = from_output[-1] if isinstance(from_output, list) else from_output # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state if "seer" in name and "in1k" in name: diff --git a/src/transformers/models/transfo_xl/tokenization_transfo_xl.py b/src/transformers/models/transfo_xl/tokenization_transfo_xl.py index 91f3d78aae761e..8a2aba92f7a828 100644 --- a/src/transformers/models/transfo_xl/tokenization_transfo_xl.py +++ b/src/transformers/models/transfo_xl/tokenization_transfo_xl.py @@ -212,7 +212,7 @@ def __init__( # Loading a torch-saved transfo-xl vocab dict with pickle results in an integer # Entering this if statement means that we tried to load a torch-saved file with pickle, and we failed. # We therefore load it with torch, if it's available. - if type(vocab_dict) == int: + if isinstance(vocab_dict, int): if not is_torch_available(): raise ImportError( "Not trying to load dict with PyTorch as you need to install pytorch to load " diff --git a/src/transformers/models/yoso/modeling_yoso.py b/src/transformers/models/yoso/modeling_yoso.py index 5edd7f8835422a..3ae4720b8bdb67 100644 --- a/src/transformers/models/yoso/modeling_yoso.py +++ b/src/transformers/models/yoso/modeling_yoso.py @@ -88,7 +88,7 @@ def to_contiguous(input_tensors): def normalize(input_tensors): - if type(input_tensors) is list: + if isinstance(input_tensors, list): out = [] for tensor in input_tensors: out.append(nn.functional.normalize(tensor, p=2, dim=-1)) diff --git a/src/transformers/trainer_pt_utils.py b/src/transformers/trainer_pt_utils.py index cb6249f19a93a5..b8dfb3124c5e9f 100644 --- a/src/transformers/trainer_pt_utils.py +++ b/src/transformers/trainer_pt_utils.py @@ -896,7 +896,7 @@ def metrics_format(self, metrics: Dict[str, float]) -> Dict[str, float]: metrics_copy[k] = _secs2timedelta(v) elif k == "total_flos": metrics_copy[k] = f"{ int(v) >> 30 }GF" - elif type(metrics_copy[k]) == float: + elif isinstance(metrics_copy[k], float): metrics_copy[k] = round(v, 4) return metrics_copy diff --git a/tests/models/encoder_decoder/test_modeling_tf_encoder_decoder.py b/tests/models/encoder_decoder/test_modeling_tf_encoder_decoder.py index ab5da3d41e6c9a..fea22db56038b7 100644 --- a/tests/models/encoder_decoder/test_modeling_tf_encoder_decoder.py +++ b/tests/models/encoder_decoder/test_modeling_tf_encoder_decoder.py @@ -474,7 +474,7 @@ def check_pt_tf_outputs(self, tf_outputs, pt_outputs, model_class, tol=1e-5, nam def prepare_pt_inputs_from_tf_inputs(self, tf_inputs_dict): pt_inputs_dict = {} for name, key in tf_inputs_dict.items(): - if type(key) == bool: + if isinstance(key, bool): pt_inputs_dict[name] = key elif name == "input_values": pt_inputs_dict[name] = torch.from_numpy(key.numpy()).to(torch.float32) diff --git a/tests/models/lxmert/test_modeling_lxmert.py b/tests/models/lxmert/test_modeling_lxmert.py index 489d22d92efee6..63d83de36b7546 100644 --- a/tests/models/lxmert/test_modeling_lxmert.py +++ b/tests/models/lxmert/test_modeling_lxmert.py @@ -751,7 +751,7 @@ def prepare_tf_inputs_from_pt_inputs(self, pt_inputs_dict): tf_inputs_dict[key] = self.prepare_pt_inputs_from_tf_inputs(value) elif isinstance(value, (list, tuple)): tf_inputs_dict[key] = (self.prepare_pt_inputs_from_tf_inputs(iter_value) for iter_value in value) - elif type(value) == bool: + elif isinstance(value, bool): tf_inputs_dict[key] = value elif key == "input_values": tf_inputs_dict[key] = tf.convert_to_tensor(value.cpu().numpy(), dtype=tf.float32) diff --git a/tests/models/lxmert/test_modeling_tf_lxmert.py b/tests/models/lxmert/test_modeling_tf_lxmert.py index 52f64d614423df..d40ffbb3b96878 100644 --- a/tests/models/lxmert/test_modeling_tf_lxmert.py +++ b/tests/models/lxmert/test_modeling_tf_lxmert.py @@ -499,7 +499,7 @@ def prepare_pt_inputs_from_tf_inputs(self, tf_inputs_dict): pt_inputs_dict[key] = self.prepare_pt_inputs_from_tf_inputs(value) elif isinstance(value, (list, tuple)): pt_inputs_dict[key] = (self.prepare_pt_inputs_from_tf_inputs(iter_value) for iter_value in value) - elif type(key) == bool: + elif isinstance(key, bool): pt_inputs_dict[key] = value elif key == "input_values": pt_inputs_dict[key] = torch.from_numpy(value.numpy()).to(torch.float32) diff --git a/tests/models/vision_encoder_decoder/test_modeling_tf_vision_encoder_decoder.py b/tests/models/vision_encoder_decoder/test_modeling_tf_vision_encoder_decoder.py index e173e21a9b5dc4..12a8afb10299ed 100644 --- a/tests/models/vision_encoder_decoder/test_modeling_tf_vision_encoder_decoder.py +++ b/tests/models/vision_encoder_decoder/test_modeling_tf_vision_encoder_decoder.py @@ -407,7 +407,7 @@ def check_pt_tf_outputs(self, tf_outputs, pt_outputs, model_class, tol=1e-5, nam def prepare_pt_inputs_from_tf_inputs(self, tf_inputs_dict): pt_inputs_dict = {} for name, key in tf_inputs_dict.items(): - if type(key) == bool: + if isinstance(key, bool): pt_inputs_dict[name] = key elif name == "input_values": pt_inputs_dict[name] = torch.from_numpy(key.numpy()).to(torch.float32) diff --git a/tests/test_modeling_common.py b/tests/test_modeling_common.py index 5a239cf0fb3bca..deedf3a632dc6d 100755 --- a/tests/test_modeling_common.py +++ b/tests/test_modeling_common.py @@ -1989,7 +1989,7 @@ def prepare_tf_inputs_from_pt_inputs(self, pt_inputs_dict): tf_inputs_dict = {} for key, tensor in pt_inputs_dict.items(): # skip key that does not exist in tf - if type(tensor) == bool: + if isinstance(tensor, bool): tf_inputs_dict[key] = tensor elif key == "input_values": tf_inputs_dict[key] = tf.convert_to_tensor(tensor.cpu().numpy(), dtype=tf.float32) diff --git a/tests/test_modeling_tf_common.py b/tests/test_modeling_tf_common.py index 0d7bf373a4d42a..d7cd62b41a025e 100644 --- a/tests/test_modeling_tf_common.py +++ b/tests/test_modeling_tf_common.py @@ -576,7 +576,7 @@ def check_pt_tf_outputs(self, tf_outputs, pt_outputs, model_class, tol=1e-5, nam def prepare_pt_inputs_from_tf_inputs(self, tf_inputs_dict): pt_inputs_dict = {} for name, key in tf_inputs_dict.items(): - if type(key) == bool: + if isinstance(key, bool): pt_inputs_dict[name] = key elif name == "input_values": pt_inputs_dict[name] = torch.from_numpy(key.numpy()).to(torch.float32) diff --git a/utils/notification_service.py b/utils/notification_service.py index 1d10fa5d821f0b..3a115cf4d36bdf 100644 --- a/utils/notification_service.py +++ b/utils/notification_service.py @@ -20,7 +20,7 @@ import os import re import sys -import time +from time import sleep from typing import Dict, List, Optional, Union import requests @@ -649,7 +649,7 @@ def post_reply(self): thread_ts=self.thread_ts["ts"], ) - time.sleep(1) + sleep(1) for job, job_result in self.additional_results.items(): if len(job_result["failures"]): @@ -672,7 +672,7 @@ def post_reply(self): thread_ts=self.thread_ts["ts"], ) - time.sleep(1) + sleep(1) def retrieve_artifact(artifact_path: str, gpu: Optional[str]): diff --git a/utils/notification_service_doc_tests.py b/utils/notification_service_doc_tests.py index aac768fb394365..cda6a4da85157f 100644 --- a/utils/notification_service_doc_tests.py +++ b/utils/notification_service_doc_tests.py @@ -17,8 +17,8 @@ import math import os import re -import time from fnmatch import fnmatch +from time import sleep from typing import Dict import requests @@ -258,7 +258,7 @@ def post_reply(self): thread_ts=self.thread_ts["ts"], ) - time.sleep(1) + sleep(1) def get_job_links(): From 6ba24135345dbc1072b6ec5606eb21ba67dd9174 Mon Sep 17 00:00:00 2001 From: Kashif Rasul Date: Tue, 17 Oct 2023 09:42:14 +0200 Subject: [PATCH 2/5] fix formatting --- .../configuration_{{cookiecutter.lowercase_modelname}}.py | 3 +-- .../test_modeling_flax_{{cookiecutter.lowercase_modelname}}.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/configuration_{{cookiecutter.lowercase_modelname}}.py b/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/configuration_{{cookiecutter.lowercase_modelname}}.py index 2898b5cf6f8f69..3f9b5d1fb67f4c 100644 --- a/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/configuration_{{cookiecutter.lowercase_modelname}}.py +++ b/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/configuration_{{cookiecutter.lowercase_modelname}}.py @@ -137,7 +137,7 @@ class {{cookiecutter.camelcase_modelname}}Config(PretrainedConfig): {% else -%} keys_to_ignore_at_inference = ["past_key_values"] {% endif -%} - + {% if cookiecutter.is_encoder_decoder_model == "False" %} {%- else %} attribute_map = { @@ -238,4 +238,3 @@ def __init__( **kwargs ) - \ No newline at end of file diff --git a/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/test_modeling_flax_{{cookiecutter.lowercase_modelname}}.py b/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/test_modeling_flax_{{cookiecutter.lowercase_modelname}}.py index 37b22a75c3e970..a01ab3e19adf58 100644 --- a/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/test_modeling_flax_{{cookiecutter.lowercase_modelname}}.py +++ b/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/test_modeling_flax_{{cookiecutter.lowercase_modelname}}.py @@ -541,7 +541,7 @@ def prepare_{{cookiecutter.lowercase_modelname}}_inputs_dict( class Flax{{cookiecutter.camelcase_modelname}}ModelTest(FlaxModelTesterMixin, unittest.TestCase): all_model_classes = ( ( - Flax{{cookiecutter.camelcase_modelname}}ForConditionalGeneration, + Flax{{cookiecutter.camelcase_modelname}}ForConditionalGeneration, Flax{{cookiecutter.camelcase_modelname}}ForQuestionAnswering, Flax{{cookiecutter.camelcase_modelname}}ForSequenceClassification, Flax{{cookiecutter.camelcase_modelname}}Model, From 2b661e659662694ac144eebe753b4b350b6e8912 Mon Sep 17 00:00:00 2001 From: Kashif Rasul Date: Mon, 30 Oct 2023 14:31:40 +0100 Subject: [PATCH 3/5] Update src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> --- .../models/gptsan_japanese/tokenization_gptsan_japanese.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py b/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py index a8b92eb9cd73b8..79986a22832be4 100644 --- a/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py +++ b/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py @@ -375,7 +375,7 @@ def _batch_encode_plus( verbose: bool = True, ) -> BatchEncoding: # This tokenizer converts input text pairs into Prefix input and subsequent input - if type(batch_text_or_text_pairs[0]) is tuple or isinstance(batch_text_or_text_pairs[0], list): + if isinstance(batch_text_or_text_pairs[0], (tuple, list)): # As a single text with an explicit un-prefix position batch_prefix_texts = [] for pref, txt in batch_text_or_text_pairs: From 22dd83d1ea116cab6f516ae1a2514bbff34b5548 Mon Sep 17 00:00:00 2001 From: Kashif Rasul Date: Mon, 30 Oct 2023 14:36:57 +0100 Subject: [PATCH 4/5] undo change to dependencies --- setup.py | 2 +- src/transformers/dependency_versions_table.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 6ffb67c4c90529..6f43fae9ca574f 100644 --- a/setup.py +++ b/setup.py @@ -156,7 +156,7 @@ "rhoknp>=1.1.0,<1.3.1", "rjieba", "rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1", - "ruff>=0.0.241", + "ruff>=0.0.241,<=0.0.259", "sacrebleu>=1.4.12,<2.0.0", "sacremoses", "safetensors>=0.3.1", diff --git a/src/transformers/dependency_versions_table.py b/src/transformers/dependency_versions_table.py index 05c7f2e4471af3..1dbedc3ea6de2b 100644 --- a/src/transformers/dependency_versions_table.py +++ b/src/transformers/dependency_versions_table.py @@ -62,7 +62,7 @@ "rhoknp": "rhoknp>=1.1.0,<1.3.1", "rjieba": "rjieba", "rouge-score": "rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1", - "ruff": "ruff>=0.0.241", + "ruff": "ruff>=0.0.241,<=0.0.259", "sacrebleu": "sacrebleu>=1.4.12,<2.0.0", "sacremoses": "sacremoses", "safetensors": "safetensors>=0.3.1", From 869aa9fecfbd34e9b9676ad0ffa11f98e6725747 Mon Sep 17 00:00:00 2001 From: Kashif Rasul Date: Mon, 30 Oct 2023 14:44:31 +0100 Subject: [PATCH 5/5] fix import --- utils/notification_service_doc_tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/notification_service_doc_tests.py b/utils/notification_service_doc_tests.py index f9728e8b9561a6..fa997b7c98e95a 100644 --- a/utils/notification_service_doc_tests.py +++ b/utils/notification_service_doc_tests.py @@ -18,6 +18,7 @@ import os import re from fnmatch import fnmatch +from time import sleep from typing import Dict, List import requests