Skip to content

Enhancement: avoid computing full mask (+ adding types)#101

Merged
Saibo-creator merged 4 commits into
epfl-dlab:mainfrom
nathanrchn:enhancement/avoid-computing-full-mask
Oct 22, 2024
Merged

Enhancement: avoid computing full mask (+ adding types)#101
Saibo-creator merged 4 commits into
epfl-dlab:mainfrom
nathanrchn:enhancement/avoid-computing-full-mask

Conversation

@nathanrchn

Copy link
Copy Markdown
Contributor

While running a generation using the CLI on the student JSON grammar with the Phi-3.5-mini-4bit model, I observed that the majority of tokens were already valid without applying a mask.

Token Generation Screenshot

As evident in the screenshot above, the green tokens were sampled without constraints and were already valid.

I implemented the following logic in the mask_logits method:

  1. Compute the argmax of the logits to determine the sampled token (with temperature set to 0). This operation has negligible computational cost (approximately 1e-5 seconds).
  2. Evaluate the validity of this token.
  3. If valid, return this token directly.
  4. If invalid, compute the entire mask.

This minor modification resulted in a significant speed increase of 3 tokens per second.

Optimized Token Generation

As shown in the second screenshot, the impact of the logits_processor has been substantially reduced. It's worth noting that this improvement becomes more pronounced as the vocabulary size increases.

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

Super nice !

@Saibo-creator

Copy link
Copy Markdown
Collaborator

@nathanrchn This looks great. I think we could provide user two execution mode: speculation and full_mask

In some cases, full_mask does has its own advantage like it allows user to measure how many probablity mass is distributed on the allowed tokens VS non-allowed tokens etc.

Once this is done, I'm ready to merge it !

@x0wllaar

x0wllaar commented Oct 18, 2024

Copy link
Copy Markdown
Contributor

Won't this lead to the generation becoming deterministic (greedy decoding essentially) when the top token is valid? This will lead to e.g. strings in JSON (where almost all tokens are valid) almost aways being generated deterministically in this mode.

@nathanrchn

Copy link
Copy Markdown
Contributor Author

Of course these changes works only when temperature = 0 (which is already deterministic).

@Saibo-creator
Saibo-creator merged commit a070180 into epfl-dlab:main Oct 22, 2024
@Saibo-creator

Copy link
Copy Markdown
Collaborator

@nathanrchn Thank you again for the amazing work ;)

@nathanrchn
nathanrchn deleted the enhancement/avoid-computing-full-mask branch October 22, 2024 07:10
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.

3 participants