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

[BugFix]update vocab_size in init_config #3260

Merged
merged 5 commits into from
Sep 14, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion paddlenlp/transformers/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import six
import logging
import inspect
from typing import Optional
from typing import Any, Optional

import paddle
import numpy as np
Expand Down Expand Up @@ -546,11 +546,31 @@ def resize_token_embeddings(self,
self.base_model.config['vocab_size'] = new_num_tokens
self.vocab_size = new_num_tokens

# update init_config
self._update_init_config(self.init_config, 'vocab_size', new_num_tokens)

# TODO([email protected]): add tie_weight.
# TODO(westfish) Add tie_weight to tie the weights between the input embeddings and the output embeddings if needed.

return new_embeddings

def _update_init_config(self, init_config: dict, key: str, value: Any):
"""update init_config by <key, value> pair

Args:
init_config (dict): the init_config instance
key (str): the key field
value (Any): the new value of instance
"""
if key in init_config:
init_config[key] = value
return

for arg in init_config.get('init_args', []):
if not isinstance(arg, PretrainedModel):
continue
self._update_init_config(arg.init_config, key, value)

Comment on lines +557 to +573
Copy link
Contributor Author

Choose a reason for hiding this comment

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

抽离成通用函数

def _get_resized_embeddings(
self,
old_embeddings: nn.Embedding,
Expand Down