Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Raphael Glon <oOraph@users.noreply.github.com>
  • Loading branch information
oOraph committed May 27, 2024
1 parent 3b4e3ae commit c3d72e2
Showing 1 changed file with 97 additions and 1 deletion.
98 changes: 97 additions & 1 deletion tests/test_modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from transformers import (
AutoConfig,
AutoModel,
AutoModelForImageClassification,
AutoModelForSequenceClassification,
OwlViTForObjectDetection,
PretrainedConfig,
Expand Down Expand Up @@ -76,7 +77,6 @@

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
Expand Down Expand Up @@ -194,6 +194,99 @@ def forward(self, mask, inputs_embeds):
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 transformers.utils import hub
from huggingface_hub import constants
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)

print("TMPDIR, %s" % tmpdir)
print("LOCAL DIR %s", local_dir)

LOG.info("Model %s downloaded in %s", TINY_IMAGE_CLASSIF, local_dir)

import pdb
pdb.set_trace()
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 transformers.utils import hub
from huggingface_hub import constants
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)

print("TMPDIR, %s" % tmpdir)
print("LOCAL DIR %s", local_dir)

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
Expand All @@ -205,6 +298,9 @@ def forward(self, mask, inputs_embeds):
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):
Expand Down

0 comments on commit c3d72e2

Please sign in to comment.