Skip to content

VLM: special multimodal Tokenizer#34461

Merged
zucchini-nlp merged 24 commits intohuggingface:mainfrom
zucchini-nlp:vlm-tokenizer
Nov 4, 2024
Merged

VLM: special multimodal Tokenizer#34461
zucchini-nlp merged 24 commits intohuggingface:mainfrom
zucchini-nlp:vlm-tokenizer

Conversation

@zucchini-nlp
Copy link
Copy Markdown
Member

What does this PR do?

Part of Major VLM standardization (#33948). We will have special tokens that are present in all VLMs to be part if XXXTokenizer attributes. This will make our lives easier when doing several processing manipulations and/or formatting the prompt manually, as we can simply call self.tokenizer.image_token.

Currently if we need any of VLM special tokens, those are saved in processor config, but not all models save it since not all models use it when calling the processor. After this PR I'll go over models and clean up the processing code given the changes. But we might still have to support old way, because we can't change stuff if that can break loading configs from the hub

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@zucchini-nlp
Copy link
Copy Markdown
Member Author

Should be ready for review @ArthurZucker !

I think we'll support simple non-multimodal tokenizers for quite a while in VLMs, no idea yet how/when to make this a new default

Copy link
Copy Markdown
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Okay super super good! The only thing I don't like is the is_multimodal!
I think what you added gives a lot of freedom to all tokenizers -> audio_cls_token or anything that ends with token / ends with id will be properly processed!

Let's remove the is_mulitmodal and should be good!

Copy link
Copy Markdown
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Perfect

Comment on lines +65 to +66
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.extra_special_tokens = ["image_token", "boi_token", "eoi_token"]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.extra_special_tokens = ["image_token", "boi_token", "eoi_token"]
tokenizer = AutoTokenizer.from_pretrained(model_id, extra_special_tokens = ["image_token", "boi_token", "eoi_token"])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

let's add a small test for this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, this is actually not correct anymore hehe, forgot to update the docs. And it has a test for that already so we are good

new way of adding extra special tokens is like
tokenizer.extra_special_tokens = {"eoi_token": "<s>", "image_token": "<image>"}. After adding this line and saving the tokenizer, loading back will do the magic and tokenizer will have self.image_token attribute

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we should be able to pass it as input as well instead of forcing people to use the setter! 🤗

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeap, realized later and added that in the docs instead of "saving-loading back". Plus extended the test

zucchini-nlp and others added 3 commits October 30, 2024 09:12
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
Copy link
Copy Markdown
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Very nice!

super().__init__(**kwargs)

self.extra_special_tokens = kwargs.pop("extra_special_tokens", {})
self._set_model_specific_special_tokens(special_tokens=self.extra_special_tokens)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

when we do this, we don't add them to the tokenizer vocab right?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think you are already checking that these tokens are added to the vocab if not already present right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

if the special token is not present in the vocab, we do add them as new tokens to the tokenizer vocab. Should we prevent users from adding new tokens and allow to use only available tokens?

It happens because the Tokenizer initially is wired to do that, irrespective of current changes

# 4. If some of the special tokens are not part of the vocab, we add them, at the end.
# the order of addition is the same as self.SPECIAL_TOKENS_ATTRIBUTES following `tokenizers`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

NO it's alright IMO we have not really seen reports about that

@zucchini-nlp zucchini-nlp merged commit 187439c into huggingface:main Nov 4, 2024
frances720 pushed a commit to Promptless/transformers-test that referenced this pull request Nov 5, 2024
* kinda works

* update

* add tests

* update

* use special tokens in processors

* typo

* fix copies

* fix

* fix moshi after rebase

* update

* fix tests

* update

* Update docs/source/en/main_classes/tokenizer.md

Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>

* update docs

* test for load time adding tokens

* fix some more tests which are now fetched better

* one more fix

---------

Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
BernardZach pushed a commit to BernardZach/transformers that referenced this pull request Dec 5, 2024
* kinda works

* update

* add tests

* update

* use special tokens in processors

* typo

* fix copies

* fix

* fix moshi after rebase

* update

* fix tests

* update

* Update docs/source/en/main_classes/tokenizer.md

Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>

* update docs

* test for load time adding tokens

* fix some more tests which are now fetched better

* one more fix

---------

Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants