Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
3876fc68
Unverified
Commit
3876fc68
authored
Apr 10, 2023
by
Sylvain Gugger
Committed by
GitHub
Apr 10, 2023
Browse files
Make dynamic code work with offline mode (#22661)
* Make dynamic code work with offline mode * Clean up * Quality
parent
98597725
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
5 deletions
+37
-5
src/transformers/dynamic_module_utils.py
src/transformers/dynamic_module_utils.py
+11
-5
tests/utils/test_offline.py
tests/utils/test_offline.py
+26
-0
No files found.
src/transformers/dynamic_module_utils.py
View file @
3876fc68
...
...
@@ -22,9 +22,14 @@ import sys
from
pathlib
import
Path
from
typing
import
Dict
,
Optional
,
Union
from
huggingface_hub
import
model_info
from
.utils
import
HF_MODULES_CACHE
,
TRANSFORMERS_DYNAMIC_MODULE_NAME
,
cached_file
,
is_offline_mode
,
logging
from
.utils
import
(
HF_MODULES_CACHE
,
TRANSFORMERS_DYNAMIC_MODULE_NAME
,
cached_file
,
extract_commit_hash
,
is_offline_mode
,
logging
,
)
logger
=
logging
.
get_logger
(
__name__
)
# pylint: disable=invalid-name
...
...
@@ -163,6 +168,7 @@ def get_cached_module_file(
use_auth_token
:
Optional
[
Union
[
bool
,
str
]]
=
None
,
revision
:
Optional
[
str
]
=
None
,
local_files_only
:
bool
=
False
,
_commit_hash
:
Optional
[
str
]
=
None
,
):
"""
Prepares Downloads a module from a local folder or a distant repo and returns its path inside the cached
...
...
@@ -233,6 +239,7 @@ def get_cached_module_file(
local_files_only
=
local_files_only
,
use_auth_token
=
use_auth_token
,
revision
=
revision
,
_commit_hash
=
_commit_hash
,
)
except
EnvironmentError
:
...
...
@@ -264,8 +271,7 @@ def get_cached_module_file(
importlib
.
invalidate_caches
()
else
:
# Get the commit hash
# TODO: we will get this info in the etag soon, so retrieve it from there and not here.
commit_hash
=
model_info
(
pretrained_model_name_or_path
,
revision
=
revision
,
token
=
use_auth_token
).
sha
commit_hash
=
extract_commit_hash
(
resolved_module_file
,
_commit_hash
)
# The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the
# benefit of versioning.
...
...
tests/utils/test_offline.py
View file @
3876fc68
...
...
@@ -177,3 +177,29 @@ socket.socket = offline_socket
self
.
assertIn
(
"You cannot infer task automatically within `pipeline` when using offline mode"
,
result
.
stderr
.
decode
()
)
@
require_torch
def
test_offline_model_dynamic_model
(
self
):
load
=
"""
from transformers import AutoModel
"""
run
=
"""
mname = "hf-internal-testing/test_dynamic_model"
AutoModel.from_pretrained(mname, trust_remote_code=True)
print("success")
"""
# baseline - just load from_pretrained with normal network
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
())
# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
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
())
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment