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

support assisted decoding in ipex 2.4 #823

Merged
merged 19 commits into from
Sep 9, 2024
Merged

Conversation

jiqing-feng
Copy link
Collaborator

Support assisted decoding if ipex >= 2.5

@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.

optimum/intel/ipex/modeling_base.py Outdated Show resolved Hide resolved
optimum/intel/ipex/modeling_base.py Outdated Show resolved Hide resolved
optimum/intel/ipex/modeling_base.py Outdated Show resolved Hide resolved
jiqing-feng and others added 4 commits July 15, 2024 17:45
Co-authored-by: Ella Charlaix <80481427+echarlaix@users.noreply.github.com>
@jiqing-feng
Copy link
Collaborator Author

Hi @echarlaix . I have fixed all your comments, please re-run the tests. Thx!

@jiqing-feng
Copy link
Collaborator Author

Hi @IlyasMoutawwakil , could you please review this PR? Thx!

Copy link
Member

@IlyasMoutawwakil IlyasMoutawwakil left a comment

Choose a reason for hiding this comment

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

Can you please add an explanation in the PR of why this patching is necessary ?
Do assistant models interfere with torchscript ?

@jiqing-feng
Copy link
Collaborator Author

jiqing-feng commented Jul 23, 2024

Can you please add an explanation in the PR of why this patching is necessary ? Do assistant models interfere with torchscript ?

Yes. In assisted decoding, both the target model and the assistant model need to call crop_pask_key_values. The patched IPEXModel has a different format of past_key_values, so we need our own crop functions to apply IPEX KV-cache.

Do you want me to write it as a comment in the codes?

@IlyasMoutawwakil
Copy link
Member

Yes. In assisted decoding, both the target model and the assistant model need to call crop_pask_key_values. The patched IPEXModel has a different format of past_key_values, so we need our own crop functions to apply IPEX KV-cache.
Do you want me to write it as a comment in the codes?

Thanks for the explanation ! One last code change and it's good for me 🤗

@jiqing-feng
Copy link
Collaborator Author

jiqing-feng commented Jul 23, 2024

Yes. In assisted decoding, both the target model and the assistant model need to call crop_pask_key_values. The patched IPEXModel has a different format of past_key_values, so we need our own crop functions to apply IPEX KV-cache.
Do you want me to write it as a comment in the codes?

Thanks for the explanation ! One last code change and it's good for me 🤗

I have fixed your required changes. The _unpatch_crop_past_key_values() can be safely added at the end of the generation because it will revert the function crop_pask_key_values if we patch and keep the same if we didn't patch.

BTW, please help to trigger the CI if it's okay for you, thx!

@jiqing-feng
Copy link
Collaborator Author

Hi @echarlaix . I have enabled assisted decoding tests in ipex 2.4. Please take a review, thx~

@jiqing-feng jiqing-feng changed the title support assisted decoding in ipex 2.5 support assisted decoding in ipex 2.4 Sep 4, 2024
optimum/exporters/ipex/model_config.py Outdated Show resolved Hide resolved
optimum/exporters/ipex/model_config.py Outdated Show resolved Hide resolved
optimum/exporters/ipex/model_config.py Show resolved Hide resolved
optimum/intel/ipex/modeling_base.py Outdated Show resolved Hide resolved
Comment on lines 696 to 697
transformers.generation.candidate_generator._crop_past_key_values = _ipex_crop_past_key_values
transformers.generation.utils._crop_past_key_values = _ipex_crop_past_key_values
Copy link
Collaborator

Choose a reason for hiding this comment

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

could this be unpatch after export ?

Copy link
Collaborator Author

@jiqing-feng jiqing-feng Sep 9, 2024

Choose a reason for hiding this comment

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

As we discussed in Teams, this is the only way to enable all assisted decoding cases:

  1. transformers target model + ipex draft model
  2. ipex target model + transformers draft model
  3. ipex target model + ipex draft model

The _crop_past_key_values function is the same level as the model, we cannot do un-patch inside the generate function because it will run after generate, see here.

I have checked the model type inside the _ipex_crop_past_key_values. It only has impact on IPEX model, transformers model will go into the original function, so there is no risk even we don't unpatch.

@jiqing-feng
Copy link
Collaborator Author

Hi @echarlaix . I can do the unpatch for target model only, please look the new changes

@echarlaix echarlaix merged commit 5db1ac7 into huggingface:main Sep 9, 2024
17 checks passed
@jiqing-feng jiqing-feng deleted the assist branch September 10, 2024 02:11
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