-
Notifications
You must be signed in to change notification settings - Fork 36
Feature/threshold remasking #30
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
base: master
Are you sure you want to change the base?
Feature/threshold remasking #30
Conversation
Summary of ChangesHello @zhangshuibai, 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! This pull request significantly enhances dInfer's parallel decoding framework by integrating remasking-based decoding, a strategy that allows the model to refine earlier predictions and achieve better overall consistency. Concurrently, it addresses and rectifies a crucial tensor slicing bug within the decoding utilities, ensuring correct tensor dimensionality is maintained throughout the process. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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 by creating a comment using either
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 Limitations & Feedback Gemini Code Assist 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. 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. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces remasking-based decoding, a significant feature enhancement, and fixes a tensor slicing bug. The core logic for remasking appears robust, particularly the safeguard to ensure decoding progress. My review has identified a critical bug in the new evaluation script, a minor issue in another evaluation script, and opportunities for code improvement regarding consistency and maintainability in the main strategy file. Addressing these points will further strengthen the quality of this contribution.
| enable_remask=True # enable remasking for threshold decoder | ||
| # for llada 1.5 use tasks gsm8k_llada1.5 mbpp_sanitized_llada1.5 | ||
| # for llada2_mini use tasks gsm8k_llada_mini mbpp_sanitized_llada_mini | ||
| if [ parallel=='tp' ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The if condition has incorrect syntax for a string comparison in bash. The expression [ parallel=='tp' ] will always evaluate to true because parallel=='tp' is treated as a non-empty string. This will cause the script to run even when parallel is not 'tp'. You should use "$parallel" == "tp" for string comparison.
| if [ parallel=='tp' ]; then | |
| if [ "$parallel" == 'tp' ]; then |
| answers = [] | ||
| gpus = [int(gpu) for gpu in self.gpus.split(';')] | ||
| args = {"gpu": gpus, "batch_size": self.batch_size, "model_name": self.model_path, "gen_len": self.gen_length, "block_length": self.block_length, "prefix_look": self.prefix_look, "after_look": self.after_look, "warmup_times": self.warmup_times, "low_threshold": self.low_threshold, "threshold": self.threshold, "cont_weight": self.cont_weight, "use_credit": self.use_credit, "cache": self.cache, "parallel_decoding": self.parallel_decoding, "tp_size": self.tp_size, "save_path": self.save_path, "use_cudagraph": self.use_cudagraph, "use_compile": self.use_compile,"use_bd": self.use_bd, "use_shift": self.use_shift, "model_type": self.model_type, "vocab_size": self.vocab_size, "batch_size": self.batch_size, "speed_path": self.speed_path} | ||
| args = {"gpu": gpus, "batch_size": self.batch_size, "model_name": self.model_path, "gen_len": self.gen_length, "block_length": self.block_length, "prefix_look": self.prefix_look, "after_look": self.after_look, "warmup_times": self.warmup_times, "low_threshold": self.low_threshold, "threshold": self.threshold, "cont_weight": self.cont_weight, "use_credit": self.use_credit, "cache": self.cache, "parallel_decoding": self.parallel_decoding, "tp_size": self.tp_size, "save_path": self.save_path, "use_cudagraph": self.use_cudagraph, "use_compile": self.use_compile,"use_bd": self.use_bd, "use_shift": self.use_shift, "model_type": self.model_type, "vocab_size": self.vocab_size, "batch_size": self.batch_size, "speed_path": self.speed_path, "enable_remask": self.enable_remask} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The args dictionary has a duplicate key for "batch_size". While it doesn't cause a functional issue in this case as both instances use the same value (self.batch_size), it makes the code confusing and prone to errors in the future. The redundant entry should be removed.
| args = {"gpu": gpus, "batch_size": self.batch_size, "model_name": self.model_path, "gen_len": self.gen_length, "block_length": self.block_length, "prefix_look": self.prefix_look, "after_look": self.after_look, "warmup_times": self.warmup_times, "low_threshold": self.low_threshold, "threshold": self.threshold, "cont_weight": self.cont_weight, "use_credit": self.use_credit, "cache": self.cache, "parallel_decoding": self.parallel_decoding, "tp_size": self.tp_size, "save_path": self.save_path, "use_cudagraph": self.use_cudagraph, "use_compile": self.use_compile,"use_bd": self.use_bd, "use_shift": self.use_shift, "model_type": self.model_type, "vocab_size": self.vocab_size, "batch_size": self.batch_size, "speed_path": self.speed_path, "enable_remask": self.enable_remask} | |
| args = {"gpu": gpus, "model_name": self.model_path, "gen_len": self.gen_length, "block_length": self.block_length, "prefix_look": self.prefix_look, "after_look": self.after_look, "warmup_times": self.warmup_times, "low_threshold": self.low_threshold, "threshold": self.threshold, "cont_weight": self.cont_weight, "use_credit": self.use_credit, "cache": self.cache, "parallel_decoding": self.parallel_decoding, "tp_size": self.tp_size, "save_path": self.save_path, "use_cudagraph": self.use_cudagraph, "use_compile": self.use_compile,"use_bd": self.use_bd, "use_shift": self.use_shift, "model_type": self.model_type, "vocab_size": self.vocab_size, "batch_size": self.batch_size, "speed_path": self.speed_path, "enable_remask": self.enable_remask} |
| if fix_mask is not None: | ||
| # fix_mask should have shape [B, L] matching unified_confidence | ||
| # Set fixed positions to +inf so they won't be selected for remasking (highest confidence) | ||
| unified_confidence = torch.where(fix_mask, torch.full_like(unified_confidence, float('inf')), unified_confidence) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an inconsistent use of float('inf') here, while torch.inf is used elsewhere in this function (e.g., line 476). For tensor operations, it's best practice to use torch.inf for consistency and to ensure proper handling across different devices and data types.
| unified_confidence = torch.where(fix_mask, torch.full_like(unified_confidence, float('inf')), unified_confidence) | |
| unified_confidence = torch.where(fix_mask, torch.full_like(unified_confidence, torch.inf), unified_confidence) |
| if self.enable_remask: | ||
| # Get fix_mask for current block (protect prompt tokens from remasking) | ||
| fix_mask = getattr(self, 'fix_mask', None) | ||
| x0, transfer_index = get_transfer_index_threshold_remask( | ||
| logits, | ||
| self.temperature, | ||
| mask_index, | ||
| curr_x, | ||
| self.mask_id, | ||
| threshold=iter_threshold, | ||
| use_float64=self.use_float64, | ||
| fix_mask=fix_mask, | ||
| ) | ||
| else: | ||
| x0, transfer_index = get_transfer_index_threshold( | ||
| logits, | ||
| self.temperature, | ||
| mask_index, | ||
| curr_x, | ||
| self.mask_id, | ||
| threshold=iter_threshold, | ||
| use_float64=self.use_float64, | ||
| ) | ||
|
|
||
| # For remasking case, transfer_index may include remasked positions | ||
| # For non-remasking case, only update masked positions | ||
| if not self.enable_remask: | ||
| transfer_index = torch.logical_and(transfer_index, mask_index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to select the decoding function (get_transfer_index_threshold_remask or get_transfer_index_threshold) and to conditionally process the transfer_index is duplicated in both the decode and batch_decode methods. This duplication increases maintenance overhead and the risk of introducing inconsistencies.
Consider refactoring this shared logic into a private helper method within the ThresholdParallelDecoder class. This would centralize the decision-making and improve code readability and maintainability.
For example, you could create a helper like this:
def _get_x0_and_transfer_index(self, logits, mask_index, curr_x, iter_threshold):
if self.enable_remask:
fix_mask = getattr(self, 'fix_mask', None)
return get_transfer_index_threshold_remask(
logits,
self.temperature,
mask_index,
curr_x,
self.mask_id,
threshold=iter_threshold,
use_float64=self.use_float64,
fix_mask=fix_mask,
)
else:
return get_transfer_index_threshold(
logits,
self.temperature,
mask_index,
curr_x,
self.mask_id,
threshold=iter_threshold,
use_float64=self.use_float64,
)Then, both decode and batch_decode can call this helper to simplify their implementations.
Summary
This PR adds Remasking-Based Decoding support to dInfer’s parallel decoding framework and fixes a tensor slicing bug in the decoding utilities.
Remasking-based decoding is a commonly used strategy during decoding and has been introduced and adopted in several recent works:
In this PR, remasking specifically refers to re-applying the mask to tokens that have already been unmasked in previous decoding steps, and re-decoding them in subsequent steps. This allows the model to revise earlier predictions and improve global consistency during parallel decoding.
The current implementation in
python/dinfer/decoding/parallel_strategy.pydoes not support this behavior. This PR extends the decoder to enable remasking while remaining fully backward compatible.What’s Changed
1. Remasking-Based Decoding Support
ThresholdParallelDecoderinpython/dinfer/decoding/parallel_strategy.pyenable_remaskenable_remaskis not setevaluations/eval_llada_mini_remask.shenable_remask2. Tensor Dimension Bug Fix
python/dinfer/decoding/utils.pyself.xUsage
To enable remasking-based decoding, simply set:
Experimental Results
Experiments were conducted using inclusionAI/LLaDA2.0-mini-preview with the following settings:
enable_remask = true)Results