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

Enable greedy sampling #70

Merged
merged 8 commits into from Jun 5, 2023
Merged

Conversation

aashiqmuhamed
Copy link
Contributor

This CR enables greedy sampling in model.generate on XLA devices such as Trainium and TPU. This addresses issues such as huggingface/transformers#18661 and huggingface/transformers#12322.

The implementation is inspired by the corresponding Tensorflow generate function in transformers. The CR uses conditional statements to support greedy sampling, and also implements kv-cache functionality that is XLA compatible.

@aashiqmuhamed
Copy link
Contributor Author

@michaelbenayoun Could you please take a look when you get a chance?

optimum/neuron/generation/utils.py Outdated Show resolved Hide resolved
optimum/neuron/generation/utils.py Outdated Show resolved Hide resolved
optimum/neuron/generation/utils.py Outdated Show resolved Hide resolved
optimum/neuron/generation/utils.py Outdated Show resolved Hide resolved
optimum/neuron/generation/utils.py Show resolved Hide resolved
optimum/neuron/generation/utils.py Outdated Show resolved Hide resolved
optimum/neuron/generation/utils.py Outdated Show resolved Hide resolved
optimum/neuron/generation/utils.py Outdated Show resolved Hide resolved
tests/test_generate.py Outdated Show resolved Hide resolved

return np.array(results)


Copy link
Member

Choose a reason for hiding this comment

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

Parameterize this test to test all the generative models we support.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are testing sampling for GPT and BART models at the moment. In the revision, I've currently included t5-small, and will include more models over the next few weeks.

Copy link
Member

Choose a reason for hiding this comment

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

Where is it tested for GPT and BART?
Anyways, alright let's do that!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GPT and BART will be committed after we merge this CR. We are currently fixing a few bugs,

optimum/neuron/generation/utils.py Outdated Show resolved Hide resolved
optimum/neuron/generation/utils.py Show resolved Hide resolved

return np.array(results)


Copy link
Member

Choose a reason for hiding this comment

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

Where is it tested for GPT and BART?
Anyways, alright let's do that!

next_token_logits = outputs.logits[:, -1, :]

# pre-process distribution
next_tokens_scores = logits_processor(input_ids, next_token_logits)
Copy link
Contributor

Choose a reason for hiding this comment

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

In the non-XLA version, input_ids is of dim bs x seq_length. With the padding we introduced here, dimensions change. Also, we don't know which exact processor is being used and whether it is supported by XLA -> I think we can't expect each logit processor to be XLA-compatible, so we should probably compute on CPU.

Suggested change
next_tokens_scores = logits_processor(input_ids, next_token_logits)
if is_torch_tpu_available():
input_ids_ = input_ids.to('cpu')[:, :seq_length]
next_token_logits_ = next_token_logits.to('cpu')
next_tokens_scores = logits_processor(input_ids_, next_token_logits_)
next_tokens_scores = next_tokens_scores.to(input_ids.device)
else:
next_tokens_scores = logits_processor(input_ids, next_token_logits)

Copy link
Member

Choose a reason for hiding this comment

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

@aashiqmuhamed want to commit this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'm committing without the is_torch_tpu_available() check, since we expect to run on Trainium by default.

Copy link
Member

@michaelbenayoun michaelbenayoun left a comment

Choose a reason for hiding this comment

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

Some tests are failing, left comments to fix that.


import torch
import torch.distributed as dist
import torch_xla.core.xla_model as xm
Copy link
Member

Choose a reason for hiding this comment

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

This causes an import error.
Can you do:

from ..utils import is_torch_xla_available
if is_torch_xla_available():
    import torch_xla.core.xla_model

Basically we want to be able to import and test code on regular machines when we do not need all of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, is the optimum neuron library designed for both CPU and Trainium?

Copy link
Member

Choose a reason for hiding this comment

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

Only Trainium but it is more to be able to run non-trainum dependent tests on regular machines. Without this the test will fail if we dont have torch_xla installed even though we want to test something unrelated.

tests/test_generate.py Outdated Show resolved Hide resolved
tests/test_generate.py Show resolved Hide resolved
Copy link
Member

@michaelbenayoun michaelbenayoun left a comment

Choose a reason for hiding this comment

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

Some tests are failing, left comments to fix that.

Copy link
Member

@michaelbenayoun michaelbenayoun left a comment

Choose a reason for hiding this comment

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

LGTM!
Will merge if the tests pass.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@michaelbenayoun
Copy link
Member

The EC2 runners cannot be used because they need secrets from this repo, which you do not have on your fork...
We can skip them for now.

Can you run the following command please:

make style

This will fix the styling error you currently have.

@michaelbenayoun michaelbenayoun merged commit 7bc8e9b into huggingface:main Jun 5, 2023
6 of 10 checks passed
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