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

Add HF_HUB_OFFLINE env var #22

Merged
merged 5 commits into from
Mar 18, 2021
Merged

Add HF_HUB_OFFLINE env var #22

merged 5 commits into from
Mar 18, 2021

Conversation

lhoestq
Copy link
Member

@lhoestq lhoestq commented Mar 15, 2021

This adds the HF_HUB_OFFLINE env var to tell huggingface_hub (same as in hf/transformers and hf/datasets).

This allows to tell the library to look directly at the cache instead of trying network calls.

Moreover I fixed/changed a few things:

  • I added a default timeout for requests in file_download.py (otherwise they could hang in a firewalled environment) and never fall back on the cache
  • I made it so force_download=True doesn't check the cache if there's no internet connection, but raises an error instead.
  • I added retries as in hf/datasets if one network call fails if you're unlucky or have bad wifi
  • I added a testing utility context manager called offline that you can use to test the library's behavior under different offline modes (same as in hf/datasets)

I'm not sure why the CI fails for python 3.9 though

@lhoestq lhoestq requested a review from julien-c March 15, 2021 17:41
@julien-c
Copy link
Member

CI error looks transient


def test_offline_with_timeout():
with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT):
with pytest.raises(RequestWouldHangIndefinitelyError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use with self.assertRaises to be consistent with other tests here @lhoestq (not a big deal if we want to stay consistent with other implems in our librairies)

timeout: float = 10.0,
**params,
) -> requests.Response:
"""Wrapper around requests to retry in case it fails with a ConnectTimeout, with exponential backoff.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do this only for Timeouts or a broader array of errors?

Also cc @Pierrci @n1t0

def _request_with_retry(
method: str,
url: str,
max_retries: int = 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default should maybe be finite no? like 5?

if isinstance(exc, requests.exceptions.SSLError) or isinstance(
exc, requests.exceptions.ProxyError
):
if isinstance(exc, requests.exceptions.ProxyError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this intended to remove this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes otherwise the test with the bogus url test fails (raises SSLError while ValueError is expected)

Copy link
Member

@julien-c julien-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one remark is that in transformers we should probably document HF_HUB_OFFLINE instead of TRANSFORMERS_OFFLINE given that we want to switch to huggingface_hub there in the codebase. Thoughts?

@julien-c
Copy link
Member

also related to huggingface/transformers#10235 @lhoestq

@julien-c julien-c merged commit be902d8 into main Mar 18, 2021
@julien-c julien-c deleted the offline-mode branch March 18, 2021 16:55
logger.info(
f"{method} request to {url} timed out, retrying... [{tries/max_retries}]"
)
sleep_time = max(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be min?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants