Skip to content

Fix ignored generator in FlowMatchEulerDiscreteScheduler#13678

Open
RobbinMarcus wants to merge 1 commit intohuggingface:mainfrom
RobbinMarcus:fix-flowmatch-generator
Open

Fix ignored generator in FlowMatchEulerDiscreteScheduler#13678
RobbinMarcus wants to merge 1 commit intohuggingface:mainfrom
RobbinMarcus:fix-flowmatch-generator

Conversation

@RobbinMarcus
Copy link
Copy Markdown

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

1 participant