Browse Source

Add Sampler Scheduler conversions

pull/165/head
Ionite 1 year ago
parent
commit
598371e140
No known key found for this signature in database
  1. 64
      StabilityMatrix.Core/Helper/GenerationParametersConverter.cs
  2. 81
      StabilityMatrix.Core/Models/Api/Comfy/ComfySampler.cs
  3. 25
      StabilityMatrix.Core/Models/Api/Comfy/ComfySamplerScheduler.cs
  4. 6
      StabilityMatrix.Core/Models/Api/Comfy/ComfyScheduler.cs
  5. 46
      StabilityMatrix.Core/Models/GenerationParameters.cs

64
StabilityMatrix.Core/Helper/GenerationParametersConverter.cs

@ -0,0 +1,64 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using StabilityMatrix.Core.Models.Api.Comfy;
namespace StabilityMatrix.Core.Helper;
public static class GenerationParametersConverter
{
private static readonly ImmutableDictionary<
string,
ComfySamplerScheduler
> ParamsToSamplerSchedulers = new Dictionary<string, ComfySamplerScheduler>
{
["DPM++ 2M Karras"] = (ComfySampler.Dpmpp2M, ComfyScheduler.Karras),
["DPM++ SDE Karras"] = (ComfySampler.DpmppSde, ComfyScheduler.Karras),
["DPM++ 2M SDE Exponential"] = (ComfySampler.Dpmpp2MSde, ComfyScheduler.Exponential),
["DPM++ 2M SDE Karras"] = (ComfySampler.Dpmpp2MSde, ComfyScheduler.Karras),
["Euler a"] = (ComfySampler.EulerAncestral, ComfyScheduler.Normal),
["Euler"] = (ComfySampler.Euler, ComfyScheduler.Normal),
["LMS"] = (ComfySampler.LMS, ComfyScheduler.Normal),
["Heun"] = (ComfySampler.Heun, ComfyScheduler.Normal),
["DPM2"] = (ComfySampler.Dpm2, ComfyScheduler.Normal),
["DPM2 Karras"] = (ComfySampler.Dpm2, ComfyScheduler.Karras),
["DPM2 a"] = (ComfySampler.Dpm2Ancestral, ComfyScheduler.Normal),
["DPM2 a Karras"] = (ComfySampler.Dpm2Ancestral, ComfyScheduler.Karras),
["DPM++ 2S a"] = (ComfySampler.Dpmpp2SAncestral, ComfyScheduler.Normal),
["DPM++ 2S a Karras"] = (ComfySampler.Dpmpp2SAncestral, ComfyScheduler.Karras),
["DPM++ 2M"] = (ComfySampler.Dpmpp2M, ComfyScheduler.Normal),
["DPM++ SDE"] = (ComfySampler.DpmppSde, ComfyScheduler.Normal),
["DPM++ 2M SDE"] = (ComfySampler.Dpmpp2MSde, ComfyScheduler.Normal),
["DPM++ 3M SDE"] = (ComfySampler.Dpmpp3MSde, ComfyScheduler.Normal),
["DPM++ 3M SDE Karras"] = (ComfySampler.Dpmpp3MSde, ComfyScheduler.Karras),
["DPM++ 3M SDE Exponential"] = (ComfySampler.Dpmpp3MSde, ComfyScheduler.Exponential),
["DPM fast"] = (ComfySampler.DpmFast, ComfyScheduler.Normal),
["DPM adaptive"] = (ComfySampler.DpmAdaptive, ComfyScheduler.Normal),
["LMS Karras"] = (ComfySampler.LMS, ComfyScheduler.Karras),
["DDIM"] = (ComfySampler.DDIM, ComfyScheduler.Normal),
["UniPC"] = (ComfySampler.UniPC, ComfyScheduler.Normal),
}.ToImmutableDictionary();
private static readonly ImmutableDictionary<
ComfySamplerScheduler,
string
> SamplerSchedulersToParams = ParamsToSamplerSchedulers.ToImmutableDictionary(
x => x.Value,
x => x.Key
);
public static bool TryGetSamplerScheduler(
string parameters,
out ComfySamplerScheduler samplerScheduler
)
{
return ParamsToSamplerSchedulers.TryGetValue(parameters, out samplerScheduler);
}
public static bool TryGetParameters(
ComfySamplerScheduler samplerScheduler,
[NotNullWhen(true)] out string? parameters
)
{
return SamplerSchedulersToParams.TryGetValue(samplerScheduler, out parameters);
}
}

81
StabilityMatrix.Core/Models/Api/Comfy/ComfySampler.cs

@ -1,39 +1,74 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
namespace StabilityMatrix.Core.Models.Api.Comfy;
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public readonly record struct ComfySampler(string Name)
{
private static Dictionary<string, string> ConvertDict { get; } =
public static ComfySampler Euler { get; } = new("euler");
public static ComfySampler EulerAncestral { get; } = new("euler_ancestral");
public static ComfySampler Heun { get; } = new("heun");
public static ComfySampler Dpm2 { get; } = new("dpm_2");
public static ComfySampler Dpm2Ancestral { get; } = new("dpm_2_ancestral");
public static ComfySampler LMS { get; } = new("lms");
public static ComfySampler DpmFast { get; } = new("dpm_fast");
public static ComfySampler DpmAdaptive { get; } = new("dpm_adaptive");
public static ComfySampler Dpmpp2SAncestral { get; } = new("dpmpp_2s_ancestral");
public static ComfySampler DpmppSde { get; } = new("dpmpp_sde");
public static ComfySampler DpmppSdeGpu { get; } = new("dpmpp_sde_gpu");
public static ComfySampler Dpmpp2M { get; } = new("dpmpp_2m");
public static ComfySampler Dpmpp2MSde { get; } = new("dpmpp_2m_sde");
public static ComfySampler Dpmpp2MSdeGpu { get; } = new("dpmpp_2m_sde_gpu");
public static ComfySampler Dpmpp3M { get; } = new("dpmpp_3m");
public static ComfySampler Dpmpp3MSde { get; } = new("dpmpp_3m_sde");
public static ComfySampler Dpmpp3MSdeGpu { get; } = new("dpmpp_3m_sde_gpu");
public static ComfySampler DDIM { get; } = new("ddim");
public static ComfySampler UniPC { get; } = new("uni_pc");
public static ComfySampler UniPCBh2 { get; } = new("uni_pc_bh2");
private static Dictionary<ComfySampler, string> ConvertDict { get; } =
new()
{
["euler"] = "Euler",
["euler_ancestral"] = "Euler Ancestral",
["heun"] = "Heun",
["dpm_2"] = "DPM 2",
["dpm_2_ancestral"] = "DPM 2 Ancestral",
["lms"] = "LMS",
["dpm_fast"] = "DPM Fast",
["dpm_adaptive"] = "DPM Adaptive",
["dpmpp_2s_ancestral"] = "DPM++ 2S Ancestral",
["dpmpp_sde"] = "DPM++ SDE",
["dpmpp_sde_gpu"] = "DPM++ SDE GPU",
["dpmpp_2m"] = "DPM++ 2M",
["dpmpp_2m_sde"] = "DPM++ 2M SDE",
["dpmpp_2m_sde_gpu"] = "DPM++ 2M SDE GPU",
["dpmpp_3m"] = "DPM++ 3M",
["dpmpp_3m_sde"] = "DPM++ 3M SDE",
["dpmpp_3m_sde_gpu"] = "DPM++ 3M SDE GPU",
["ddim"] = "DDIM",
["uni_pc"] = "UniPC",
["uni_pc_bh2"] = "UniPC BH2"
[Euler] = "Euler",
[EulerAncestral] = "Euler Ancestral",
[Heun] = "Heun",
[Dpm2] = "DPM 2",
[Dpm2Ancestral] = "DPM 2 Ancestral",
[LMS] = "LMS",
[DpmFast] = "DPM Fast",
[DpmAdaptive] = "DPM Adaptive",
[Dpmpp2SAncestral] = "DPM++ 2S Ancestral",
[DpmppSde] = "DPM++ SDE",
[DpmppSdeGpu] = "DPM++ SDE GPU",
[Dpmpp2M] = "DPM++ 2M",
[Dpmpp2MSde] = "DPM++ 2M SDE",
[Dpmpp2MSdeGpu] = "DPM++ 2M SDE GPU",
[Dpmpp3M] = "DPM++ 3M",
[Dpmpp3MSde] = "DPM++ 3M SDE",
[Dpmpp3MSdeGpu] = "DPM++ 3M SDE GPU",
[DDIM] = "DDIM",
[UniPC] = "UniPC",
[UniPCBh2] = "UniPC BH2"
};
public static IReadOnlyList<ComfySampler> Defaults { get; } =
ConvertDict.Keys.Select(k => new ComfySampler(k)).ToImmutableArray();
ConvertDict.Keys.ToImmutableArray();
public string DisplayName =>
ConvertDict.TryGetValue(Name, out var displayName) ? displayName : Name;
ConvertDict.TryGetValue(this, out var displayName) ? displayName : Name;
/// <inheritdoc />
public bool Equals(ComfySampler other)
{
return Name == other.Name;
}
/// <inheritdoc />
public override int GetHashCode()
{
return Name.GetHashCode();
}
private sealed class NameEqualityComparer : IEqualityComparer<ComfySampler>
{

25
StabilityMatrix.Core/Models/Api/Comfy/ComfySamplerScheduler.cs

@ -0,0 +1,25 @@
namespace StabilityMatrix.Core.Models.Api.Comfy;
/// <summary>
/// Pair of <see cref="ComfySampler"/> and <see cref="ComfyScheduler"/>
/// </summary>
public readonly record struct ComfySamplerScheduler(ComfySampler Sampler, ComfyScheduler Scheduler)
{
/// <inheritdoc />
public bool Equals(ComfySamplerScheduler other)
{
return Sampler.Equals(other.Sampler) && Scheduler.Equals(other.Scheduler);
}
/// <inheritdoc />
public override int GetHashCode()
{
return HashCode.Combine(Sampler, Scheduler);
}
// Implicit conversion from (ComfySampler, ComfyScheduler)
public static implicit operator ComfySamplerScheduler((ComfySampler, ComfyScheduler) tuple)
{
return new ComfySamplerScheduler(tuple.Item1, tuple.Item2);
}
}

6
StabilityMatrix.Core/Models/Api/Comfy/ComfyScheduler.cs

@ -4,10 +4,14 @@ namespace StabilityMatrix.Core.Models.Api.Comfy;
public readonly record struct ComfyScheduler(string Name)
{
public static ComfyScheduler Normal { get; } = new("normal");
public static ComfyScheduler Karras { get; } = new("karras");
public static ComfyScheduler Exponential { get; } = new("exponential");
private static Dictionary<string, string> ConvertDict { get; } =
new()
{
["normal"] = "Normal",
[Normal.Name] = "Normal",
["karras"] = "Karras",
["exponential"] = "Exponential",
["sgm_uniform"] = "SGM Uniform",

46
StabilityMatrix.Core/Models/GenerationParameters.cs

@ -1,11 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using StabilityMatrix.Core.Models.Api.Comfy;
namespace StabilityMatrix.Core.Models;
[JsonSerializable(typeof(GenerationParameters))]
public partial class GenerationParameters
public partial record GenerationParameters
{
public string? PositivePrompt { get; set; }
public string? NegativePrompt { get; set; }
@ -78,6 +79,8 @@ public partial class GenerationParameters
Sampler = match.Groups["Sampler"].Value,
CfgScale = double.Parse(match.Groups["CfgScale"].Value),
Seed = ulong.Parse(match.Groups["Seed"].Value),
Height = int.Parse(match.Groups["Height"].Value),
Width = int.Parse(match.Groups["Width"].Value),
ModelHash = match.Groups["ModelHash"].Value,
ModelName = match.Groups["ModelName"].Value,
};
@ -85,8 +88,47 @@ public partial class GenerationParameters
return true;
}
/// <summary>
/// Converts current <see cref="Sampler"/> string to <see cref="ComfySampler"/> and <see cref="ComfyScheduler"/>.
/// </summary>
/// <returns></returns>
public (ComfySampler sampler, ComfyScheduler scheduler)? GetComfySamplers()
{
if (Sampler is not { } source)
return null;
var scheduler = source switch
{
_ when source.Contains("Karras") => ComfyScheduler.Karras,
_ when source.Contains("Exponential") => ComfyScheduler.Exponential,
_ => ComfyScheduler.Normal,
};
var sampler = source switch
{
"LMS" => ComfySampler.LMS,
"DDIM" => ComfySampler.DDIM,
"UniPC" => ComfySampler.UniPC,
"DPM fast" => ComfySampler.DpmFast,
"DPM adaptive" => ComfySampler.DpmAdaptive,
"Heun" => ComfySampler.Heun,
_ when source.StartsWith("DPM2 a") => ComfySampler.Dpm2Ancestral,
_ when source.StartsWith("DPM2") => ComfySampler.Dpm2,
_ when source.StartsWith("DPM++ 2M SDE") => ComfySampler.Dpmpp2MSde,
_ when source.StartsWith("DPM++ 2M") => ComfySampler.Dpmpp2M,
_ when source.StartsWith("DPM++ 3M SDE") => ComfySampler.Dpmpp3MSde,
_ when source.StartsWith("DPM++ 3M") => ComfySampler.Dpmpp3M,
_ when source.StartsWith("DPM++ SDE") => ComfySampler.DpmppSde,
_ when source.StartsWith("DPM++ 2S a") => ComfySampler.Dpmpp2SAncestral,
_ => default
};
return (sampler, scheduler);
}
// Example: Steps: 30, Sampler: DPM++ 2M Karras, CFG scale: 7, Seed: 2216407431, Size: 640x896, Model hash: eb2h052f91, Model: anime_v1
[GeneratedRegex(
"""^Steps: (?<Steps>\d+), Sampler: (?<Sampler>.+?), CFG scale: (?<CfgScale>\d+(\.\d+)?), Seed: (?<Seed>\d+), Size: \d+x\d+, Model hash: (?<ModelHash>.+?), Model: (?<ModelName>.+)$"""
"""^Steps: (?<Steps>\d+), Sampler: (?<Sampler>.+?), CFG scale: (?<CfgScale>\d+(\.\d+)?), Seed: (?<Seed>\d+), Size: (?<Width>\d+)x(?<Height>\d+), Model hash: (?<ModelHash>.+?), Model: (?<ModelName>.+)$"""
)]
private static partial Regex ParseLastLineRegex();
}

Loading…
Cancel
Save