Skip to content

Fix ignored generator in FlowMatchEulerDiscreteScheduler#13678

Merged
yiyixuxu merged 1 commit into
huggingface:mainfrom
RobbinMarcus:fix-flowmatch-generator
May 5, 2026
Merged

Fix ignored generator in FlowMatchEulerDiscreteScheduler#13678
yiyixuxu merged 1 commit into
huggingface:mainfrom
RobbinMarcus:fix-flowmatch-generator

Conversation

@RobbinMarcus
Copy link
Copy Markdown
Contributor

What does this PR do?

FlowMatchEulerDiscreteScheduler.step() accepts a generator argument, but when stochastic_sampling=True it calls plain torch.randn_like(sample) instead of using the generator. This makes results non-deterministic even with a fixed seed.

This PR replaces torch.randn_like call with the existing randn_tensor(..., generator=generator, device=sample.device, dtype=sample.dtype) so the scheduler respects the passed generator in the stochastic sampling path, consistent with how other schedulers in diffusers handle randomness.

I'm using z-image with euler_ancestral which gave me inconsistent results, which led me to to fixing it.

Simple reproduce script

import torch
from diffusers import FlowMatchEulerDiscreteScheduler
scheduler = FlowMatchEulerDiscreteScheduler(stochastic_sampling=True)
scheduler.set_timesteps(10, device="cpu")
sample = torch.zeros(1, 4, 64, 64)
model_output = torch.ones(1, 4, 64, 64) * 0.5

# First run
generator = torch.Generator().manual_seed(42)
out1 = scheduler.step(model_output, scheduler.timesteps[0], sample, generator=generator).prev_sample

# Second run with same seed — must reset scheduler to reset step_index
scheduler.set_timesteps(10, device="cpu")
generator = torch.Generator().manual_seed(42)
out2 = scheduler.step(model_output, scheduler.timesteps[0], sample, generator=generator).prev_sample

# Before this fix: out1 != out2  (torch.randn_like ignores generator)
# After this fix:  out1 == out2
print("Same seed produces identical results:", torch.allclose(out1, out2))

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed.

Core library:

(Sorry for the spam ping re-do, it looks like github overwrote main with a previous repository I had hosted under the same name.. This time it's from a branch in my fork)

@github-actions github-actions Bot added schedulers size/S PR with diff < 50 LOC labels May 4, 2026
@RobbinMarcus RobbinMarcus deleted the fix-flowmatch-generator branch May 4, 2026 14:06
@RobbinMarcus RobbinMarcus restored the fix-flowmatch-generator branch May 4, 2026 19:13
@RobbinMarcus
Copy link
Copy Markdown
Contributor Author

RobbinMarcus commented May 4, 2026

It seems github doesn't like I had a previous repository with the same name and keeps overriding it? It's closing this automatically for some reason.

Edit: skill-issue. I had a push-mirror that overwrote the whole repo. All good now.

@RobbinMarcus RobbinMarcus reopened this May 4, 2026
@github-actions github-actions Bot added size/S PR with diff < 50 LOC and removed size/S PR with diff < 50 LOC labels May 4, 2026
Copy link
Copy Markdown
Collaborator

@yiyixuxu yiyixuxu left a comment

Choose a reason for hiding this comment

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

thanks!

@yiyixuxu
Copy link
Copy Markdown
Collaborator

yiyixuxu commented May 4, 2026

note: this may cause the output of a previous LTX checkpoint to change

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

@yiyixuxu yiyixuxu merged commit dcbb18a into huggingface:main May 5, 2026
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

schedulers size/S PR with diff < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants