Skip to content

Refine the code of Universal Assisted Generation - #34823

Merged
ArthurZucker merged 8 commits into
huggingface:mainfrom
xinpengzz:refine-UAG
Nov 28, 2024
Merged

Refine the code of Universal Assisted Generation#34823
ArthurZucker merged 8 commits into
huggingface:mainfrom
xinpengzz:refine-UAG

Conversation

@xinpengzz

@xinpengzz xinpengzz commented Nov 20, 2024

Copy link
Copy Markdown
Contributor

What does this PR do?

co-Authors: @WenjingKangIntel

Universal Assisted Generation(UAG) allowed the assisted generation with any assistant model regardless of its tokenizer.
This work is awesome and makes the different models work together possible !

This PR is to refine its code.
The code of the Universal Assisted Generation (UAG) contains some useless attributes and some processes that need improvement.

I have contacted with the @danielkorat, the one of authors of UAG, about it, and he suggested me to create a pull request to discuss it conveniently and detailedly.

Code changes:

  • removed the useless attributes: self.prev_tokens, self.prev_target_ids.
  • added a judge statement when the start_assistant_look_index < 0.

Todo:

  • the size of window (the number of past tokens) is an experience value. Perhaps this value could be set to be adjustable, or dynamic

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?

@danielkorat
@gante
and anyone interested in the program 😄

@danielkorat

Copy link
Copy Markdown
Contributor

cc @mosheber

@xinpengzz

Copy link
Copy Markdown
Contributor Author

The usage:

from transformers import AutoModelForCausalLM, AutoTokenizer

prompt = "Alice and Bob"
checkpoint = "google/gemma-2-9b"
assistant_checkpoint = "double7/vicuna-68m"

assistant_tokenizer = AutoTokenizer.from_pretrained(assistant_checkpoint)
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
inputs = tokenizer(prompt, return_tensors="pt")

model = AutoModelForCausalLM.from_pretrained(checkpoint)
assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint)

# assistant_model.generation_config.assistant_lookbehind = 10 # the default value
# assistant_model.generation_config.target_lookbehind = 10 # the default value

outputs = model.generate(**inputs, assistant_model=assistant_model, tokenizer=tokenizer, assistant_tokenizer=assistant_tokenizer)
tokenizer.batch_decode(outputs, skip_special_tokens=True)

@xinpengzz
xinpengzz marked this pull request as ready for review November 22, 2024 02:58
@danielkorat

Copy link
Copy Markdown
Contributor

Hi @ArthurZucker @zucchini-nlp 👋

Comment thread src/transformers/generation/configuration_utils.py
Comment thread src/transformers/generation/candidate_generator.py

@zucchini-nlp zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@xinpengzz

Copy link
Copy Markdown
Contributor Author

I have tested these changes on 2000 samples in the alpaca-cleaned dataset, and the output is identical to those without assistant decoding. main model is Qwen1.5-0.5B and draft model is vicuna-68m.

@xinpengzz

Copy link
Copy Markdown
Contributor Author

Hi @ArthurZucker 👋 , Would it be convenient to review the code?

@ArthurZucker

Copy link
Copy Markdown
Collaborator

Hey yeah sorry reviewing now!

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice and simple, thanks for adding the arg descriptions! 🤗

@ArthurZucker
ArthurZucker merged commit 44af935 into huggingface:main Nov 28, 2024
BernardZach pushed a commit to BernardZach/transformers that referenced this pull request Dec 5, 2024
* removed the useless attritbutes

* add configs for window size

* fixed the wrong kwargs

* added docstring
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