From d13279d7544fc0ad819552e8b1af13bcd30c0761 Mon Sep 17 00:00:00 2001 From: Tom Isles Date: Mon, 25 Mar 2024 11:23:15 +1100 Subject: [PATCH 1/4] [feat] add cache to remaining import utils --- src/peft/import_utils.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/peft/import_utils.py b/src/peft/import_utils.py index 6c32d96d52..5169b5a01c 100644 --- a/src/peft/import_utils.py +++ b/src/peft/import_utils.py @@ -13,15 +13,15 @@ # limitations under the License. import importlib import importlib.metadata as importlib_metadata -from functools import lru_cache +from functools import cache, lru_cache import packaging.version - +@cache def is_bnb_available() -> bool: return importlib.util.find_spec("bitsandbytes") is not None - +@cache def is_bnb_4bit_available() -> bool: if not is_bnb_available(): return False @@ -30,7 +30,7 @@ def is_bnb_4bit_available() -> bool: return hasattr(bnb.nn, "Linear4bit") - +@cache def is_auto_gptq_available(): if importlib.util.find_spec("auto_gptq") is not None: AUTOGPTQ_MINIMUM_VERSION = packaging.version.parse("0.5.0") @@ -43,7 +43,7 @@ def is_auto_gptq_available(): f"but only versions above {AUTOGPTQ_MINIMUM_VERSION} are supported" ) - +@cache def is_optimum_available() -> bool: return importlib.util.find_spec("optimum") is not None @@ -64,10 +64,10 @@ def is_torch_tpu_available(check_device=True): return True return False - +@cache def is_aqlm_available(): return importlib.util.find_spec("aqlm") is not None - +@cache def is_auto_awq_available(): return importlib.util.find_spec("awq") is not None From 70fbf55b4910d5aa74a4f3c9749c991180dc8eaf Mon Sep 17 00:00:00 2001 From: Tom Isles Date: Mon, 25 Mar 2024 13:34:47 +1100 Subject: [PATCH 2/4] [fix] format + make quality fixes --- src/peft/import_utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/peft/import_utils.py b/src/peft/import_utils.py index 5169b5a01c..a6e59b972f 100644 --- a/src/peft/import_utils.py +++ b/src/peft/import_utils.py @@ -17,10 +17,12 @@ import packaging.version + @cache def is_bnb_available() -> bool: return importlib.util.find_spec("bitsandbytes") is not None + @cache def is_bnb_4bit_available() -> bool: if not is_bnb_available(): @@ -30,6 +32,7 @@ def is_bnb_4bit_available() -> bool: return hasattr(bnb.nn, "Linear4bit") + @cache def is_auto_gptq_available(): if importlib.util.find_spec("auto_gptq") is not None: @@ -43,6 +46,7 @@ def is_auto_gptq_available(): f"but only versions above {AUTOGPTQ_MINIMUM_VERSION} are supported" ) + @cache def is_optimum_available() -> bool: return importlib.util.find_spec("optimum") is not None @@ -64,10 +68,12 @@ def is_torch_tpu_available(check_device=True): return True return False + @cache def is_aqlm_available(): return importlib.util.find_spec("aqlm") is not None + @cache def is_auto_awq_available(): return importlib.util.find_spec("awq") is not None From b6f5d44c0cf246a4636996c8bde250ba5a589cfa Mon Sep 17 00:00:00 2001 From: Tom Isles Date: Mon, 25 Mar 2024 13:37:37 +1100 Subject: [PATCH 3/4] [fix] replace cache with lru_cache to respect python 3.8 --- src/peft/import_utils.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/peft/import_utils.py b/src/peft/import_utils.py index a6e59b972f..0b79668314 100644 --- a/src/peft/import_utils.py +++ b/src/peft/import_utils.py @@ -13,17 +13,17 @@ # limitations under the License. import importlib import importlib.metadata as importlib_metadata -from functools import cache, lru_cache +from functools import lru_cache import packaging.version -@cache +@lru_cache(maxsize=None) def is_bnb_available() -> bool: return importlib.util.find_spec("bitsandbytes") is not None -@cache +@lru_cache(maxsize=None) def is_bnb_4bit_available() -> bool: if not is_bnb_available(): return False @@ -33,7 +33,7 @@ def is_bnb_4bit_available() -> bool: return hasattr(bnb.nn, "Linear4bit") -@cache +@lru_cache(maxsize=None) def is_auto_gptq_available(): if importlib.util.find_spec("auto_gptq") is not None: AUTOGPTQ_MINIMUM_VERSION = packaging.version.parse("0.5.0") @@ -47,7 +47,7 @@ def is_auto_gptq_available(): ) -@cache +@lru_cache(maxsize=None) def is_optimum_available() -> bool: return importlib.util.find_spec("optimum") is not None @@ -69,11 +69,11 @@ def is_torch_tpu_available(check_device=True): return False -@cache +@lru_cache(maxsize=None) def is_aqlm_available(): return importlib.util.find_spec("aqlm") is not None -@cache +@lru_cache(maxsize=None) def is_auto_awq_available(): return importlib.util.find_spec("awq") is not None From b28020cd736c12947c222bafcd451db3abf3a6d2 Mon Sep 17 00:00:00 2001 From: Tom Isles Date: Tue, 26 Mar 2024 13:12:41 +1100 Subject: [PATCH 4/4] remove maxsize=None --- src/peft/import_utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/peft/import_utils.py b/src/peft/import_utils.py index 0b79668314..6799058e0c 100644 --- a/src/peft/import_utils.py +++ b/src/peft/import_utils.py @@ -18,12 +18,12 @@ import packaging.version -@lru_cache(maxsize=None) +@lru_cache def is_bnb_available() -> bool: return importlib.util.find_spec("bitsandbytes") is not None -@lru_cache(maxsize=None) +@lru_cache def is_bnb_4bit_available() -> bool: if not is_bnb_available(): return False @@ -33,7 +33,7 @@ def is_bnb_4bit_available() -> bool: return hasattr(bnb.nn, "Linear4bit") -@lru_cache(maxsize=None) +@lru_cache def is_auto_gptq_available(): if importlib.util.find_spec("auto_gptq") is not None: AUTOGPTQ_MINIMUM_VERSION = packaging.version.parse("0.5.0") @@ -47,7 +47,7 @@ def is_auto_gptq_available(): ) -@lru_cache(maxsize=None) +@lru_cache def is_optimum_available() -> bool: return importlib.util.find_spec("optimum") is not None @@ -69,11 +69,11 @@ def is_torch_tpu_available(check_device=True): return False -@lru_cache(maxsize=None) +@lru_cache def is_aqlm_available(): return importlib.util.find_spec("aqlm") is not None -@lru_cache(maxsize=None) +@lru_cache def is_auto_awq_available(): return importlib.util.find_spec("awq") is not None