Conversation
SD3 and Flux2 generated their initial latent noise with the NumPy RNG, so they did not match torch.randn, which the diffusers reference pipelines use. Three fixes to the torch RNG path: 1. TorchRandomSource.normalArray re-filled the trailing partial block with nextDouble(); PyTorch uses nextFloat(). PyTorch also fills every element with uniforms before applying Box-Muller, then re-fills the last 16 for the remainder. Matching this corrects count=17 and every size that is not a multiple of 16. 2. generateNoise(.torch) used the scalar nextNormal() loop, which yields a different sequence than torch.randn's batch-16 fill. Route it through normalArray. 3. SD3Pipeline and Flux2Pipeline called generateNoise without a sourceType and defaulted to .numPy. Set both to .torch. Adds TorchRandomSource parity tests against torch.randn reference values (seeds 0/42; counts 16/17/32/4096) covering the scalar, batch, boundary, remainder, and realistic-shape cases. Closes apple#151
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SD3 and Flux2 generated their initial latent noise with the NumPy RNG instead of
torch.randn, which the diffusers reference pipelines use. For a given seed the generated image therefore did not match the reference. This routes both pipelines through the torch noise source and correctsTorchRandomSourceso its output matchestorch.randnin float32.Changes
SD3Pipeline/Flux2Pipeline: generate initial noise withsourceType: .torch.TorchRandomSource.normalArray: implement PyTorch'snormal_fill_16fill order and fix the trailing-remainder refill (nextFloat, notnextDouble).generateNoise(.torch): route throughnormalArrayrather than the scalarnextNormal()loop.TorchRandomSourceparity cases againsttorch.randn(seeds 0/42; counts 16/17/32/4096) covering the scalar, batch, boundary, remainder, and realistic-shape paths.Validation
torch.randnis float32-exact (max abs error <= 1e-6, cosine 1.000000) up to the largest pipeline latent count (SD3 262144, Flux2 524288).Closes #151