From caccf430d75ad82b5c5e144fe4bec88bcf9834ef Mon Sep 17 00:00:00 2001 From: Yuanyuan Chen Date: Thu, 5 Feb 2026 10:06:33 +0800 Subject: [PATCH] Fix ruff warnings Signed-off-by: cyy --- benchmark_v2/framework/benchmark_runner.py | 4 +- .../modeling_new_task_model.py | 6 +- examples/pytorch/image-pretraining/run_mae.py | 2 +- examples/pytorch/image-pretraining/run_mim.py | 2 +- .../image-pretraining/run_mim_no_trainer.py | 2 +- examples/pytorch/multiple-choice/run_swag.py | 4 +- examples/pytorch/question-answering/run_qa.py | 4 +- .../question-answering/run_qa_beam_search.py | 4 +- .../question-answering/run_seq2seq_qa.py | 4 +- .../run_speech_recognition_seq2seq.py | 2 +- .../summarization/run_summarization.py | 4 +- .../text-classification/run_classification.py | 4 +- .../pytorch/text-classification/run_glue.py | 4 +- .../pytorch/text-classification/run_xnli.py | 4 +- .../pytorch/text-generation/run_generation.py | 8 +- .../pytorch/token-classification/run_ner.py | 4 +- .../run_ner_no_trainer.py | 2 +- .../pytorch/translation/run_translation.py | 4 +- .../data/metrics/squad_metrics.py | 6 +- src/transformers/generation/utils.py | 12 ++- .../integrations/finegrained_fp8.py | 2 +- .../integrations/integration_utils.py | 2 +- src/transformers/modelcard.py | 2 +- .../models/clap/feature_extraction_clap.py | 4 +- .../models/deit/configuration_deit.py | 2 +- .../configuration_depth_anything.py | 2 +- .../convert_distill_any_depth_to_hf.py | 8 +- .../models/dpt/configuration_dpt.py | 2 +- .../convert_efficientloftr_to_hf.py | 80 ++++++++++++++----- .../configuration_granitemoehybrid.py | 2 +- .../models/ijepa/configuration_ijepa.py | 2 +- .../models/llama4/configuration_llama4.py | 2 +- .../models/marian/tokenization_marian.py | 2 +- .../models/mimi/configuration_mimi.py | 2 +- .../feature_extraction_musicgen_melody.py | 2 +- .../models/paligemma/modeling_paligemma.py | 6 +- .../configuration_prompt_depth_anything.py | 2 +- .../convert_prompt_depth_anything_to_hf.py | 32 ++++++-- .../models/tvp/image_processing_tvp.py | 2 +- .../models/vit/configuration_vit.py | 2 +- .../whisper/feature_extraction_whisper.py | 2 +- src/transformers/pipelines/text_to_audio.py | 4 +- src/transformers/processing_utils.py | 4 +- .../quantizers/quantizer_quanto.py | 2 +- src/transformers/tokenization_python.py | 2 +- src/transformers/tokenization_utils_base.py | 2 +- src/transformers/utils/auto_docstring.py | 10 +-- src/transformers/utils/hub.py | 2 +- src/transformers/utils/import_utils.py | 2 +- .../pytorch/run_glue_model_parallelism.py | 4 +- utils/get_test_reports.py | 2 +- utils/modular_model_converter.py | 2 +- 52 files changed, 168 insertions(+), 116 deletions(-) diff --git a/benchmark_v2/framework/benchmark_runner.py b/benchmark_v2/framework/benchmark_runner.py index 37aadd3f2c08..127396e435a2 100644 --- a/benchmark_v2/framework/benchmark_runner.py +++ b/benchmark_v2/framework/benchmark_runner.py @@ -407,7 +407,7 @@ def save_results(self, model_name: str, results: dict, timestamp: str = "", summ os.makedirs(model_dir, exist_ok=True) # Create filename with timestamp - timestamp = timestamp if timestamp else datetime.now().strftime("%Y%m%d_%H%M%S") + timestamp = timestamp or datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"{model_name}_benchmark_{timestamp}.json" filepath = os.path.join(model_dir, filename) @@ -458,7 +458,7 @@ def push_results_to_hub(self, dataset_id: str, results: dict[Any, Any], timestam f.write("\n".join(json_lines)) # NOTE: we expect the repository to already exist - timestamp = timestamp if timestamp else datetime.now().strftime("%Y%m%d_%H%M%S") + timestamp = timestamp or datetime.now().strftime("%Y%m%d_%H%M%S") file_name = file_name + "/" + f"benchmark_run_{timestamp}.jsonl" api.upload_file( path_or_fileobj=jsonl_path, diff --git a/examples/modular-transformers/modeling_new_task_model.py b/examples/modular-transformers/modeling_new_task_model.py index 72642ad9cfab..6a87915986a3 100644 --- a/examples/modular-transformers/modeling_new_task_model.py +++ b/examples/modular-transformers/modeling_new_task_model.py @@ -173,10 +173,8 @@ def create_causal_mask_mapping( # from `forward` call. If users run a `forward` call, we have no option to infer `is_first_iteration` because users may be # running generation with custom loop. Thus we need to infer it in a `non-perfect` way # NOTE: Determining prefill in that case requires checking data values, which is not compile-compatible. - is_first_iteration = ( - is_first_iteration - if is_first_iteration - else (past_key_values is None or not past_key_values.is_initialized or pixel_values is not None) + is_first_iteration = is_first_iteration or ( + past_key_values is None or not past_key_values.is_initialized or pixel_values is not None ) if is_first_iteration or not kwargs.get("use_cache", True): diff --git a/examples/pytorch/image-pretraining/run_mae.py b/examples/pytorch/image-pretraining/run_mae.py index 4adc55063a02..1fefdceb91be 100644 --- a/examples/pytorch/image-pretraining/run_mae.py +++ b/examples/pytorch/image-pretraining/run_mae.py @@ -112,7 +112,7 @@ def __post_init__(self): data_files["train"] = self.train_dir if self.validation_dir is not None: data_files["val"] = self.validation_dir - self.data_files = data_files if data_files else None + self.data_files = data_files or None @dataclass diff --git a/examples/pytorch/image-pretraining/run_mim.py b/examples/pytorch/image-pretraining/run_mim.py index 47e73ee2bca5..6cac169264f0 100644 --- a/examples/pytorch/image-pretraining/run_mim.py +++ b/examples/pytorch/image-pretraining/run_mim.py @@ -115,7 +115,7 @@ def __post_init__(self): data_files["train"] = self.train_dir if self.validation_dir is not None: data_files["val"] = self.validation_dir - self.data_files = data_files if data_files else None + self.data_files = data_files or None @dataclass diff --git a/examples/pytorch/image-pretraining/run_mim_no_trainer.py b/examples/pytorch/image-pretraining/run_mim_no_trainer.py index 1c5636088632..c91d4abc31c2 100644 --- a/examples/pytorch/image-pretraining/run_mim_no_trainer.py +++ b/examples/pytorch/image-pretraining/run_mim_no_trainer.py @@ -331,7 +331,7 @@ def parse_args(): data_files["train"] = args.train_dir if args.validation_dir is not None: data_files["val"] = args.validation_dir - args.data_files = data_files if data_files else None + args.data_files = data_files or None if args.push_to_hub: assert args.output_dir is not None, "Need an `output_dir` to create a repo when `--push_to_hub` is passed." diff --git a/examples/pytorch/multiple-choice/run_swag.py b/examples/pytorch/multiple-choice/run_swag.py index b1fc16e613a7..763b6a422233 100755 --- a/examples/pytorch/multiple-choice/run_swag.py +++ b/examples/pytorch/multiple-choice/run_swag.py @@ -254,14 +254,14 @@ def main(): # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, diff --git a/examples/pytorch/question-answering/run_qa.py b/examples/pytorch/question-answering/run_qa.py index bebed230b63d..488a5d18ff71 100755 --- a/examples/pytorch/question-answering/run_qa.py +++ b/examples/pytorch/question-answering/run_qa.py @@ -308,14 +308,14 @@ def main(): # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, use_fast=True, revision=model_args.model_revision, diff --git a/examples/pytorch/question-answering/run_qa_beam_search.py b/examples/pytorch/question-answering/run_qa_beam_search.py index 92a2b89b9273..61f16d134212 100755 --- a/examples/pytorch/question-answering/run_qa_beam_search.py +++ b/examples/pytorch/question-answering/run_qa_beam_search.py @@ -306,13 +306,13 @@ def main(): # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = XLNetConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, ) tokenizer = XLNetTokenizerFast.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, diff --git a/examples/pytorch/question-answering/run_seq2seq_qa.py b/examples/pytorch/question-answering/run_seq2seq_qa.py index cf81398d8d88..ea40f3cf6019 100644 --- a/examples/pytorch/question-answering/run_seq2seq_qa.py +++ b/examples/pytorch/question-answering/run_seq2seq_qa.py @@ -354,14 +354,14 @@ def main(): # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, diff --git a/examples/pytorch/speech-recognition/run_speech_recognition_seq2seq.py b/examples/pytorch/speech-recognition/run_speech_recognition_seq2seq.py index f158ee2bd9bc..09969cc0bd61 100755 --- a/examples/pytorch/speech-recognition/run_speech_recognition_seq2seq.py +++ b/examples/pytorch/speech-recognition/run_speech_recognition_seq2seq.py @@ -370,7 +370,7 @@ def main(): # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently config = AutoConfig.from_pretrained( - (model_args.config_name if model_args.config_name else model_args.model_name_or_path), + (model_args.config_name or model_args.model_name_or_path), cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, diff --git a/examples/pytorch/summarization/run_summarization.py b/examples/pytorch/summarization/run_summarization.py index b0044fbbf337..98b35f2045e3 100755 --- a/examples/pytorch/summarization/run_summarization.py +++ b/examples/pytorch/summarization/run_summarization.py @@ -420,14 +420,14 @@ def main(): # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, diff --git a/examples/pytorch/text-classification/run_classification.py b/examples/pytorch/text-classification/run_classification.py index 457ccc9001bf..72796c1bf4d8 100755 --- a/examples/pytorch/text-classification/run_classification.py +++ b/examples/pytorch/text-classification/run_classification.py @@ -471,7 +471,7 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, num_labels=num_labels, finetuning_task="text-classification", cache_dir=model_args.cache_dir, @@ -491,7 +491,7 @@ def main(): logger.info("setting problem type to single label classification") tokenizer = AutoTokenizer.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, diff --git a/examples/pytorch/text-classification/run_glue.py b/examples/pytorch/text-classification/run_glue.py index 77e4193e7a3c..3a1477140f81 100755 --- a/examples/pytorch/text-classification/run_glue.py +++ b/examples/pytorch/text-classification/run_glue.py @@ -361,7 +361,7 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, num_labels=num_labels, finetuning_task=data_args.task_name, cache_dir=model_args.cache_dir, @@ -370,7 +370,7 @@ def main(): trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, diff --git a/examples/pytorch/text-classification/run_xnli.py b/examples/pytorch/text-classification/run_xnli.py index 3015b3537428..758d5ed9c520 100755 --- a/examples/pytorch/text-classification/run_xnli.py +++ b/examples/pytorch/text-classification/run_xnli.py @@ -274,7 +274,7 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, num_labels=num_labels, id2label={str(i): label for i, label in enumerate(label_list)}, label2id={label: i for i, label in enumerate(label_list)}, @@ -285,7 +285,7 @@ def main(): trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, do_lower_case=model_args.do_lower_case, cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, diff --git a/examples/pytorch/text-generation/run_generation.py b/examples/pytorch/text-generation/run_generation.py index 9d9477f8f099..d4044138b013 100755 --- a/examples/pytorch/text-generation/run_generation.py +++ b/examples/pytorch/text-generation/run_generation.py @@ -133,13 +133,13 @@ def prepare_xlm_input(args, model, tokenizer, prompt_text): def prepare_xlnet_input(args, _, tokenizer, prompt_text): - prefix = args.prefix if args.prefix else args.padding_text if args.padding_text else PREFIX + prefix = args.prefix or (args.padding_text or PREFIX) prompt_text = prefix + prompt_text return prompt_text def prepare_transfoxl_input(args, _, tokenizer, prompt_text): - prefix = args.prefix if args.prefix else args.padding_text if args.padding_text else PREFIX + prefix = args.prefix or (args.padding_text or PREFIX) prompt_text = prefix + prompt_text return prompt_text @@ -366,7 +366,7 @@ def main(): args.length = adjust_length_to_model(args.length, max_sequence_length=max_seq_length) logger.info(args) - prompt_text = args.prompt if args.prompt else input("Model prompt >>> ") + prompt_text = args.prompt or input("Model prompt >>> ") # Different models need different input formatting and/or extra arguments requires_preprocessing = args.model_type in PREPROCESSING_FUNCTIONS @@ -380,7 +380,7 @@ def main(): preprocessed_prompt_text, add_special_tokens=False, return_tensors="pt", **tokenizer_kwargs ) else: - prefix = args.prefix if args.prefix else args.padding_text + prefix = args.prefix or args.padding_text encoded_prompt = tokenizer.encode(prefix + prompt_text, add_special_tokens=False, return_tensors="pt") encoded_prompt = encoded_prompt.to(distributed_state.device) diff --git a/examples/pytorch/token-classification/run_ner.py b/examples/pytorch/token-classification/run_ner.py index 23ad008e09f4..41704589b742 100755 --- a/examples/pytorch/token-classification/run_ner.py +++ b/examples/pytorch/token-classification/run_ner.py @@ -345,7 +345,7 @@ def get_label_list(labels): # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, num_labels=num_labels, finetuning_task=data_args.task_name, cache_dir=model_args.cache_dir, @@ -354,7 +354,7 @@ def get_label_list(labels): trust_remote_code=model_args.trust_remote_code, ) - tokenizer_name_or_path = model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path + tokenizer_name_or_path = model_args.tokenizer_name or model_args.model_name_or_path if config.model_type in {"bloom", "gpt2", "roberta", "deberta"}: tokenizer = AutoTokenizer.from_pretrained( tokenizer_name_or_path, diff --git a/examples/pytorch/token-classification/run_ner_no_trainer.py b/examples/pytorch/token-classification/run_ner_no_trainer.py index 138c99b6d0ca..4c2f58e23ad1 100755 --- a/examples/pytorch/token-classification/run_ner_no_trainer.py +++ b/examples/pytorch/token-classification/run_ner_no_trainer.py @@ -417,7 +417,7 @@ def get_label_list(labels): config = CONFIG_MAPPING[args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") - tokenizer_name_or_path = args.tokenizer_name if args.tokenizer_name else args.model_name_or_path + tokenizer_name_or_path = args.tokenizer_name or args.model_name_or_path if not tokenizer_name_or_path: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script. " diff --git a/examples/pytorch/translation/run_translation.py b/examples/pytorch/translation/run_translation.py index a705bc94a7f3..d4c17d934c4f 100755 --- a/examples/pytorch/translation/run_translation.py +++ b/examples/pytorch/translation/run_translation.py @@ -372,14 +372,14 @@ def main(): # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, diff --git a/src/transformers/data/metrics/squad_metrics.py b/src/transformers/data/metrics/squad_metrics.py index 0ffc025b65a0..7357d41b1bea 100644 --- a/src/transformers/data/metrics/squad_metrics.py +++ b/src/transformers/data/metrics/squad_metrics.py @@ -479,7 +479,7 @@ def compute_predictions_logits( end_logit=null_end_logit, ) ) - prelim_predictions = sorted(prelim_predictions, key=lambda x: (x.start_logit + x.end_logit), reverse=True) + prelim_predictions = sorted(prelim_predictions, key=lambda x: x.start_logit + x.end_logit, reverse=True) _NbestPrediction = collections.namedtuple( # pylint: disable=invalid-name "NbestPrediction", ["text", "start_logit", "end_logit"] @@ -681,9 +681,7 @@ def compute_predictions_log_probs( ) ) - prelim_predictions = sorted( - prelim_predictions, key=lambda x: (x.start_log_prob + x.end_log_prob), reverse=True - ) + prelim_predictions = sorted(prelim_predictions, key=lambda x: x.start_log_prob + x.end_log_prob, reverse=True) seen_predictions = {} nbest = [] diff --git a/src/transformers/generation/utils.py b/src/transformers/generation/utils.py index e305e76b1fd5..f1e8a4b87511 100644 --- a/src/transformers/generation/utils.py +++ b/src/transformers/generation/utils.py @@ -571,13 +571,11 @@ def branch_3(input_ids, cache_position): torch.cond( cache_position[-1] >= input_ids.shape[1], branch_2, - lambda input_ids, cache_position: ( - torch.cond( - input_ids.shape[1] != cache_position.shape[0], - branch_3, - (lambda input_ids, cache_position: input_ids.clone()), - [input_ids, cache_position], - ) + lambda input_ids, cache_position: torch.cond( + input_ids.shape[1] != cache_position.shape[0], + branch_3, + (lambda input_ids, cache_position: input_ids.clone()), + [input_ids, cache_position], ), [input_ids, cache_position], ), diff --git a/src/transformers/integrations/finegrained_fp8.py b/src/transformers/integrations/finegrained_fp8.py index 0766d71a8663..45246b34aee4 100644 --- a/src/transformers/integrations/finegrained_fp8.py +++ b/src/transformers/integrations/finegrained_fp8.py @@ -42,7 +42,7 @@ def _get_quantization_kernel(): except Exception as e: logger.warning_once(f"Failed to load CUTLASS quantization kernel: {e}. Falling back to Triton.") _quantization_kernel = False # Mark as unavailable - return _quantization_kernel if _quantization_kernel else None + return _quantization_kernel or None def _supports_cutlass(block_size: list[int] | None, output_dtype: torch.dtype) -> bool: diff --git a/src/transformers/integrations/integration_utils.py b/src/transformers/integrations/integration_utils.py index aabfd0bbe268..9bf08098e3e8 100755 --- a/src/transformers/integrations/integration_utils.py +++ b/src/transformers/integrations/integration_utils.py @@ -465,7 +465,7 @@ def run_hp_search_wandb(trainer, n_trials: int, direction: str, **kwargs) -> Bes sweep_config["name"] = name def _objective(): - run = wandb.run if wandb.run else wandb.init() + run = wandb.run or wandb.init() trainer.state.trial_name = run.name run.config.update({"assignments": {}, "metric": metric}) config = wandb.config diff --git a/src/transformers/modelcard.py b/src/transformers/modelcard.py index 922dca493645..790bba6d7938 100644 --- a/src/transformers/modelcard.py +++ b/src/transformers/modelcard.py @@ -562,7 +562,7 @@ def extract_hyperparameters_from_trainer(trainer): if trainer.args.optim: optimizer_name = trainer.args.optim - optimizer_args = trainer.args.optim_args if trainer.args.optim_args else "No additional optimizer arguments" + optimizer_args = trainer.args.optim_args or "No additional optimizer arguments" if "adam" in optimizer_name.lower(): hyperparameters["optimizer"] = ( diff --git a/src/transformers/models/clap/feature_extraction_clap.py b/src/transformers/models/clap/feature_extraction_clap.py index 8f0a34d2cf4e..cecd120adef3 100644 --- a/src/transformers/models/clap/feature_extraction_clap.py +++ b/src/transformers/models/clap/feature_extraction_clap.py @@ -296,7 +296,7 @@ def __call__( pipeline. """ truncation = truncation if truncation is not None else self.truncation - padding = padding if padding else self.padding + padding = padding or self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: @@ -331,7 +331,7 @@ def __call__( # convert to mel spectrogram, truncate and pad if needed. padded_inputs = [ - self._get_input_mel(waveform, max_length if max_length else self.nb_max_samples, truncation, padding) + self._get_input_mel(waveform, max_length or self.nb_max_samples, truncation, padding) for waveform in raw_speech ] diff --git a/src/transformers/models/deit/configuration_deit.py b/src/transformers/models/deit/configuration_deit.py index 1b2ebaad785b..0edea6014ca5 100644 --- a/src/transformers/models/deit/configuration_deit.py +++ b/src/transformers/models/deit/configuration_deit.py @@ -120,7 +120,7 @@ def __init__( self.num_channels = num_channels self.qkv_bias = qkv_bias self.encoder_stride = encoder_stride - self.pooler_output_size = pooler_output_size if pooler_output_size else hidden_size + self.pooler_output_size = pooler_output_size or hidden_size self.pooler_act = pooler_act diff --git a/src/transformers/models/depth_anything/configuration_depth_anything.py b/src/transformers/models/depth_anything/configuration_depth_anything.py index ccd816f18796..b7ea3851c18a 100644 --- a/src/transformers/models/depth_anything/configuration_depth_anything.py +++ b/src/transformers/models/depth_anything/configuration_depth_anything.py @@ -115,7 +115,7 @@ def __init__( if depth_estimation_type not in ["relative", "metric"]: raise ValueError("depth_estimation_type must be one of ['relative', 'metric']") self.depth_estimation_type = depth_estimation_type - self.max_depth = max_depth if max_depth else 1 + self.max_depth = max_depth or 1 super().__init__(**kwargs) diff --git a/src/transformers/models/depth_anything/convert_distill_any_depth_to_hf.py b/src/transformers/models/depth_anything/convert_distill_any_depth_to_hf.py index 205b6a4ffc96..45ec7a08fde9 100644 --- a/src/transformers/models/depth_anything/convert_distill_any_depth_to_hf.py +++ b/src/transformers/models/depth_anything/convert_distill_any_depth_to_hf.py @@ -49,8 +49,12 @@ r"depth_head\.scratch\.output_conv(\d+)(?:\.(\d+))?\.(weight|bias)": lambda m: ( f"head.conv{int(m[1]) + (int(m[2]) // 2 if m[2] else 0)}.{m[3]}" if m[1] == "2" else f"head.conv{m[1]}.{m[3]}" ), - r"depth_head\.scratch\.refinenet(\d+)\.out_conv\.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{3 - (int(m[1]) - 1)}.projection.{m[2]}", - r"depth_head\.scratch\.refinenet(\d+)\.resConfUnit(\d+)\.conv(\d+)\.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{3 - (int(m[1]) - 1)}.residual_layer{m[2]}.convolution{m[3]}.{m[4]}", + r"depth_head\.scratch\.refinenet(\d+)\.out_conv\.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{3 - (int(m[1]) - 1)}.projection.{m[2]}" + ), + r"depth_head\.scratch\.refinenet(\d+)\.resConfUnit(\d+)\.conv(\d+)\.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{3 - (int(m[1]) - 1)}.residual_layer{m[2]}.convolution{m[3]}.{m[4]}" + ), } diff --git a/src/transformers/models/dpt/configuration_dpt.py b/src/transformers/models/dpt/configuration_dpt.py index 9330a475a6cd..a338f6066420 100644 --- a/src/transformers/models/dpt/configuration_dpt.py +++ b/src/transformers/models/dpt/configuration_dpt.py @@ -226,7 +226,7 @@ def __init__( self.auxiliary_loss_weight = auxiliary_loss_weight self.semantic_loss_ignore_index = semantic_loss_ignore_index self.semantic_classifier_dropout = semantic_classifier_dropout - self.pooler_output_size = pooler_output_size if pooler_output_size else hidden_size + self.pooler_output_size = pooler_output_size or hidden_size self.pooler_act = pooler_act super().__init__(**kwargs) diff --git a/src/transformers/models/efficientloftr/convert_efficientloftr_to_hf.py b/src/transformers/models/efficientloftr/convert_efficientloftr_to_hf.py index 4a64719d8faa..e74e7b5d398a 100644 --- a/src/transformers/models/efficientloftr/convert_efficientloftr_to_hf.py +++ b/src/transformers/models/efficientloftr/convert_efficientloftr_to_hf.py @@ -82,27 +82,67 @@ def verify_model_outputs(model, device): r"matcher.backbone.layer(\d+).(\d+).rbr_1x1.conv": r"efficientloftr.backbone.stages.\1.blocks.\2.conv2.conv", r"matcher.backbone.layer(\d+).(\d+).rbr_1x1.bn": r"efficientloftr.backbone.stages.\1.blocks.\2.conv2.norm", r"matcher.backbone.layer(\d+).(\d+).rbr_identity": r"efficientloftr.backbone.stages.\1.blocks.\2.identity", - r"matcher.loftr_coarse.layers.(\d*[02468]).aggregate": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.aggregation.q_aggregation", - r"matcher.loftr_coarse.layers.(\d*[02468]).norm1": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.aggregation.norm", - r"matcher.loftr_coarse.layers.(\d*[02468]).q_proj": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.attention.q_proj", - r"matcher.loftr_coarse.layers.(\d*[02468]).k_proj": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.attention.k_proj", - r"matcher.loftr_coarse.layers.(\d*[02468]).v_proj": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.attention.v_proj", - r"matcher.loftr_coarse.layers.(\d*[02468]).merge": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.attention.o_proj", - r"matcher.loftr_coarse.layers.(\d*[02468]).mlp.(\d+)": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.mlp.fc{1 if m.group(2) == '0' else 2}", - r"matcher.loftr_coarse.layers.(\d*[02468]).norm2": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.mlp.layer_norm", - r"matcher.loftr_coarse.layers.(\d*[13579]).aggregate": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.aggregation.q_aggregation", - r"matcher.loftr_coarse.layers.(\d*[13579]).norm1": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.aggregation.norm", - r"matcher.loftr_coarse.layers.(\d*[13579]).q_proj": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.attention.q_proj", - r"matcher.loftr_coarse.layers.(\d*[13579]).k_proj": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.attention.k_proj", - r"matcher.loftr_coarse.layers.(\d*[13579]).v_proj": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.attention.v_proj", - r"matcher.loftr_coarse.layers.(\d*[13579]).merge": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.attention.o_proj", - r"matcher.loftr_coarse.layers.(\d*[13579]).mlp.(\d+)": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.mlp.fc{1 if m.group(2) == '0' else 2}", - r"matcher.loftr_coarse.layers.(\d*[13579]).norm2": lambda m: f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.mlp.layer_norm", + r"matcher.loftr_coarse.layers.(\d*[02468]).aggregate": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.aggregation.q_aggregation" + ), + r"matcher.loftr_coarse.layers.(\d*[02468]).norm1": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.aggregation.norm" + ), + r"matcher.loftr_coarse.layers.(\d*[02468]).q_proj": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.attention.q_proj" + ), + r"matcher.loftr_coarse.layers.(\d*[02468]).k_proj": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.attention.k_proj" + ), + r"matcher.loftr_coarse.layers.(\d*[02468]).v_proj": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.attention.v_proj" + ), + r"matcher.loftr_coarse.layers.(\d*[02468]).merge": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.attention.o_proj" + ), + r"matcher.loftr_coarse.layers.(\d*[02468]).mlp.(\d+)": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.mlp.fc{1 if m.group(2) == '0' else 2}" + ), + r"matcher.loftr_coarse.layers.(\d*[02468]).norm2": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.self_attention.mlp.layer_norm" + ), + r"matcher.loftr_coarse.layers.(\d*[13579]).aggregate": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.aggregation.q_aggregation" + ), + r"matcher.loftr_coarse.layers.(\d*[13579]).norm1": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.aggregation.norm" + ), + r"matcher.loftr_coarse.layers.(\d*[13579]).q_proj": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.attention.q_proj" + ), + r"matcher.loftr_coarse.layers.(\d*[13579]).k_proj": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.attention.k_proj" + ), + r"matcher.loftr_coarse.layers.(\d*[13579]).v_proj": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.attention.v_proj" + ), + r"matcher.loftr_coarse.layers.(\d*[13579]).merge": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.attention.o_proj" + ), + r"matcher.loftr_coarse.layers.(\d*[13579]).mlp.(\d+)": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.mlp.fc{1 if m.group(2) == '0' else 2}" + ), + r"matcher.loftr_coarse.layers.(\d*[13579]).norm2": lambda m: ( + f"efficientloftr.local_feature_transformer.layers.{int(m.group(1)) // 2}.cross_attention.mlp.layer_norm" + ), r"matcher.fine_preprocess.layer3_outconv": "refinement_layer.out_conv", - r"matcher.fine_preprocess.layer(\d+)_outconv.weight": lambda m: f"refinement_layer.out_conv_layers.{0 if int(m.group(1)) == 2 else m.group(1)}.out_conv1.weight", - r"matcher.fine_preprocess.layer(\d+)_outconv2\.0": lambda m: f"refinement_layer.out_conv_layers.{0 if int(m.group(1)) == 2 else m.group(1)}.out_conv2", - r"matcher.fine_preprocess.layer(\d+)_outconv2\.1": lambda m: f"refinement_layer.out_conv_layers.{0 if int(m.group(1)) == 2 else m.group(1)}.batch_norm", - r"matcher.fine_preprocess.layer(\d+)_outconv2\.3": lambda m: f"refinement_layer.out_conv_layers.{0 if int(m.group(1)) == 2 else m.group(1)}.out_conv3", + r"matcher.fine_preprocess.layer(\d+)_outconv.weight": lambda m: ( + f"refinement_layer.out_conv_layers.{0 if int(m.group(1)) == 2 else m.group(1)}.out_conv1.weight" + ), + r"matcher.fine_preprocess.layer(\d+)_outconv2\.0": lambda m: ( + f"refinement_layer.out_conv_layers.{0 if int(m.group(1)) == 2 else m.group(1)}.out_conv2" + ), + r"matcher.fine_preprocess.layer(\d+)_outconv2\.1": lambda m: ( + f"refinement_layer.out_conv_layers.{0 if int(m.group(1)) == 2 else m.group(1)}.batch_norm" + ), + r"matcher.fine_preprocess.layer(\d+)_outconv2\.3": lambda m: ( + f"refinement_layer.out_conv_layers.{0 if int(m.group(1)) == 2 else m.group(1)}.out_conv3" + ), } diff --git a/src/transformers/models/granitemoehybrid/configuration_granitemoehybrid.py b/src/transformers/models/granitemoehybrid/configuration_granitemoehybrid.py index e69347f363af..08e4af2d939d 100644 --- a/src/transformers/models/granitemoehybrid/configuration_granitemoehybrid.py +++ b/src/transformers/models/granitemoehybrid/configuration_granitemoehybrid.py @@ -248,7 +248,7 @@ def __init__( # overwrite the function to use in `HybridMambaAttentionDynamicCache` @property def layers_block_type(self): - return self.layer_types if self.layer_types else ["mamba"] * self.num_hidden_layers + return self.layer_types or ["mamba"] * self.num_hidden_layers __all__ = ["GraniteMoeHybridConfig"] diff --git a/src/transformers/models/ijepa/configuration_ijepa.py b/src/transformers/models/ijepa/configuration_ijepa.py index e19131515df2..30c67e0cc980 100644 --- a/src/transformers/models/ijepa/configuration_ijepa.py +++ b/src/transformers/models/ijepa/configuration_ijepa.py @@ -111,7 +111,7 @@ def __init__( self.patch_size = patch_size self.num_channels = num_channels self.qkv_bias = qkv_bias - self.pooler_output_size = pooler_output_size if pooler_output_size else hidden_size + self.pooler_output_size = pooler_output_size or hidden_size self.pooler_act = pooler_act diff --git a/src/transformers/models/llama4/configuration_llama4.py b/src/transformers/models/llama4/configuration_llama4.py index 6b3de2d4e749..f7918da48f58 100644 --- a/src/transformers/models/llama4/configuration_llama4.py +++ b/src/transformers/models/llama4/configuration_llama4.py @@ -337,7 +337,7 @@ def __init__( int((layer_idx + 1) % no_rope_layer_interval != 0) for layer_idx in range(self.num_hidden_layers) ] - self.no_rope_layers = no_rope_layers if no_rope_layers else default_no_rope_layers + self.no_rope_layers = no_rope_layers or default_no_rope_layers self.interleave_moe_layer_step = interleave_moe_layer_step self.moe_layers = ( diff --git a/src/transformers/models/marian/tokenization_marian.py b/src/transformers/models/marian/tokenization_marian.py index 8b82e1d0a1db..3799c3fc8412 100644 --- a/src/transformers/models/marian/tokenization_marian.py +++ b/src/transformers/models/marian/tokenization_marian.py @@ -207,7 +207,7 @@ def _convert_id_to_token(self, index: int) -> str: # Fall back to SPM model for IDs not in external vocab spm_model = self.spm_source if self._decode_use_source_tokenizer else self.spm_target piece = spm_model.IdToPiece(index) - return piece if piece else self.unk_token + return piece or self.unk_token def batch_decode(self, sequences, **kwargs): """ diff --git a/src/transformers/models/mimi/configuration_mimi.py b/src/transformers/models/mimi/configuration_mimi.py index 223c79004ab2..2203d79dceb8 100644 --- a/src/transformers/models/mimi/configuration_mimi.py +++ b/src/transformers/models/mimi/configuration_mimi.py @@ -192,7 +192,7 @@ def __init__( self.hidden_size = hidden_size self.num_filters = num_filters self.num_residual_layers = num_residual_layers - self.upsampling_ratios = upsampling_ratios if upsampling_ratios else [8, 6, 5, 4] + self.upsampling_ratios = upsampling_ratios or [8, 6, 5, 4] self.kernel_size = kernel_size self.last_kernel_size = last_kernel_size self.residual_kernel_size = residual_kernel_size diff --git a/src/transformers/models/musicgen_melody/feature_extraction_musicgen_melody.py b/src/transformers/models/musicgen_melody/feature_extraction_musicgen_melody.py index 1811fa11e630..e766828b2f4c 100644 --- a/src/transformers/models/musicgen_melody/feature_extraction_musicgen_melody.py +++ b/src/transformers/models/musicgen_melody/feature_extraction_musicgen_melody.py @@ -293,7 +293,7 @@ def __call__( padded_inputs = self.pad( batched_speech, padding=padding, - max_length=max_length if max_length else self.n_samples, + max_length=max_length or self.n_samples, truncation=truncation, pad_to_multiple_of=pad_to_multiple_of, return_attention_mask=return_attention_mask, diff --git a/src/transformers/models/paligemma/modeling_paligemma.py b/src/transformers/models/paligemma/modeling_paligemma.py index db9ff80cac15..2f6a42dc15c5 100644 --- a/src/transformers/models/paligemma/modeling_paligemma.py +++ b/src/transformers/models/paligemma/modeling_paligemma.py @@ -173,10 +173,8 @@ def create_causal_mask_mapping( # from `forward` call. If users run a `forward` call, we have no option to infer `is_first_iteration` because users may be # running generation with custom loop. Thus we need to infer it in a `non-perfect` way # NOTE: Determining prefill in that case requires checking data values, which is not compile-compatible. - is_first_iteration = ( - is_first_iteration - if is_first_iteration - else (past_key_values is None or not past_key_values.is_initialized or pixel_values is not None) + is_first_iteration = is_first_iteration or ( + past_key_values is None or not past_key_values.is_initialized or pixel_values is not None ) if is_first_iteration or not kwargs.get("use_cache", True): diff --git a/src/transformers/models/prompt_depth_anything/configuration_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/configuration_prompt_depth_anything.py index ffa4e3086f34..72226d9a9867 100644 --- a/src/transformers/models/prompt_depth_anything/configuration_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/configuration_prompt_depth_anything.py @@ -116,7 +116,7 @@ def __init__( if depth_estimation_type not in ["relative", "metric"]: raise ValueError("depth_estimation_type must be one of ['relative', 'metric']") self.depth_estimation_type = depth_estimation_type - self.max_depth = max_depth if max_depth else 1 + self.max_depth = max_depth or 1 super().__init__(**kwargs) diff --git a/src/transformers/models/prompt_depth_anything/convert_prompt_depth_anything_to_hf.py b/src/transformers/models/prompt_depth_anything/convert_prompt_depth_anything_to_hf.py index b8c7788ba191..5fab040e68ad 100644 --- a/src/transformers/models/prompt_depth_anything/convert_prompt_depth_anything_to_hf.py +++ b/src/transformers/models/prompt_depth_anything/convert_prompt_depth_anything_to_hf.py @@ -115,14 +115,30 @@ def transform_qkv_weights(key, value, config): r"depth_head.scratch.layer(\d+)_rn.weight": lambda m: f"neck.convs.{int(m.group(1)) - 1}.weight", r"depth_head.resize_layers.(\d+).(weight|bias)": r"neck.reassemble_stage.layers.\1.resize.\2", # Refinenet (with reversed indices) - r"depth_head.scratch.refinenet(\d+).out_conv.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{4 - int(m.group(1))}.projection.{m.group(2)}", - r"depth_head.scratch.refinenet(\d+).resConfUnit1.conv1.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{4 - int(m.group(1))}.residual_layer1.convolution1.{m.group(2)}", - r"depth_head.scratch.refinenet(\d+).resConfUnit1.conv2.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{4 - int(m.group(1))}.residual_layer1.convolution2.{m.group(2)}", - r"depth_head.scratch.refinenet(\d+).resConfUnit2.conv1.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{4 - int(m.group(1))}.residual_layer2.convolution1.{m.group(2)}", - r"depth_head.scratch.refinenet(\d+).resConfUnit2.conv2.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{4 - int(m.group(1))}.residual_layer2.convolution2.{m.group(2)}", - r"depth_head.scratch.refinenet(\d+).resConfUnit_depth.0.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{4 - int(m.group(1))}.prompt_depth_layer.convolution1.{m.group(2)}", - r"depth_head.scratch.refinenet(\d+).resConfUnit_depth.2.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{4 - int(m.group(1))}.prompt_depth_layer.convolution2.{m.group(2)}", - r"depth_head.scratch.refinenet(\d+).resConfUnit_depth.4.(weight|bias)": lambda m: f"neck.fusion_stage.layers.{4 - int(m.group(1))}.prompt_depth_layer.convolution3.{m.group(2)}", + r"depth_head.scratch.refinenet(\d+).out_conv.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{4 - int(m.group(1))}.projection.{m.group(2)}" + ), + r"depth_head.scratch.refinenet(\d+).resConfUnit1.conv1.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{4 - int(m.group(1))}.residual_layer1.convolution1.{m.group(2)}" + ), + r"depth_head.scratch.refinenet(\d+).resConfUnit1.conv2.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{4 - int(m.group(1))}.residual_layer1.convolution2.{m.group(2)}" + ), + r"depth_head.scratch.refinenet(\d+).resConfUnit2.conv1.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{4 - int(m.group(1))}.residual_layer2.convolution1.{m.group(2)}" + ), + r"depth_head.scratch.refinenet(\d+).resConfUnit2.conv2.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{4 - int(m.group(1))}.residual_layer2.convolution2.{m.group(2)}" + ), + r"depth_head.scratch.refinenet(\d+).resConfUnit_depth.0.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{4 - int(m.group(1))}.prompt_depth_layer.convolution1.{m.group(2)}" + ), + r"depth_head.scratch.refinenet(\d+).resConfUnit_depth.2.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{4 - int(m.group(1))}.prompt_depth_layer.convolution2.{m.group(2)}" + ), + r"depth_head.scratch.refinenet(\d+).resConfUnit_depth.4.(weight|bias)": lambda m: ( + f"neck.fusion_stage.layers.{4 - int(m.group(1))}.prompt_depth_layer.convolution3.{m.group(2)}" + ), # Head r"depth_head.scratch.output_conv1.(weight|bias)": r"head.conv1.\1", r"depth_head.scratch.output_conv2.0.(weight|bias)": r"head.conv2.\1", diff --git a/src/transformers/models/tvp/image_processing_tvp.py b/src/transformers/models/tvp/image_processing_tvp.py index 56848ef4cace..2f85625f1546 100644 --- a/src/transformers/models/tvp/image_processing_tvp.py +++ b/src/transformers/models/tvp/image_processing_tvp.py @@ -436,7 +436,7 @@ def preprocess( do_pad = do_pad if do_pad is not None else self.do_pad pad_size = pad_size if pad_size is not None else self.pad_size constant_values = constant_values if constant_values is not None else self.constant_values - pad_mode = pad_mode if pad_mode else self.pad_mode + pad_mode = pad_mode or self.pad_mode do_normalize = do_normalize if do_normalize is not None else self.do_normalize do_flip_channel_order = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order diff --git a/src/transformers/models/vit/configuration_vit.py b/src/transformers/models/vit/configuration_vit.py index 9849f2380992..5c3b5a6ef83e 100644 --- a/src/transformers/models/vit/configuration_vit.py +++ b/src/transformers/models/vit/configuration_vit.py @@ -117,7 +117,7 @@ def __init__( self.num_channels = num_channels self.qkv_bias = qkv_bias self.encoder_stride = encoder_stride - self.pooler_output_size = pooler_output_size if pooler_output_size else hidden_size + self.pooler_output_size = pooler_output_size or hidden_size self.pooler_act = pooler_act super().__init__(**kwargs) diff --git a/src/transformers/models/whisper/feature_extraction_whisper.py b/src/transformers/models/whisper/feature_extraction_whisper.py index 4151a3824dfd..2c7a25e61475 100644 --- a/src/transformers/models/whisper/feature_extraction_whisper.py +++ b/src/transformers/models/whisper/feature_extraction_whisper.py @@ -296,7 +296,7 @@ def __call__( padded_inputs = self.pad( batched_speech, padding=padding, - max_length=max_length if max_length else self.n_samples, + max_length=max_length or self.n_samples, truncation=truncation, pad_to_multiple_of=pad_to_multiple_of, return_attention_mask=return_attention_mask or do_normalize, diff --git a/src/transformers/pipelines/text_to_audio.py b/src/transformers/pipelines/text_to_audio.py index 81c6e34f95a3..837b51144c79 100644 --- a/src/transformers/pipelines/text_to_audio.py +++ b/src/transformers/pipelines/text_to_audio.py @@ -279,8 +279,8 @@ def _sanitize_parameters( generate_kwargs["assistant_tokenizer"] = self.assistant_tokenizer params = { - "forward_params": forward_params if forward_params else {}, - "generate_kwargs": generate_kwargs if generate_kwargs else {}, + "forward_params": forward_params or {}, + "generate_kwargs": generate_kwargs or {}, } if preprocess_params is None: diff --git a/src/transformers/processing_utils.py b/src/transformers/processing_utils.py index 5924099efe20..c8e402b77604 100644 --- a/src/transformers/processing_utils.py +++ b/src/transformers/processing_utils.py @@ -1812,7 +1812,7 @@ def apply_chat_template( text=prompt, images=batch_images if images_exist else None, videos=batch_videos if videos_exist else None, - audio=batch_audios if batch_audios else None, + audio=batch_audios or None, **kwargs, ) @@ -1839,7 +1839,7 @@ def apply_chat_template( # Ensure end_pos is also within bounds if end_pos > len(input_ids[i]): end_pos = len(input_ids[i]) - for token_id in range(start_pos, end_pos if end_pos else len(input_ids[i])): + for token_id in range(start_pos, end_pos or len(input_ids[i])): current_mask[token_id] = 1 assistant_masks.append(current_mask) out["assistant_masks"] = assistant_masks diff --git a/src/transformers/quantizers/quantizer_quanto.py b/src/transformers/quantizers/quantizer_quanto.py index 1e01e5a593c8..96aa5ff729f5 100644 --- a/src/transformers/quantizers/quantizer_quanto.py +++ b/src/transformers/quantizers/quantizer_quanto.py @@ -50,7 +50,7 @@ def __init__(self, quantization_config: QuantoConfig, **kwargs): "int4": 0.5, "int2": 0.25, } - self.quantized_param_size = map_to_param_size.get(self.quantization_config.weights, None) + self.quantized_param_size = map_to_param_size.get(self.quantization_config.weights) def validate_environment(self, *args, **kwargs): if not is_optimum_quanto_available(): diff --git a/src/transformers/tokenization_python.py b/src/transformers/tokenization_python.py index de6326a7ccd4..fa457dbd6740 100644 --- a/src/transformers/tokenization_python.py +++ b/src/transformers/tokenization_python.py @@ -1172,7 +1172,7 @@ def prepare_for_model( sequence = self.build_inputs_with_special_tokens(ids, pair_ids) token_type_ids = self.create_token_type_ids_from_sequences(ids, pair_ids) else: - sequence = ids + (pair_ids if pair_ids else []) + sequence = ids + (pair_ids or []) token_type_ids = [0] * len(sequence) # Build output diff --git a/src/transformers/tokenization_utils_base.py b/src/transformers/tokenization_utils_base.py index 89433159b183..a864d5b5d10e 100644 --- a/src/transformers/tokenization_utils_base.py +++ b/src/transformers/tokenization_utils_base.py @@ -3372,7 +3372,7 @@ def find_sentencepiece_model_file(pretrained_model_name_or_path, **kwargs): entries = list_repo_tree( repo_id=pretrained_model_name_or_path, revision=kwargs.get("revision"), - path_in_repo=subfolder if subfolder else None, + path_in_repo=subfolder or None, recursive=False, token=kwargs.get("token"), ) diff --git a/src/transformers/utils/auto_docstring.py b/src/transformers/utils/auto_docstring.py index 02613cd53922..4642bfeab156 100644 --- a/src/transformers/utils/auto_docstring.py +++ b/src/transformers/utils/auto_docstring.py @@ -1678,7 +1678,7 @@ def _get_parameter_info(param_name, documented_params, source_args_dict, param_t param_type = documented_params[param_name]["type"] optional = documented_params[param_name]["optional"] shape = documented_params[param_name].get("shape", None) - shape_string = shape if shape else "" + shape_string = shape or "" additional_info = documented_params[param_name]["additional_info"] or "" description = f"{documented_params[param_name]['description']}\n" elif param_name in source_args_dict: @@ -1767,10 +1767,10 @@ def _process_regular_parameters( ) else: missing_args[param_name] = { - "type": param_type if param_type else "", + "type": param_type or "", "optional": optional, "shape": shape_string, - "description": description if description else "\n ", + "description": description or "\n ", "default": param_default, } undocumented_parameters.append( @@ -2393,9 +2393,9 @@ def auto_class_docstring(cls, custom_intro=None, custom_args=None, checkpoint=No docstring += set_min_indent(f"\n{docstring_init}", indent_level) elif is_dataclass: # No init function, we have a data class - docstring += docstring_args if docstring_args else "\nArgs:\n" + docstring += docstring_args or "\nArgs:\n" source_args_dict = get_args_doc_from_source(ModelOutputArgs) - doc_class = cls.__doc__ if cls.__doc__ else "" + doc_class = cls.__doc__ or "" documented_kwargs = parse_docstring(doc_class)[0] for param_name, param_type_annotation in cls.__annotations__.items(): param_type = str(param_type_annotation) diff --git a/src/transformers/utils/hub.py b/src/transformers/utils/hub.py index 127ae9bdc595..a1ba842cb896 100644 --- a/src/transformers/utils/hub.py +++ b/src/transformers/utils/hub.py @@ -383,7 +383,7 @@ def cached_files( existing_files.append(resolved_file) if os.path.isdir(path_or_repo_id): - return existing_files if existing_files else None + return existing_files or None if cache_dir is None: cache_dir = constants.HF_HUB_CACHE diff --git a/src/transformers/utils/import_utils.py b/src/transformers/utils/import_utils.py index 00864bdcc35b..0898638e2eb6 100644 --- a/src/transformers/utils/import_utils.py +++ b/src/transformers/utils/import_utils.py @@ -1916,7 +1916,7 @@ def __init__( super().__init__(name) self._object_missing_backend = {} - self._explicit_import_shortcut = explicit_import_shortcut if explicit_import_shortcut else {} + self._explicit_import_shortcut = explicit_import_shortcut or {} if any(isinstance(key, frozenset) for key in import_structure): self._modules = set() diff --git a/tests/sagemaker/scripts/pytorch/run_glue_model_parallelism.py b/tests/sagemaker/scripts/pytorch/run_glue_model_parallelism.py index 79e982bc4832..deb84961903e 100644 --- a/tests/sagemaker/scripts/pytorch/run_glue_model_parallelism.py +++ b/tests/sagemaker/scripts/pytorch/run_glue_model_parallelism.py @@ -280,14 +280,14 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( - model_args.config_name if model_args.config_name else model_args.model_name_or_path, + model_args.config_name or model_args.model_name_or_path, num_labels=num_labels, finetuning_task=data_args.task_name, cache_dir=model_args.cache_dir, revision=model_args.model_revision, ) tokenizer = AutoTokenizer.from_pretrained( - model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, + model_args.tokenizer_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, diff --git a/utils/get_test_reports.py b/utils/get_test_reports.py index d37535c9cf45..63ac980018c3 100644 --- a/utils/get_test_reports.py +++ b/utils/get_test_reports.py @@ -251,7 +251,7 @@ def handle_suite( machine_type = args.machine_type # Reduce the scope for models if necessary - only_in = args.only_in if args.only_in else None + only_in = args.only_in or None if only_in == ["IMPORTANT_MODELS"]: only_in = IMPORTANT_MODELS diff --git a/utils/modular_model_converter.py b/utils/modular_model_converter.py index f39f6c046d15..d9a51917ff3a 100644 --- a/utils/modular_model_converter.py +++ b/utils/modular_model_converter.py @@ -1054,7 +1054,7 @@ def replace_class_node( new_decorators = modular_node.decorators if len(modular_node.decorators) > 0 else node.decorators # Keep return annotation in modular if any, else original return annotation - new_return_annotation = modular_node.returns if modular_node.returns else node.returns + new_return_annotation = modular_node.returns or node.returns # Update the method node node = node.with_changes(