-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Support local offline environment to read community config and model files #5817
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -841,6 +841,8 @@ def _get_config_dict( | |
resolved_config_file = resolve_hf_config_path( | ||
repo_id=pretrained_model_name_or_path, cache_dir=cache_dir, subfolder=subfolder | ||
) | ||
elif os.path.isfile(os.path.join(cache_dir, CONFIG_NAME)): | ||
resolved_config_file = os.path.join(cache_dir, CONFIG_NAME) | ||
Comment on lines
+844
to
+845
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是会优先根据变量 如果你想从 cache_dir 来加载文件的话,可以手动指定路径: # 从默认缓存路径加载模型
AutoModel.from_pretrained("bert-base-uncased")
# 从自定义缓存路径加载模型
AutoModel.from_pretrained("cache_dir") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 感谢您的 review,在不进行上述代码调整的 description 所描述环境中,我联网时执行如下命令,能够成功:
但当我断开网络连接,再次执行时,则会遇到与上面描述中类似的问题(目前这个 PR 的改动也没能解决这个方法在离线环境不可用的问题),堆栈信息如下:
不知道我对 |
||
else: | ||
community_url = "/".join([COMMUNITY_MODEL_PREFIX, pretrained_model_name_or_path, CONFIG_NAME]) | ||
if url_file_exists(community_url): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -877,6 +877,8 @@ def _resolve_model_file_path( | |
# 0. when it is local file | ||
if os.path.isfile(pretrained_model_name_or_path): | ||
return pretrained_model_name_or_path | ||
elif os.path.isfile(os.path.join(cache_dir, cls.resource_files_names["model_state"])): | ||
return os.path.join(cache_dir, cls.resource_files_names["model_state"]) | ||
Comment on lines
+880
to
+881
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 此部分代码说明同上。 |
||
|
||
# 1. when it is model-name | ||
if pretrained_model_name_or_path in cls.pretrained_init_configuration: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样应该可以解决上面提到的
AutoModel
无法在离线环境使用已经下载好的社区模型问题了通过已下载模型的绝对路径进行加载确实能够在离线环境下使用,但第一次下载社区模型用
community/model-name
的形式,下载后在离线环境使用时,把代码再改成/path/to/community/model-name
的形式,总是要麻烦一些,不如直接在下载社区模型前检查一下 cache_dir 中是否已经包含下载好的文件,有的话直接使用,也避免了去校验配置文件是否存在等的网络请求There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AlphaHinex@1e4bca0