transformers icon indicating copy to clipboard operation
transformers copied to clipboard

[generate] return past_key_values

Open patil-suraj opened this issue 2 years ago • 11 comments

What does this PR do?

Allows returning past_key_values from generate when use_cache=True.

Like other returned values, past_key_values are also returned as Tuple, one element per generated token.

Fixes #17016

patil-suraj avatar Jun 06 '22 14:06 patil-suraj

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

We'll just need to fix the failing tests now :-) Think you'll have to overwrite this "checking" function in the respective individual test files

patrickvonplaten avatar Jun 08 '22 16:06 patrickvonplaten

Hey there, sorry to nag, but any chance of moving this along? Anything I can do to help?

dblakely avatar Jul 29 '22 18:07 dblakely

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

github-actions[bot] avatar Aug 23 '22 15:08 github-actions[bot]

(@patrickvonplaten @patil-suraj should I take over this PR? :) )

gante avatar Aug 25 '22 10:08 gante

If ok for you @gante this would be amazing!

patrickvonplaten avatar Aug 31 '22 12:08 patrickvonplaten

Hi, Thank you all for working on this feature! Is this going to be merged into the main branch soon?

shunzh avatar Oct 01 '22 04:10 shunzh

@shunzh I haven't started working on it and it's hard to give estimates -- hopefully less than a month :)

gante avatar Oct 03 '22 09:10 gante

Was this closed because it's now possible to retrieve past_key_values or was there another reason?

gilljon avatar Jan 24 '23 05:01 gilljon

@gilljon it is not closed :)

gante avatar Jan 24 '23 10:01 gante

@gante I'm sorry for the confusion! Any idea when it will be merged?

gilljon avatar Jan 24 '23 17:01 gilljon

hi @gante . Any idea when this will be merged? Interested in using it and building something on top of it. I'll happy to put on the finishing touches if needed too!

sijunhe avatar May 05 '23 13:05 sijunhe

Hey! Just a friendly reminder. Any chance to get it merged soon?

slyalin avatar Jun 16 '23 15:06 slyalin

I would absolutely love this feature! This would open up so much for me, because I have prompts like:

prompt = '''
Stuff
* <generate X>
* <generate Y>

Stuff
You said [X], and [Y] previously, now:
* <generate Z>
'''

This is so expensive without past_key_values.

So this PR is now Merge-Conflicting, and I tried applying the patch but upon inspection, it's quite severely out of date now.

Is there another way to accomplish this?

I notice that model.forward typically allows to return past_key_values. But then I... have to make use of a sampling alg myself? Would this be the best way without needing upstream changes, and if so, how can I chain together model.forward and a sampler?

EDIT: IIUC, generation_utils is where model.generate comes from, so the new place to make these edits is: https://github.com/huggingface/transformers/blob/0b192de1f353b0e04dad4813e02e2c672de077be/src/transformers/generation/utils.py#L1301

freckletonj avatar Oct 02 '23 04:10 freckletonj

Is this ticket dead because some other technique exists already for returning and reusing past_key_values? This is a killer feature.

freckletonj avatar Oct 10 '23 21:10 freckletonj

The following PR is more up to date: https://github.com/huggingface/transformers/pull/25086

freckletonj avatar Oct 10 '23 21:10 freckletonj

(deprecated in favor of #25086)

gante avatar Oct 18 '23 09:10 gante

Hey folks 👋

#25086 was merged.

If you install from main and add return_dict_in_generate=True to generate, past_key_values will be part of the output, assuming your model is configured with use_cache=True (the default).

You can then pass past_key_values to generate to continue generating!

gante avatar Nov 02 '23 15:11 gante

I cant get it to work with intel neural_chat what vartion was this on?

nevakrien avatar Nov 30 '23 13:11 nevakrien