mirror of
https://github.com/DarthAffe/StableDiffusion.NET.git
synced 2025-12-12 21:38:45 +00:00
Added validations for parameters
This commit is contained in:
parent
de732763b8
commit
c6f99d080d
70
StableDiffusion.NET/Extensions/ParameterExtension.cs
Normal file
70
StableDiffusion.NET/Extensions/ParameterExtension.cs
Normal file
@ -0,0 +1,70 @@
|
||||
#pragma warning disable CA2208
|
||||
|
||||
using System;
|
||||
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
public static class ParameterExtension
|
||||
{
|
||||
public static void Validate(this StableDiffusionParameter parameter)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(parameter, nameof(parameter));
|
||||
ArgumentNullException.ThrowIfNull(parameter.NegativePrompt, nameof(StableDiffusionParameter.NegativePrompt));
|
||||
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.Width, nameof(StableDiffusionParameter.Width));
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.Height, nameof(StableDiffusionParameter.Height));
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.SampleSteps, nameof(StableDiffusionParameter.SampleSteps));
|
||||
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CfgScale, nameof(StableDiffusionParameter.CfgScale));
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(parameter.Strength, nameof(StableDiffusionParameter.Strength));
|
||||
|
||||
if (!Enum.IsDefined(parameter.SampleMethod)) throw new ArgumentOutOfRangeException(nameof(StableDiffusionParameter.SampleMethod));
|
||||
|
||||
// ReSharper disable ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
|
||||
parameter.ControlNet?.Validate();
|
||||
parameter.PhotoMaker?.Validate();
|
||||
// ReSharper restore ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
|
||||
}
|
||||
|
||||
public static void Validate(this StableDiffusionControlNetParameter parameter)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(parameter, nameof(StableDiffusionParameter.ControlNet));
|
||||
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(parameter.Strength, nameof(StableDiffusionControlNetParameter.Strength));
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyHighThreshold, nameof(StableDiffusionControlNetParameter.CannyHighThreshold));
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyLowThreshold, nameof(StableDiffusionControlNetParameter.CannyLowThreshold));
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyWeak, nameof(StableDiffusionControlNetParameter.CannyWeak));
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyStrong, nameof(StableDiffusionControlNetParameter.CannyStrong));
|
||||
}
|
||||
|
||||
public static void Validate(this PhotoMakerParameter parameter)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(parameter, nameof(StableDiffusionParameter.PhotoMaker));
|
||||
ArgumentNullException.ThrowIfNull(parameter.InputIdImageDirectory, nameof(PhotoMakerParameter.InputIdImageDirectory));
|
||||
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(parameter.StyleRatio, nameof(PhotoMakerParameter.StyleRatio));
|
||||
}
|
||||
|
||||
public static void Validate(this ModelParameter parameter)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(parameter, nameof(parameter));
|
||||
ArgumentNullException.ThrowIfNull(parameter.TaesdPath, nameof(ModelParameter.TaesdPath));
|
||||
ArgumentNullException.ThrowIfNull(parameter.LoraModelDir, nameof(ModelParameter.LoraModelDir));
|
||||
ArgumentNullException.ThrowIfNull(parameter.VaePath, nameof(ModelParameter.VaePath));
|
||||
ArgumentNullException.ThrowIfNull(parameter.ControlNetPath, nameof(ModelParameter.ControlNetPath));
|
||||
ArgumentNullException.ThrowIfNull(parameter.EmbeddingsDirectory, nameof(ModelParameter.EmbeddingsDirectory));
|
||||
ArgumentNullException.ThrowIfNull(parameter.StackedIdEmbeddingsDirectory, nameof(ModelParameter.StackedIdEmbeddingsDirectory));
|
||||
|
||||
if (!Enum.IsDefined(parameter.RngType)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.RngType));
|
||||
if (!Enum.IsDefined(parameter.Quantization)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.Quantization));
|
||||
if (!Enum.IsDefined(parameter.Schedule)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.Schedule));
|
||||
}
|
||||
|
||||
public static void Validate(this UpscalerModelParameter parameter)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(parameter, nameof(parameter));
|
||||
ArgumentNullException.ThrowIfNull(parameter.ESRGANPath, nameof(UpscalerModelParameter.ESRGANPath));
|
||||
|
||||
if (!Enum.IsDefined(parameter.Quantization)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.Quantization));
|
||||
}
|
||||
}
|
||||
@ -42,6 +42,11 @@ public sealed unsafe class StableDiffusionModel : IDisposable
|
||||
|
||||
public StableDiffusionModel(string modelPath, ModelParameter parameter, UpscalerModelParameter? upscalerParameter = null)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(modelPath, nameof(modelPath));
|
||||
|
||||
parameter.Validate();
|
||||
upscalerParameter?.Validate();
|
||||
|
||||
this._modelPath = modelPath;
|
||||
this._parameter = parameter;
|
||||
this._upscalerParameter = upscalerParameter;
|
||||
@ -88,6 +93,9 @@ public sealed unsafe class StableDiffusionModel : IDisposable
|
||||
public StableDiffusionImage TextToImage(string prompt, StableDiffusionParameter parameter)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
ArgumentNullException.ThrowIfNull(prompt);
|
||||
|
||||
parameter.Validate();
|
||||
|
||||
Native.sd_image_t* result;
|
||||
if (parameter.ControlNet.IsEnabled)
|
||||
@ -186,6 +194,9 @@ public sealed unsafe class StableDiffusionModel : IDisposable
|
||||
public StableDiffusionImage ImageToImage(string prompt, in ReadOnlySpan<byte> image, StableDiffusionParameter parameter)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
ArgumentNullException.ThrowIfNull(prompt);
|
||||
|
||||
parameter.Validate();
|
||||
|
||||
fixed (byte* imagePtr = image)
|
||||
{
|
||||
@ -207,9 +218,12 @@ public sealed unsafe class StableDiffusionModel : IDisposable
|
||||
private StableDiffusionImage ImageToImage(string prompt, Native.sd_image_t image, StableDiffusionParameter parameter)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
ArgumentNullException.ThrowIfNull(prompt);
|
||||
|
||||
parameter.Validate();
|
||||
|
||||
Native.sd_image_t* result;
|
||||
if (parameter.ControlNet.IsEnabled)
|
||||
if (parameter!.ControlNet.IsEnabled)
|
||||
{
|
||||
fixed (byte* imagePtr = parameter.ControlNet.Image)
|
||||
{
|
||||
@ -361,7 +375,14 @@ public sealed unsafe class StableDiffusionModel : IDisposable
|
||||
}
|
||||
|
||||
public static void Convert(string modelPath, string vaePath, Quantization quantization, string outputPath)
|
||||
=> Native.convert(modelPath, vaePath, outputPath, quantization);
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(nameof(modelPath));
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(nameof(outputPath));
|
||||
ArgumentNullException.ThrowIfNull(vaePath);
|
||||
if (!Enum.IsDefined(quantization)) throw new ArgumentOutOfRangeException(nameof(quantization));
|
||||
|
||||
Native.convert(modelPath, vaePath, outputPath, quantization);
|
||||
}
|
||||
|
||||
public static string GetSystemInfo()
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user