Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 87341f0

Browse files
committed
DDPMScheduler AddNoise function fixed to support Img2Img
1 parent 776f152 commit 87341f0

File tree

4 files changed

+12
-36
lines changed

4 files changed

+12
-36
lines changed

OnnxStack.StableDiffusion/Config/SchedulerOptions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using NumSharp;
2-
using OnnxStack.StableDiffusion.Enums;
1+
using OnnxStack.StableDiffusion.Enums;
32
using System.Collections.Generic;
4-
using System.Reflection.Metadata;
53

64
namespace OnnxStack.StableDiffusion.Config
75
{
@@ -47,7 +45,6 @@ public class SchedulerOptions
4745
/// </value>
4846
public float GuidanceScale { get; set; } = 7.5f;
4947

50-
5148
/// <summary>
5249
/// Gets or sets the strength use for Image 2 Image
5350
/// </summary>

OnnxStack.StableDiffusion/Schedulers/DDPMScheduler.cs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -246,36 +246,15 @@ public override DenseTensor<float> Step(DenseTensor<float> modelOutput, int time
246246
/// <returns></returns>
247247
public override DenseTensor<float> AddNoise(DenseTensor<float> originalSamples, DenseTensor<float> noise, IReadOnlyList<int> timesteps)
248248
{
249-
//TODO: https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L456
250-
251-
// Make sure alphas_cumprod and timestep have the same device and dtype as originalSamples
252-
var alphasCumprod = new DenseTensor<float>(_alphasCumulativeProducts.ToArray(), new int[] { _alphasCumulativeProducts.Count });// Convert to DenseTensor
253-
254-
var sqrtAlphaProd = new DenseTensor<float>(originalSamples.Dimensions);
255-
var sqrtOneMinusAlphaProd = new DenseTensor<float>(originalSamples.Dimensions);
256-
257-
for (int i = 0; i < timesteps.Count; i++)
258-
{
259-
int timestep = timesteps[i];
260-
float alphaProd = alphasCumprod[timestep]; // Assuming alphasCumprod is a 2D tensor
261-
float sqrtAlpha = (float)Math.Sqrt(alphaProd);
262-
float sqrtOneMinusAlpha = (float)Math.Sqrt(1.0f - alphaProd);
263-
264-
sqrtAlphaProd.SetValue(i, sqrtAlpha);
265-
sqrtOneMinusAlphaProd.SetValue(i, sqrtOneMinusAlpha);
266-
}
267-
268-
// Reshape sqrtAlphaProd and sqrtOneMinusAlphaProd to match the shape of originalSamples
269-
sqrtAlphaProd = sqrtAlphaProd.Reshape(originalSamples.Dimensions).ToDenseTensor();
270-
sqrtOneMinusAlphaProd = sqrtOneMinusAlphaProd.Reshape(originalSamples.Dimensions).ToDenseTensor();
271-
272-
// Compute noisy samples
273-
var noisySamples = new DenseTensor<float>(originalSamples.Dimensions);
274-
for (int i = 0; i < originalSamples.Length; i++)
275-
{
276-
noisySamples.SetValue(i, (noise.GetValue(i) * sqrtOneMinusAlphaProd.GetValue(i)) + (originalSamples.GetValue(i) * sqrtAlphaProd.GetValue(i)));
277-
}
278-
return noisySamples;
249+
// Ref: https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L456
250+
int timestep = timesteps[0];
251+
float alphaProd = _alphasCumulativeProducts[timestep];
252+
float sqrtAlpha = (float)Math.Sqrt(alphaProd);
253+
float sqrtOneMinusAlpha = (float)Math.Sqrt(1.0f - alphaProd);
254+
255+
return noise
256+
.MultipleTensorByFloat(sqrtOneMinusAlpha)
257+
.AddTensors(originalSamples.MultipleTensorByFloat(sqrtAlpha));
279258
}
280259

281260
/// <summary>

OnnxStack.StableDiffusion/Schedulers/EulerAncestralScheduler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public override DenseTensor<float> Step(DenseTensor<float> modelOutput, int time
207207
/// <returns></returns>
208208
public override DenseTensor<float> AddNoise(DenseTensor<float> originalSamples, DenseTensor<float> noise, IReadOnlyList<int> timesteps)
209209
{
210-
// TODO: https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_euler_ancestral_discrete.py#L389
210+
// Ref: https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_euler_ancestral_discrete.py#L389
211211
var sigma = timesteps
212212
.Select(x => Timesteps.IndexOf(x))
213213
.Select(x => _sigmas[x])

OnnxStack.StableDiffusion/Schedulers/LMSScheduler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public override DenseTensor<float> Step(DenseTensor<float> modelOutput, int time
227227
/// <returns></returns>
228228
public override DenseTensor<float> AddNoise(DenseTensor<float> originalSamples, DenseTensor<float> noise, IReadOnlyList<int> timesteps)
229229
{
230-
// TODO: https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_lms_discrete.py#L439
230+
// Ref: https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_lms_discrete.py#L439
231231
var sigma = timesteps
232232
.Select(x => Timesteps.IndexOf(x))
233233
.Select(x => _sigmas[x])

0 commit comments

Comments
 (0)