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

Commit 86d7195

Browse files
committed
LCM ImageToImage support
1 parent a213a17 commit 86d7195

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Microsoft.ML.OnnxRuntime;
2+
using Microsoft.ML.OnnxRuntime.Tensors;
3+
using OnnxStack.Core.Config;
4+
using OnnxStack.Core.Services;
5+
using OnnxStack.StableDiffusion.Common;
6+
using OnnxStack.StableDiffusion.Config;
7+
using OnnxStack.StableDiffusion.Helpers;
8+
using SixLabors.ImageSharp;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
13+
14+
namespace OnnxStack.StableDiffusion.Diffusers.LatentConsistency
15+
{
16+
public sealed class ImageDiffuser : TextDiffuser
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="ImageDiffuser"/> class.
20+
/// </summary>
21+
/// <param name="configuration">The configuration.</param>
22+
/// <param name="onnxModelService">The onnx model service.</param>
23+
public ImageDiffuser(IOnnxModelService onnxModelService, IPromptService promptService)
24+
: base(onnxModelService, promptService)
25+
{
26+
}
27+
28+
29+
/// <summary>
30+
/// Gets the timesteps.
31+
/// </summary>
32+
/// <param name="prompt">The prompt.</param>
33+
/// <param name="options">The options.</param>
34+
/// <param name="scheduler">The scheduler.</param>
35+
/// <returns></returns>
36+
protected override IReadOnlyList<int> GetTimesteps(PromptOptions prompt, SchedulerOptions options, IScheduler scheduler)
37+
{
38+
// Image2Image we narrow step the range by the Strength
39+
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
40+
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
41+
return scheduler.Timesteps.Skip(start).ToList();
42+
}
43+
44+
45+
/// <summary>
46+
/// Prepares the latents for inference.
47+
/// </summary>
48+
/// <param name="prompt">The prompt.</param>
49+
/// <param name="options">The options.</param>
50+
/// <param name="scheduler">The scheduler.</param>
51+
/// <returns></returns>
52+
protected override DenseTensor<float> PrepareLatents(IModelOptions model, PromptOptions prompt, SchedulerOptions options, IScheduler scheduler, IReadOnlyList<int> timesteps)
53+
{
54+
// Image input, decode, add noise, return as latent 0
55+
var imageTensor = prompt.InputImage.ToDenseTensor(new[] { 1, 3, options.Width, options.Height });
56+
var inputNames = _onnxModelService.GetInputNames(model, OnnxModelType.VaeEncoder);
57+
var inputParameters = CreateInputParameters(NamedOnnxValue.CreateFromTensor(inputNames[0], imageTensor));
58+
using (var inferResult = _onnxModelService.RunInference(model, OnnxModelType.VaeEncoder, inputParameters))
59+
{
60+
var sample = inferResult.FirstElementAs<DenseTensor<float>>();
61+
var scaledSample = sample
62+
.Add(scheduler.CreateRandomSample(sample.Dimensions, options.InitialNoiseLevel))
63+
.MultiplyBy(model.ScaleFactor);
64+
65+
var noisySample = scheduler.AddNoise(scaledSample, scheduler.CreateRandomSample(scaledSample.Dimensions), timesteps);
66+
if (prompt.BatchCount > 1)
67+
return noisySample.Repeat(prompt.BatchCount);
68+
69+
return noisySample;
70+
}
71+
}
72+
73+
}
74+
}

OnnxStack.StableDiffusion/Diffusers/LatentConsistency/TextDiffuser.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using OnnxStack.StableDiffusion.Common;
66
using OnnxStack.StableDiffusion.Config;
77
using OnnxStack.StableDiffusion.Enums;
8-
using OnnxStack.StableDiffusion.Helpers;
9-
using OnnxStack.StableDiffusion.Schedulers;
108
using OnnxStack.StableDiffusion.Schedulers.LatentConsistency;
119
using System;
1210
using System.Collections.Generic;
@@ -16,7 +14,7 @@
1614

1715
namespace OnnxStack.StableDiffusion.Diffusers.LatentConsistency
1816
{
19-
public sealed class TextDiffuser : DiffuserBase
17+
public class TextDiffuser : DiffuserBase
2018
{
2119
/// <summary>
2220
/// Initializes a new instance of the <see cref="TextDiffuser"/> class.

OnnxStack.StableDiffusion/Pipelines/LatentConsistency.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public LatentConsistencyPipeline(IOnnxModelService onnxModelService, IPromptServ
1717
{
1818
var diffusers = new Dictionary<DiffuserType, IDiffuser>
1919
{
20-
{ DiffuserType.TextToImage, new TextDiffuser(onnxModelService, promptService) }
20+
{ DiffuserType.TextToImage, new TextDiffuser(onnxModelService, promptService) },
21+
{ DiffuserType.ImageToImage, new ImageDiffuser(onnxModelService, promptService) }
2122
};
2223
_pipelineType = DiffuserPipelineType.LatentConsistency;
2324
_diffusers = new ConcurrentDictionary<DiffuserType, IDiffuser>(diffusers);

OnnxStack.StableDiffusion/Schedulers/LatentConsistency/LCMScheduler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public override DenseTensor<float> AddNoise(DenseTensor<float> originalSamples,
197197
public (float cSkip, float cOut) GetBoundaryConditionScalings(float timestep)
198198
{
199199
//self.sigma_data = 0.5 # Default: 0.5
200-
var sigmaData = 0.1f;
200+
var sigmaData = 0.5f;
201201

202202
float c = MathF.Pow(timestep / 0.1f, 2f) + MathF.Pow(sigmaData, 2f);
203203
float cSkip = MathF.Pow(sigmaData, 2f) / c;

0 commit comments

Comments
 (0)