Skip to content

Commit

Permalink
Merge pull request LykosAI#622 from ionite34/fix-inference-lora
Browse files Browse the repository at this point in the history
Fix Inference Lora resetting due to slider maximums & Add number format setting
  • Loading branch information
ionite34 authored Apr 30, 2024
2 parents f58a82e + c0c73b7 commit 7a12270
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 66 deletions.
5 changes: 4 additions & 1 deletion StabilityMatrix.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ private static void ConfigureServiceProvider()

if (settingsManager.TryFindLibrary())
{
Cultures.SetSupportedCultureOrDefault(settingsManager.Settings.Language);
Cultures.SetSupportedCultureOrDefault(
settingsManager.Settings.Language,
settingsManager.Settings.NumberFormatMode
);
}
else
{
Expand Down
26 changes: 10 additions & 16 deletions StabilityMatrix.Avalonia/Controls/Inference/ExtraNetworkCard.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,15 @@
Grid.Column="1"
Margin="4,0,0,0"
ValidationMode="InvalidInputOverwritten"
SmallChange="0.01"
SmallChange="0.05"
LargeChange="0.1"
Maximum="100"
Minimum="-100"
SimpleNumberFormat="F2"
Value="{Binding ModelWeight}"
HorizontalAlignment="Stretch"
SpinButtonPlacementMode="Compact"/>
SpinButtonPlacementMode="Inline"/>
</Grid>
<Slider
Minimum="0"
Maximum="1"
Value="{Binding ModelWeight}"
TickFrequency="0.25"
TickPlacement="BottomRight"/>
</StackPanel>

<!-- CLIP Strength -->
Expand All @@ -79,18 +76,15 @@
Grid.Column="1"
Margin="4,0,0,0"
ValidationMode="InvalidInputOverwritten"
SmallChange="0.01"
SmallChange="0.05"
LargeChange="0.1"
Maximum="100"
Minimum="-100"
SimpleNumberFormat="F2"
Value="{Binding ClipWeight}"
HorizontalAlignment="Stretch"
SpinButtonPlacementMode="Compact"/>
SpinButtonPlacementMode="Inline"/>
</Grid>
<Slider
Minimum="0"
Maximum="1"
Value="{Binding ClipWeight}"
TickFrequency="0.25"
TickPlacement="BottomRight"/>
</StackPanel>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Globalization;
using Avalonia.Data.Converters;
using StabilityMatrix.Core.Models.Settings;

namespace StabilityMatrix.Avalonia.Converters;

/// <summary>
/// Converts a <see cref="NumberFormatMode"/> to a sample number string
/// </summary>
public class NumberFormatModeSampleConverter : IValueConverter
{
/// <inheritdoc />
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not NumberFormatMode mode)
return null;

const double sample = 12345.67;

// Format the sample number based on the number format mode
return mode switch
{
NumberFormatMode.Default => sample.ToString("N2", culture),
NumberFormatMode.CurrentCulture => sample.ToString("N2", culture),
NumberFormatMode.InvariantCulture => sample.ToString("N2", CultureInfo.InvariantCulture),
_ => throw new ArgumentOutOfRangeException()
};
}

/// <inheritdoc />
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
129 changes: 108 additions & 21 deletions StabilityMatrix.Avalonia/Languages/Cultures.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Models.Settings;

namespace StabilityMatrix.Avalonia.Languages;

Expand All @@ -13,23 +16,23 @@ public static class Cultures

public static CultureInfo? Current => Resources.Culture;

public static readonly Dictionary<string, CultureInfo> SupportedCulturesByCode = new Dictionary<
string,
CultureInfo
>
{
["en-US"] = Default,
["ja-JP"] = new("ja-JP"),
["zh-Hans"] = new("zh-Hans"),
["zh-Hant"] = new("zh-Hant"),
["it-IT"] = new("it-IT"),
["fr-FR"] = new("fr-FR"),
["es"] = new("es"),
["ru-RU"] = new("ru-RU"),
["tr-TR"] = new("tr-TR"),
["de"] = new("de"),
["pt-PT"] = new("pt-PT")
};
public static NumberFormatInfo CurrentNumberFormat => Thread.CurrentThread.CurrentCulture.NumberFormat;

public static readonly Dictionary<string, CultureInfo> SupportedCulturesByCode =
new()
{
["en-US"] = Default,
["ja-JP"] = new CultureInfo("ja-JP"),
["zh-Hans"] = new CultureInfo("zh-Hans"),
["zh-Hant"] = new CultureInfo("zh-Hant"),
["it-IT"] = new CultureInfo("it-IT"),
["fr-FR"] = new CultureInfo("fr-FR"),
["es"] = new CultureInfo("es"),
["ru-RU"] = new CultureInfo("ru-RU"),
["tr-TR"] = new CultureInfo("tr-TR"),
["de"] = new CultureInfo("de"),
["pt-PT"] = new CultureInfo("pt-PT")
};

public static IReadOnlyList<CultureInfo> SupportedCultures =>
SupportedCulturesByCode.Values.ToImmutableList();
Expand All @@ -44,11 +47,19 @@ public static CultureInfo GetSupportedCultureOrDefault(string? cultureCode)
return culture;
}

public static void SetSupportedCultureOrDefault(string? cultureCode)
public static void SetSupportedCultureOrDefault(string? cultureCode, NumberFormatInfo numberFormat)
{
if (!TrySetSupportedCulture(cultureCode, numberFormat))
{
TrySetSupportedCulture(Default, numberFormat);
}
}

public static void SetSupportedCultureOrDefault(string? cultureCode, NumberFormatMode numberFormatMode)
{
if (!TrySetSupportedCulture(cultureCode))
if (!TrySetSupportedCulture(cultureCode, numberFormatMode))
{
TrySetSupportedCulture(Default);
TrySetSupportedCulture(Default, numberFormatMode);
}
}

Expand All @@ -62,6 +73,59 @@ public static bool TrySetSupportedCulture(string? cultureCode)
if (Current?.Name != culture.Name)
{
Resources.Culture = culture;

Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

EventManager.Instance.OnCultureChanged(culture);
}

return true;
}

public static bool TrySetSupportedCulture(string? cultureCode, NumberFormatInfo numberFormat)
{
if (cultureCode is null || !SupportedCulturesByCode.TryGetValue(cultureCode, out var culture))
{
return false;
}

if (Current?.Name != culture.Name || CurrentNumberFormat != numberFormat)
{
Resources.Culture = culture;

var cultureInfo = GetCultureInfoWithNumberFormat(culture, numberFormat);
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

EventManager.Instance.OnCultureChanged(culture);
}

return true;
}

public static bool TrySetSupportedCulture(string? cultureCode, NumberFormatMode numberFormatMode)
{
if (cultureCode is null || !SupportedCulturesByCode.TryGetValue(cultureCode, out var culture))
{
return false;
}

var numberFormat = numberFormatMode switch
{
NumberFormatMode.CurrentCulture => culture.NumberFormat,
NumberFormatMode.InvariantCulture => CultureInfo.InvariantCulture.NumberFormat,
_ => culture.NumberFormat,
};

if (Current?.Name != culture.Name || CurrentNumberFormat != numberFormat)
{
Resources.Culture = culture;

var cultureInfo = GetCultureInfoWithNumberFormat(culture, numberFormat);
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

EventManager.Instance.OnCultureChanged(culture);
}

Expand All @@ -72,4 +136,27 @@ public static bool TrySetSupportedCulture(CultureInfo? cultureInfo)
{
return cultureInfo is not null && TrySetSupportedCulture(cultureInfo.Name);
}

public static bool TrySetSupportedCulture(CultureInfo? cultureInfo, NumberFormatInfo numberFormat)
{
return cultureInfo is not null && TrySetSupportedCulture(cultureInfo.Name, numberFormat);
}

public static bool TrySetSupportedCulture(CultureInfo? cultureInfo, NumberFormatMode numberFormatMode)
{
return cultureInfo is not null && TrySetSupportedCulture(cultureInfo.Name, numberFormatMode);
}

// ReSharper disable once SuggestBaseTypeForParameter
private static CultureInfo GetCultureInfoWithNumberFormat(
CultureInfo culture,
NumberFormatInfo numberFormat
)
{
ArgumentNullException.ThrowIfNull(culture);

var cultureInfo = (CultureInfo)culture.Clone();
cultureInfo.NumberFormat = numberFormat;
return cultureInfo;
}
}
9 changes: 9 additions & 0 deletions StabilityMatrix.Avalonia/Languages/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions StabilityMatrix.Avalonia/Languages/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1077,4 +1077,7 @@
<data name="Label_CLIPStrength" xml:space="preserve">
<value>CLIP Strength</value>
</data>
<data name="Label_NumberFormat" xml:space="preserve">
<value>Number Format</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public partial class MainSettingsViewModel : PageViewModelBase
// ReSharper disable once MemberCanBeMadeStatic.Global
public IReadOnlyList<CultureInfo> AvailableLanguages => Cultures.SupportedCultures;

[ObservableProperty]
private NumberFormatMode selectedNumberFormatMode;

public IReadOnlyList<NumberFormatMode> NumberFormatModes { get; } =
Enum.GetValues<NumberFormatMode>().Where(mode => mode != default).ToList();

public IReadOnlyList<float> AnimationScaleOptions { get; } =
new[] { 0f, 0.25f, 0.5f, 0.75f, 1f, 1.25f, 1.5f, 1.75f, 2f, };

Expand Down Expand Up @@ -228,6 +234,13 @@ IAccountsService accountsService
true
);

settingsManager.RelayPropertyFor(
this,
vm => vm.SelectedNumberFormatMode,
settings => settings.NumberFormatMode,
true
);

DebugThrowAsyncExceptionCommand.WithNotificationErrorHandler(notificationService, LogLevel.Warn);

hardwareInfoUpdateTimer.Tick += OnHardwareInfoUpdateTimerTick;
Expand Down Expand Up @@ -298,7 +311,7 @@ partial void OnSelectedLanguageChanged(CultureInfo? oldValue, CultureInfo newVal
{
Logger.Info("Changing language from {Old} to {New}", oldValue, newValue);

Cultures.TrySetSupportedCulture(newValue);
Cultures.TrySetSupportedCulture(newValue, settingsManager.Settings.NumberFormatMode);
settingsManager.Transaction(s => s.Language = newValue.Name);

var dialog = new BetterContentDialog
Expand Down
Loading

0 comments on commit 7a12270

Please sign in to comment.