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

TF: generate without tf.TensorArray #17801

Merged
merged 5 commits into from Jun 23, 2022
Merged

Conversation

gante
Copy link
Member

@gante gante commented Jun 21, 2022

What does this PR do?

Some models, like XLNet, need more than just the previous token when past is used. This PR solves this problem with the help of some refactoring -- we no longer use TensorArray, instead we scatter updates into a fixed-size tensor. This refactor simplifies generate, especially beam_search, which may prove to be helpful in enabling XLA.

Slow tests have been run for the usual generate models (gpt2, t5, rag, speech_to_text, encoder_decoder, vision_encoder_decoder, bart).

Why was this refactor needed?

As it can be read in this issue, TensorArray is meant to be used as a write-once array, anything else falls in the unexpected behavior domain -- in other words, our use was dangerous. The original solution to the XLNet problem was to read all existing tokens from the TensorArray, using the same logic as in this PR, but it failed with XLA -- and the behavior depended on what was written into the variable on its first write. Since we use fixed-size tensors, a normal tensor works just fine, and with simpler code (assuming the reader is familiar with how scatter works :D ).

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jun 21, 2022

The documentation is not available anymore as the PR was closed or merged.

@gante gante marked this pull request as ready for review June 21, 2022 18:08
@gante gante changed the title TF: generate without Tensor Array TF: generate without TensorArray Jun 21, 2022
@gante gante changed the title TF: generate without TensorArray TF: generate without tf.TensorArray Jun 21, 2022
@gante
Copy link
Member Author

gante commented Jun 21, 2022

cc @ydshieh -- this PR fixes the XLNet generate error we have been seeing :)

@patrickvonplaten
Copy link
Contributor

Cool!

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.

LGTM! (And agree about TensorArray being cursed). Did you see any performance changes from doing it this way?

@gante
Copy link
Member Author

gante commented Jun 23, 2022

@Rocketknight1 no differences in terms of execution speed 👍

GPT2 sample on a 3090, average of 10 runs (excluding compilation time)

  • Eager: 884 ms -> 888 ms
  • XLA: 29.2 ms -> 29.3 ms
  • JAX: 19.7 ms

@gante gante merged commit 5cce307 into huggingface:main Jun 23, 2022
@gante gante deleted the xlnet_generate branch June 23, 2022 11:28
younesbelkada pushed a commit to younesbelkada/transformers that referenced this pull request Jun 25, 2022
younesbelkada pushed a commit to younesbelkada/transformers that referenced this pull request Jun 29, 2022
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.

None yet

4 participants