-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[Bug fixes] update attribute map handler #4421
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,8 +195,12 @@ def convert_to_legacy_config(attribute_map: Dict[str, str], config: Dict[str, An | |
args.append(init_arg) | ||
config["init_args"] = args | ||
|
||
# TODO(wj-Mcat): to improve compatibility for: old local config and new PretrainedConfig, eg: | ||
# { "init_args": [], "init_class": "", "num_classes": 12 } | ||
for standard_field, paddle_field in attribute_map.items(): | ||
config[paddle_field] = config.pop(standard_field, None) or config.pop(paddle_field, None) | ||
value = config.pop(standard_field, None) or config.pop(paddle_field, None) | ||
if value is not None: | ||
config[paddle_field] = value | ||
return config | ||
|
||
|
||
|
@@ -729,16 +733,6 @@ def from_pretrained( | |
|
||
config_dict, kwargs = cls.get_config_dict(pretrained_model_name_or_path, **kwargs) | ||
|
||
# do standard config map: there are some old-school pretrained-config not refactored. | ||
config_dict = convert_to_legacy_config(cls.attribute_map, config_dict) | ||
|
||
config_dict = flatten_model_config(config_dict) | ||
if "model_type" in config_dict and hasattr(cls, "model_type") and config_dict["model_type"] != cls.model_type: | ||
logger.warning( | ||
f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " | ||
f"{cls.model_type}. This is not supported for all configurations of models and can yield errors." | ||
) | ||
|
||
Comment on lines
-732
to
-741
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. 将 |
||
return cls.from_dict(config_dict, **kwargs) | ||
|
||
@classmethod | ||
|
@@ -859,12 +853,18 @@ def from_dict(cls, config_dict: Dict[str, Any], **kwargs) -> "PretrainedConfig": | |
[`PretrainedConfig`]: The configuration object instantiated from those parameters. | ||
""" | ||
return_unused_kwargs = kwargs.pop("return_unused_kwargs", False) | ||
# Those arguments may be passed along for our internal telemetry. | ||
# We remove them so they don't appear in `return_unused_kwargs`. | ||
|
||
# convert local config to legacy config | ||
# do standard config map: there are some old-school pretrained-config not refactored. | ||
config_dict = convert_to_legacy_config(cls.attribute_map, config_dict) | ||
|
||
config_dict = flatten_model_config(config_dict) | ||
|
||
if "model_type" in config_dict and hasattr(cls, "model_type") and config_dict["model_type"] != cls.model_type: | ||
logger.warning( | ||
f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " | ||
f"{cls.model_type}. This is not supported for all configurations of models and can yield errors." | ||
) | ||
|
||
config = cls(**config_dict) | ||
|
||
if hasattr(config, "pruned_heads"): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
问题
问题是出在:将
attribute_map
中的values 参数都映射到第一层级参数上,且都是:target_paddle_field: None
。故导致init_args
下的参数没办法正确 map 上来,比如d_model
为什么这样调整
model_config.json
在老板本配置文件中,模型的参数是放在
init_args
参数中,而这个模块是先递归调用convert_to_legacy_config
函数(深度优先),针对于模型参数做 map,此时可能会将hidden_size
->d_model
。可是执行完毕之后,老代码会在根目录上设置:
d_model: None
,导致flatten_model_config
中没办法将init_args
中的正确d_model
映射回来。现在这种做法是可以解决这个问题。
config.json
中因为没有
init_args
参数,也是可以针对于模型参数做 map。