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

Commit 5b5072e

Browse files
committed
Move SchedulerType form PromptOptions to SchedulerOptions
1 parent 16e5d29 commit 5b5072e

29 files changed

+172
-174
lines changed

OnnxStack.Console/Examples/StableDebug.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public async Task RunAsync()
3737
{
3838
Prompt = prompt,
3939
NegativePrompt = negativePrompt,
40-
SchedulerType = SchedulerType.LMS
4140
};
4241

4342
var schedulerOptions = new SchedulerOptions
4443
{
44+
SchedulerType = SchedulerType.LMS,
4545
Seed = 624461087,
4646
//Seed = Random.Shared.Next(),
4747
GuidanceScale = 8,
@@ -56,7 +56,7 @@ public async Task RunAsync()
5656

5757
foreach (var schedulerType in Helpers.GetPipelineSchedulers(model.PipelineType))
5858
{
59-
promptOptions.SchedulerType = schedulerType;
59+
schedulerOptions.SchedulerType = schedulerType;
6060
OutputHelpers.WriteConsole($"Generating {schedulerType} Image...", ConsoleColor.Green);
6161
await GenerateImage(model, promptOptions, schedulerOptions);
6262
}
@@ -72,12 +72,12 @@ public async Task RunAsync()
7272
private async Task<bool> GenerateImage(ModelOptions model, PromptOptions prompt, SchedulerOptions options)
7373
{
7474
var timestamp = Stopwatch.GetTimestamp();
75-
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{prompt.SchedulerType}.png");
75+
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{options.SchedulerType}.png");
7676
var result = await _stableDiffusionService.GenerateAsImageAsync(model, prompt, options);
7777
if (result is not null)
7878
{
7979
await result.SaveAsPngAsync(outputFilename);
80-
OutputHelpers.WriteConsole($"{prompt.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
80+
OutputHelpers.WriteConsole($"{options.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
8181
OutputHelpers.WriteConsole($"Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Yellow);
8282
return true;
8383
}

OnnxStack.Console/Examples/StableDiffusionBatch.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using OnnxStack.StableDiffusion.Common;
22
using OnnxStack.StableDiffusion.Config;
3+
using OnnxStack.StableDiffusion.Enums;
34
using OnnxStack.StableDiffusion.Helpers;
45
using SixLabors.ImageSharp;
56

@@ -66,7 +67,7 @@ public async Task RunAsync()
6667

6768
foreach (var schedulerType in Helpers.GetPipelineSchedulers(model.PipelineType).Take(1))
6869
{
69-
promptOptions.SchedulerType = schedulerType;
70+
schedulerOptions.SchedulerType = schedulerType;
7071
await foreach (var result in _stableDiffusionService.GenerateBatchAsync(model, promptOptions, schedulerOptions, batchOptions, callback))
7172
{
7273
var outputFilename = Path.Combine(_outputDirectory, $"{batchIndex}_{result.SchedulerOptions.Seed}.png");

OnnxStack.Console/Examples/StableDiffusionExample.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public async Task RunAsync()
5555

5656
foreach (var schedulerType in Helpers.GetPipelineSchedulers(model.PipelineType))
5757
{
58-
promptOptions.SchedulerType = schedulerType;
58+
schedulerOptions.SchedulerType = schedulerType;
5959
OutputHelpers.WriteConsole($"Generating {schedulerType} Image...", ConsoleColor.Green);
6060
await GenerateImage(model, promptOptions, schedulerOptions);
6161
}
@@ -68,13 +68,13 @@ public async Task RunAsync()
6868

6969
private async Task<bool> GenerateImage(ModelOptions model, PromptOptions prompt, SchedulerOptions options)
7070
{
71-
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{prompt.SchedulerType}.png");
71+
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{options.SchedulerType}.png");
7272
var result = await _stableDiffusionService.GenerateAsImageAsync(model, prompt, options);
7373
if (result == null)
7474
return false;
7575

7676
await result.SaveAsPngAsync(outputFilename);
77-
OutputHelpers.WriteConsole($"{prompt.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
77+
OutputHelpers.WriteConsole($"{options.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
7878
return true;
7979
}
8080
}

OnnxStack.Console/Examples/StableDiffusionGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task RunAsync()
5050
};
5151
foreach (var schedulerType in Helpers.GetPipelineSchedulers(model.PipelineType))
5252
{
53-
promptOptions.SchedulerType = schedulerType;
53+
schedulerOptions.SchedulerType = schedulerType;
5454
OutputHelpers.WriteConsole($"Generating {schedulerType} Image...", ConsoleColor.Green);
5555
await GenerateImage(model, promptOptions, schedulerOptions, generationPrompt.Key);
5656
}
@@ -65,13 +65,13 @@ public async Task RunAsync()
6565

6666
private async Task<bool> GenerateImage(ModelOptions model, PromptOptions prompt, SchedulerOptions options, string key)
6767
{
68-
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{prompt.SchedulerType}_{key}.png");
68+
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{options.SchedulerType}_{key}.png");
6969
var result = await _stableDiffusionService.GenerateAsImageAsync(model, prompt, options);
7070
if (result == null)
7171
return false;
7272

7373
await result.SaveAsPngAsync(outputFilename);
74-
OutputHelpers.WriteConsole($"{prompt.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
74+
OutputHelpers.WriteConsole($"{options.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
7575
return true;
7676
}
7777

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using OnnxStack.StableDiffusion.Enums;
62

73
namespace OnnxStack.StableDiffusion.Config
84
{
9-
public class BatchOptions
5+
public record BatchOptions
106
{
117
public BatchOptionType BatchType { get; set; }
128
public int Count { get; set; }
13-
149
public float ValueTo { get; set; }
1510
public float ValueFrom { get; set; }
1611
public float Increment { get; set; } = 1f;
1712
}
18-
19-
public enum BatchOptionType
20-
{
21-
Seed = 0,
22-
Step = 1,
23-
Guidance = 2
24-
}
2513
}

OnnxStack.StableDiffusion/Config/PromptOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class PromptOptions
1414

1515
[StringLength(512)]
1616
public string NegativePrompt { get; set; }
17-
public SchedulerType SchedulerType { get; set; }
1817

1918
public int BatchCount { get; set; } = 1;
2019

OnnxStack.StableDiffusion/Config/SchedulerOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ namespace OnnxStack.StableDiffusion.Config
66
{
77
public record SchedulerOptions
88
{
9+
/// <summary>
10+
/// Gets or sets the type of scheduler.
11+
/// </summary>
12+
public SchedulerType SchedulerType { get; set; }
13+
914
/// <summary>
1015
/// Gets or sets the height.
1116
/// </summary>

OnnxStack.StableDiffusion/Diffusers/LatentConsistency/LatentConsistencyDiffuser.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public virtual async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOp
8585
schedulerOptions.Seed = schedulerOptions.Seed > 0 ? schedulerOptions.Seed : Random.Shared.Next();
8686

8787
var diffuseTime = _logger?.LogBegin("Begin...");
88-
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {promptOptions.SchedulerType}");
88+
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {schedulerOptions.SchedulerType}");
8989

9090
// LCM does not support negative prompting
9191
var performGuidance = false;
@@ -117,7 +117,7 @@ public virtual async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOp
117117
public async IAsyncEnumerable<BatchResult> DiffuseBatchAsync(IModelOptions modelOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions, BatchOptions batchOptions, Action<int, int, int, int> progressCallback = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
118118
{
119119
var diffuseBatchTime = _logger?.LogBegin("Begin...");
120-
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {promptOptions.SchedulerType}");
120+
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {schedulerOptions.SchedulerType}");
121121

122122
// LCM does not support negative prompting
123123
var performGuidance = false;
@@ -130,9 +130,7 @@ public async IAsyncEnumerable<BatchResult> DiffuseBatchAsync(IModelOptions model
130130
var batchSchedulerOptions = BatchGenerator.GenerateBatch(batchOptions, schedulerOptions);
131131

132132
var batchIndex = 1;
133-
var batchCount = batchSchedulerOptions.Count;
134-
var schedulerCallback = (int p, int t) => progressCallback?.Invoke(batchIndex, batchCount, p, t);
135-
133+
var schedulerCallback = (int step, int steps) => progressCallback?.Invoke(batchIndex, batchSchedulerOptions.Count, step, steps);
136134
foreach (var batchSchedulerOption in batchSchedulerOptions)
137135
{
138136
yield return new BatchResult(batchSchedulerOption, await RunSchedulerSteps(modelOptions, promptOptions, batchSchedulerOption, promptEmbeddings, performGuidance, schedulerCallback, cancellationToken));
@@ -281,7 +279,7 @@ protected virtual IReadOnlyList<NamedOnnxValue> CreateUnetInputParams(IModelOpti
281279
/// <returns></returns>
282280
protected IScheduler GetScheduler(PromptOptions prompt, SchedulerOptions options)
283281
{
284-
return prompt.SchedulerType switch
282+
return options.SchedulerType switch
285283
{
286284
SchedulerType.LCM => new LCMScheduler(options),
287285
_ => default

OnnxStack.StableDiffusion/Diffusers/StableDiffusion/InpaintDiffuser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelO
5353
schedulerOptions.Seed = schedulerOptions.Seed > 0 ? schedulerOptions.Seed : Random.Shared.Next();
5454

5555
var diffuseTime = _logger?.LogBegin("Begin...");
56-
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {promptOptions.SchedulerType}");
56+
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {schedulerOptions.SchedulerType}");
5757

5858
// Should we perform classifier free guidance
5959
var performGuidance = schedulerOptions.GuidanceScale > 1.0f;

OnnxStack.StableDiffusion/Diffusers/StableDiffusion/InpaintLegacyDiffuser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelO
5353
schedulerOptions.Seed = schedulerOptions.Seed > 0 ? schedulerOptions.Seed : Random.Shared.Next();
5454

5555
var diffuseTime = _logger?.LogBegin("Begin...");
56-
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {promptOptions.SchedulerType}");
56+
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {schedulerOptions.SchedulerType}");
5757

5858
// Should we perform classifier free guidance
5959
var performGuidance = schedulerOptions.GuidanceScale > 1.0f;

0 commit comments

Comments
 (0)