diff --git a/src/peft/__init__.py b/src/peft/__init__.py index e141347..1314009 100644 --- a/src/peft/__init__.py +++ b/src/peft/__init__.py @@ -51,3 +51,4 @@ from .utils import ( set_peft_model_state_dict, shift_tokens_right, ) +from .import_utils import is_bnb_available diff --git a/src/peft/import_utils.py b/src/peft/import_utils.py new file mode 100644 index 0000000..71db603 --- /dev/null +++ b/src/peft/import_utils.py @@ -0,0 +1,19 @@ +# coding=utf-8 +# Copyright 2023-present the HuggingFace Inc. team. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import importlib + + +def is_bnb_available(): + return importlib.util.find_spec("bitsandbytes") is not None diff --git a/src/peft/tuners/lora.py b/src/peft/tuners/lora.py index 51cd56f..f18f961 100644 --- a/src/peft/tuners/lora.py +++ b/src/peft/tuners/lora.py @@ -12,7 +12,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import importlib import math import re import warnings @@ -25,11 +24,7 @@ import torch.nn as nn import torch.nn.functional as F from transformers.pytorch_utils import Conv1D -from ..utils import PeftConfig, PeftType, transpose - - -def is_bnb_available(): - return importlib.util.find_spec("bitsandbytes") is not None +from ..utils import PeftConfig, PeftType, is_bnb_available, transpose if is_bnb_available(): diff --git a/tests/test_common_gpu.py b/tests/test_common_gpu.py index 7b5a39e..2f30018 100644 --- a/tests/test_common_gpu.py +++ b/tests/test_common_gpu.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. import gc -import importlib import unittest import pytest @@ -21,14 +20,11 @@ import torch from transformers import AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoTokenizer, WhisperForConditionalGeneration from peft import LoraConfig, PeftModel, get_peft_model +from peft.utils import is_bnb_available from .testing_utils import require_bitsandbytes, require_torch_gpu, require_torch_multi_gpu -def is_bnb_available(): - return importlib.util.find_spec("bitsandbytes") is not None - - if is_bnb_available(): from peft.tuners.lora import Linear8bitLt