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

Commit 7ac3b93

Browse files
committed
Fix LCM Guidance Scale
1 parent 8df2118 commit 7ac3b93

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

OnnxStack.StableDiffusion/Diffusers/LatentConsistency/LatentConsistencyDiffuser.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using OnnxStack.StableDiffusion.Common;
99
using OnnxStack.StableDiffusion.Config;
1010
using OnnxStack.StableDiffusion.Enums;
11+
using OnnxStack.StableDiffusion.Helpers;
1112
using OnnxStack.StableDiffusion.Models;
1213
using OnnxStack.StableDiffusion.Schedulers.LatentConsistency;
1314
using System;
@@ -176,21 +177,16 @@ protected override IScheduler GetScheduler(SchedulerOptions options)
176177
/// <returns></returns>
177178
protected DenseTensor<float> GetGuidanceScaleEmbedding(float guidance, int embeddingDim = 256)
178179
{
179-
var scale = guidance - 1f;
180+
var scale = (guidance - 1f) * 1000.0f;
180181
var halfDim = embeddingDim / 2;
181182
float log = MathF.Log(10000.0f) / (halfDim - 1);
182183
var emb = Enumerable.Range(0, halfDim)
183-
.Select(x => MathF.Exp(x * -log))
184+
.Select(x => scale * MathF.Exp(-log * x))
184185
.ToArray();
185-
var embSin = emb.Select(MathF.Sin).ToArray();
186-
var embCos = emb.Select(MathF.Cos).ToArray();
187-
var result = new DenseTensor<float>(new[] { 1, 2 * halfDim });
188-
for (int i = 0; i < halfDim; i++)
189-
{
190-
result[0, i] = embSin[i];
191-
result[0, i + halfDim] = embCos[i];
192-
}
193-
return result;
186+
var embSin = emb.Select(MathF.Sin);
187+
var embCos = emb.Select(MathF.Cos);
188+
var guidanceEmbedding = embSin.Concat(embCos).ToArray();
189+
return new DenseTensor<float>(guidanceEmbedding, new[] { 1, embeddingDim });
194190
}
195191
}
196192
}

0 commit comments

Comments
 (0)