Skip to content

Conversation

@zhangshuibai
Copy link

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.py does 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

  • Added remasking-based decoding to ThresholdParallelDecoder in
    python/dinfer/decoding/parallel_strategy.py
  • Introduced a new optional parameter: enable_remask
    • When enabled, previously unmasked tokens can be re-masked and re-decoded in later steps
    • Default behavior remains unchanged when enable_remask is not set
    • Existing workflows continue to work without modification
  • Added an example evaluation script:
    • evaluations/eval_llada_mini_remask.sh
    • Demonstrates usage with remasking enabled
    • The only additional argument is enable_remask

2. Tensor Dimension Bug Fix

  • Fixed an incorrect slicing operation in:
    • python/dinfer/decoding/utils.py
  • The original implementation dropped the batch dimension when slicing self.x
  • Updated the slicing logic to preserve the batch dimension, preventing downstream shape mismatches

Usage

To enable remasking-based decoding, simply set:

enable_remask=true

Experimental Results

Experiments were conducted using inclusionAI/LLaDA2.0-mini-preview with the following settings:

  • Block size: 32
  • Threshold: 0.98
  • Remasking: enabled (enable_remask = true)

Results

Dataset Temperature = 0.0 Temperature = 0.8
MBPP 0.7611 0.7658
GSM8K 0.8810 0.8749

@gemini-code-assist
Copy link

Summary of Changes

Hello @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

  • Remasking-Based Decoding: Introduced support for remasking-based decoding in ThresholdParallelDecoder, allowing previously unmasked tokens to be re-masked and re-decoded for improved global consistency. This feature is controlled by a new enable_remask parameter.
  • Tensor Slicing Bug Fix: Resolved a critical bug in python/dinfer/decoding/utils.py where a tensor slicing operation incorrectly dropped the batch dimension, preventing downstream shape mismatches.
  • Backward Compatibility: The remasking feature is fully backward compatible; existing workflows continue to function without modification when enable_remask is not explicitly set.
  • Example Script: Added a new evaluation script, evaluations/eval_llada_mini_remask.sh, to demonstrate the usage and configuration of the remasking-based decoding.

🧠 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 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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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 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

  1. 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.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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

Choose a reason for hiding this comment

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

critical

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.

Suggested change
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}

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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)

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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)

Comment on lines +539 to +566
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)

Choose a reason for hiding this comment

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

medium

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.

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.

1 participant