Conversation
There was a problem hiding this comment.
Hello @flyinglandlord, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
Hello team, Gemini here with a summary of this pull request. The primary goal of this PR, as indicated by the title and description, is to fix a bug in the XGrammar constrained mode backend that was causing the generation of illegal tokens. This fix involves ensuring that only valid tokens within the model's vocabulary are considered during the decoding process, specifically by masking out logits corresponding to token IDs outside the vocabulary range.
Highlights
- Bug Fix: Illegal Tokens: The main highlight is the fix for a bug where the XGrammar backend could generate tokens that are not part of the valid vocabulary, likely padding tokens or similar artifacts appearing in the logits tensor.
- Logit Masking: To address the illegal token issue, the PR introduces logic to explicitly mask out (set to a very low value) the logits for token IDs that are greater than or equal to the vocabulary size.
- Tokenizer Info Access: A minor change involves ensuring the
tokenizer_infoobject is stored and accessed as a class attribute (self.tokenizer_info) for consistency.
Changelog
- lightllm/server/router/model_infer/mode_backend/chunked_prefill/impl_for_xgrammar_mode.py
- Corrected the assignment and usage of
tokenizer_infoto be a class attribute (self.tokenizer_info) on lines 30, 31, and 32. - Added logic on line 65 to mask out logits for token IDs beyond the vocabulary size in the
decodemethod's main loop. - Added similar logic on line 99 to mask out logits for token IDs beyond the vocabulary size within the batch processing loop in the
decodemethod.
- Corrected the assignment and usage of
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Grammar guides the way,
But sometimes tokens go astray,
Masking logits right,
Brings order to the light,
Ensuring valid words hold sway.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request aims to fix a bug where the XGrammar backend might generate illegal tokens. The core change involves masking out logits for padding tokens, which directly addresses the described issue and seems like a solid improvement for correctness.
The refactoring of tokenizer_info to be an instance variable (self.tokenizer_info) is also a good change that improves code structure and makes tokenizer_info.vocab_size readily available where needed.
I have a couple of suggestions regarding the use of a magic number for masking logits, which would enhance maintainability.
Summary of Findings
- Magic Number Usage: The numeric literal
-1000000.0is used multiple times (lines 65, 99, and implicitly related to lines 63, 97) for masking logits. Introducing a named constant would improve code clarity and ease of maintenance.
Merge Readiness
The pull request introduces a crucial fix for preventing the generation of illegal tokens. The primary logic seems sound. However, there are a couple of medium severity suggestions regarding the use of magic numbers that should be considered for improved code quality and maintainability.
I recommend addressing these points before merging. As an AI reviewer, I am not authorized to approve pull requests; please ensure further review and approval from other team members.
|
|
||
| logits[logits == float("-inf")] = -1000000.0 | ||
| # mask out the padding token logits | ||
| logits[:, self.tokenizer_info.vocab_size :] = -1000000.0 |
There was a problem hiding this comment.
The literal value -1000000.0 is used here to mask padding token logits. For improved readability and maintainability, would it be beneficial to define this as a named constant at the module or class level (e.g., _LOGIT_MASK_VALUE = -1000000.0)?
This constant could then be used consistently in this spot, as well as on line 63 where -inf values are replaced. For example:
# At module or class level (e.g., near the top of the file or class definition)
_LOGIT_MASK_VALUE = -1000000.0
class XgrammarBackend(ChunkedPrefillBackend):
# ... other code ...
def decode(self):
# ...
if decode_reqs:
# ...
logits[logits == float("-inf")] = _LOGIT_MASK_VALUE
# mask out the padding token logits
logits[:, self.tokenizer_info.vocab_size :] = _LOGIT_MASK_VALUE
# ...| logits[:, self.tokenizer_info.vocab_size :] = -1000000.0 | |
| logits[:, self.tokenizer_info.vocab_size :] = _LOGIT_MASK_VALUE # Assuming _LOGIT_MASK_VALUE is defined as suggested |
| # fix the logics with -inf to a large negative value | ||
| logits[logits == float("-inf")] = -1000000.0 | ||
| # mask out the padding token logits | ||
| logits[:, self.tokenizer_info.vocab_size :] = -1000000.0 |
There was a problem hiding this comment.
Similar to the point raised for line 65, the literal value -1000000.0 is used here. Adopting a shared named constant (e.g., _LOGIT_MASK_VALUE = -1000000.0, as suggested for the earlier part of the method) would enhance consistency and maintainability. This constant could also be applied to line 97 for replacing -inf values.
| logits[:, self.tokenizer_info.vocab_size :] = -1000000.0 | |
| logits[:, self.tokenizer_info.vocab_size :] = _LOGIT_MASK_VALUE # Assuming _LOGIT_MASK_VALUE is defined as suggested |
Fix the bug that XGrammar backend will generate illegal tokens.