Refactor Discrete Diffusion Pipelines and Schedulers to use Common Conventions by dg845 · Pull Request #14741 · huggingface/diffusers · GitHub
Skip to content

Refactor Discrete Diffusion Pipelines and Schedulers to use Common Conventions - #14741

Open
dg845 wants to merge 3 commits into
mainfrom
discrete-diffusion-refactor
Open

dg845 wants to merge 3 commits into
mainfrom
discrete-diffusion-refactor

Conversation

@dg845

@dg845 dg845 commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR refactors the existing discrete diffusion pipelines LLaDA2Pipeline and DiffusionGemmaPipeline and schedulers with the goal of establishing common conventions among discrete diffusion pipelines, which should make it easier to integrate and review discrete diffusion models, and making discrete schedulers interchangeable between pipelines.

The key design principles are as follows:

  1. Discrete scheduler timesteps are in $(0, 1]$ and follow the diffusers decreasing timestep convention. This matches the usual timestep convention in the discrete diffusion literature, but is generally different from continuous schedulers in that num_train_timesteps is not defined and thus timesteps are not scaled by it (so here timesteps are more analogous to sigmas in e.g. FlowMatchEulerDiscreteScheduler). The motivation is for discrete pipelines to use consistent denoising loop logic.
  2. Common discrete scheduler outputs are defined in DiscreteSchedulerOutput. The goal here is to give standard names to concepts such as "the (possibly shaped) distribution the tokens are actually sampled from" (pred_logits), which should help with scheduler interchangeability.
  3. Discrete schedulers own logit shaping logic. This includes temperature, top-$k$/top-$p$, etc.
  4. Discrete pipelines own loop control (e.g. early stopping). The motivation is to make it easier to reuse schedulers, since they will not have to re-implement loop control methods (and this is more naturally a pipeline concept rather than a scheduler concept). An example is the check_eos_finished and check_block_should_continue methods which are currently in BlockRefinementScheduler, which have been refactored into the LLaDA 2 pipeline.
  5. Discrete schedulers have a common API. This PR gives discrete scheduler methods such as set_timesteps, step, etc. a common signature so that schedulers are easily interoperable (without needing e.g. reflection via inspect.signature).
Target Discrete Scheduler API
def set_timesteps(self, num_inference_steps: int, device: str | torch.device | None = None) -> None: ...

def step(
    self,
    model_output: torch.Tensor,          # logits, already shaped by the pipeline
    timestep: float | torch.Tensor,      # continuous t in [0, 1], decreasing; 0 = clean, 1 = noise
    sample: torch.LongTensor,
    *,
    generator: torch.Generator | None = None,
    return_dict: bool = True,
) -> DiscreteSchedulerOutput | tuple: ...

def add_noise(
    self,
    original_samples: torch.LongTensor,
    timesteps: float | torch.Tensor,
    *,
    generator: torch.Generator | None = None,
) -> tuple[torch.LongTensor, torch.BoolTensor]: ...   # (noisy, mask)

Some other notable changes are as follows:

  1. BlockRefinementScheduler has been split into a masked scheduler (BlockRefinementScheduler) and a uniform scheduler (UniformRefinementScheduler). The latter is now used by DiffusionGemmaPipeline.
  2. Deprecation logic has been added for the modified logic in LLaDA2Pipeline and BlockRefinementScheduler.
  3. sampled_probs now has a unified definition as the unmodified denoiser logit distribution, whereas it used to mean different things in different schedulers.
  4. Because sampling knobs like temperature have been moved to scheduler configs, the default pipeline behavior with a bare scheduler has changed.

This should help with #12911 by giving the discrete diffusion pipelines there a common set of conventions to aim at.

EDIT: will need to be be reconciled with #14661 when that PR is merged.

Before submitting

  • Did you use an AI agent (Claude Code, Codex, Cursor, etc.) to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • 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?
  • Are you the author (or part of the team) of the model/pipeline (only applicable for model/pipeline related PRs)?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@yiyixuxu
@sayakpaul
@kashif

Discrete (token-space) diffusion had grown three schedulers with three
different step signatures, three different return types, and pipelines that
reached into scheduler internals or dispatched on `inspect.signature`. That
made a scheduler unusable outside the one pipeline it shipped with.

This gives them one contract, so that a single denoising loop drives any of
them and a new discrete model only has to add a scheduler:

    scheduler.set_timesteps(num_inference_steps, device=device)
    for t in scheduler.timesteps:
        sample = scheduler.step(logits, t, sample).prev_sample

- Add `DiscreteSchedulerOutput` in `scheduling_utils`, replacing the
  per-scheduler `DiscreteDDIMSchedulerOutput` / `EntropyBoundSchedulerOutput`.
  It carries `prev_sample`, `pred_original_sample`, `sampled_probs`,
  `pred_logits`, `committed_mask` and the optional `edited_mask`.
- Give every discrete scheduler the same timestep grid, `step_index` /
  `begin_index` bookkeeping, and `step(model_output, timestep, sample)` order.
- Move sampling knobs (temperature, top-k, top-p, thresholds) out of pipeline
  `__call__` and into the scheduler config, where they belong; the pipeline
  arguments are deprecated for 1.0.0 and still honored meanwhile.
- Split the uniform-corruption process out of `BlockRefinementScheduler` into
  `UniformRefinementScheduler`, which is what `DiffusionGemmaPipeline` uses.
- Drop the `inspect.signature` dispatch from both pipelines and make the
  denoising loops explicit.
- Add `tests/schedulers/test_discrete_scheduler_contract.py`, which
  parameterizes the contract over every discrete scheduler.

Two intended behavior changes: `sampled_probs` is now always taken from the
unmodified denoiser distribution (so a confidence threshold means the same
thing across schedulers and temperatures), and editing is gated on
`editing_threshold` being set rather than on the pipeline's arguments.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added size/L PR with diff > 200 LOC documentation Improvements or additions to documentation tests utils pipelines examples schedulers and removed size/L PR with diff > 200 LOC labels Sep 8, 2026
@dg845

dg845 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@sayakpaul
sayakpaul requested a review from kashif September 9, 2026 05:14
@github-actions github-actions Bot added the size/L PR with diff > 200 LOC label Sep 11, 2026
@dg845
dg845 requested review from sayakpaul and yiyixuxu September 11, 2026 23:09
@github-actions

github-actions Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

@sayakpaul sayakpaul added the no-issue-needed for PRs that do not require link to an issue label Sep 17, 2026
@sayakpaul
sayakpaul requested a review from DN6 September 17, 2026 07:45

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation examples no-issue-needed for PRs that do not require link to an issue pipelines schedulers size/L PR with diff > 200 LOC tests utils

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants