Skip to content

Commit dc58d4d

Browse files
authored
Merge pull request #7 from DarthAffe/ParameterValidation
Added Parameter validation
2 parents de73276 + 4afbcdb commit dc58d4d

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#pragma warning disable CA2208
2+
3+
using System;
4+
5+
namespace StableDiffusion.NET;
6+
7+
public static class ParameterExtension
8+
{
9+
public static void Validate(this StableDiffusionParameter parameter)
10+
{
11+
ArgumentNullException.ThrowIfNull(parameter, nameof(parameter));
12+
ArgumentNullException.ThrowIfNull(parameter.ControlNet, nameof(StableDiffusionParameter.ControlNet));
13+
ArgumentNullException.ThrowIfNull(parameter.PhotoMaker, nameof(StableDiffusionParameter.PhotoMaker));
14+
ArgumentNullException.ThrowIfNull(parameter.NegativePrompt, nameof(StableDiffusionParameter.NegativePrompt));
15+
16+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.Width, nameof(StableDiffusionParameter.Width));
17+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.Height, nameof(StableDiffusionParameter.Height));
18+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.SampleSteps, nameof(StableDiffusionParameter.SampleSteps));
19+
20+
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CfgScale, nameof(StableDiffusionParameter.CfgScale));
21+
ArgumentOutOfRangeException.ThrowIfNegative(parameter.Strength, nameof(StableDiffusionParameter.Strength));
22+
23+
if (!Enum.IsDefined(parameter.SampleMethod)) throw new ArgumentOutOfRangeException(nameof(StableDiffusionParameter.SampleMethod));
24+
25+
parameter.ControlNet.Validate();
26+
parameter.PhotoMaker.Validate();
27+
}
28+
29+
public static void Validate(this StableDiffusionControlNetParameter parameter)
30+
{
31+
ArgumentNullException.ThrowIfNull(parameter, nameof(StableDiffusionParameter.ControlNet));
32+
33+
ArgumentOutOfRangeException.ThrowIfNegative(parameter.Strength, nameof(StableDiffusionControlNetParameter.Strength));
34+
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyHighThreshold, nameof(StableDiffusionControlNetParameter.CannyHighThreshold));
35+
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyLowThreshold, nameof(StableDiffusionControlNetParameter.CannyLowThreshold));
36+
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyWeak, nameof(StableDiffusionControlNetParameter.CannyWeak));
37+
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyStrong, nameof(StableDiffusionControlNetParameter.CannyStrong));
38+
}
39+
40+
public static void Validate(this PhotoMakerParameter parameter)
41+
{
42+
ArgumentNullException.ThrowIfNull(parameter, nameof(StableDiffusionParameter.PhotoMaker));
43+
ArgumentNullException.ThrowIfNull(parameter.InputIdImageDirectory, nameof(PhotoMakerParameter.InputIdImageDirectory));
44+
45+
ArgumentOutOfRangeException.ThrowIfNegative(parameter.StyleRatio, nameof(PhotoMakerParameter.StyleRatio));
46+
}
47+
48+
public static void Validate(this ModelParameter parameter)
49+
{
50+
ArgumentNullException.ThrowIfNull(parameter, nameof(parameter));
51+
ArgumentNullException.ThrowIfNull(parameter.TaesdPath, nameof(ModelParameter.TaesdPath));
52+
ArgumentNullException.ThrowIfNull(parameter.LoraModelDir, nameof(ModelParameter.LoraModelDir));
53+
ArgumentNullException.ThrowIfNull(parameter.VaePath, nameof(ModelParameter.VaePath));
54+
ArgumentNullException.ThrowIfNull(parameter.ControlNetPath, nameof(ModelParameter.ControlNetPath));
55+
ArgumentNullException.ThrowIfNull(parameter.EmbeddingsDirectory, nameof(ModelParameter.EmbeddingsDirectory));
56+
ArgumentNullException.ThrowIfNull(parameter.StackedIdEmbeddingsDirectory, nameof(ModelParameter.StackedIdEmbeddingsDirectory));
57+
58+
if (!Enum.IsDefined(parameter.RngType)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.RngType));
59+
if (!Enum.IsDefined(parameter.Quantization)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.Quantization));
60+
if (!Enum.IsDefined(parameter.Schedule)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.Schedule));
61+
}
62+
63+
public static void Validate(this UpscalerModelParameter parameter)
64+
{
65+
ArgumentNullException.ThrowIfNull(parameter, nameof(parameter));
66+
ArgumentNullException.ThrowIfNull(parameter.ESRGANPath, nameof(UpscalerModelParameter.ESRGANPath));
67+
68+
if (!Enum.IsDefined(parameter.Quantization)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.Quantization));
69+
}
70+
}

StableDiffusion.NET/StableDiffusionModel.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ static StableDiffusionModel()
4242

4343
public StableDiffusionModel(string modelPath, ModelParameter parameter, UpscalerModelParameter? upscalerParameter = null)
4444
{
45+
ArgumentException.ThrowIfNullOrWhiteSpace(modelPath, nameof(modelPath));
46+
47+
parameter.Validate();
48+
upscalerParameter?.Validate();
49+
4550
this._modelPath = modelPath;
4651
this._parameter = parameter;
4752
this._upscalerParameter = upscalerParameter;
@@ -88,6 +93,9 @@ private void Initialize()
8893
public StableDiffusionImage TextToImage(string prompt, StableDiffusionParameter parameter)
8994
{
9095
ObjectDisposedException.ThrowIf(_disposed, this);
96+
ArgumentNullException.ThrowIfNull(prompt);
97+
98+
parameter.Validate();
9199

92100
Native.sd_image_t* result;
93101
if (parameter.ControlNet.IsEnabled)
@@ -186,6 +194,9 @@ public StableDiffusionImage TextToImage(string prompt, StableDiffusionParameter
186194
public StableDiffusionImage ImageToImage(string prompt, in ReadOnlySpan<byte> image, StableDiffusionParameter parameter)
187195
{
188196
ObjectDisposedException.ThrowIf(_disposed, this);
197+
ArgumentNullException.ThrowIfNull(prompt);
198+
199+
parameter.Validate();
189200

190201
fixed (byte* imagePtr = image)
191202
{
@@ -207,6 +218,9 @@ public StableDiffusionImage ImageToImage(string prompt, StableDiffusionImage ima
207218
private StableDiffusionImage ImageToImage(string prompt, Native.sd_image_t image, StableDiffusionParameter parameter)
208219
{
209220
ObjectDisposedException.ThrowIf(_disposed, this);
221+
ArgumentNullException.ThrowIfNull(prompt);
222+
223+
parameter.Validate();
210224

211225
Native.sd_image_t* result;
212226
if (parameter.ControlNet.IsEnabled)
@@ -361,7 +375,14 @@ public void Dispose()
361375
}
362376

363377
public static void Convert(string modelPath, string vaePath, Quantization quantization, string outputPath)
364-
=> Native.convert(modelPath, vaePath, outputPath, quantization);
378+
{
379+
ArgumentException.ThrowIfNullOrWhiteSpace(nameof(modelPath));
380+
ArgumentException.ThrowIfNullOrWhiteSpace(nameof(outputPath));
381+
ArgumentNullException.ThrowIfNull(vaePath);
382+
if (!Enum.IsDefined(quantization)) throw new ArgumentOutOfRangeException(nameof(quantization));
383+
384+
Native.convert(modelPath, vaePath, outputPath, quantization);
385+
}
365386

366387
public static string GetSystemInfo()
367388
{

0 commit comments

Comments
 (0)