Skip to content

Commit 07828b3

Browse files
committed
v0.4.8 - Model weight caching
1 parent ccc21dc commit 07828b3

35 files changed

+573
-51
lines changed

DiffuseApp/DiffuseApp.Common/Message/PipelineRequest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public PipelineRequest(PipelineConfig config)
2121
Type = RequestType.PipelineLoad;
2222
}
2323

24+
public PipelineRequest(PipelineReloadOptions options)
25+
{
26+
PipelineReloadOptions = options;
27+
Type = RequestType.PipelineReload;
28+
}
29+
2430
public PipelineRequest(EnvironmentConfig config, bool isRebuild, bool isReinstall)
2531
{
2632
Environment = new EnvironmentRequest
@@ -45,6 +51,7 @@ public PipelineRequest(PipelineOptions options)
4551
public RequestType Type { get; init; }
4652
public EnvironmentRequest Environment { get; set; }
4753
public PipelineConfig PipelineConfig { get; set; }
54+
public PipelineReloadOptions PipelineReloadOptions { get; set; }
4855
public PipelineOptions PipelineOptions { get; set; }
4956
public int ImageTensorCount { get; set; }
5057
public int ControlNetTensorCount { get; set; }

DiffuseApp/DiffuseApp.Common/Message/RequestType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ internal enum RequestType
77
Environment = 2,
88
PipelineLoad = 3,
99
PipelineUnload = 4,
10-
PipelineRun = 5
10+
PipelineRun = 5,
11+
PipelineReload = 6
1112
}
1213
}

DiffuseApp/DiffuseApp.Common/PipelineClient.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ public async Task LoadAsync(PipelineConfig pipeline, CancellationToken cancellat
112112
}
113113

114114

115+
/// <summary>
116+
/// Reload as an asynchronous operation.
117+
/// </summary>
118+
/// <param name="pipeline">The pipeline.</param>
119+
/// <param name="cancellationToken">The cancellation token.</param>
120+
public async Task ReloadAsync(PipelineReloadOptions options, CancellationToken cancellationToken = default)
121+
{
122+
try
123+
{
124+
_isCanceled = false;
125+
await SendPipelineRequestAsync(new PipelineRequest(options), cancellationToken);
126+
}
127+
catch (OperationCanceledException)
128+
{
129+
await KillServerAsync();
130+
throw;
131+
}
132+
}
133+
134+
115135
/// <summary>
116136
/// Unload the PythonPipeline
117137
/// </summary>

DiffuseApp/DiffuseApp.Common/PipelineServer.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ private async Task StartPipelineChannelAsync(CancellationToken cancellationToken
120120
{
121121
pipeline = await LoadPipelineAsync(request, cancellationToken);
122122
}
123+
else if (request.Type == RequestType.PipelineReload)
124+
{
125+
await ReloadPipelineAsync(request, pipeline, cancellationToken);
126+
}
123127
else if (request.Type == RequestType.PipelineUnload)
124128
{
125129
await UnloadPipelineAsync(request, pipeline, cancellationToken);
@@ -307,6 +311,32 @@ private async Task<PythonPipeline> LoadPipelineAsync(PipelineRequest request, Ca
307311
}
308312

309313

314+
/// <summary>
315+
/// Reload the pipeline
316+
/// </summary>
317+
/// <param name="request">The request.</param>
318+
/// <param name="pipeline">The pipeline.</param>
319+
/// <param name="cancellationToken">The cancellation token.</param>
320+
private async Task<PythonPipeline> ReloadPipelineAsync(PipelineRequest request, PythonPipeline pipeline, CancellationToken cancellationToken)
321+
{
322+
try
323+
{
324+
CallbackMessage("Reloading Pipeline...", "Initialize");
325+
await pipeline.ReloadAsync(request.PipelineReloadOptions);
326+
await _pipelineChannel.SendResponse(cancellationToken);
327+
328+
CallbackMessage(string.Empty, "Initialize");
329+
return pipeline;
330+
}
331+
catch (Exception ex)
332+
{
333+
_logger.LogError(ex, "[PipelineServer] [ReloadPipeline] An exception occurred reloading pipeline.");
334+
await _pipelineChannel.SendMessage(new PipelineResponse(ex), cancellationToken);
335+
return default;
336+
}
337+
}
338+
339+
310340
/// <summary>
311341
/// Unloads the pipeline
312342
/// </summary>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Diffuse.Common;
2+
using TensorStack.WPF;
3+
4+
namespace DiffuseApp.Common
5+
{
6+
public class MemoryProfileModel : BaseModel
7+
{
8+
private int _memoryGB;
9+
private MemoryMode _memoryMode;
10+
11+
public MemoryMode MemoryMode
12+
{
13+
get { return _memoryMode; }
14+
set { SetProperty(ref _memoryMode, value); }
15+
}
16+
public int MemoryGB
17+
{
18+
get { return _memoryGB; }
19+
set { SetProperty(ref _memoryGB, value); }
20+
}
21+
}
22+
23+
24+
}

DiffuseApp/DiffuseApp/Common/PipelineModel.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,24 @@ public DataType DataType
7171
}
7272

7373

74-
public bool IsReloadRequired(PipelineModel pipeline)
74+
public bool IsLoadRequired(PipelineModel pipeline)
7575
{
7676
return pipeline is null
7777
|| pipeline.DiffusionModel != _diffusionModel
78-
|| pipeline.LoraAdapterModel.HasChanged(_loraAdapterModel)
79-
|| pipeline.ControlNetModel != _controlNetModel
8078
|| pipeline.MemoryMode != _memoryMode
81-
|| pipeline.DataType != _dataType
82-
|| pipeline.ProcessType != _processType;
79+
|| pipeline.DataType != _dataType;
8380
}
8481

82+
83+
public bool IsReloadRequired(PipelineModel pipeline)
84+
{
85+
if (pipeline is null || pipeline.DiffusionModel != _diffusionModel)
86+
return false;
87+
88+
// ProcessType, LoraAdapters and ControlNet are the only options that can be modified
89+
return pipeline.ProcessType != _processType
90+
|| pipeline.ControlNetModel != _controlNetModel
91+
|| pipeline.LoraAdapterModel.HasChanged(_loraAdapterModel);
92+
}
8593
}
8694
}

DiffuseApp/DiffuseApp/Controls/DiffusionModelControl.xaml.cs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Diffuse.Common;
2+
using DiffuseApp.Common;
23
using System;
34
using System.Collections.ObjectModel;
45
using System.Linq;
@@ -566,24 +567,56 @@ public bool HasLoraChanged()
566567
return _currentLora.HasChanged(LoraAdapters);
567568
}
568569

569-
}
570-
571-
public class MemoryProfileModel : BaseModel
572-
{
573-
private int _memoryGB;
574-
private MemoryMode _memoryMode;
575570

576-
public MemoryMode MemoryMode
571+
public void SetPipeline(PipelineModel pipeline)
577572
{
578-
get { return _memoryMode; }
579-
set { SetProperty(ref _memoryMode, value); }
580-
}
581-
public int MemoryGB
582-
{
583-
get { return _memoryGB; }
584-
set { SetProperty(ref _memoryGB, value); }
573+
if (pipeline == null)
574+
return;
575+
576+
if (!ModelCollectionView.Contains(pipeline.DiffusionModel))
577+
return;
578+
579+
SelectedDevice = pipeline.Device;
580+
SelectedModel = pipeline.DiffusionModel;
581+
582+
SelectedDataType = pipeline.DataType;
583+
SelectedMemoryMode = SelectedMemoryMode = MemoryModes.FirstOrDefault(x => x.MemoryMode == pipeline.MemoryMode);
584+
585+
if (IsUpscalerSupported)
586+
{
587+
IsUpscalerEnabled = pipeline.UpscaleModel is not null;
588+
if (pipeline.UpscaleModel is not null)
589+
SelectedUpscaler = pipeline.UpscaleModel;
590+
}
591+
592+
if (IsExtractorSupported)
593+
{
594+
IsExtractorEnabled = pipeline.ExtractModel is not null;
595+
if (pipeline.ExtractModel is not null)
596+
SelectedExtractor = pipeline.ExtractModel;
597+
}
598+
599+
if (IsControlNetSupported)
600+
{
601+
if (pipeline.ControlNetModel is not null)
602+
SelectedControlNet = pipeline.ControlNetModel;
603+
}
604+
605+
if (IsLoraSupported)
606+
{
607+
IsLoraEnabled = !pipeline.LoraAdapterModel.IsNullOrEmpty();
608+
if (IsLoraEnabled)
609+
{
610+
LoraAdapters.Clear();
611+
foreach (var loraAdapter in pipeline.LoraAdapterModel)
612+
{
613+
LoraAdapters.Add(loraAdapter);
614+
}
615+
}
616+
}
617+
618+
ValidateSelection();
585619
}
586620
}
587621

588-
589622
}

DiffuseApp/DiffuseApp/Controls/ModelControl.xaml.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ private bool CanUnload()
161161
private bool HasCurrentChanged()
162162
{
163163
return _currentDevice != SelectedDevice
164-
|| _currentExtractor != SelectedExtractor
165-
|| _currentUpscaler != SelectedUpscaler;
164+
|| (IsExtractorEnabled && _currentExtractor != SelectedExtractor)
165+
|| (IsUpscalerEnabled && _currentUpscaler != SelectedUpscaler);
166166
}
167167

168168

@@ -240,12 +240,27 @@ private Task OnIsPipelineLoadedChanged()
240240

241241
private void ValidateSelection()
242242
{
243-
244243
var isExtractValid = !IsExtractorEnabled || ExtractCollectionView?.IsEmpty == false;
245244
var isUpscaleValid = !IsUpscalerEnabled || UpscaleCollectionView?.IsEmpty == false;
246245
var isCurrentValid = !HasCurrentChanged();
247246
IsSelectionValid = isCurrentValid && isExtractValid && isUpscaleValid && IsPipelineLoaded;
248247
LoadCommand.RaiseCanExecuteChanged();
249248
}
249+
250+
251+
public void SetPipeline(PipelineModel pipeline)
252+
{
253+
if (pipeline == null)
254+
return;
255+
256+
SelectedDevice = pipeline.Device;
257+
if (IsUpscalerEnabled && pipeline.UpscaleModel is not null)
258+
SelectedUpscaler = pipeline.UpscaleModel;
259+
260+
if (IsExtractorEnabled && pipeline.ExtractModel is not null)
261+
SelectedExtractor = pipeline.ExtractModel;
262+
263+
ValidateSelection();
264+
}
250265
}
251266
}

0 commit comments

Comments
 (0)