-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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
Avoid using torch's Tensor or PIL's Image in chat template utils if not available #34165
Conversation
Also cc @zucchini-nlp |
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.
Yes, this looks good to me! For clarity, this function is called when auto-generating JSON schema for functions passed as tools to the chat template interface.
The main thing to be wary of here is that this change might cause slightly different behaviour on systems where Image
and Tensor
types are unavailable. However, I think we can safely assume that when the user has defined a function with Image
or Tensor
type hints in the header, that means they have the relevant libraries.
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 @LysandreJik this PR is quite straightforward, and I think it's okay to accept - are you okay with me merging it once tests pass? |
I am not qualified for review here, so whatever you find reasonable @Rocketknight1 :) |
Never mind - this is actually a CI issue. Rerunning it, you don't need to do anything @RezaRahemtola ! |
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.
Let's go!
Documentation build is failing but this PR doesn't touch the docs at all, so I'm assuming that's an issue with the doc builder. Merging, and thanks @RezaRahemtola for the PR! |
…ot available (huggingface#34165) * fix(utils): Avoid using torch Tensor or PIL Image if not available * Trigger CI --------- Co-authored-by: Matt <rocketknight1@gmail.com>
What does this PR do?
This PR adds conditional checks to the types mapping definitions to avoid defining types that are conditionally imported previously, if the same check doesn't succeed.
This allows a user (like me) to use
transformers
for simple stuff like tokenizers without needed to install PIL and/or torch if not needed (I'm not using the tokenizers library directly as it doesn't include chat templating functions, and I'm not using torch/tensorflow/JAX as I'm just calling models run locally with llamacpp, installing one of those just for chat templating seems overkill).Before, a simple call to
tokenizer.apply_chat_template
would result in this error:With this fix, there's not error: if PIL (or torch) isn't installed, it's not imported (already the case) and not used (added here).
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?