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

Fix conflicting key in init kwargs in PreTrainedTokenizerBase #31233

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions src/transformers/tokenization_utils_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,13 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin):
def __init__(self, **kwargs):
# inputs and kwargs for saving and re-loading (see ``from_pretrained`` and ``save_pretrained``)
self.init_inputs = ()
if "add_special_tokens" in kwargs:
kwargs["_add_special_tokens"] = kwargs.pop("add_special_tokens")
Copy link
Collaborator

Choose a reason for hiding this comment

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

why are we saving this? It's never used btw no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you mentioned we might be able to use it.
#31233 (review)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah but if we don't then let's not save it!


for key in kwargs:
if hasattr(self, key) and callable(getattr(self, key)):
raise AttributeError(f"{key} conflicts with the method {key} in {self.__class__.__name__}")

self.init_kwargs = copy.deepcopy(kwargs)
self.name_or_path = kwargs.pop("name_or_path", "")
self._processor_class = kwargs.pop("processor_class", None)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_tokenization_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4319,3 +4319,17 @@ def test_special_token_addition(self):
replace_additional_special_tokens=False,
)
self.assertEqual(tokenizer_2.additional_special_tokens, ["<other>", "<another>", "<tok>"])

def test_tokenizer_initialization_with_conflicting_key(self):
if self.test_rust_tokenizer:
tokenizer = self.get_rust_tokenizer(add_special_tokens=True)
else:
tokenizer = self.get_tokenizer(add_special_tokens=True)

with tempfile.TemporaryDirectory() as tmp_dir_1:
tokenizer.save_pretrained(tmp_dir_1)
loaded_tokenizer = tokenizer.from_pretrained(tmp_dir_1)
assert loaded_tokenizer.init_kwargs.get("_add_special_tokens")

with self.assertRaises(AttributeError, msg="conflicts with the method"):
self.get_tokenizer(get_vocab=True)