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

Commit 2122641

Browse files
committed
Support user defined Timesteps
1 parent 7381750 commit 2122641

24 files changed

+75
-0
lines changed

OnnxStack.StableDiffusion/Config/SchedulerOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public record SchedulerOptions
7878
public PredictionType PredictionType { get; set; } = PredictionType.Epsilon;
7979
public AlphaTransformType AlphaTransformType { get; set; } = AlphaTransformType.Cosine;
8080
public float MaximumBeta { get; set; } = 0.999f;
81+
public List<int> Timesteps { get; set; }
8182

8283
public int OriginalInferenceSteps { get; set; } = 50;
8384

OnnxStack.StableDiffusion/Diffusers/InstaFlow/ControlNetDiffuser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
164164
/// <returns></returns>
165165
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
166166
{
167+
if (!options.Timesteps.IsNullOrEmpty())
168+
return options.Timesteps;
169+
167170
return scheduler.Timesteps;
168171
}
169172

OnnxStack.StableDiffusion/Diffusers/InstaFlow/TextDiffuser.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.ML.OnnxRuntime.Tensors;
3+
using OnnxStack.Core;
34
using OnnxStack.Core.Model;
45
using OnnxStack.StableDiffusion.Common;
56
using OnnxStack.StableDiffusion.Config;
@@ -38,6 +39,9 @@ public TextDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoEn
3839
/// <returns></returns>
3940
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
4041
{
42+
if (!options.Timesteps.IsNullOrEmpty())
43+
return options.Timesteps;
44+
4145
return scheduler.Timesteps;
4246
}
4347

OnnxStack.StableDiffusion/Diffusers/LatentConsistency/ControlNetDiffuser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
160160
/// <returns></returns>
161161
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
162162
{
163+
if (!options.Timesteps.IsNullOrEmpty())
164+
return options.Timesteps;
165+
163166
return scheduler.Timesteps;
164167
}
165168

OnnxStack.StableDiffusion/Diffusers/LatentConsistency/ControlNetImageDiffuser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public ControlNetImageDiffuser(ControlNetModel controlNet, UNetConditionModel un
4545
/// <returns></returns>
4646
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
4747
{
48+
if (!options.Timesteps.IsNullOrEmpty())
49+
return options.Timesteps;
50+
4851
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
4952
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
5053
return scheduler.Timesteps.Skip(start).ToList();

OnnxStack.StableDiffusion/Diffusers/LatentConsistency/ImageDiffuser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public ImageDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoE
4444
/// <returns></returns>
4545
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
4646
{
47+
if (!options.Timesteps.IsNullOrEmpty())
48+
return options.Timesteps;
49+
4750
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
4851
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
4952
return scheduler.Timesteps.Skip(start).ToList();

OnnxStack.StableDiffusion/Diffusers/LatentConsistency/InpaintLegacyDiffuser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public InpaintLegacyDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecode
4747
/// <returns></returns>
4848
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
4949
{
50+
if (!options.Timesteps.IsNullOrEmpty())
51+
return options.Timesteps;
52+
5053
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
5154
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
5255
return scheduler.Timesteps.Skip(start).ToList();

OnnxStack.StableDiffusion/Diffusers/LatentConsistency/TextDiffuser.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.ML.OnnxRuntime.Tensors;
3+
using OnnxStack.Core;
34
using OnnxStack.Core.Model;
45
using OnnxStack.StableDiffusion.Common;
56
using OnnxStack.StableDiffusion.Config;
@@ -38,6 +39,9 @@ public TextDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoEn
3839
/// <returns></returns>
3940
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
4041
{
42+
if (!options.Timesteps.IsNullOrEmpty())
43+
return options.Timesteps;
44+
4145
return scheduler.Timesteps;
4246
}
4347

OnnxStack.StableDiffusion/Diffusers/LatentConsistencyXL/ControlNetDiffuser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
166166
/// <returns></returns>
167167
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
168168
{
169+
if (!options.Timesteps.IsNullOrEmpty())
170+
return options.Timesteps;
171+
169172
return scheduler.Timesteps;
170173
}
171174

OnnxStack.StableDiffusion/Diffusers/LatentConsistencyXL/ControlNetImageDiffuser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public ControlNetImageDiffuser(ControlNetModel controlNet, UNetConditionModel un
4545
/// <returns></returns>
4646
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
4747
{
48+
if (!options.Timesteps.IsNullOrEmpty())
49+
return options.Timesteps;
50+
4851
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
4952
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
5053
return scheduler.Timesteps.Skip(start).ToList();

0 commit comments

Comments
 (0)