Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 80445d9

Browse files
authored
Merge pull request #39 from saddam213/RTSD
Realtime Stable-Diffusion
2 parents 5d7cedb + 6f34888 commit 80445d9

33 files changed

+2098
-259
lines changed

OnnxStack.StableDiffusion/Enums/DiffuserType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public enum DiffuserType
66
ImageToImage = 1,
77
ImageInpaint = 2,
88
ImageInpaintLegacy = 3,
9-
ImageToAnimation = 4,
9+
ImageToAnimation = 4
1010
}
1111
}

OnnxStack.StableDiffusion/Services/StableDiffusionService.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Collections.Concurrent;
1313
using System.Collections.Generic;
1414
using System.IO;
15+
using System.Linq;
1516
using System.Runtime.CompilerServices;
1617
using System.Threading;
1718
using System.Threading.Tasks;
@@ -217,6 +218,10 @@ private async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOptions,
217218
if (diffuser is null)
218219
throw new Exception("Diffuser not found or is unsupported");
219220

221+
var schedulerSupported = pipeline.PipelineType.GetSchedulerTypes().Contains(schedulerOptions.SchedulerType);
222+
if (!schedulerSupported)
223+
throw new Exception($"Scheduler '{schedulerOptions.SchedulerType}' is not compatible with the `{pipeline.PipelineType}` pipeline.");
224+
220225
return await diffuser.DiffuseAsync(modelOptions, promptOptions, schedulerOptions, progress, cancellationToken);
221226
}
222227

@@ -230,6 +235,10 @@ private IAsyncEnumerable<BatchResult> DiffuseBatchAsync(IModelOptions modelOptio
230235
if (diffuser is null)
231236
throw new Exception("Diffuser not found or is unsupported");
232237

238+
var schedulerSupported = pipeline.PipelineType.GetSchedulerTypes().Contains(schedulerOptions.SchedulerType);
239+
if (!schedulerSupported)
240+
throw new Exception($"Scheduler '{schedulerOptions.SchedulerType}' is not compatible with the `{pipeline.PipelineType}` pipeline.");
241+
233242
return diffuser.DiffuseBatchAsync(modelOptions, promptOptions, schedulerOptions, batchOptions, progress, cancellationToken);
234243
}
235244
}

OnnxStack.UI/App.xaml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@
106106

107107

108108

109-
110-
109+
111110

112111

113112

@@ -248,10 +247,10 @@
248247
<userControls:FontAwesome Icon="&#xf302;" IconStyle="Light" Size="13" ToolTip="Send To Image To Image"/>
249248
</Button>
250249
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateImageInpaintCommand}" CommandParameter="{Binding}" >
251-
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light" Size="13" ToolTip="Send To Image Inpaint"/>
250+
<userControls:FontAwesome Icon="&#xf1c5;" IconStyle="Light" Size="13" ToolTip="Send To Image Inpaint"/>
252251
</Button>
253-
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateImageUpscaleCommand}" CommandParameter="{Binding}" Padding="2,2" BorderThickness="0,1,1,1">
254-
<userControls:FontAwesome Icon="&#xf065;" IconStyle="Light" Size="13" ToolTip="Send To Upscaler"/>
252+
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateImagePaintToImageCommand}" CommandParameter="{Binding}" Padding="2,2" BorderThickness="0,1,1,1">
253+
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light" Size="13" ToolTip="Send To Upscaler"/>
255254
</Button>
256255
</UniformGrid>
257256
</StackPanel>
@@ -1978,7 +1977,35 @@
19781977

19791978

19801979

1980+
<Style x:Key="ExecutionProviderTypes" TargetType="{x:Type ComboBoxItem}" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
1981+
<Setter Property="Visibility" Value="Collapsed" />
1982+
<Style.Triggers>
1983+
<DataTrigger Binding="{Binding}" Value="Cpu">
1984+
<Setter Property="Visibility" Value="Visible" />
1985+
</DataTrigger>
1986+
<DataTrigger Binding="{Binding}" Value="DirectML">
1987+
<Setter Property="Visibility" Value="Visible" />
1988+
</DataTrigger>
1989+
</Style.Triggers>
1990+
</Style>
1991+
1992+
<Style x:Key="ButtonGenerateStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
1993+
<Setter Property="Content" Value="Generate" />
1994+
<Style.Triggers>
1995+
<DataTrigger Binding="{Binding BatchOptions.IsRealtimeEnabled, ElementName=UI}" Value="True">
1996+
<Setter Property="Content" Value="Start" />
1997+
</DataTrigger>
1998+
</Style.Triggers>
1999+
</Style>
19812000

2001+
<Style x:Key="ButtonGenerateCancelStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
2002+
<Setter Property="Content" Value="Cancel" />
2003+
<Style.Triggers>
2004+
<DataTrigger Binding="{Binding BatchOptions.IsRealtimeEnabled, ElementName=UI}" Value="True">
2005+
<Setter Property="Content" Value="Stop" />
2006+
</DataTrigger>
2007+
</Style.Triggers>
2008+
</Style>
19822009

19832010

19842011
</Application.Resources>

OnnxStack.UI/Behaviors/SliderMouseWheelBehavior.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Xaml.Behaviors;
2+
using Newtonsoft.Json.Linq;
23
using System.Windows.Controls;
34
using System.Windows.Input;
45

@@ -16,11 +17,19 @@ private void AssociatedObject_PreviewMouseWheel(object sender, MouseWheelEventAr
1617
var slider = (Slider)sender;
1718
if (e.Delta > 0)
1819
{
19-
slider.Value += slider.TickFrequency;
20+
var newValue = slider.Value + slider.TickFrequency;
21+
if (newValue > slider.Maximum)
22+
return;
23+
24+
slider.Value = newValue;
2025
}
2126
else
2227
{
23-
slider.Value -= slider.TickFrequency;
28+
var newValue = slider.Value - slider.TickFrequency;
29+
if (newValue < slider.Minimum)
30+
return;
31+
32+
slider.Value = newValue;
2433
}
2534
}
2635

OnnxStack.UI/MainWindow.xaml

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
TextOptions.TextFormattingMode="Ideal"
1919
TextOptions.TextRenderingMode="ClearType"
2020
TextOptions.TextHintingMode="Fixed"
21-
UseLayoutRounding="True"
2221
SnapsToDevicePixels="True"
2322
Style="{StaticResource BaseWindow}">
2423
<Grid DataContext="{Binding ElementName=UI}">
@@ -51,22 +50,37 @@
5150
<TextBlock Text="Image To Image" Margin="5,0,0,0"/>
5251
</StackPanel>
5352
</TabItem.Header>
54-
<views:ImageToImage UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
53+
<views:ImageToImageView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
5554
</TabItem>
5655

5756
<!--Image Inpaint-->
5857
<TabItem>
5958
<TabItem.Header>
6059
<StackPanel Orientation="Horizontal" Margin="5">
6160
<StackPanel Orientation="Horizontal">
62-
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light"/>
61+
<userControls:FontAwesome Icon="&#xf15b;" IconStyle="Light"/>
6362
<userControls:FontAwesome Icon="&#xf054;" IconStyle="Regular" Size="8" Margin="3"/>
6463
<userControls:FontAwesome Icon="&#xf1c5;" IconStyle="Light"/>
6564
</StackPanel>
6665
<TextBlock Text="Image Inpaint" Margin="5,0,0,0"/>
6766
</StackPanel>
6867
</TabItem.Header>
69-
<views:ImageInpaint UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
68+
<views:ImageInpaintView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
69+
</TabItem>
70+
71+
<!--Paint To Image-->
72+
<TabItem>
73+
<TabItem.Header>
74+
<StackPanel Orientation="Horizontal" Margin="5">
75+
<StackPanel Orientation="Horizontal">
76+
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light"/>
77+
<userControls:FontAwesome Icon="&#xf054;" IconStyle="Regular" Size="8" Margin="3"/>
78+
<userControls:FontAwesome Icon="&#xf1c5;" IconStyle="Light"/>
79+
</StackPanel>
80+
<TextBlock Text="Paint To Image" Margin="5,0,0,0"/>
81+
</StackPanel>
82+
</TabItem.Header>
83+
<views:PaintToImageView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
7084
</TabItem>
7185

7286
<!--Log Window-->
@@ -78,7 +92,7 @@
7892
</StackPanel>
7993
</StackPanel>
8094
</TabItem.Header>
81-
<views:Logger UISettings="{Binding UISettings}" LogOutput="{Binding OutputLog, Mode=TwoWay}" Margin="0,6,0,0"/>
95+
<views:LoggerView UISettings="{Binding UISettings}" LogOutput="{Binding OutputLog, Mode=TwoWay}" Margin="0,6,0,0"/>
8296
</TabItem>
8397

8498
<!--Settings Window-->
@@ -90,7 +104,7 @@
90104
</StackPanel>
91105
</StackPanel>
92106
</TabItem.Header>
93-
<views:Settings UISettings="{Binding UISettings, Mode=TwoWay}" Margin="0,6,0,0"/>
107+
<views:SettingsView UISettings="{Binding UISettings, Mode=TwoWay}" Margin="0,6,0,0"/>
94108
</TabItem>
95109

96110

OnnxStack.UI/MainWindow.xaml.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public MainWindow(StableDiffusionConfig configuration, OnnxStackUIConfig uiSetti
3737
NavigateTextToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateTextToImage);
3838
NavigateImageToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateImageToImage);
3939
NavigateImageInpaintCommand = new AsyncRelayCommand<ImageResult>(NavigateImageInpaint);
40-
NavigateImageUpscaleCommand = new AsyncRelayCommand<ImageResult>(NavigateImageUpscale);
40+
NavigateImagePaintToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateImagePaintToImage);
4141

4242
WindowCloseCommand = new AsyncRelayCommand(WindowClose);
4343
WindowRestoreCommand = new AsyncRelayCommand(WindowRestore);
@@ -56,7 +56,7 @@ public MainWindow(StableDiffusionConfig configuration, OnnxStackUIConfig uiSetti
5656
public AsyncRelayCommand<ImageResult> NavigateTextToImageCommand { get; }
5757
public AsyncRelayCommand<ImageResult> NavigateImageToImageCommand { get; }
5858
public AsyncRelayCommand<ImageResult> NavigateImageInpaintCommand { get; }
59-
public AsyncRelayCommand<ImageResult> NavigateImageUpscaleCommand { get; }
59+
public AsyncRelayCommand<ImageResult> NavigateImagePaintToImageCommand { get; }
6060

6161
public OnnxStackUIConfig UISettings
6262
{
@@ -86,31 +86,39 @@ public INavigatable SelectedTabItem
8686

8787
private async Task NavigateTextToImage(ImageResult result)
8888
{
89-
await NavigateToTab(DiffuserType.TextToImage, result);
89+
await NavigateToTab(TabId.TextToImage, result);
9090
}
9191

9292
private async Task NavigateImageToImage(ImageResult result)
9393
{
94-
await NavigateToTab(DiffuserType.ImageToImage, result);
94+
await NavigateToTab(TabId.ImageToImage, result);
9595
}
9696

9797
private async Task NavigateImageInpaint(ImageResult result)
9898
{
99-
await NavigateToTab(DiffuserType.ImageInpaint, result);
99+
await NavigateToTab(TabId.ImageInpaint, result);
100100
}
101101

102-
private Task NavigateImageUpscale(ImageResult result)
102+
private async Task NavigateImagePaintToImage(ImageResult result)
103103
{
104-
return Task.CompletedTask;
104+
await NavigateToTab(TabId.PaintToImage, result);
105105
}
106106

107107

108-
private async Task NavigateToTab(DiffuserType diffuserType, ImageResult imageResult)
108+
private async Task NavigateToTab(TabId tab, ImageResult imageResult)
109109
{
110-
SelectedTabIndex = (int)diffuserType;
110+
SelectedTabIndex = (int)tab;
111111
await SelectedTabItem.NavigateAsync(imageResult);
112112
}
113113

114+
private enum TabId
115+
{
116+
TextToImage = 0,
117+
ImageToImage = 1,
118+
ImageInpaint = 2,
119+
PaintToImage = 3
120+
}
121+
114122
private ObservableCollection<ModelOptionsModel> CreateModelOptions(List<ModelOptions> onnxModelSets)
115123
{
116124
var models = onnxModelSets

OnnxStack.UI/Models/BatchOptionsModel.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class BatchOptionsModel : INotifyPropertyChanged
1515
private int _stepsValue = 1;
1616
private int _batchValue;
1717
private int _batchsValue = 1;
18+
private bool _disableHistory = true;
19+
private bool _isRealtimeEnabled;
1820

1921
public BatchOptionType BatchType
2022
{
@@ -40,12 +42,6 @@ public float Increment
4042
set { _increment = value; NotifyPropertyChanged(); }
4143
}
4244

43-
public bool IsAutomationEnabled
44-
{
45-
get { return _isAutomationEnabled; }
46-
set { _isAutomationEnabled = value; NotifyPropertyChanged(); }
47-
}
48-
4945
public int StepValue
5046
{
5147
get { return _stepValue; }
@@ -70,6 +66,35 @@ public int BatchsValue
7066
set { _batchsValue = value; NotifyPropertyChanged(); }
7167
}
7268

69+
public bool DisableHistory
70+
{
71+
get { return _disableHistory; }
72+
set { _disableHistory = value; NotifyPropertyChanged(); }
73+
}
74+
75+
public bool IsAutomationEnabled
76+
{
77+
get { return _isAutomationEnabled; }
78+
set
79+
{
80+
_isAutomationEnabled = value;
81+
if (_isAutomationEnabled)
82+
IsRealtimeEnabled = false;
83+
NotifyPropertyChanged();
84+
}
85+
}
86+
87+
public bool IsRealtimeEnabled
88+
{
89+
get { return _isRealtimeEnabled; }
90+
set
91+
{
92+
_isRealtimeEnabled = value;
93+
if (_isRealtimeEnabled)
94+
IsAutomationEnabled = false;
95+
NotifyPropertyChanged();
96+
}
97+
}
7398

7499
#region INotifyPropertyChanged
75100
public event PropertyChangedEventHandler PropertyChanged;

OnnxStack.UI/Models/OnnxStackUIConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class OnnxStackUIConfig : IConfigSection
1414
public bool ImageAutoSave { get; set; }
1515
public bool ImageAutoSaveBlueprint { get; set; }
1616
public string ImageAutoSaveDirectory { get; set; }
17-
18-
17+
public int RealtimeRefreshRate { get; set; } = 100;
18+
public bool RealtimeHistoryEnabled { get; set; }
1919
public int DefaultDeviceId { get; set; }
2020
public int DefaultInterOpNumThreads { get; set; }
2121
public int DefaultIntraOpNumThreads { get; set; }

OnnxStack.UI/Models/PromptOptionsModel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class PromptOptionsModel : INotifyPropertyChanged
1010
{
1111
private string _prompt;
1212
private string _negativePrompt;
13+
private bool _hasChanged;
1314

1415
[Required]
1516
[StringLength(512, MinimumLength = 1)]
@@ -26,10 +27,20 @@ public string NegativePrompt
2627
set { _prompt = value; NotifyPropertyChanged(); }
2728
}
2829

30+
public bool HasChanged
31+
{
32+
get { return _hasChanged; }
33+
set { _hasChanged = value; NotifyPropertyChanged(); }
34+
}
35+
36+
2937
#region INotifyPropertyChanged
3038
public event PropertyChangedEventHandler PropertyChanged;
3139
public void NotifyPropertyChanged([CallerMemberName] string property = "")
3240
{
41+
if (!property.Equals(nameof(HasChanged)) && !HasChanged)
42+
HasChanged = true;
43+
3344
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
3445
}
3546
#endregion

OnnxStack.UI/Models/SchedulerOptionsModel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class SchedulerOptionsModel : INotifyPropertyChanged
3333
private float _maximumBeta = 0.999f;
3434
private int _originalInferenceSteps = 100;
3535
private SchedulerType _schedulerType;
36+
private bool _hasChanged;
3637

3738
/// <summary>
3839
/// Gets or sets the height.
@@ -210,11 +211,21 @@ public SchedulerType SchedulerType
210211
set { _schedulerType = value; NotifyPropertyChanged(); }
211212
}
212213

214+
public bool HasChanged
215+
{
216+
get { return _hasChanged; }
217+
set { _hasChanged = value; NotifyPropertyChanged(); }
218+
}
219+
220+
213221

214222
#region INotifyPropertyChanged
215223
public event PropertyChangedEventHandler PropertyChanged;
216224
public void NotifyPropertyChanged([CallerMemberName] string property = "")
217225
{
226+
if (!property.Equals(nameof(HasChanged)) && !HasChanged)
227+
HasChanged = true;
228+
218229
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
219230
}
220231
#endregion

OnnxStack.UI/OnnxStack.UI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
</ItemGroup>
4141

4242
<ItemGroup>
43+
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.5.1" />
4344
<PackageReference Include="OnnxStack.StableDiffusion" Version="0.8.0" Condition=" '$(Configuration)' == 'Release' " />
4445
<ProjectReference Include="..\OnnxStack.StableDiffusion\OnnxStack.StableDiffusion.csproj" Condition=" '$(Configuration)' == 'Debug' " />
4546
</ItemGroup>

0 commit comments

Comments
 (0)