fix from_pretrained in offline mode when model is preloaded in cache (#31010)
* Unit test to verify fix Signed-off-by: Raphael Glon <oOraph@users.noreply.github.com> * fix from_pretrained in offline mode when model is preloaded in cache Signed-off-by: Raphael Glon <oOraph@users.noreply.github.com> * minor: fmt Signed-off-by: Raphael Glon <oOraph@users.noreply.github.com> --------- Signed-off-by: Raphael Glon <oOraph@users.noreply.github.com> Co-authored-by: Raphael Glon <oOraph@users.noreply.github.com>
This commit is contained in:
@@ -33,6 +33,7 @@ from requests.exceptions import HTTPError
|
||||
from transformers import (
|
||||
AutoConfig,
|
||||
AutoModel,
|
||||
AutoModelForImageClassification,
|
||||
AutoModelForSequenceClassification,
|
||||
OwlViTForObjectDetection,
|
||||
PretrainedConfig,
|
||||
@@ -76,7 +77,6 @@ sys.path.append(str(Path(__file__).parent.parent / "utils"))
|
||||
|
||||
from test_module.custom_configuration import CustomConfig, NoSuperInitConfig # noqa E402
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
from safetensors.torch import save_file as safe_save_file
|
||||
@@ -194,6 +194,97 @@ if is_torch_available():
|
||||
attention_mask = _prepare_4d_attention_mask(mask, dtype=inputs_embeds.dtype)
|
||||
return attention_mask
|
||||
|
||||
class TestOffline(unittest.TestCase):
|
||||
def test_offline(self):
|
||||
# Ugly setup with monkeypatches, amending env vars here is too late as libs have already been imported
|
||||
from huggingface_hub import constants
|
||||
|
||||
from transformers.utils import hub
|
||||
|
||||
offlfine_env = hub._is_offline_mode
|
||||
hub_cache_env = constants.HF_HUB_CACHE
|
||||
hub_cache_env1 = constants.HUGGINGFACE_HUB_CACHE
|
||||
default_cache = constants.default_cache_path
|
||||
transformers_cache = hub.TRANSFORMERS_CACHE
|
||||
|
||||
try:
|
||||
hub._is_offline_mode = True
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
LOG.info("Temporary cache dir %s", tmpdir)
|
||||
constants.HF_HUB_CACHE = tmpdir
|
||||
constants.HUGGINGFACE_HUB_CACHE = tmpdir
|
||||
constants.default_cache_path = tmpdir
|
||||
hub.TRANSFORMERS_CACHE = tmpdir
|
||||
# First offline load should fail
|
||||
try:
|
||||
AutoModelForImageClassification.from_pretrained(
|
||||
TINY_IMAGE_CLASSIF, revision="main", use_auth_token=None
|
||||
)
|
||||
except OSError:
|
||||
LOG.info("Loading model %s in offline mode failed as expected", TINY_IMAGE_CLASSIF)
|
||||
else:
|
||||
self.fail("Loading model {} in offline mode should fail".format(TINY_IMAGE_CLASSIF))
|
||||
|
||||
# Download model -> Huggingface Hub not concerned by our offline mode
|
||||
LOG.info("Downloading %s for offline tests", TINY_IMAGE_CLASSIF)
|
||||
hub_api = HfApi()
|
||||
local_dir = hub_api.snapshot_download(TINY_IMAGE_CLASSIF, cache_dir=tmpdir)
|
||||
|
||||
LOG.info("Model %s downloaded in %s", TINY_IMAGE_CLASSIF, local_dir)
|
||||
|
||||
AutoModelForImageClassification.from_pretrained(
|
||||
TINY_IMAGE_CLASSIF, revision="main", use_auth_token=None
|
||||
)
|
||||
finally:
|
||||
# Tear down: reset env as it was before calling this test
|
||||
hub._is_offline_mode = offlfine_env
|
||||
constants.HF_HUB_CACHE = hub_cache_env
|
||||
constants.HUGGINGFACE_HUB_CACHE = hub_cache_env1
|
||||
constants.default_cache_path = default_cache
|
||||
hub.TRANSFORMERS_CACHE = transformers_cache
|
||||
|
||||
def test_local_files_only(self):
|
||||
# Ugly setup with monkeypatches, amending env vars here is too late as libs have already been imported
|
||||
from huggingface_hub import constants
|
||||
|
||||
from transformers.utils import hub
|
||||
|
||||
hub_cache_env = constants.HF_HUB_CACHE
|
||||
hub_cache_env1 = constants.HUGGINGFACE_HUB_CACHE
|
||||
default_cache = constants.default_cache_path
|
||||
transformers_cache = hub.TRANSFORMERS_CACHE
|
||||
try:
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
LOG.info("Temporary cache dir %s", tmpdir)
|
||||
constants.HF_HUB_CACHE = tmpdir
|
||||
constants.HUGGINGFACE_HUB_CACHE = tmpdir
|
||||
constants.default_cache_path = tmpdir
|
||||
hub.TRANSFORMERS_CACHE = tmpdir
|
||||
try:
|
||||
AutoModelForImageClassification.from_pretrained(
|
||||
TINY_IMAGE_CLASSIF, revision="main", use_auth_token=None, local_files_only=True
|
||||
)
|
||||
except OSError:
|
||||
LOG.info("Loading model %s in offline mode failed as expected", TINY_IMAGE_CLASSIF)
|
||||
else:
|
||||
self.fail("Loading model {} in offline mode should fail".format(TINY_IMAGE_CLASSIF))
|
||||
|
||||
LOG.info("Downloading %s for offline tests", TINY_IMAGE_CLASSIF)
|
||||
hub_api = HfApi()
|
||||
local_dir = hub_api.snapshot_download(TINY_IMAGE_CLASSIF, cache_dir=tmpdir)
|
||||
|
||||
LOG.info("Model %s downloaded in %s", TINY_IMAGE_CLASSIF, local_dir)
|
||||
|
||||
AutoModelForImageClassification.from_pretrained(
|
||||
TINY_IMAGE_CLASSIF, revision="main", use_auth_token=None, local_files_only=True
|
||||
)
|
||||
finally:
|
||||
# Tear down: reset env as it was before calling this test
|
||||
constants.HF_HUB_CACHE = hub_cache_env
|
||||
constants.HUGGINGFACE_HUB_CACHE = hub_cache_env1
|
||||
constants.default_cache_path = default_cache
|
||||
hub.TRANSFORMERS_CACHE = transformers_cache
|
||||
|
||||
|
||||
if is_flax_available():
|
||||
from transformers import FlaxBertModel
|
||||
@@ -205,6 +296,9 @@ if is_tf_available():
|
||||
TINY_T5 = "patrickvonplaten/t5-tiny-random"
|
||||
TINY_BERT_FOR_TOKEN_CLASSIFICATION = "hf-internal-testing/tiny-bert-for-token-classification"
|
||||
TINY_MISTRAL = "hf-internal-testing/tiny-random-MistralForCausalLM"
|
||||
TINY_IMAGE_CLASSIF = "hf-internal-testing/tiny-random-SiglipForImageClassification"
|
||||
|
||||
LOG = logging.get_logger(__name__)
|
||||
|
||||
|
||||
def check_models_equal(model1, model2):
|
||||
|
||||
Reference in New Issue
Block a user