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

Commit 5a4d599

Browse files
committed
Only re-render on prompt/options update
1 parent 985b2af commit 5a4d599

File tree

6 files changed

+94
-27
lines changed

6 files changed

+94
-27
lines changed

OnnxStack.UI/Models/BatchOptionsModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class BatchOptionsModel : INotifyPropertyChanged
1616
private int _batchValue;
1717
private int _batchsValue = 1;
1818
private int _realtimeRefreshRate = 1000;
19+
private bool _disableHistory;
1920

2021
public BatchOptionType BatchType
2122
{
@@ -77,6 +78,13 @@ public int RealtimeRefreshRate
7778
set { _realtimeRefreshRate = value; NotifyPropertyChanged(); }
7879
}
7980

81+
public bool DisableHistory
82+
{
83+
get { return _disableHistory; }
84+
set { _disableHistory = value; NotifyPropertyChanged(); }
85+
}
86+
87+
8088

8189
#region INotifyPropertyChanged
8290
public event PropertyChangedEventHandler PropertyChanged;

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/UserControls/SchedulerControl.xaml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@
357357
</UniformGrid>
358358
<UniformGrid Columns="2">
359359
<TextBox Text="{Binding BatchOptions.RealtimeRefreshRate}" Margin="0,0,2,0"/>
360+
<CheckBox Content="Disable History" IsChecked="{Binding BatchOptions.DisableHistory}" Margin="10,2,0,0"/>
360361
</UniformGrid>
361362
<StackPanel.Style>
362363
<Style TargetType="{x:Type StackPanel}">
@@ -380,13 +381,28 @@
380381
</StackPanel>
381382
<ProgressBar Value="{Binding BatchOptions.StepValue}" Maximum="{Binding BatchOptions.StepsValue}" Height="22"/>
382383

383-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,5,0,0 ">
384-
<TextBlock Text="Batch " />
385-
<TextBlock Text="{Binding BatchOptions.BatchValue}" DockPanel.Dock="Left" />
386-
<TextBlock Text=" / " />
387-
<TextBlock Text="{Binding BatchOptions.BatchsValue}" DockPanel.Dock="Right"/>
384+
<StackPanel>
385+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,5,0,0 ">
386+
<TextBlock Text="Batch " />
387+
<TextBlock Text="{Binding BatchOptions.BatchValue}" DockPanel.Dock="Left" />
388+
<TextBlock Text=" / " />
389+
<TextBlock Text="{Binding BatchOptions.BatchsValue}" DockPanel.Dock="Right"/>
390+
</StackPanel>
391+
<ProgressBar Value="{Binding BatchOptions.BatchValue}" Maximum="{Binding BatchOptions.BatchsValue}" Height="22"/>
392+
<StackPanel.Style>
393+
<Style TargetType="{x:Type StackPanel}">
394+
<Setter Property="Visibility" Value="Visible" />
395+
<Style.Triggers>
396+
<DataTrigger Binding="{Binding BatchOptions.BatchType, ElementName=UI}" Value="Realtime" >
397+
<Setter Property="Visibility" Value="Collapsed" />
398+
</DataTrigger>
399+
</Style.Triggers>
400+
</Style>
401+
</StackPanel.Style>
388402
</StackPanel>
389-
<ProgressBar Value="{Binding BatchOptions.BatchValue}" Maximum="{Binding BatchOptions.BatchsValue}" Height="22"/>
403+
404+
405+
390406
</StackPanel>
391407

392408
</StackPanel>

OnnxStack.UI/Utils.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
using OnnxStack.StableDiffusion.Enums;
44
using OnnxStack.UI.Models;
55
using System;
6+
using System.Diagnostics;
67
using System.IO;
8+
using System.Reflection;
79
using System.Text.Json;
810
using System.Text.Json.Serialization;
11+
using System.Threading;
912
using System.Threading.Tasks;
1013
using System.Windows.Media.Imaging;
1114
using System.Windows.Threading;
@@ -193,7 +196,13 @@ public static BatchOptions ToBatchOptions(this BatchOptionsModel batchOptionsMod
193196
};
194197
}
195198

196-
199+
internal static async Task RefreshDelay(long startTime, int refreshRate, CancellationToken cancellationToken)
200+
{
201+
var endTime = Stopwatch.GetTimestamp();
202+
var elapsedMilliseconds = (endTime - startTime) * 1000.0 / Stopwatch.Frequency;
203+
int adjustedDelay = Math.Max(0, refreshRate - (int)elapsedMilliseconds);
204+
await Task.Delay(adjustedDelay, cancellationToken).ConfigureAwait(false);
205+
}
197206

198207
public static void LogToWindow(string message)
199208
{

OnnxStack.UI/Views/TextToImage.xaml.cs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using OnnxStack.StableDiffusion.Helpers;
77
using OnnxStack.UI.Commands;
88
using OnnxStack.UI.Models;
9-
using OnnxStack.UI.UserControls;
109
using System;
1110
using System.Collections.Generic;
1211
using System.Collections.ObjectModel;
@@ -18,7 +17,6 @@
1817
using System.Threading.Tasks;
1918
using System.Windows;
2019
using System.Windows.Controls;
21-
using System.Windows.Media.Imaging;
2220

2321
namespace OnnxStack.UI.Views
2422
{
@@ -201,8 +199,11 @@ private async Task Generate()
201199
if (resultImage != null)
202200
{
203201
ResultImage = resultImage;
204-
ImageResults.Add(resultImage);
205202
HasResult = true;
203+
if (BatchOptions.IsAutomationEnabled && BatchOptions.DisableHistory)
204+
continue;
205+
206+
ImageResults.Add(resultImage);
206207
}
207208
}
208209
}
@@ -295,6 +296,9 @@ private void Reset()
295296
/// <returns></returns>
296297
private async IAsyncEnumerable<ImageResult> ExecuteStableDiffusion(IModelOptions modelOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions, BatchOptions batchOptions)
297298
{
299+
if (!IsExecuteOptionsValid(PromptOptions))
300+
yield break;
301+
298302
_cancelationTokenSource = new CancellationTokenSource();
299303
if (!BatchOptions.IsAutomationEnabled)
300304
{
@@ -320,21 +324,23 @@ private async IAsyncEnumerable<ImageResult> ExecuteStableDiffusion(IModelOptions
320324
while (!_cancelationTokenSource.IsCancellationRequested)
321325
{
322326
var refreshTimestamp = Stopwatch.GetTimestamp();
323-
var realtimePromptOptions = GetPromptOptions(PromptOptions);
324-
var realtimeSchedulerOptions = SchedulerOptions.ToSchedulerOptions();
325-
if (IsLiveOptionsValid(realtimePromptOptions))
327+
if (SchedulerOptions.HasChanged || PromptOptions.HasChanged)
326328
{
329+
PromptOptions.HasChanged = false;
330+
SchedulerOptions.HasChanged = false;
331+
var realtimePromptOptions = GetPromptOptions(PromptOptions);
332+
var realtimeSchedulerOptions = SchedulerOptions.ToSchedulerOptions();
333+
327334
var timestamp = Stopwatch.GetTimestamp();
328-
var result = await _stableDiffusionService.GenerateAsBytesAsync(modelOptions, realtimePromptOptions, realtimeSchedulerOptions, ProgressCallback(), _cancelationTokenSource.Token);
335+
var result = await _stableDiffusionService.GenerateAsBytesAsync(modelOptions, realtimePromptOptions, realtimeSchedulerOptions, RealtimeProgressCallback(), _cancelationTokenSource.Token);
329336
yield return await GenerateResultAsync(result, promptOptions, schedulerOptions, timestamp);
330337
}
331-
await RealtimeRefreshDelay(refreshTimestamp, _cancelationTokenSource.Token);
338+
await Utils.RefreshDelay(refreshTimestamp, BatchOptions.RealtimeRefreshRate, _cancelationTokenSource.Token);
332339
}
333340
}
334341
}
335342

336-
337-
private bool IsLiveOptionsValid(PromptOptions prompt)
343+
private bool IsExecuteOptionsValid(PromptOptionsModel prompt)
338344
{
339345
if (string.IsNullOrEmpty(prompt.Prompt))
340346
return false;
@@ -353,16 +359,6 @@ private PromptOptions GetPromptOptions(PromptOptionsModel promptOptionsModel)
353359
};
354360
}
355361

356-
357-
private async Task RealtimeRefreshDelay(long startTime, CancellationToken cancellationToken)
358-
{
359-
var endTime = Stopwatch.GetTimestamp();
360-
var elapsedMilliseconds = (endTime - startTime) * 1000.0 / Stopwatch.Frequency;
361-
int adjustedDelay = Math.Max(0, BatchOptions.RealtimeRefreshRate - (int)elapsedMilliseconds);
362-
await Task.Delay(adjustedDelay, cancellationToken).ConfigureAwait(false);
363-
}
364-
365-
366362
private async Task<ImageResult> GenerateResultAsync(byte[] imageBytes, PromptOptions promptOptions, SchedulerOptions schedulerOptions, long timestamp)
367363
{
368364
var image = Utils.CreateBitmap(imageBytes);
@@ -428,6 +424,22 @@ private Action<int, int, int, int> ProgressBatchCallback()
428424
};
429425
}
430426

427+
private Action<int, int> RealtimeProgressCallback()
428+
{
429+
return (value, maximum) =>
430+
{
431+
App.UIInvoke(() =>
432+
{
433+
if (_cancelationTokenSource.IsCancellationRequested)
434+
return;
435+
436+
if (BatchOptions.StepValue != value)
437+
BatchOptions.StepValue = value;
438+
if (BatchOptions.StepsValue != maximum)
439+
BatchOptions.StepsValue = maximum;
440+
});
441+
};
442+
}
431443

432444

433445
#region INotifyPropertyChanged

0 commit comments

Comments
 (0)