Skip to content

Commit

Permalink
Merge pull request LykosAI#402 from ionite34/fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ionite34 authored Dec 13, 2023
2 parents 0f4baaa + 5f15800 commit b7b9c11
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning 2.0](https://semver.org/spec/v2
- Addons usually affect guidance like ControlNet, T2I, FreeU, and other addons to come. They apply to the individual sampler, so you can mix and match different ControlNets for Base and Hires Fix, or use the current output from a previous sampler as ControlNet guidance image for HighRes passes.
- Added SD Turbo Scheduler
- Added display names for new samplers ("Heun++ 2", "DDPM", "LCM")
- Added Ctrl+Enter as a shortcut for the Generate Image button
#### Accounts Settings Subpage
- Lykos Account sign-up and login - currently for Patreon OAuth connections but GitHub requests caching and settings sync are planned
- Supporters can now connect your Patreon accounts, then head to the Updates page to choose to receive auto-updates from the Dev or Preview channels
Expand Down Expand Up @@ -71,6 +72,7 @@ and this project adheres to [Semantic Versioning 2.0](https://semver.org/spec/v2
- InvokeAI model links for T2I/IpAdapters now point to the correct folders
- Added extra checks to help prevent settings resetting in certain scenarios
- Fixed Refiner model enabled state not saving to Inference project files
- Fixed NullReference error labels when clearing the Inference batch size settings, now shows improved message with minimum and maximum value constraints

## v2.7.0-pre.4
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:icons="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia"
xmlns:ui="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia"
xmlns:lang="clr-namespace:StabilityMatrix.Avalonia.Languages"
xmlns:converters="clr-namespace:StabilityMatrix.Avalonia.Converters"
x:DataType="vmInference:BatchSizeCardViewModel">

<Design.PreviewWith>
Expand All @@ -19,7 +20,7 @@
</Design.PreviewWith>

<Style Selector="controls|BatchSizeCard">
<!-- Set Defaults -->
<Setter Property="Focusable" Value="True"/>
<Setter Property="ContextFlyout">
<ui:FAMenuFlyout Placement="BottomEdgeAlignedLeft">
<ui:ToggleMenuFlyoutItem
Expand Down Expand Up @@ -48,7 +49,7 @@
Minimum="1"
Increment="1"
ParsingNumberStyle="Integer"
Value="{Binding BatchSize}"
Value="{Binding BatchSize, Converter={x:Static converters:NullableDefaultNumericConverters.IntToDecimal}}"
ClipValueToMinMax="True"/>
</StackPanel>

Expand All @@ -71,7 +72,7 @@
Minimum="1"
Increment="1"
ParsingNumberStyle="Integer"
Value="{Binding BatchCount}"
Value="{Binding BatchCount, Converter={x:Static converters:NullableDefaultNumericConverters.IntToDecimal}}"
ClipValueToMinMax="True"/>
</StackPanel>

Expand Down Expand Up @@ -99,7 +100,7 @@
Maximum="{Binding BatchSize}"
Increment="1"
ParsingNumberStyle="Integer"
Value="{Binding BatchIndex}"
Value="{Binding BatchIndex, Converter={x:Static converters:NullableDefaultNumericConverters.IntToDecimal}}"
ClipValueToMinMax="True"/>
</StackPanel>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
using Avalonia.Data.Converters;

namespace StabilityMatrix.Avalonia.Converters;

/// <summary>
/// Converts a possibly boxed nullable value type to its default value
/// </summary>
public class NullableDefaultNumericConverter<TSource, TTarget> : IValueConverter
where TSource : unmanaged, INumber<TSource>
where TTarget : unmanaged, INumber<TTarget>
{
public ReturnBehavior NanHandling { get; set; } = ReturnBehavior.DefaultValue;

/// <summary>
/// Unboxes a nullable value type
/// </summary>
private TSource Unbox(TTarget? value)
{
if (!value.HasValue)
{
return default;
}

if (TTarget.IsNaN(value.Value))
{
return NanHandling switch
{
ReturnBehavior.DefaultValue => default,
ReturnBehavior.Throw => throw new InvalidCastException("Cannot convert NaN to a numeric type"),
_
=> throw new InvalidEnumArgumentException(
nameof(NanHandling),
(int)NanHandling,
typeof(ReturnBehavior)
)
};
}

return (TSource)System.Convert.ChangeType(value.Value, typeof(TSource));
}

/// <summary>
/// Convert a value type to a nullable value type
/// </summary>
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (targetType != typeof(TTarget?) && !targetType.IsAssignableTo(typeof(TTarget)))
{
// ReSharper disable once LocalizableElement
throw new ArgumentException(
$"Convert Target type {targetType.Name} must be assignable to {typeof(TTarget).Name}"
);
}

return (TTarget?)System.Convert.ChangeType(value, typeof(TTarget));
}

/// <summary>
/// Convert a nullable value type to a value type
/// </summary>
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (!targetType.IsAssignableTo(typeof(TSource)))
{
// ReSharper disable once LocalizableElement
throw new ArgumentException(
$"ConvertBack Target type {targetType.Name} must be assignable to {typeof(TSource).Name}"
);
}

return Unbox((TTarget?)value);
}

public enum ReturnBehavior
{
DefaultValue,
Throw
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace StabilityMatrix.Avalonia.Converters;

public static class NullableDefaultNumericConverters
{
public static readonly NullableDefaultNumericConverter<int, decimal> IntToDecimal = new();
}
6 changes: 6 additions & 0 deletions StabilityMatrix.Avalonia/Models/AppArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public class AppArgs
[Option("disable-gpu-rendering", HelpText = "Disable hardware acceleration / GPU rendering")]
public bool DisableGpuRendering { get; set; }

/// <summary>
/// Flag to use OpenGL rendering
/// </summary>
[Option("opengl", HelpText = "Prefer OpenGL rendering")]
public bool UseOpenGlRendering { get; set; }

/// <summary>
/// Override global app home directory
/// Defaults to (%APPDATA%|~/.config)/StabilityMatrix
Expand Down
7 changes: 7 additions & 0 deletions StabilityMatrix.Avalonia/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ public static AppBuilder BuildAvaloniaApp()

var app = AppBuilder.Configure<App>().UsePlatformDetect().WithInterFont().LogToTrace();

if (Args.UseOpenGlRendering)
{
app = app.With(
new Win32PlatformOptions { RenderingMode = [Win32RenderingMode.Wgl, Win32RenderingMode.Software] }
);
}

if (Args.DisableGpuRendering)
{
app = app.With(new Win32PlatformOptions { RenderingMode = new[] { Win32RenderingMode.Software } })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel;
using System.ComponentModel.DataAnnotations;
using CommunityToolkit.Mvvm.ComponentModel;
using StabilityMatrix.Avalonia.Controls;
using StabilityMatrix.Avalonia.Models.Inference;
using StabilityMatrix.Avalonia.ViewModels.Base;
Expand All @@ -12,16 +13,24 @@ namespace StabilityMatrix.Avalonia.ViewModels.Inference;
[Transient]
public partial class BatchSizeCardViewModel : LoadableViewModelBase, IComfyStep
{
[NotifyDataErrorInfo]
[ObservableProperty]
[Range(1, 1024)]
private int batchSize = 1;

[NotifyDataErrorInfo]
[ObservableProperty]
[Range(1, int.MaxValue)]
private int batchCount = 1;

[NotifyDataErrorInfo]
[ObservableProperty]
[Required]
private bool isBatchIndexEnabled;

[NotifyDataErrorInfo]
[ObservableProperty]
[Range(1, 1024)]
private int batchIndex = 1;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
Width="130"
HorizontalAlignment="Stretch"
Classes="accent"
ToolTip.Tip="{Binding $self.HotKey}"
HotKey="Ctrl+Enter"
Command="{Binding GenerateImageCommand}"
CommandParameter="{x:Static modelsInference:GenerateFlags.None}"
IsVisible="{Binding !GenerateImageCommand.CanBeCanceled}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
Width="130"
HorizontalAlignment="Stretch"
Classes="accent"
HotKey="Ctrl+Enter"
ToolTip.Tip="{Binding $self.HotKey}"
Command="{Binding GenerateImageCommand}"
CommandParameter="{x:Static modelsInference:GenerateFlags.None}"
IsVisible="{Binding !GenerateImageCommand.CanBeCanceled}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
HorizontalAlignment="Stretch"
Classes="accent"
Command="{Binding GenerateImageCommand}"
ToolTip.Tip="{Binding $self.HotKey}"
HotKey="Ctrl+Enter"
CommandParameter="{x:Static modelsInference:GenerateFlags.None}"
IsVisible="{Binding !GenerateImageCommand.CanBeCanceled}">
<Panel>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Globalization;
using StabilityMatrix.Avalonia.Converters;

namespace StabilityMatrix.Tests.Avalonia.Converters;

[TestClass]
public class NullableDefaultNumericConverterTests
{
[TestMethod]
public void Convert_IntToDecimal_ValueReturnsNullable()
{
const int value = 123;

var converter = NullableDefaultNumericConverters.IntToDecimal;

var result = converter.Convert(value, typeof(decimal?), null, CultureInfo.InvariantCulture);

Assert.AreEqual((decimal?)123, result);
}

[TestMethod]
public void ConvertBack_IntToDecimal_NullableReturnsDefault()
{
decimal? value = null;

var converter = NullableDefaultNumericConverters.IntToDecimal;

var result = converter.ConvertBack(value, typeof(int), null, CultureInfo.InvariantCulture);

Assert.AreEqual(0, result);
}

[TestMethod]
public void ConvertBack_IntToDouble_NanReturnsDefault()
{
const double value = double.NaN;

var converter = new NullableDefaultNumericConverter<int, double>();

var result = converter.ConvertBack(value, typeof(int), null, CultureInfo.InvariantCulture);

Assert.AreEqual(0, result);
}
}

0 comments on commit b7b9c11

Please sign in to comment.