Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

offline mode for firewalled envs (part 2) #10569

Merged
merged 6 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion examples/research_projects/bertabs/run_summarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from torch.utils.data import DataLoader, SequentialSampler
from tqdm import tqdm

from filelock import FileLock
stas00 marked this conversation as resolved.
Show resolved Hide resolved
from modeling_bertabs import BertAbs, build_predictor
from transformers import BertTokenizer
from transformers.file_utils import is_offline_mode

from .utils_summarization import (
CNNDMDataset,
Expand Down Expand Up @@ -48,7 +50,16 @@ def evaluate(args):

import rouge

nltk.download("punkt")
try:
nltk.data.find("tokenizers/punkt")
except (LookupError, OSError):
if is_offline_mode():
raise LookupError(
"Offline mode: run this script without TRANSFORMERS_OFFLINE first to download nltk data files"
)
with FileLock(".lock") as lock: # noqa
nltk.download("punkt", quiet=True)

rouge_evaluator = rouge.Rouge(
metrics=["rouge-n", "rouge-l"],
max_n=2,
Expand Down
5 changes: 5 additions & 0 deletions src/transformers/feature_extraction_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
cached_path,
hf_bucket_url,
is_flax_available,
is_offline_mode,
is_remote_url,
is_tf_available,
is_torch_available,
Expand Down Expand Up @@ -342,6 +343,10 @@ def get_feature_extractor_dict(
local_files_only = kwargs.pop("local_files_only", False)
revision = kwargs.pop("revision", None)

if is_offline_mode() and not local_files_only:
logger.info("Offline mode: forcing local_files_only=True")
local_files_only = True

pretrained_model_name_or_path = str(pretrained_model_name_or_path)
if os.path.isdir(pretrained_model_name_or_path):
feature_extractor_file = os.path.join(pretrained_model_name_or_path, FEATURE_EXTRACTOR_NAME)
Expand Down
4 changes: 4 additions & 0 deletions src/transformers/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,10 @@ def cached_path(
if isinstance(cache_dir, Path):
cache_dir = str(cache_dir)

if is_offline_mode() and not local_files_only:
logger.info("Offline mode: forcing local_files_only=True")
local_files_only = True

if is_remote_url(url_or_filename):
# URL, so get it from the cache (downloading if necessary)
output_path = get_from_cache(
Expand Down
6 changes: 5 additions & 1 deletion src/transformers/modeling_flax_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from jax.random import PRNGKey

from .configuration_utils import PretrainedConfig
from .file_utils import FLAX_WEIGHTS_NAME, WEIGHTS_NAME, cached_path, hf_bucket_url, is_remote_url
from .file_utils import FLAX_WEIGHTS_NAME, WEIGHTS_NAME, cached_path, hf_bucket_url, is_offline_mode, is_remote_url
from .utils import logging


Expand Down Expand Up @@ -229,6 +229,10 @@ def from_pretrained(
use_auth_token = kwargs.pop("use_auth_token", None)
revision = kwargs.pop("revision", None)

if is_offline_mode() and not local_files_only:
logger.info("Offline mode: forcing local_files_only=True")
local_files_only = True

# Load config if we don't provide a configuration
if not isinstance(config, PretrainedConfig):
config_path = config if config is not None else pretrained_model_name_or_path
Expand Down
5 changes: 5 additions & 0 deletions src/transformers/modeling_tf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ModelOutput,
cached_path,
hf_bucket_url,
is_offline_mode,
is_remote_url,
)
from .generation_tf_utils import TFGenerationMixin
Expand Down Expand Up @@ -1151,6 +1152,10 @@ def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
revision = kwargs.pop("revision", None)
mirror = kwargs.pop("mirror", None)

if is_offline_mode() and not local_files_only:
logger.info("Offline mode: forcing local_files_only=True")
local_files_only = True

# Load config if we don't provide a configuration
if not isinstance(config, PretrainedConfig):
config_path = config if config is not None else pretrained_model_name_or_path
Expand Down
28 changes: 23 additions & 5 deletions tests/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,37 @@ def test_offline_mode(self):
# while running an external program

# python one-liner segments
load = "from transformers import BertConfig, BertModel, BertTokenizer;"
run = "mname = 'lysandre/tiny-bert-random'; BertConfig.from_pretrained(mname) and BertModel.from_pretrained(mname) and BertTokenizer.from_pretrained(mname);"
mock = 'import socket; exec("def offline_socket(*args, **kwargs): raise socket.error(\\"Offline mode is enabled.\\")"); socket.socket = offline_socket;'

# this must be loaded before socket.socket is monkey-patched
load = """
from transformers import BertConfig, BertModel, BertTokenizer
"""

run = """
mname = "lysandre/tiny-bert-random"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
print("success")
"""

mock = """
import socket
def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")
socket.socket = offline_socket
"""

# baseline - just load from_pretrained with normal network
cmd = [sys.executable, "-c", f"{load} {run}"]
cmd = [sys.executable, "-c", "\n".join([load, run])]

# should succeed
env = self.get_env()
result = subprocess.run(cmd, env=env, check=False, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn("success", result.stdout.decode())

# next emulate no network
cmd = [sys.executable, "-c", f"{load} {mock} {run}"]
cmd = [sys.executable, "-c", "\n".join([load, mock, run])]

# should normally fail as it will fail to lookup the model files w/o the network
env["TRANSFORMERS_OFFLINE"] = "0"
Expand All @@ -51,3 +68,4 @@ def test_offline_mode(self):
env["TRANSFORMERS_OFFLINE"] = "1"
result = subprocess.run(cmd, env=env, check=False, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn("success", result.stdout.decode())