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

Output dicts support in text generation pipeline #35092

Conversation

jonasrohw
Copy link
Contributor

@jonasrohw jonasrohw commented Dec 4, 2024

What does this PR do?

It is a minor fix to the text generation pipeline. When calling with the generation argument return_dict_in_generate=True, the code breaks because it does not expect a dict from the model.generate(...) call. If you want to view logits, for example, a dict is required. This fix can handle return_dict_in_generate=True as a pipeline parameter.

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@Rocketknight1

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

Hi - firstly, thanks for the PR, but I notice that in the code there's a lot more than just adding output dict support! It seems like you're also changing the handling for batched generation a lot.

I can understand the rationale there, but I think it would be better to split that into a separate PR. In other words, make this into a smaller PR that just adds output dict support, with a second PR focused on batching. WDYT?

)

for key, value in other_outputs.items():
if isinstance(value, (torch.Tensor, tf.Tensor)) and value.shape[0] == out_b:
Copy link
Member

Choose a reason for hiding this comment

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

This line seems dangerous - remember that tf.Tensor may not be available or imported on most systems! You may have to make this a more complicated conditional that only checks tf.Tensor after checking is_tensorflow_available(), to ensure no issues on Torch-only systems (i.e. most of them)

for key, value in other_outputs.items():
if isinstance(value, (list, tuple)):
record[key] = value[idx]
elif isinstance(value, (torch.Tensor, tf.Tensor)):
Copy link
Member

Choose a reason for hiding this comment

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

Same issue here

@HuggingFaceDocBuilderDev

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.

@jonasrohw
Copy link
Contributor Author

@Rocketknight1 Hi. Thanks for looking at the PR and pointing that out. I agree with you; I got a bit ahead of myself. I've removed most of the changes and just made the changes to support dictionary output. I'd be happy if you could take a look. Thanks again!

@jonasrohw
Copy link
Contributor Author

@Rocketknight1 Any update on this? Thanks!

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

I think this looks good to me now, and sorry for the delay! @gante since you're probably more familiar with return_dict_in_generate, can you take a look and make sure it's all okay before I ping a core maintainer?

@jonasrohw
Copy link
Contributor Author

@gante Friendly reminder if you could take a look. Thank you very much!

@jonasrohw
Copy link
Contributor Author

@Rocketknight1 Can't seem to get a hold of @gante. Any idea how to proceed?

@Rocketknight1
Copy link
Member

Hang on, I'll get him!

@jonasrohw
Copy link
Contributor Author

@Rocketknight1 Any success in finding him?

@gante
Copy link
Member

gante commented Jan 29, 2025

@jonasrohw I exist, but I'm being pinged in many places :D

Copy link
Member

@gante gante left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for expanding the capabilities of the pipeline :D

@gante
Copy link
Member

gante commented Jan 29, 2025

(fixed conflicts, will merge as soon as CI gets green)

@gante
Copy link
Member

gante commented Jan 29, 2025

(failing tests are unrelated to this PR, and are being fixed in other PRs. Waiting for them to be merged first)

@gante gante merged commit 23d782e into huggingface:main Jan 29, 2025
23 checks passed
bursteratom pushed a commit to bursteratom/transformers that referenced this pull request Feb 5, 2025
* Support for generate_argument: return_dict_in_generate=True, instead of returning a error

* fix: call test with return_dict_in_generate=True

* fix: Only import torch if it is present

* update: Encapsulate output_dict changes

* fix: added back original comments

---------

Co-authored-by: Joao Gante <[email protected]>
elvircrn pushed a commit to elvircrn/transformers that referenced this pull request Feb 13, 2025
* Support for generate_argument: return_dict_in_generate=True, instead of returning a error

* fix: call test with return_dict_in_generate=True

* fix: Only import torch if it is present

* update: Encapsulate output_dict changes

* fix: added back original comments

---------

Co-authored-by: Joao Gante <[email protected]>
sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Feb 16, 2025
* Support for generate_argument: return_dict_in_generate=True, instead of returning a error

* fix: call test with return_dict_in_generate=True

* fix: Only import torch if it is present

* update: Encapsulate output_dict changes

* fix: added back original comments

---------

Co-authored-by: Joao Gante <[email protected]>
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.

4 participants