Skip to content

Commit

Permalink
Merge branch 'master' into StableCascade
Browse files Browse the repository at this point in the history
  • Loading branch information
saddam213 committed Apr 25, 2024
2 parents 6a62767 + dc2ed2f commit b6920e0
Show file tree
Hide file tree
Showing 26 changed files with 139 additions and 1 deletion.
63 changes: 63 additions & 0 deletions OnnxStack.Console/Examples/ModelDebug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using OnnxStack.Core.Image;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Pipelines;

namespace OnnxStack.Console.Runner
{
public sealed class ModelDebug : IExampleRunner
{
private readonly string _outputDirectory;
private readonly StableDiffusionConfig _configuration;

public ModelDebug(StableDiffusionConfig configuration)
{
_configuration = configuration;
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(ModelDebug));
Directory.CreateDirectory(_outputDirectory);
}

public int Index => 1;

public string Name => "Model Debug";

public string Description => "Model Debug";

public async Task RunAsync()
{
// Create Pipeline
// var pipeline = InstaFlowPipeline.CreatePipeline("D:\\Repositories\\Instaflow-onnx");
// var pipeline = LatentConsistencyPipeline.CreatePipeline("D:\\Repositories\\LCM_Dreamshaper_v7-onnx");
// var pipeline = LatentConsistencyXLPipeline.CreatePipeline("D:\\Repositories\\Latent-Consistency-xl-Olive-Onnx");
// var pipeline = StableDiffusionPipeline.CreatePipeline("D:\\Repositories\\stable-diffusion-v1-5");
var pipeline = StableDiffusionXLPipeline.CreatePipeline("D:\\Repositories\\Hyper-SD-onnx");

// Prompt
var promptOptions = new PromptOptions
{
Prompt = "a photo of a cat drinking at a bar with a penguin"
};

// Scheduler
var schedulerOptions = pipeline.DefaultSchedulerOptions with
{
InferenceSteps = 1,
GuidanceScale = 0,
SchedulerType = SchedulerType.DDIM,
Timesteps = new List<int> { 800 }
};

// Run pipeline
var result = await pipeline.RunAsync(promptOptions, schedulerOptions, progressCallback: OutputHelpers.ProgressCallback);

// Create Image from Tensor result
var image = new OnnxImage(result);

// Save Image File
await image.SaveAsync(Path.Combine(_outputDirectory, $"{pipeline.GetType().Name}-{schedulerOptions.Seed}.png"));

//Unload
await pipeline.UnloadAsync();
}
}
}
1 change: 1 addition & 0 deletions OnnxStack.StableDiffusion/Config/SchedulerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public record SchedulerOptions
public PredictionType PredictionType { get; set; } = PredictionType.Epsilon;
public AlphaTransformType AlphaTransformType { get; set; } = AlphaTransformType.Cosine;
public float MaximumBeta { get; set; } = 0.999f;
public List<int> Timesteps { get; set; }

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
4 changes: 4 additions & 0 deletions OnnxStack.StableDiffusion/Diffusers/InstaFlow/TextDiffuser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.ML.OnnxRuntime.Tensors;
using OnnxStack.Core;
using OnnxStack.Core.Model;
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
Expand Down Expand Up @@ -38,6 +39,9 @@ public TextDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoEn
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public ControlNetImageDiffuser(ControlNetModel controlNet, UNetConditionModel un
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
return scheduler.Timesteps.Skip(start).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public ImageDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoE
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
return scheduler.Timesteps.Skip(start).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public InpaintLegacyDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecode
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
return scheduler.Timesteps.Skip(start).ToList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.ML.OnnxRuntime.Tensors;
using OnnxStack.Core;
using OnnxStack.Core.Model;
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
Expand Down Expand Up @@ -38,6 +39,9 @@ public TextDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoEn
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public ControlNetImageDiffuser(ControlNetModel controlNet, UNetConditionModel un
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
return scheduler.Timesteps.Skip(start).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public ImageDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoE
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

// Image2Image we narrow step the range by the Strength
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
return scheduler.Timesteps.Skip(start).ToList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.ML.OnnxRuntime.Tensors;
using OnnxStack.Core;
using OnnxStack.Core.Model;
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
Expand Down Expand Up @@ -38,6 +39,9 @@ public TextDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoEn
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public ControlNetImageDiffuser(ControlNetModel controlNet, UNetConditionModel un
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
return scheduler.Timesteps.Skip(start).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public ImageDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoE
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

// Image2Image we narrow step the range by the Strength
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ private async Task<DenseTensor<float>> PrepareImageMask(PromptOptions promptOpti
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
return scheduler.Timesteps.Skip(start).ToList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.ML.OnnxRuntime.Tensors;
using OnnxStack.Core;
using OnnxStack.Core.Model;
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
Expand Down Expand Up @@ -39,6 +40,9 @@ public TextDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoEn
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public ControlNetImageDiffuser(ControlNetModel controlNet, UNetConditionModel un
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
return scheduler.Timesteps.Skip(start).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public ImageDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoE
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

// Image2Image we narrow step the range by the Strength
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
return scheduler.Timesteps.Skip(start).ToList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.ML.OnnxRuntime.Tensors;
using OnnxStack.Core;
using OnnxStack.Core.Model;
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
Expand Down Expand Up @@ -39,6 +40,9 @@ public TextDiffuser(UNetConditionModel unet, AutoEncoderModel vaeDecoder, AutoEn
/// <returns></returns>
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
{
if (!options.Timesteps.IsNullOrEmpty())
return options.Timesteps;

return scheduler.Timesteps;
}

Expand Down
2 changes: 1 addition & 1 deletion OnnxStack.StableDiffusion/Schedulers/SchedulerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected virtual float[] GetTimesteps()
}
else if (Options.TimestepSpacing == TimestepSpacingType.Trailing)
{
var stepRatio = Options.TrainTimesteps / (Options.InferenceSteps - 1);
var stepRatio = Options.TrainTimesteps / Math.Max(1, (Options.InferenceSteps - 1));
return Enumerable.Range(0, Options.TrainTimesteps)
.Where((number, index) => index % stepRatio == 0)
.Select(x => (float)x)
Expand Down

0 comments on commit b6920e0

Please sign in to comment.