From e82134894a383d1be0f20100be1d1f599cb7f85c Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 7 Feb 2024 13:52:35 +0200 Subject: [PATCH] MNT Move code quality fully to ruff (#1421) --- Makefile | 6 +-- ...ft_lora_clm_accelerate_ds_zero3_offload.py | 32 ++++++------ ...ora_seq2seq_accelerate_ds_zero3_offload.py | 32 ++++++------ .../peft_lora_embedding_semantic_search.py | 3 +- .../int8_training/fine_tune_blip2_int8.py | 1 - .../loftq_finetuning/quantize_save_load.py | 1 - .../loftq_finetuning/train_gsm8k_llama.py | 1 - examples/lora_dreambooth/train_dreambooth.py | 16 +++--- examples/oft_dreambooth/train_dreambooth.py | 16 +++--- .../convert_sd_adapter_to_peft.py | 4 +- examples/stable_diffusion/train_dreambooth.py | 16 +++--- pyproject.toml | 40 +++++++-------- scripts/launch_notebook_mp.py | 4 +- scripts/log_reports.py | 50 +++++++++++-------- scripts/stale.py | 4 +- setup.py | 13 +++-- src/peft/auto.py | 3 +- src/peft/config.py | 3 +- src/peft/import_utils.py | 3 +- src/peft/mapping.py | 9 ++-- src/peft/mixed_model.py | 1 - src/peft/peft_model.py | 13 +++-- src/peft/tuners/adalora/__init__.py | 1 - src/peft/tuners/adalora/bnb.py | 1 - src/peft/tuners/adalora/config.py | 1 - src/peft/tuners/adalora/gptq.py | 1 - src/peft/tuners/adalora/layer.py | 1 - src/peft/tuners/adalora/model.py | 3 +- src/peft/tuners/adaption_prompt/__init__.py | 1 - src/peft/tuners/adaption_prompt/config.py | 1 - src/peft/tuners/adaption_prompt/layer.py | 1 - src/peft/tuners/adaption_prompt/model.py | 1 - src/peft/tuners/adaption_prompt/utils.py | 1 - src/peft/tuners/ia3/__init__.py | 1 - src/peft/tuners/ia3/bnb.py | 1 - src/peft/tuners/ia3/config.py | 1 - src/peft/tuners/ia3/layer.py | 1 - src/peft/tuners/ia3/model.py | 7 ++- src/peft/tuners/loha/__init__.py | 1 - src/peft/tuners/loha/config.py | 1 - src/peft/tuners/loha/layer.py | 1 - src/peft/tuners/loha/model.py | 3 +- src/peft/tuners/lokr/__init__.py | 1 - src/peft/tuners/lokr/config.py | 1 - src/peft/tuners/lokr/layer.py | 1 - src/peft/tuners/lokr/model.py | 3 +- src/peft/tuners/lora/__init__.py | 1 - src/peft/tuners/lora/bnb.py | 1 - src/peft/tuners/lora/config.py | 11 ++-- src/peft/tuners/lora/gptq.py | 1 - src/peft/tuners/lora/layer.py | 1 - src/peft/tuners/lora/model.py | 9 ++-- src/peft/tuners/lora/tp_layer.py | 1 - src/peft/tuners/lycoris_utils.py | 13 +++-- src/peft/tuners/mixed/__init__.py | 1 - src/peft/tuners/mixed/model.py | 1 - .../multitask_prompt_tuning/__init__.py | 1 - .../tuners/multitask_prompt_tuning/config.py | 1 - .../tuners/multitask_prompt_tuning/model.py | 1 - src/peft/tuners/oft/__init__.py | 1 - src/peft/tuners/oft/config.py | 1 - src/peft/tuners/oft/layer.py | 5 +- src/peft/tuners/oft/model.py | 3 +- src/peft/tuners/p_tuning/__init__.py | 1 - src/peft/tuners/p_tuning/config.py | 1 - src/peft/tuners/p_tuning/model.py | 1 - src/peft/tuners/poly/__init__.py | 1 - src/peft/tuners/poly/config.py | 1 - src/peft/tuners/poly/layer.py | 1 - src/peft/tuners/poly/model.py | 1 - src/peft/tuners/poly/router.py | 1 - src/peft/tuners/prefix_tuning/__init__.py | 1 - src/peft/tuners/prefix_tuning/config.py | 1 - src/peft/tuners/prefix_tuning/model.py | 1 - src/peft/tuners/prompt_tuning/__init__.py | 1 - src/peft/tuners/prompt_tuning/config.py | 1 - src/peft/tuners/prompt_tuning/model.py | 1 - src/peft/tuners/tuners_utils.py | 9 ++-- src/peft/utils/constants.py | 1 - src/peft/utils/loftq_utils.py | 1 - src/peft/utils/other.py | 1 - src/peft/utils/save_and_load.py | 1 - tests/conftest.py | 1 - tests/regression/test_regression.py | 3 +- tests/test_adaption_prompt.py | 1 - tests/test_auto.py | 1 - tests/test_common_gpu.py | 3 +- tests/test_config.py | 1 - tests/test_custom_models.py | 4 +- tests/test_decoder_models.py | 2 +- tests/test_encoder_decoder_models.py | 2 +- tests/test_feature_extraction_models.py | 2 +- tests/test_gpu_examples.py | 2 +- tests/test_hub_features.py | 1 - tests/test_initialization.py | 1 - tests/test_mixed.py | 1 - tests/test_multitask_prompt_tuning.py | 1 - tests/test_stablediffusion.py | 2 +- tests/testing_common.py | 7 ++- tests/testing_utils.py | 1 - 100 files changed, 168 insertions(+), 255 deletions(-) diff --git a/Makefile b/Makefile index 86c5aaba28..fa22e5db99 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,19 @@ .PHONY: quality style test docs -check_dirs := src tests examples docs +check_dirs := . # Check that source code meets quality standards # this target runs checks on all files quality: - black --check $(check_dirs) ruff $(check_dirs) + ruff format --check $(check_dirs) doc-builder style src/peft tests docs/source --max_len 119 --check_only # Format source code automatically and check is there are any problems left that need manual fixing style: - black $(check_dirs) ruff $(check_dirs) --fix + ruff format $(check_dirs) doc-builder style src/peft tests docs/source --max_len 119 test: diff --git a/examples/causal_language_modeling/peft_lora_clm_accelerate_ds_zero3_offload.py b/examples/causal_language_modeling/peft_lora_clm_accelerate_ds_zero3_offload.py index 2b7fcf23b5..8b4a0af239 100644 --- a/examples/causal_language_modeling/peft_lora_clm_accelerate_ds_zero3_offload.py +++ b/examples/causal_language_modeling/peft_lora_clm_accelerate_ds_zero3_offload.py @@ -250,18 +250,16 @@ def test_preprocess_function(examples): lr_scheduler.step() optimizer.zero_grad() # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage - accelerator.print("GPU Memory before entering the train : {}".format(b2mb(tracemalloc.begin))) - accelerator.print("GPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.used)) - accelerator.print("GPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.peaked)) + accelerator.print(f"GPU Memory before entering the train : {b2mb(tracemalloc.begin)}") + accelerator.print(f"GPU Memory consumed at the end of the train (end-begin): {tracemalloc.used}") + accelerator.print(f"GPU Peak Memory consumed during the train (max-begin): {tracemalloc.peaked}") accelerator.print( - "GPU Total Peak Memory consumed during the train (max): {}".format( - tracemalloc.peaked + b2mb(tracemalloc.begin) - ) + f"GPU Total Peak Memory consumed during the train (max): {tracemalloc.peaked + b2mb(tracemalloc.begin)}" ) - accelerator.print("CPU Memory before entering the train : {}".format(b2mb(tracemalloc.cpu_begin))) - accelerator.print("CPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.cpu_used)) - accelerator.print("CPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.cpu_peaked)) + accelerator.print(f"CPU Memory before entering the train : {b2mb(tracemalloc.cpu_begin)}") + accelerator.print(f"CPU Memory consumed at the end of the train (end-begin): {tracemalloc.cpu_used}") + accelerator.print(f"CPU Peak Memory consumed during the train (max-begin): {tracemalloc.cpu_peaked}") accelerator.print( "CPU Total Peak Memory consumed during the train (max): {}".format( tracemalloc.cpu_peaked + b2mb(tracemalloc.cpu_begin) @@ -286,18 +284,16 @@ def test_preprocess_function(examples): eval_preds.extend(tokenizer.batch_decode(preds, skip_special_tokens=True)) # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage - accelerator.print("GPU Memory before entering the eval : {}".format(b2mb(tracemalloc.begin))) - accelerator.print("GPU Memory consumed at the end of the eval (end-begin): {}".format(tracemalloc.used)) - accelerator.print("GPU Peak Memory consumed during the eval (max-begin): {}".format(tracemalloc.peaked)) + accelerator.print(f"GPU Memory before entering the eval : {b2mb(tracemalloc.begin)}") + accelerator.print(f"GPU Memory consumed at the end of the eval (end-begin): {tracemalloc.used}") + accelerator.print(f"GPU Peak Memory consumed during the eval (max-begin): {tracemalloc.peaked}") accelerator.print( - "GPU Total Peak Memory consumed during the eval (max): {}".format( - tracemalloc.peaked + b2mb(tracemalloc.begin) - ) + f"GPU Total Peak Memory consumed during the eval (max): {tracemalloc.peaked + b2mb(tracemalloc.begin)}" ) - accelerator.print("CPU Memory before entering the eval : {}".format(b2mb(tracemalloc.cpu_begin))) - accelerator.print("CPU Memory consumed at the end of the eval (end-begin): {}".format(tracemalloc.cpu_used)) - accelerator.print("CPU Peak Memory consumed during the eval (max-begin): {}".format(tracemalloc.cpu_peaked)) + accelerator.print(f"CPU Memory before entering the eval : {b2mb(tracemalloc.cpu_begin)}") + accelerator.print(f"CPU Memory consumed at the end of the eval (end-begin): {tracemalloc.cpu_used}") + accelerator.print(f"CPU Peak Memory consumed during the eval (max-begin): {tracemalloc.cpu_peaked}") accelerator.print( "CPU Total Peak Memory consumed during the eval (max): {}".format( tracemalloc.cpu_peaked + b2mb(tracemalloc.cpu_begin) diff --git a/examples/conditional_generation/peft_lora_seq2seq_accelerate_ds_zero3_offload.py b/examples/conditional_generation/peft_lora_seq2seq_accelerate_ds_zero3_offload.py index dc202580b0..8b0e748bdf 100644 --- a/examples/conditional_generation/peft_lora_seq2seq_accelerate_ds_zero3_offload.py +++ b/examples/conditional_generation/peft_lora_seq2seq_accelerate_ds_zero3_offload.py @@ -201,18 +201,16 @@ def collate_fn(examples): lr_scheduler.step() optimizer.zero_grad() # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage - accelerator.print("GPU Memory before entering the train : {}".format(b2mb(tracemalloc.begin))) - accelerator.print("GPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.used)) - accelerator.print("GPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.peaked)) + accelerator.print(f"GPU Memory before entering the train : {b2mb(tracemalloc.begin)}") + accelerator.print(f"GPU Memory consumed at the end of the train (end-begin): {tracemalloc.used}") + accelerator.print(f"GPU Peak Memory consumed during the train (max-begin): {tracemalloc.peaked}") accelerator.print( - "GPU Total Peak Memory consumed during the train (max): {}".format( - tracemalloc.peaked + b2mb(tracemalloc.begin) - ) + f"GPU Total Peak Memory consumed during the train (max): {tracemalloc.peaked + b2mb(tracemalloc.begin)}" ) - accelerator.print("CPU Memory before entering the train : {}".format(b2mb(tracemalloc.cpu_begin))) - accelerator.print("CPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.cpu_used)) - accelerator.print("CPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.cpu_peaked)) + accelerator.print(f"CPU Memory before entering the train : {b2mb(tracemalloc.cpu_begin)}") + accelerator.print(f"CPU Memory consumed at the end of the train (end-begin): {tracemalloc.cpu_used}") + accelerator.print(f"CPU Peak Memory consumed during the train (max-begin): {tracemalloc.cpu_peaked}") accelerator.print( "CPU Total Peak Memory consumed during the train (max): {}".format( tracemalloc.cpu_peaked + b2mb(tracemalloc.cpu_begin) @@ -236,18 +234,16 @@ def collate_fn(examples): eval_preds.extend(tokenizer.batch_decode(preds, skip_special_tokens=True)) # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage - accelerator.print("GPU Memory before entering the eval : {}".format(b2mb(tracemalloc.begin))) - accelerator.print("GPU Memory consumed at the end of the eval (end-begin): {}".format(tracemalloc.used)) - accelerator.print("GPU Peak Memory consumed during the eval (max-begin): {}".format(tracemalloc.peaked)) + accelerator.print(f"GPU Memory before entering the eval : {b2mb(tracemalloc.begin)}") + accelerator.print(f"GPU Memory consumed at the end of the eval (end-begin): {tracemalloc.used}") + accelerator.print(f"GPU Peak Memory consumed during the eval (max-begin): {tracemalloc.peaked}") accelerator.print( - "GPU Total Peak Memory consumed during the eval (max): {}".format( - tracemalloc.peaked + b2mb(tracemalloc.begin) - ) + f"GPU Total Peak Memory consumed during the eval (max): {tracemalloc.peaked + b2mb(tracemalloc.begin)}" ) - accelerator.print("CPU Memory before entering the eval : {}".format(b2mb(tracemalloc.cpu_begin))) - accelerator.print("CPU Memory consumed at the end of the eval (end-begin): {}".format(tracemalloc.cpu_used)) - accelerator.print("CPU Peak Memory consumed during the eval (max-begin): {}".format(tracemalloc.cpu_peaked)) + accelerator.print(f"CPU Memory before entering the eval : {b2mb(tracemalloc.cpu_begin)}") + accelerator.print(f"CPU Memory consumed at the end of the eval (end-begin): {tracemalloc.cpu_used}") + accelerator.print(f"CPU Peak Memory consumed during the eval (max-begin): {tracemalloc.cpu_peaked}") accelerator.print( "CPU Total Peak Memory consumed during the eval (max): {}".format( tracemalloc.cpu_peaked + b2mb(tracemalloc.cpu_begin) diff --git a/examples/feature_extraction/peft_lora_embedding_semantic_search.py b/examples/feature_extraction/peft_lora_embedding_semantic_search.py index 4ce4bd5455..e0a462ac04 100644 --- a/examples/feature_extraction/peft_lora_embedding_semantic_search.py +++ b/examples/feature_extraction/peft_lora_embedding_semantic_search.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -170,7 +169,7 @@ def load_model_hook(models, input_dir): class AutoModelForSentenceEmbedding(nn.Module): def __init__(self, model_name, tokenizer, normalize=True): - super(AutoModelForSentenceEmbedding, self).__init__() + super().__init__() self.model = AutoModel.from_pretrained(model_name) # , load_in_8bit=True, device_map={"":0}) self.normalize = normalize diff --git a/examples/int8_training/fine_tune_blip2_int8.py b/examples/int8_training/fine_tune_blip2_int8.py index aa1cf4bc97..294137f1a6 100644 --- a/examples/int8_training/fine_tune_blip2_int8.py +++ b/examples/int8_training/fine_tune_blip2_int8.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/examples/loftq_finetuning/quantize_save_load.py b/examples/loftq_finetuning/quantize_save_load.py index 4a7fbf45c5..2110ed9086 100644 --- a/examples/loftq_finetuning/quantize_save_load.py +++ b/examples/loftq_finetuning/quantize_save_load.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/examples/loftq_finetuning/train_gsm8k_llama.py b/examples/loftq_finetuning/train_gsm8k_llama.py index 16da4bfb2a..8c462da4c8 100644 --- a/examples/loftq_finetuning/train_gsm8k_llama.py +++ b/examples/loftq_finetuning/train_gsm8k_llama.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/examples/lora_dreambooth/train_dreambooth.py b/examples/lora_dreambooth/train_dreambooth.py index 73d827a4c3..b9b5362d73 100644 --- a/examples/lora_dreambooth/train_dreambooth.py +++ b/examples/lora_dreambooth/train_dreambooth.py @@ -1048,22 +1048,18 @@ def main(args): # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage if not args.no_tracemalloc: - accelerator.print("GPU Memory before entering the train : {}".format(b2mb(tracemalloc.begin))) - accelerator.print("GPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.used)) - accelerator.print("GPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.peaked)) + accelerator.print(f"GPU Memory before entering the train : {b2mb(tracemalloc.begin)}") + accelerator.print(f"GPU Memory consumed at the end of the train (end-begin): {tracemalloc.used}") + accelerator.print(f"GPU Peak Memory consumed during the train (max-begin): {tracemalloc.peaked}") accelerator.print( "GPU Total Peak Memory consumed during the train (max): {}".format( tracemalloc.peaked + b2mb(tracemalloc.begin) ) ) - accelerator.print("CPU Memory before entering the train : {}".format(b2mb(tracemalloc.cpu_begin))) - accelerator.print( - "CPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.cpu_used) - ) - accelerator.print( - "CPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.cpu_peaked) - ) + accelerator.print(f"CPU Memory before entering the train : {b2mb(tracemalloc.cpu_begin)}") + accelerator.print(f"CPU Memory consumed at the end of the train (end-begin): {tracemalloc.cpu_used}") + accelerator.print(f"CPU Peak Memory consumed during the train (max-begin): {tracemalloc.cpu_peaked}") accelerator.print( "CPU Total Peak Memory consumed during the train (max): {}".format( tracemalloc.cpu_peaked + b2mb(tracemalloc.cpu_begin) diff --git a/examples/oft_dreambooth/train_dreambooth.py b/examples/oft_dreambooth/train_dreambooth.py index cacce70647..e0de59e4e7 100644 --- a/examples/oft_dreambooth/train_dreambooth.py +++ b/examples/oft_dreambooth/train_dreambooth.py @@ -1056,22 +1056,18 @@ def main(args): # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage if not args.no_tracemalloc: - accelerator.print("GPU Memory before entering the train : {}".format(b2mb(tracemalloc.begin))) - accelerator.print("GPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.used)) - accelerator.print("GPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.peaked)) + accelerator.print(f"GPU Memory before entering the train : {b2mb(tracemalloc.begin)}") + accelerator.print(f"GPU Memory consumed at the end of the train (end-begin): {tracemalloc.used}") + accelerator.print(f"GPU Peak Memory consumed during the train (max-begin): {tracemalloc.peaked}") accelerator.print( "GPU Total Peak Memory consumed during the train (max): {}".format( tracemalloc.peaked + b2mb(tracemalloc.begin) ) ) - accelerator.print("CPU Memory before entering the train : {}".format(b2mb(tracemalloc.cpu_begin))) - accelerator.print( - "CPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.cpu_used) - ) - accelerator.print( - "CPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.cpu_peaked) - ) + accelerator.print(f"CPU Memory before entering the train : {b2mb(tracemalloc.cpu_begin)}") + accelerator.print(f"CPU Memory consumed at the end of the train (end-begin): {tracemalloc.cpu_used}") + accelerator.print(f"CPU Peak Memory consumed during the train (max-begin): {tracemalloc.cpu_peaked}") accelerator.print( "CPU Total Peak Memory consumed during the train (max): {}".format( tracemalloc.cpu_peaked + b2mb(tracemalloc.cpu_begin) diff --git a/examples/stable_diffusion/convert_sd_adapter_to_peft.py b/examples/stable_diffusion/convert_sd_adapter_to_peft.py index 31e9e3f679..04da849eea 100644 --- a/examples/stable_diffusion/convert_sd_adapter_to_peft.py +++ b/examples/stable_diffusion/convert_sd_adapter_to_peft.py @@ -184,7 +184,7 @@ def construct_peft_lohaconfig(info: Dict[str, LoHaInfo], **kwargs) -> LoHaConfig alpha_pattern = dict(sorted(filter(lambda x: x[1] != alpha, alphas.items()), key=lambda x: x[0])) # Determine whether any of modules have effective conv2d decomposition - use_effective_conv2d = any(((val.hada_t1 is not None) or (val.hada_t2 is not None) for val in info.values())) + use_effective_conv2d = any((val.hada_t1 is not None) or (val.hada_t2 is not None) for val in info.values()) config = LoHaConfig( r=r, @@ -227,7 +227,7 @@ def construct_peft_lokrconfig(info: Dict[str, LoKrInfo], decompose_factor: int = alpha_pattern = dict(sorted(filter(lambda x: x[1] != alpha, alphas.items()), key=lambda x: x[0])) # Determine whether any of modules have effective conv2d decomposition - use_effective_conv2d = any(((val.lokr_t2 is not None) for val in info.values())) + use_effective_conv2d = any((val.lokr_t2 is not None) for val in info.values()) # decompose_both should be enabled if any w1 matrix in any layer is decomposed into 2 decompose_both = any((val.lokr_w1_a is not None and val.lokr_w1_b is not None) for val in info.values()) diff --git a/examples/stable_diffusion/train_dreambooth.py b/examples/stable_diffusion/train_dreambooth.py index 6fc3a30fc6..9e5b79154f 100644 --- a/examples/stable_diffusion/train_dreambooth.py +++ b/examples/stable_diffusion/train_dreambooth.py @@ -1214,18 +1214,16 @@ def main(args): if global_step >= args.max_train_steps: break # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage - accelerator.print("GPU Memory before entering the train : {}".format(b2mb(tracemalloc.begin))) - accelerator.print("GPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.used)) - accelerator.print("GPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.peaked)) + accelerator.print(f"GPU Memory before entering the train : {b2mb(tracemalloc.begin)}") + accelerator.print(f"GPU Memory consumed at the end of the train (end-begin): {tracemalloc.used}") + accelerator.print(f"GPU Peak Memory consumed during the train (max-begin): {tracemalloc.peaked}") accelerator.print( - "GPU Total Peak Memory consumed during the train (max): {}".format( - tracemalloc.peaked + b2mb(tracemalloc.begin) - ) + f"GPU Total Peak Memory consumed during the train (max): {tracemalloc.peaked + b2mb(tracemalloc.begin)}" ) - accelerator.print("CPU Memory before entering the train : {}".format(b2mb(tracemalloc.cpu_begin))) - accelerator.print("CPU Memory consumed at the end of the train (end-begin): {}".format(tracemalloc.cpu_used)) - accelerator.print("CPU Peak Memory consumed during the train (max-begin): {}".format(tracemalloc.cpu_peaked)) + accelerator.print(f"CPU Memory before entering the train : {b2mb(tracemalloc.cpu_begin)}") + accelerator.print(f"CPU Memory consumed at the end of the train (end-begin): {tracemalloc.cpu_used}") + accelerator.print(f"CPU Peak Memory consumed during the train (max-begin): {tracemalloc.cpu_peaked}") accelerator.print( "CPU Total Peak Memory consumed during the train (max): {}".format( tracemalloc.cpu_peaked + b2mb(tracemalloc.cpu_begin) diff --git a/pyproject.toml b/pyproject.toml index d9884e4b7a..4222054a89 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,33 +1,31 @@ [tool.black] +# Only used by `hf-doc-builder´. line-length = 119 -target-version = ['py36'] +target-version = ['py38'] [tool.ruff] -ignore = ["C901", "E501", "E741", "W605"] -select = ["C", "E", "F", "I", "W"] +target-version = "py38" line-length = 119 -[tool.ruff.isort] +[tool.ruff.lint] +extend-select = [ + "C", # Complexity + "E", # PEP8 errors + "F", # PEP8 formatting + "I", # Import sorting + "UP", # Pyupgrade upgrades + "W", # PEP8 warnings +] +ignore = [ + "C901", # Function too complex + "E501", # Line length (handled by ruff-format) + "UP007", # X | Y style Unions +] + +[tool.ruff.lint.isort] lines-after-imports = 2 known-first-party = ["peft"] -[isort] -default_section = "FIRSTPARTY" -known_first_party = "peft" -known_third_party = [ - "numpy", - "torch", - "accelerate", - "transformers", -] -line_length = 119 -lines_after_imports = 2 -multi_line_output = 3 -include_trailing_comma = true -force_grid_wrap = 0 -use_parentheses = true -ensure_newline_before_comments = true - [tool.pytest] doctest_optionflags = [ "NORMALIZE_WHITESPACE", diff --git a/scripts/launch_notebook_mp.py b/scripts/launch_notebook_mp.py index 56f6ddbd13..5421474588 100644 --- a/scripts/launch_notebook_mp.py +++ b/scripts/launch_notebook_mp.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,9 +19,10 @@ # This script exists to ensure that this issue does not reoccur. import torch -import peft from accelerate import notebook_launcher +import peft + def init(): class MyModule(torch.nn.Module): diff --git a/scripts/log_reports.py b/scripts/log_reports.py index 648af3af35..834f4ae6fc 100644 --- a/scripts/log_reports.py +++ b/scripts/log_reports.py @@ -1,7 +1,9 @@ -import json, os import argparse -from pathlib import Path +import json +import os from datetime import date +from pathlib import Path + from tabulate import tabulate @@ -10,7 +12,7 @@ parser = argparse.ArgumentParser() parser.add_argument( "--slack_channel_name", - default="peft-ci-daily" + default="peft-ci-daily", ) @@ -28,7 +30,7 @@ def main(slack_channel_name=None): for log in Path().glob("*.log"): section_num_failed = 0 i = 0 - with open(log, "r") as f: + with open(log) as f: for line in f: line = json.loads(line) i += 1 @@ -38,22 +40,27 @@ def main(slack_channel_name=None): duration = f'{line["duration"]:.4f}' if line.get("outcome", "") == "failed": section_num_failed += 1 - failed.append([test, duration, log.name.split('_')[0]]) + failed.append([test, duration, log.name.split("_")[0]]) total_num_failed += 1 else: - passed.append([test, duration, log.name.split('_')[0]]) + passed.append([test, duration, log.name.split("_")[0]]) empty_file = i == 0 group_info.append([str(log), section_num_failed, failed]) total_empty_files.append(empty_file) os.remove(log) failed = [] + text = ( + "🌞 There were no failures!" + if not any(total_empty_files) + else "Something went wrong there is at least one empty file - please check GH action results." + ) no_error_payload = { "type": "section", "text": { "type": "plain_text", - "text": "🌞 There were no failures!" if not any(total_empty_files) else "Something went wrong there is at least one empty file - please check GH action results.", - "emoji": True - } + "text": text, + "emoji": True, + }, } message = "" @@ -63,7 +70,7 @@ def main(slack_channel_name=None): "text": { "type": "plain_text", "text": "🤗 Results of the {} PEFT scheduled tests.".format(os.environ.get("TEST_TYPE", "")), - } + }, }, ] if total_num_failed > 0: @@ -76,12 +83,18 @@ def main(slack_channel_name=None): failed_table = [] for test in failed_tests: failed_table.append(test[0].split("::")) - failed_table = tabulate(failed_table, headers=["Test Location", "Test Case", "Test Name"], showindex="always", tablefmt="grid", maxcolwidths=[12, 12, 12]) - message += '\n```\n' +failed_table + '\n```' + failed_table = tabulate( + failed_table, + headers=["Test Location", "Test Case", "Test Name"], + showindex="always", + tablefmt="grid", + maxcolwidths=[12, 12, 12], + ) + message += "\n```\n" + failed_table + "\n```" if total_empty_files[i]: message += f"\n*{name}: Warning! Empty file - please check the GitHub action job *\n" - print(f'### {message}') + print(f"### {message}") else: payload.append(no_error_payload) @@ -95,18 +108,12 @@ def main(slack_channel_name=None): if len(message) != 0: md_report = { "type": "section", - "text": { - "type": "mrkdwn", - "text": message - }, + "text": {"type": "mrkdwn", "text": message}, } payload.append(md_report) action_button = { "type": "section", - "text": { - "type": "mrkdwn", - "text": "*For more details:*" - }, + "text": {"type": "mrkdwn", "text": "*For more details:*"}, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, @@ -131,6 +138,7 @@ def main(slack_channel_name=None): client = WebClient(token=os.environ.get("SLACK_API_TOKEN")) client.chat_postMessage(channel=f"#{slack_channel_name}", text=message, blocks=payload) + if __name__ == "__main__": args = parser.parse_args() main(args.slack_channel_name) diff --git a/scripts/stale.py b/scripts/stale.py index b057524717..7a1606cfbc 100644 --- a/scripts/stale.py +++ b/scripts/stale.py @@ -39,10 +39,10 @@ def main(): open_issues = repo.get_issues(state="open") for issue in open_issues: - comments = sorted([comment for comment in issue.get_comments()], key=lambda i: i.created_at, reverse=True) + comments = sorted(issue.get_comments(), key=lambda i: i.created_at, reverse=True) last_comment = comments[0] if len(comments) > 0 else None if ( - last_comment is not None and last_comment.user.login == "github-actions[bot]" + (last_comment is not None and last_comment.user.login == "github-actions[bot]") and (dt.now(timezone.utc) - issue.updated_at).days > 7 and (dt.now(timezone.utc) - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()) diff --git a/setup.py b/setup.py index b9c1f09038..8b5f4f0329 100644 --- a/setup.py +++ b/setup.py @@ -18,8 +18,15 @@ VERSION = "0.8.2" extras = {} -extras["quality"] = ["black ~= 22.0", "ruff>=0.0.241", "urllib3<=2.0.0"] -extras["docs_specific"] = ["hf-doc-builder"] +extras["quality"] = [ + "black", # doc-builder has an implicit dependency on Black, see huggingface/doc-builder#434 + "hf-doc-builder", + "ruff~=0.2.1", +] +extras["docs_specific"] = [ + "black", # doc-builder has an implicit dependency on Black, see huggingface/doc-builder#434 + "hf-doc-builder", +] extras["dev"] = extras["quality"] + extras["docs_specific"] extras["test"] = extras["dev"] + [ "pytest", @@ -36,7 +43,7 @@ version=VERSION, description="Parameter-Efficient Fine-Tuning (PEFT)", license_files=["LICENSE"], - long_description=open("README.md", "r", encoding="utf-8").read(), + long_description=open("README.md", encoding="utf-8").read(), long_description_content_type="text/markdown", keywords="deep learning", license="Apache", diff --git a/src/peft/auto.py b/src/peft/auto.py index 326441d626..15798349b3 100644 --- a/src/peft/auto.py +++ b/src/peft/auto.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -50,7 +49,7 @@ class _BaseAutoPeftModel: def __init__(self, *args, **kwargs): # For consistency with transformers: https://github.com/huggingface/transformers/blob/91d7df58b6537d385e90578dac40204cb550f706/src/transformers/models/auto/auto_factory.py#L400 - raise EnvironmentError( + raise EnvironmentError( # noqa: UP024 f"{self.__class__.__name__} is designed to be instantiated " f"using the `{self.__class__.__name__}.from_pretrained(pretrained_model_name_or_path)` or " f"`{self.__class__.__name__}.from_config(config)` methods." diff --git a/src/peft/config.py b/src/peft/config.py index c92897526d..99aff43ca4 100644 --- a/src/peft/config.py +++ b/src/peft/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -160,7 +159,7 @@ def from_json_file(cls, path_json_file: str, **kwargs): path_json_file (`str`): The path to the json file. """ - with open(path_json_file, "r") as file: + with open(path_json_file) as file: json_object = json.load(file) return json_object diff --git a/src/peft/import_utils.py b/src/peft/import_utils.py index f82d2238f1..677cf23ed6 100644 --- a/src/peft/import_utils.py +++ b/src/peft/import_utils.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -49,7 +48,7 @@ def is_optimum_available() -> bool: return importlib.util.find_spec("optimum") is not None -@lru_cache() +@lru_cache def is_torch_tpu_available(check_device=True): "Checks if `torch_xla` is installed and potentially if a TPU is in the environment" if importlib.util.find_spec("torch_xla") is not None: diff --git a/src/peft/mapping.py b/src/peft/mapping.py index 4b43c71615..a175e08bc7 100644 --- a/src/peft/mapping.py +++ b/src/peft/mapping.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,7 +14,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Dict +from typing import TYPE_CHECKING, Any import torch @@ -60,7 +59,7 @@ from transformers import PreTrainedModel -MODEL_TYPE_TO_PEFT_MODEL_MAPPING: Dict[str, PeftModel] = { +MODEL_TYPE_TO_PEFT_MODEL_MAPPING: dict[str, PeftModel] = { "SEQ_CLS": PeftModelForSequenceClassification, "SEQ_2_SEQ_LM": PeftModelForSeq2SeqLM, "CAUSAL_LM": PeftModelForCausalLM, @@ -69,7 +68,7 @@ "FEATURE_EXTRACTION": PeftModelForFeatureExtraction, } -PEFT_TYPE_TO_CONFIG_MAPPING: Dict[str, PeftConfig] = { +PEFT_TYPE_TO_CONFIG_MAPPING: dict[str, PeftConfig] = { "ADAPTION_PROMPT": AdaptionPromptConfig, "PROMPT_TUNING": PromptTuningConfig, "PREFIX_TUNING": PrefixTuningConfig, @@ -97,7 +96,7 @@ } -def get_peft_config(config_dict: Dict[str, Any]) -> PeftConfig: +def get_peft_config(config_dict: dict[str, Any]) -> PeftConfig: """ Returns a Peft config object from a dictionary. diff --git a/src/peft/mixed_model.py b/src/peft/mixed_model.py index 2e3aef0c92..73e2c2e227 100644 --- a/src/peft/mixed_model.py +++ b/src/peft/mixed_model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/peft_model.py b/src/peft/peft_model.py index fc89768813..96870e87ca 100644 --- a/src/peft/peft_model.py +++ b/src/peft/peft_model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,7 +20,7 @@ import warnings from contextlib import contextmanager from copy import deepcopy -from typing import Any, Dict, List, Optional, Union +from typing import Any, Optional, Union import packaging.version import torch @@ -139,7 +138,7 @@ def __init__(self, model: PreTrainedModel, peft_config: PeftConfig, adapter_name self.base_model.config.pretraining_tp = 1 @property - def peft_config(self) -> Dict[str, PeftConfig]: + def peft_config(self) -> dict[str, PeftConfig]: if self._is_prompt_learning: return self._peft_config return self.base_model.peft_config @@ -155,7 +154,7 @@ def active_adapters(self) -> list[str]: return adapters @peft_config.setter - def peft_config(self, value: Dict[str, PeftConfig]): + def peft_config(self, value: dict[str, PeftConfig]): if self._is_prompt_learning: self._peft_config = value else: @@ -165,7 +164,7 @@ def save_pretrained( self, save_directory: str, safe_serialization: bool = True, - selected_adapters: Optional[List[str]] = None, + selected_adapters: Optional[list[str]] = None, save_embedding_layers: Union[str, bool] = "auto", is_main_process: bool = True, **kwargs: Any, @@ -292,7 +291,7 @@ def from_pretrained( is_trainable: bool = False, config: Optional[PeftConfig] = None, **kwargs: Any, - ) -> "PeftModel": + ) -> PeftModel: r""" Instantiate a PEFT model from a pretrained model and loaded PEFT weights. @@ -639,7 +638,7 @@ def set_additional_trainable_modules(self, peft_config, adapter_name): _set_trainable(self, adapter_name) @classmethod - def _split_kwargs(cls, kwargs: Dict[str, Any]): + def _split_kwargs(cls, kwargs: dict[str, Any]): _kwargs_not_in_hf_hub_download_signature = ("use_auth_token",) hf_hub_download_kwargs = {} other_kwargs = {} diff --git a/src/peft/tuners/adalora/__init__.py b/src/peft/tuners/adalora/__init__.py index 431ac44066..4bdb8a540b 100644 --- a/src/peft/tuners/adalora/__init__.py +++ b/src/peft/tuners/adalora/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/adalora/bnb.py b/src/peft/tuners/adalora/bnb.py index 40b6e47852..b8c32a815c 100644 --- a/src/peft/tuners/adalora/bnb.py +++ b/src/peft/tuners/adalora/bnb.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/adalora/config.py b/src/peft/tuners/adalora/config.py index 55b4ee81a5..93905ff28b 100644 --- a/src/peft/tuners/adalora/config.py +++ b/src/peft/tuners/adalora/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/adalora/gptq.py b/src/peft/tuners/adalora/gptq.py index f04fd8d85e..910377c5db 100644 --- a/src/peft/tuners/adalora/gptq.py +++ b/src/peft/tuners/adalora/gptq.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/adalora/layer.py b/src/peft/tuners/adalora/layer.py index d0641f5be2..6cb644d9ce 100644 --- a/src/peft/tuners/adalora/layer.py +++ b/src/peft/tuners/adalora/layer.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/adalora/model.py b/src/peft/tuners/adalora/model.py index 27d848c316..bf334b39ce 100644 --- a/src/peft/tuners/adalora/model.py +++ b/src/peft/tuners/adalora/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -246,7 +245,7 @@ def forward(self, *args, **kwargs): for n, p in self.model.named_parameters(): if ("lora_A" in n or "lora_B" in n) and self.trainable_adapter_name in n: para_cov = p @ p.T if "lora_A" in n else p.T @ p - I = torch.eye(*para_cov.size(), out=torch.empty_like(para_cov)) + I = torch.eye(*para_cov.size(), out=torch.empty_like(para_cov)) # noqa: E741 I.requires_grad = False num_param += 1 regu_loss += torch.norm(para_cov - I, p="fro") diff --git a/src/peft/tuners/adaption_prompt/__init__.py b/src/peft/tuners/adaption_prompt/__init__.py index d57116b379..4ede9455f7 100644 --- a/src/peft/tuners/adaption_prompt/__init__.py +++ b/src/peft/tuners/adaption_prompt/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/adaption_prompt/config.py b/src/peft/tuners/adaption_prompt/config.py index 37d206248a..fdce845edf 100644 --- a/src/peft/tuners/adaption_prompt/config.py +++ b/src/peft/tuners/adaption_prompt/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/adaption_prompt/layer.py b/src/peft/tuners/adaption_prompt/layer.py index 0ca4701f58..cdd7895eaa 100644 --- a/src/peft/tuners/adaption_prompt/layer.py +++ b/src/peft/tuners/adaption_prompt/layer.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/adaption_prompt/model.py b/src/peft/tuners/adaption_prompt/model.py index e547a1ca37..08aea27f8e 100644 --- a/src/peft/tuners/adaption_prompt/model.py +++ b/src/peft/tuners/adaption_prompt/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/adaption_prompt/utils.py b/src/peft/tuners/adaption_prompt/utils.py index 0cbc95c1a1..aadde6680c 100644 --- a/src/peft/tuners/adaption_prompt/utils.py +++ b/src/peft/tuners/adaption_prompt/utils.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/ia3/__init__.py b/src/peft/tuners/ia3/__init__.py index 44100190b5..763d0133a2 100644 --- a/src/peft/tuners/ia3/__init__.py +++ b/src/peft/tuners/ia3/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/ia3/bnb.py b/src/peft/tuners/ia3/bnb.py index a081f0896d..628e3ce722 100644 --- a/src/peft/tuners/ia3/bnb.py +++ b/src/peft/tuners/ia3/bnb.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/ia3/config.py b/src/peft/tuners/ia3/config.py index a672e12613..322ea068d3 100644 --- a/src/peft/tuners/ia3/config.py +++ b/src/peft/tuners/ia3/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/ia3/layer.py b/src/peft/tuners/ia3/layer.py index 312173a6d6..9ea04e6873 100644 --- a/src/peft/tuners/ia3/layer.py +++ b/src/peft/tuners/ia3/layer.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/ia3/model.py b/src/peft/tuners/ia3/model.py index 0eae65f497..e00d318a89 100644 --- a/src/peft/tuners/ia3/model.py +++ b/src/peft/tuners/ia3/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,7 +17,7 @@ import warnings from dataclasses import asdict from enum import Enum -from typing import List, Optional +from typing import Optional import torch from torch import nn @@ -284,7 +283,7 @@ def _prepare_adapter_config(self, peft_config, model_config): return peft_config def _unload_and_optionally_merge( - self, merge: bool = True, safe_merge: bool = False, adapter_names: Optional[List[str]] = None + self, merge: bool = True, safe_merge: bool = False, adapter_names: Optional[list[str]] = None ): r""" This method merges the (IA)^3 layers into the base model. This is needed if someone wants to use the base model @@ -323,7 +322,7 @@ def _unload_and_optionally_merge( return self.model - def merge_and_unload(self, safe_merge: bool = False, adapter_names: Optional[List[str]] = None) -> torch.nn.Module: + def merge_and_unload(self, safe_merge: bool = False, adapter_names: Optional[list[str]] = None) -> torch.nn.Module: r""" This method merges the IA³ layers into the base model. This is needed if someone wants to use the base model as a standalone model. diff --git a/src/peft/tuners/loha/__init__.py b/src/peft/tuners/loha/__init__.py index ece669e705..2f39deee17 100644 --- a/src/peft/tuners/loha/__init__.py +++ b/src/peft/tuners/loha/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/loha/config.py b/src/peft/tuners/loha/config.py index da9872fef3..c38ba7828b 100644 --- a/src/peft/tuners/loha/config.py +++ b/src/peft/tuners/loha/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/loha/layer.py b/src/peft/tuners/loha/layer.py index 03422ad717..b958decfad 100644 --- a/src/peft/tuners/loha/layer.py +++ b/src/peft/tuners/loha/layer.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/loha/model.py b/src/peft/tuners/loha/model.py index 37d3bb4d8c..6f1aaac9d5 100644 --- a/src/peft/tuners/loha/model.py +++ b/src/peft/tuners/loha/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -102,7 +101,7 @@ def _create_and_replace( # Regexp matching - Find key which matches current target_name in patterns provided pattern_keys = list(chain(config.rank_pattern.keys(), config.alpha_pattern.keys())) - target_name_key = next(filter(lambda key: re.match(f"(.*\.)?{key}$", current_key), pattern_keys), target_name) + target_name_key = next(filter(lambda key: re.match(rf"(.*\.)?{key}$", current_key), pattern_keys), target_name) kwargs = config.to_dict() kwargs["r"] = config.rank_pattern.get(target_name_key, config.r) diff --git a/src/peft/tuners/lokr/__init__.py b/src/peft/tuners/lokr/__init__.py index b137f22c96..408cf2a54a 100644 --- a/src/peft/tuners/lokr/__init__.py +++ b/src/peft/tuners/lokr/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/lokr/config.py b/src/peft/tuners/lokr/config.py index 1df0715aa6..c8d60a7463 100644 --- a/src/peft/tuners/lokr/config.py +++ b/src/peft/tuners/lokr/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/lokr/layer.py b/src/peft/tuners/lokr/layer.py index 6c059b875b..28e4e5ca61 100644 --- a/src/peft/tuners/lokr/layer.py +++ b/src/peft/tuners/lokr/layer.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/lokr/model.py b/src/peft/tuners/lokr/model.py index 48a564e706..eecad8dd13 100644 --- a/src/peft/tuners/lokr/model.py +++ b/src/peft/tuners/lokr/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -103,7 +102,7 @@ def _create_and_replace( # Regexp matching - Find key which matches current target_name in patterns provided pattern_keys = list(chain(config.rank_pattern.keys(), config.alpha_pattern.keys())) - target_name_key = next(filter(lambda key: re.match(f"(.*\.)?{key}$", current_key), pattern_keys), target_name) + target_name_key = next(filter(lambda key: re.match(rf"(.*\.)?{key}$", current_key), pattern_keys), target_name) kwargs = config.to_dict() kwargs["r"] = config.rank_pattern.get(target_name_key, config.r) diff --git a/src/peft/tuners/lora/__init__.py b/src/peft/tuners/lora/__init__.py index 1eb90b3ccf..3115fff724 100644 --- a/src/peft/tuners/lora/__init__.py +++ b/src/peft/tuners/lora/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/lora/bnb.py b/src/peft/tuners/lora/bnb.py index 5b2a51fea4..06a6f431cd 100644 --- a/src/peft/tuners/lora/bnb.py +++ b/src/peft/tuners/lora/bnb.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/lora/config.py b/src/peft/tuners/lora/config.py index 51cb3bf2fc..0df8b5b623 100644 --- a/src/peft/tuners/lora/config.py +++ b/src/peft/tuners/lora/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,7 +15,7 @@ from __future__ import annotations from dataclasses import dataclass, field -from typing import List, Literal, Optional, Union +from typing import Literal, Optional, Union from peft.config import PeftConfig from peft.utils import PeftType @@ -105,7 +104,7 @@ class LoraConfig(PeftConfig): """ r: int = field(default=8, metadata={"help": "Lora attention dimension"}) - target_modules: Optional[Union[List[str], str]] = field( + target_modules: Optional[Union[list[str], str]] = field( default=None, metadata={ "help": ( @@ -137,7 +136,7 @@ class LoraConfig(PeftConfig): ) }, ) - modules_to_save: Optional[List[str]] = field( + modules_to_save: Optional[list[str]] = field( default=None, metadata={ "help": "List of modules apart from LoRA layers to be set as trainable and saved in the final checkpoint. " @@ -157,14 +156,14 @@ class LoraConfig(PeftConfig): ), }, ) - layers_to_transform: Optional[Union[List[int], int]] = field( + layers_to_transform: Optional[Union[list[int], int]] = field( default=None, metadata={ "help": "The layer indexes to transform, is this argument is specified, PEFT will transform only the layers indexes that are specified inside this list. If a single integer is passed, PEFT will transform only the layer at this index. " "This only works when target_modules is a list of str." }, ) - layers_pattern: Optional[Union[List[str], str]] = field( + layers_pattern: Optional[Union[list[str], str]] = field( default=None, metadata={ "help": "The layer pattern name, used only if `layers_to_transform` is different to None and if the layer pattern is not in the common layers pattern." diff --git a/src/peft/tuners/lora/gptq.py b/src/peft/tuners/lora/gptq.py index a80bea5f05..c7d7ceefc5 100644 --- a/src/peft/tuners/lora/gptq.py +++ b/src/peft/tuners/lora/gptq.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/lora/layer.py b/src/peft/tuners/lora/layer.py index 2593e1edcc..0dfe97966c 100644 --- a/src/peft/tuners/lora/layer.py +++ b/src/peft/tuners/lora/layer.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/lora/model.py b/src/peft/tuners/lora/model.py index 40a4782c90..111a65f2fa 100644 --- a/src/peft/tuners/lora/model.py +++ b/src/peft/tuners/lora/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,7 +21,7 @@ from enum import Enum from functools import reduce from itertools import chain -from typing import List, Optional +from typing import Optional import torch from torch import nn @@ -141,7 +140,7 @@ def _create_and_replace( # Regexp matching - Find key which matches current target_name in patterns provided pattern_keys = list(chain(lora_config.rank_pattern.keys(), lora_config.alpha_pattern.keys())) - target_name_key = next(filter(lambda key: re.match(f".*\.{key}$", current_key), pattern_keys), current_key) + target_name_key = next(filter(lambda key: re.match(rf".*\.{key}$", current_key), pattern_keys), current_key) r = lora_config.rank_pattern.get(target_name_key, lora_config.r) alpha = lora_config.alpha_pattern.get(target_name_key, lora_config.lora_alpha) @@ -333,7 +332,7 @@ def _unload_and_optionally_merge( merge=True, progressbar: bool = False, safe_merge: bool = False, - adapter_names: Optional[List[str]] = None, + adapter_names: Optional[list[str]] = None, ): if merge: if getattr(self.model, "quantization_method", None) == "gptq": @@ -590,7 +589,7 @@ def delete_adapter(self, adapter_name: str) -> None: self.active_adapter = new_adapter or [] def merge_and_unload( - self, progressbar: bool = False, safe_merge: bool = False, adapter_names: Optional[List[str]] = None + self, progressbar: bool = False, safe_merge: bool = False, adapter_names: Optional[list[str]] = None ) -> torch.nn.Module: r""" This method merges the LoRa layers into the base model. This is needed if someone wants to use the base model diff --git a/src/peft/tuners/lora/tp_layer.py b/src/peft/tuners/lora/tp_layer.py index b3f0809ed2..f3e8215234 100644 --- a/src/peft/tuners/lora/tp_layer.py +++ b/src/peft/tuners/lora/tp_layer.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/lycoris_utils.py b/src/peft/tuners/lycoris_utils.py index 667fc9340a..931df92bed 100644 --- a/src/peft/tuners/lycoris_utils.py +++ b/src/peft/tuners/lycoris_utils.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,7 +16,7 @@ import warnings from abc import abstractmethod from dataclasses import dataclass, field -from typing import Any, Dict, List, Optional, Set, Type, Union +from typing import Any, Optional, Union import torch import torch.nn as nn @@ -80,7 +79,7 @@ def __init__(self, base_layer: nn.Module) -> None: @property @abstractmethod - def _available_adapters(self) -> Set[str]: + def _available_adapters(self) -> set[str]: ... def _init_empty_weights(self, cls, *args, **kwargs) -> None: @@ -108,7 +107,7 @@ def _get_delta_activations(self, adapter_name: str, x: torch.Tensor, *args: Any, def get_delta_weight(self, adapter_name: str) -> torch.Tensor: ... - def merge(self, safe_merge: bool = False, adapter_names: Optional[List[str]] = None) -> None: + def merge(self, safe_merge: bool = False, adapter_names: Optional[list[str]] = None) -> None: """ Merge the active adapter weights into the base weights @@ -197,7 +196,7 @@ class LycorisTuner(BaseTuner): """ prefix: str - layers_mapping: Dict[Type[torch.nn.Module], Type[LycorisLayer]] + layers_mapping: dict[type[torch.nn.Module], type[LycorisLayer]] def __init__(self, model, config, adapter_name): super().__init__(model, config, adapter_name) @@ -311,7 +310,7 @@ def _unload_and_optionally_merge( merge: bool = True, progressbar: bool = False, safe_merge: bool = False, - adapter_names: Optional[List[str]] = None, + adapter_names: Optional[list[str]] = None, ): if merge: if getattr(self.model, "quantization_method", None) == "gptq": @@ -351,7 +350,7 @@ def disable_adapter_layers(self) -> None: self._set_adapter_layers(enabled=False) def merge_and_unload( - self, progressbar: bool = False, safe_merge: bool = False, adapter_names: Optional[List[str]] = None + self, progressbar: bool = False, safe_merge: bool = False, adapter_names: Optional[list[str]] = None ) -> torch.nn.Module: r""" This method merges the adapter layers into the base model. This is needed if someone wants to use the base diff --git a/src/peft/tuners/mixed/__init__.py b/src/peft/tuners/mixed/__init__.py index f21cff3b29..2955d7258d 100644 --- a/src/peft/tuners/mixed/__init__.py +++ b/src/peft/tuners/mixed/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/mixed/model.py b/src/peft/tuners/mixed/model.py index ee1b785b78..2743230d0d 100644 --- a/src/peft/tuners/mixed/model.py +++ b/src/peft/tuners/mixed/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/multitask_prompt_tuning/__init__.py b/src/peft/tuners/multitask_prompt_tuning/__init__.py index 10c96ce0c4..214f772248 100644 --- a/src/peft/tuners/multitask_prompt_tuning/__init__.py +++ b/src/peft/tuners/multitask_prompt_tuning/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/multitask_prompt_tuning/config.py b/src/peft/tuners/multitask_prompt_tuning/config.py index f1c797fa62..67a3c323a2 100644 --- a/src/peft/tuners/multitask_prompt_tuning/config.py +++ b/src/peft/tuners/multitask_prompt_tuning/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/multitask_prompt_tuning/model.py b/src/peft/tuners/multitask_prompt_tuning/model.py index 72c7feda0e..a87f0ebf44 100644 --- a/src/peft/tuners/multitask_prompt_tuning/model.py +++ b/src/peft/tuners/multitask_prompt_tuning/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/oft/__init__.py b/src/peft/tuners/oft/__init__.py index 456c46ee07..52ac7131e2 100644 --- a/src/peft/tuners/oft/__init__.py +++ b/src/peft/tuners/oft/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/oft/config.py b/src/peft/tuners/oft/config.py index 1d84de57ce..ba3b9a4401 100644 --- a/src/peft/tuners/oft/config.py +++ b/src/peft/tuners/oft/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/oft/layer.py b/src/peft/tuners/oft/layer.py index b192447574..f4427304b5 100644 --- a/src/peft/tuners/oft/layer.py +++ b/src/peft/tuners/oft/layer.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -244,7 +243,7 @@ def _cayley_batch(self, data: torch.Tensor) -> torch.Tensor: b, r, c = data.shape # Ensure the input matrix is skew-symmetric skew = 0.5 * (data - data.transpose(1, 2)) - I = torch.eye(r, device=data.device).unsqueeze(0).expand(b, r, c) + I = torch.eye(r, device=data.device).unsqueeze(0).expand(b, r, c) # noqa: E741 # Perform the Cayley parametrization Q = torch.bmm(I - skew, torch.inverse(I + skew)) @@ -268,7 +267,7 @@ def _block_diagonal(self, oft_r: torch.Tensor, rank: int) -> torch.Tensor: def _project_batch(self, oft_r, eps=1e-5): # scaling factor for each of the smaller block matrix eps = eps * 1 / torch.sqrt(torch.tensor(oft_r.shape[0])) - I = ( + I = ( # noqa: E741 torch.zeros((oft_r.size(1), oft_r.size(1)), device=oft_r.device, dtype=oft_r.dtype) .unsqueeze(0) .expand_as(oft_r) diff --git a/src/peft/tuners/oft/model.py b/src/peft/tuners/oft/model.py index fda212599b..fd96325c6f 100644 --- a/src/peft/tuners/oft/model.py +++ b/src/peft/tuners/oft/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -95,7 +94,7 @@ def _create_and_replace( # Regexp matching - Find key which matches current target_name in patterns provided pattern_keys = list(config.rank_pattern.keys()) - target_name_key = next(filter(lambda key: re.match(f"(.*\.)?{key}$", current_key), pattern_keys), target_name) + target_name_key = next(filter(lambda key: re.match(rf"(.*\.)?{key}$", current_key), pattern_keys), target_name) kwargs = config.to_dict() kwargs["r"] = config.rank_pattern.get(target_name_key, config.r) diff --git a/src/peft/tuners/p_tuning/__init__.py b/src/peft/tuners/p_tuning/__init__.py index 5724d98729..7dd3a6ba3e 100644 --- a/src/peft/tuners/p_tuning/__init__.py +++ b/src/peft/tuners/p_tuning/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/p_tuning/config.py b/src/peft/tuners/p_tuning/config.py index 0ae87b32cf..75deffb429 100644 --- a/src/peft/tuners/p_tuning/config.py +++ b/src/peft/tuners/p_tuning/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/p_tuning/model.py b/src/peft/tuners/p_tuning/model.py index 04b6e2814a..ade2b11281 100644 --- a/src/peft/tuners/p_tuning/model.py +++ b/src/peft/tuners/p_tuning/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/poly/__init__.py b/src/peft/tuners/poly/__init__.py index b7d39cea0b..b0f368695e 100644 --- a/src/peft/tuners/poly/__init__.py +++ b/src/peft/tuners/poly/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/poly/config.py b/src/peft/tuners/poly/config.py index 5ce41f33c2..3abbc93b02 100644 --- a/src/peft/tuners/poly/config.py +++ b/src/peft/tuners/poly/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/poly/layer.py b/src/peft/tuners/poly/layer.py index 0b35be98c5..debb40beee 100644 --- a/src/peft/tuners/poly/layer.py +++ b/src/peft/tuners/poly/layer.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/poly/model.py b/src/peft/tuners/poly/model.py index 25b09c7db9..943a287955 100644 --- a/src/peft/tuners/poly/model.py +++ b/src/peft/tuners/poly/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/poly/router.py b/src/peft/tuners/poly/router.py index 28777db242..0249398a9f 100644 --- a/src/peft/tuners/poly/router.py +++ b/src/peft/tuners/poly/router.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/prefix_tuning/__init__.py b/src/peft/tuners/prefix_tuning/__init__.py index bf63488fbe..28f4bedbb4 100644 --- a/src/peft/tuners/prefix_tuning/__init__.py +++ b/src/peft/tuners/prefix_tuning/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/prefix_tuning/config.py b/src/peft/tuners/prefix_tuning/config.py index 30c8b51d43..39203ff7be 100644 --- a/src/peft/tuners/prefix_tuning/config.py +++ b/src/peft/tuners/prefix_tuning/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/prefix_tuning/model.py b/src/peft/tuners/prefix_tuning/model.py index 9884eecc5f..ffd51892a3 100644 --- a/src/peft/tuners/prefix_tuning/model.py +++ b/src/peft/tuners/prefix_tuning/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/prompt_tuning/__init__.py b/src/peft/tuners/prompt_tuning/__init__.py index 0bee05ecaa..71795b61d8 100644 --- a/src/peft/tuners/prompt_tuning/__init__.py +++ b/src/peft/tuners/prompt_tuning/__init__.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/prompt_tuning/config.py b/src/peft/tuners/prompt_tuning/config.py index 9f15b1a881..4b94538a9b 100644 --- a/src/peft/tuners/prompt_tuning/config.py +++ b/src/peft/tuners/prompt_tuning/config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/prompt_tuning/model.py b/src/peft/tuners/prompt_tuning/model.py index e7c7888599..a04221c2ab 100644 --- a/src/peft/tuners/prompt_tuning/model.py +++ b/src/peft/tuners/prompt_tuning/model.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/tuners/tuners_utils.py b/src/peft/tuners/tuners_utils.py index dacc0ae832..d71c676f9d 100644 --- a/src/peft/tuners/tuners_utils.py +++ b/src/peft/tuners/tuners_utils.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,7 +18,7 @@ import warnings from abc import ABC, abstractmethod from contextlib import contextmanager -from typing import Any, List, Optional, Union +from typing import Any, Optional, Union import torch from accelerate.hooks import AlignDevicesHook @@ -358,7 +357,7 @@ def unmerge_adapter(self): with onload_layer(module): module.unmerge() - def _unloading_checks(self, adapter_names: Optional[List[str]]): + def _unloading_checks(self, adapter_names: Optional[list[str]]): adapters_to_consider = adapter_names or self.active_adapters is_modules_to_save_available = any( self.peft_config[adapter].modules_to_save for adapter in adapters_to_consider @@ -572,7 +571,7 @@ def check_target_module_exists(config, key: str) -> bool | re.Match[str] | None: else: layers_pattern = [layers_pattern] if isinstance(layers_pattern, str) else layers_pattern for pattern in layers_pattern: - layer_index = re.match(r".*\.{layer}\.(\d+)\.".format(layer=pattern), key) + layer_index = re.match(rf".*\.{pattern}\.(\d+)\.", key) if layer_index is not None: break @@ -639,7 +638,7 @@ def _maybe_include_all_linear_layers(peft_config: PeftConfig, model: nn.Module) return peft_config -def check_adapters_to_merge(module: BaseTunerLayer, adapter_names: Optional[List[str]] = None) -> list[str]: +def check_adapters_to_merge(module: BaseTunerLayer, adapter_names: Optional[list[str]] = None) -> list[str]: """ Helper function to check which adapters should be merged. diff --git a/src/peft/utils/constants.py b/src/peft/utils/constants.py index 08b372f617..63d81646e5 100644 --- a/src/peft/utils/constants.py +++ b/src/peft/utils/constants.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/utils/loftq_utils.py b/src/peft/utils/loftq_utils.py index bfbdabf5a0..5b948a4abe 100644 --- a/src/peft/utils/loftq_utils.py +++ b/src/peft/utils/loftq_utils.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/utils/other.py b/src/peft/utils/other.py index e4023de7b7..00b36e4486 100644 --- a/src/peft/utils/other.py +++ b/src/peft/utils/other.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/peft/utils/save_and_load.py b/src/peft/utils/save_and_load.py index 1777a33e0d..509fcd4e3a 100644 --- a/src/peft/utils/save_and_load.py +++ b/src/peft/utils/save_and_load.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/conftest.py b/tests/conftest.py index 9641f535f8..5c2d14cbeb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/regression/test_regression.py b/tests/regression/test_regression.py index b3451cecb8..7c658b9b79 100644 --- a/tests/regression/test_regression.py +++ b/tests/regression/test_regression.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -112,7 +111,7 @@ def strtobool(val): elif val in ("n", "no", "f", "false", "off", "0"): return 0 else: - raise ValueError("invalid truth value {!r}".format(val)) + raise ValueError(f"invalid truth value {val!r}") # same as in ..testing_utils.py but cannot be imported diff --git a/tests/test_adaption_prompt.py b/tests/test_adaption_prompt.py index fb4f422f4b..0bc9a9d180 100644 --- a/tests/test_adaption_prompt.py +++ b/tests/test_adaption_prompt.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_auto.py b/tests/test_auto.py index 9dc24ec2e8..4df857bc26 100644 --- a/tests/test_auto.py +++ b/tests/test_auto.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_common_gpu.py b/tests/test_common_gpu.py index df76624f5a..8b2930e575 100644 --- a/tests/test_common_gpu.py +++ b/tests/test_common_gpu.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -599,7 +598,7 @@ def test_modules_to_save_grad(self): original_module = lm_head.original_module modules_to_save = lm_head.modules_to_save.default - inputs = torch.randn((1024)) + inputs = torch.randn(1024) o1 = lm_head(inputs) o1.mean().backward() diff --git a/tests/test_config.py b/tests/test_config.py index 77e28fd73e..ce0b68643b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_custom_models.py b/tests/test_custom_models.py index 8b80f661b3..a709b72165 100644 --- a/tests/test_custom_models.py +++ b/tests/test_custom_models.py @@ -788,7 +788,7 @@ def test_existing_model_card(self): f.write(text) model.save_pretrained(tmp_dirname) - with open(os.path.join(tmp_dirname, "README.md"), "r") as f: + with open(os.path.join(tmp_dirname, "README.md")) as f: model_card = f.read() self.assertIn("library_name: peft", model_card) @@ -803,7 +803,7 @@ def test_non_existing_model_card(self): with tempfile.TemporaryDirectory() as tmp_dirname: model.save_pretrained(tmp_dirname) - with open(os.path.join(tmp_dirname, "README.md"), "r") as f: + with open(os.path.join(tmp_dirname, "README.md")) as f: model_card = f.read() self.assertIn("library_name: peft", model_card) diff --git a/tests/test_decoder_models.py b/tests/test_decoder_models.py index 2427dbecf6..a9e85bcfd8 100644 --- a/tests/test_decoder_models.py +++ b/tests/test_decoder_models.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -52,6 +51,7 @@ class PeftDecoderModelTester(unittest.TestCase, PeftCommonTester): We use parametrized.expand for debugging purposes to test each model individually. """ + transformers_class = AutoModelForCausalLM def prepare_inputs_for_testing(self): diff --git a/tests/test_encoder_decoder_models.py b/tests/test_encoder_decoder_models.py index c1e32607a4..6c2b687530 100644 --- a/tests/test_encoder_decoder_models.py +++ b/tests/test_encoder_decoder_models.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -39,6 +38,7 @@ class PeftEncoderDecoderModelTester(unittest.TestCase, PeftCommonTester): We use parametrized.expand for debugging purposes to test each model individually. """ + transformers_class = AutoModelForSeq2SeqLM def prepare_inputs_for_testing(self): diff --git a/tests/test_feature_extraction_models.py b/tests/test_feature_extraction_models.py index c1aed54899..30a16b260f 100644 --- a/tests/test_feature_extraction_models.py +++ b/tests/test_feature_extraction_models.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -64,6 +63,7 @@ class PeftFeatureExtractionModelTester(unittest.TestCase, PeftCommonTester): We use parametrized.expand for debugging purposes to test each model individually. """ + transformers_class = AutoModel def prepare_inputs_for_testing(self): diff --git a/tests/test_gpu_examples.py b/tests/test_gpu_examples.py index a23d4cbb56..a73a1c6461 100644 --- a/tests/test_gpu_examples.py +++ b/tests/test_gpu_examples.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -72,6 +71,7 @@ class DataCollatorSpeechSeq2SeqWithPadding: Directly copied from: https://github.com/huggingface/peft/blob/main/examples/int8_training/peft_bnb_whisper_large_v2_training.ipynb """ + processor: Any def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]: diff --git a/tests/test_hub_features.py b/tests/test_hub_features.py index 44e9d6b05b..2522a35d97 100644 --- a/tests/test_hub_features.py +++ b/tests/test_hub_features.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_initialization.py b/tests/test_initialization.py index 74207da123..cf31cab363 100644 --- a/tests/test_initialization.py +++ b/tests/test_initialization.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_mixed.py b/tests/test_mixed.py index ea35df391f..f2a3dda2c8 100644 --- a/tests/test_mixed.py +++ b/tests/test_mixed.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_multitask_prompt_tuning.py b/tests/test_multitask_prompt_tuning.py index b9e229073a..001a36dc40 100644 --- a/tests/test_multitask_prompt_tuning.py +++ b/tests/test_multitask_prompt_tuning.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/tests/test_stablediffusion.py b/tests/test_stablediffusion.py index 6b357cc3cd..909c7bf9e5 100644 --- a/tests/test_stablediffusion.py +++ b/tests/test_stablediffusion.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -89,6 +88,7 @@ class StableDiffusionModelTester(TestCase, PeftCommonTester): Tests that diffusers StableDiffusion model works with PEFT as expected. """ + transformers_class = StableDiffusionPipeline def instantiate_sd_peft(self, model_id, config_cls, config_kwargs): diff --git a/tests/testing_common.py b/tests/testing_common.py index 717501fadb..dbc5bed26e 100644 --- a/tests/testing_common.py +++ b/tests/testing_common.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -136,7 +135,7 @@ def get_grid_parameters(self, grid_parameters, filter_params_func=None): for model_id in model_list: for key, value in self.items(): - if "{}_kwargs".format(key) in grid_parameters: + if f"{key}_kwargs" in grid_parameters: peft_configs = [] current_peft_config = value[1].copy() for current_key, current_value in grid_parameters[f"{key}_kwargs"].items(): @@ -186,7 +185,7 @@ def check_modelcard(self, tmp_dirname, model): # check the generated README.md filename = os.path.join(tmp_dirname, "README.md") self.assertTrue(os.path.exists(filename)) - with open(filename, "r", encoding="utf-8") as f: + with open(filename, encoding="utf-8") as f: readme = f.read() metainfo = re.search(r"---\n(.*?)\n---", readme, re.DOTALL).group(1) dct = yaml.safe_load(metainfo) @@ -201,7 +200,7 @@ def check_config_json(self, tmp_dirname, model): # check the generated config.json filename = os.path.join(tmp_dirname, "adapter_config.json") self.assertTrue(os.path.exists(filename)) - with open(filename, "r", encoding="utf-8") as f: + with open(filename, encoding="utf-8") as f: config = json.load(f) if hasattr(model, "config"): # custom models don't have a config attribute diff --git a/tests/testing_utils.py b/tests/testing_utils.py index cb5b55e877..bd7047e31b 100644 --- a/tests/testing_utils.py +++ b/tests/testing_utils.py @@ -1,4 +1,3 @@ -# coding=utf-8 # Copyright 2023-present the HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License");