Skip to content

Commit 2e4033e

Browse files
committed
v0.4.0 - Memory Modes, Checkpoints, Quantization
1 parent 80f9c80 commit 2e4033e

File tree

62 files changed

+2771
-1995
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2771
-1995
lines changed

DiffuseApp/DiffuseApp/App.xaml.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public partial class App : Application
3232
private static string _directoryBase;
3333
private static string _directoryData;
3434
private static string _directoryPython;
35-
private static string _fileSettings;
3635

3736
public App()
3837
{
Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1-
using TensorStack.Python.Common;
1+
using System.Runtime.CompilerServices;
2+
using TensorStack.Python.Common;
23

34
namespace Diffuse.Common
45
{
56
public record DiffusionDefaultOptions
67
{
7-
public float GuidanceScale { get; set; } = 1;
8-
public float GuidanceScale2 { get; set; } = 1;
8+
public float GuidanceScale { get; set; } = 0;
9+
public float GuidanceScale2 { get; set; } = 0;
910
public int Steps { get; set; } = 50;
1011
public int Steps2 { get; set; }
1112
public int Height { get; set; }
1213
public int Width { get; set; }
1314
public int Frames { get; set; }
1415
public float FrameRate { get; set; } = 16;
15-
public float Shift { get; set; } = 1;
1616
public SchedulerType Scheduler { get; set; }
1717
public SchedulerType[] Schedulers { get; set; }
18+
19+
20+
public float BetaStart { get; set; } = 0.00085f;
21+
public float BetaEnd { get; set; } = 0.012f;
22+
public BetaScheduleType BetaSchedule { get; set; } = BetaScheduleType.ScaledLinear;
23+
public TimestepSpacingType TimestepSpacing { get; set; } = TimestepSpacingType.Linspace;
24+
public PredictionType PredictionType { get; set; } = PredictionType.Epsilon;
25+
public SolverType SolverType { get; set; }
26+
public int StepsOffset { get; set; }
27+
public float Shift { get; set; } = 1;
28+
public float BaseShift { get; set; } = 1.15f;
29+
public float MaxShift { get; set; } = 0.5f;
30+
public int BaseImageSeqLen { get; set; } = 256;
31+
public int MaxImageSeqLen { get; set; } = 4096;
32+
public bool UseDynamicShifting { get; set; }
33+
34+
35+
public virtual bool Equals(DiffusionDefaultOptions other) => ReferenceEquals(this, other);
36+
public override int GetHashCode() => RuntimeHelpers.GetHashCode(this);
1837
}
1938
}

DiffuseApp/DiffuseApp/Common/DiffusionModel.cs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ public class DiffusionModel : BaseModel
1717
public string Path { get; set; }
1818
public ModelSourceType Source { get; set; }
1919
public bool IsDefault { get; set; }
20-
public int[] MemoryModes { get; set; }
21-
public DataType[] DataTypes { get; set; }
20+
public MemoryProfile[] MemoryProfile { get; set; }
21+
public DataType BaseType { get; set; }
2222
public ProcessType[] ProcessTypes { get; set; }
2323
public List<SizeOption> Resolutions { get; set; }
2424
public DiffusionDefaultOptions DefaultOptions { get; set; }
2525

26+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
27+
public DiffusionCheckpointModel Checkpoint { get; set; }
28+
2629

2730
[JsonIgnore]
2831
public bool IsValid
@@ -36,10 +39,55 @@ public void Initialize(string modelDirectory)
3639
{
3740
if (Source == ModelSourceType.Folder)
3841
IsValid = Directory.Exists(Path);
39-
else if (Source == ModelSourceType.SingleFile)
40-
IsValid = File.Exists(Path);
4142
else if (Source == ModelSourceType.HuggingFace)
4243
IsValid = Directory.Exists(System.IO.Path.Combine(modelDirectory, Utils.GetHuggingFaceCacheId(Path)));
44+
else if (Source == ModelSourceType.Checkpoint)
45+
{
46+
IsValid = Checkpoint is not null
47+
&& Utils.TryParseHuggingFaceRepo(Path, out _)
48+
&& (string.IsNullOrEmpty(Checkpoint.VaeCheckpoint) || File.Exists(Checkpoint.VaeCheckpoint))
49+
&& (string.IsNullOrEmpty(Checkpoint.ModelCheckpoint) || File.Exists(Checkpoint.ModelCheckpoint))
50+
&& (string.IsNullOrEmpty(Checkpoint.TextEncoderCheckpoint) || File.Exists(Checkpoint.TextEncoderCheckpoint));
51+
}
52+
}
53+
}
54+
55+
public sealed class MemoryProfile : BaseModel
56+
{
57+
public MemoryProfile() { }
58+
public MemoryProfile(DataType dataType, int[] memoryModes)
59+
{
60+
DataType = dataType;
61+
MemoryModes = memoryModes;
62+
}
63+
64+
public DataType DataType { get; set; }
65+
public int[] MemoryModes { get; set; }
66+
}
67+
68+
69+
public sealed class DiffusionCheckpointModel : BaseModel
70+
{
71+
private string _modelCheckpoint;
72+
private string _vaeCheckpoint;
73+
private string _textEncoderCheckpoint;
74+
75+
public string ModelCheckpoint
76+
{
77+
get { return _modelCheckpoint; }
78+
set { SetProperty(ref _modelCheckpoint, value); }
79+
}
80+
81+
public string VaeCheckpoint
82+
{
83+
get { return _vaeCheckpoint; }
84+
set { SetProperty(ref _vaeCheckpoint, value); }
85+
}
86+
87+
public string TextEncoderCheckpoint
88+
{
89+
get { return _textEncoderCheckpoint; }
90+
set { SetProperty(ref _textEncoderCheckpoint, value); }
4391
}
4492
}
4593
}

DiffuseApp/DiffuseApp/Common/ExtractInputOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Diffuse.Common
66
{
7-
public record ExtractInputOptions : BaseRecord
7+
public class ExtractInputOptions : BaseModel
88
{
99
// Default
1010
public TileMode TileMode { get; set; }
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
namespace Diffuse.Common
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Diffuse.Common
24
{
35
public enum MemoryMode
46
{
7+
[Display(Description = "Automatically selects the best memory strategy for the selected device")]
58
Auto = 0,
69

7-
// IsFullOffloadEnabled: true, IsModelOffloadEnabled: false, IsVaeSlicingEnabled: true, IsVaeTilingEnabled: true
8-
Minimum = 1,
10+
[Display(Description = "Balances model weights across all available GPUs and the CPU")]
11+
Balanced = 1,
12+
13+
[Display(Description = "Sequential CPU offload for minimum GPU memory usage")]
14+
Lowest = 2,
15+
16+
[Display(Description = "Model CPU offload with VAE slicing and tiling")]
17+
Low = 3,
918

10-
// IsFullOffloadEnabled: false, IsModelOffloadEnabled: true, IsVaeSlicingEnabled: true, IsVaeTilingEnabled: true
11-
Medium = 3,
19+
[Display(Description = "Model CPU Offload")]
20+
Medium = 4,
1221

13-
// IsFullOffloadEnabled: false, IsModelOffloadEnabled: true, IsVaeSlicingEnabled: false, IsVaeTilingEnabled: false
14-
High = 4,
22+
[Display(Description = "All models on the selected device with VAE slicing and tiling")]
23+
High = 5,
1524

16-
// IsFullOffloadEnabled: false, IsModelOffloadEnabled: false, IsVaeSlicingEnabled: false, IsVaeTilingEnabled: false
17-
Maximum = 5
25+
[Display(Description = "All models on the selected device")]
26+
Highest = 6
1827
}
1928
}

DiffuseApp/DiffuseApp/Common/ModelSourceType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public enum ModelSourceType
44
{
55
Folder = 0,
66
SingleFile = 1,
7+
Checkpoint = 2,
78
HuggingFace = 20
89
}
910
}

DiffuseApp/DiffuseApp/Common/SchedulerInputOptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,9 @@ public record SchedulerInputOptions : BaseRecord
7878

7979
[JsonIgnore]
8080
public float FlowShift => Shift;
81+
82+
public int BaseImageSeqLen { get; set; }
83+
public int MaxImageSeqLen { get; set; }
84+
8185
}
8286
}

DiffuseApp/DiffuseApp/Common/UpscaleInputOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Diffuse.Common
55
{
6-
public record UpscaleInputOptions : BaseRecord
6+
public class UpscaleInputOptions : BaseModel
77
{
88
private TileMode _tileMode;
99
private int _tileSize;

DiffuseApp/DiffuseApp/Controls/DiffusionInputControl.xaml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,28 @@
161161
<CommonControls:SliderControl Text="Input Images" Value="{Binding Options.InputImageCount}" Minimum="1" Maximum="4" TickFrequency="1" ValueFormat="N0" />
162162
</StackPanel>
163163

164-
<StackPanel>
164+
<!--Steps-->
165+
<StackPanel Visibility="{Binding IsSteps2Enabled, Converter={StaticResource InverseBooleanToVisibilityConverter}, ConverterParameter=0}">
165166
<CommonControls:SliderControl Text="Steps" Value="{Binding Options.Steps}" Minimum="1" Maximum="100" TickFrequency="1" />
167+
</StackPanel>
168+
<StackPanel Visibility="{Binding IsSteps2Enabled, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}">
169+
<UniformGrid Columns="2">
170+
<CommonControls:SliderControl Text="Steps (Stage1)" Value="{Binding Options.Steps}" Minimum="1" Maximum="100" TickFrequency="1" Margin="0,0,3,0"/>
171+
<CommonControls:SliderControl Text="Steps (Stage2)" Value="{Binding Options.Steps2}" Minimum="1" Maximum="100" TickFrequency="1" Margin="3,0,0,0"/>
172+
</UniformGrid>
173+
</StackPanel>
174+
175+
176+
<!--GuidanceScale-->
177+
<StackPanel Visibility="{Binding IsGuidance2Enabled, Converter={StaticResource InverseBooleanToVisibilityConverter}}">
166178
<CommonControls:SliderControl Text="Guidance" Value="{Binding Options.GuidanceScale}" Minimum="1" Maximum="20" TickFrequency="0.1" ValueFormat="F2"/>
167179
</StackPanel>
180+
<StackPanel Visibility="{Binding IsGuidance2Enabled, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}">
181+
<UniformGrid Columns="2">
182+
<CommonControls:SliderControl Text="Guidance (Model)" Value="{Binding Options.GuidanceScale2}" Minimum="1" Maximum="20" TickFrequency="0.1" ValueFormat="F2" Margin="0,0,3,0"/>
183+
<CommonControls:SliderControl Text="Guidance (CFG)" Value="{Binding Options.GuidanceScale}" Minimum="1" Maximum="20" TickFrequency="0.1" ValueFormat="F2" Margin="3,0,0,0"/>
184+
</UniformGrid>
185+
</StackPanel>
168186

169187
<!--TextToImage-->
170188
<StackPanel Visibility="{Binding ProcessType, Converter={StaticResource EnumToVisibilityConverter}, ConverterParameter=TextToImage}">
@@ -434,12 +452,12 @@
434452

435453
<!--UpscaleOptions-->
436454
<GroupBox Header="Upscale Options" Margin="0,0,0,6" Visibility="{Binding Pipeline.UpscaleModel, Converter={StaticResource NullVisibilityConverter}, FallbackValue=Collapsed}">
437-
<Controls:UpscaleInputControl Options="{Binding UpscaleOptions}" />
455+
<Controls:UpscaleInputControl Pipeline="{Binding Pipeline, Mode=OneWay}" Options="{Binding UpscaleOptions, Mode=OneWayToSource}" />
438456
</GroupBox>
439457

440458
<!--ExtractOptions-->
441459
<GroupBox Header="Extract Options" Margin="0,0,0,6" Visibility="{Binding Pipeline.ExtractModel, Converter={StaticResource NullVisibilityConverter}, FallbackValue=Collapsed}">
442-
<Controls:ExtractInputControl Options="{Binding ExtractOptions}" ExtractorType="{Binding Pipeline.ExtractModel.Type}" />
460+
<Controls:ExtractInputControl Pipeline="{Binding Pipeline, Mode=OneWay}" Options="{Binding ExtractOptions, Mode=OneWayToSource}" />
443461
</GroupBox>
444462

445463
</UniformGrid>

DiffuseApp/DiffuseApp/Controls/DiffusionInputControl.xaml.cs

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public partial class DiffusionInputControl : BaseControl
2626
private bool _isSchedulerStochastic;
2727
private bool _isSchedulerClipSample;
2828
private bool _isSchedulerThresholding;
29+
private bool _isSteps2Enabled;
30+
private bool _isGuidance2Enabled;
2931

3032
public DiffusionInputControl()
3133
{
@@ -35,7 +37,7 @@ public DiffusionInputControl()
3537
}
3638

3739
public static readonly DependencyProperty PipelineProperty = DependencyProperty.Register(nameof(Pipeline), typeof(PipelineModel), typeof(DiffusionInputControl), new PropertyMetadata<DiffusionInputControl, PipelineModel>((c, o, n) => c.OnPipelineChanged(o, n)));
38-
public static readonly DependencyProperty OptionsProperty = DependencyProperty.Register(nameof(Options), typeof(DiffusionInputOptions), typeof(DiffusionInputControl), new PropertyMetadata<DiffusionInputControl, DiffusionInputOptions>((c, o, n) => c.OnOptionsChanged(o, n)));
40+
public static readonly DependencyProperty OptionsProperty = DependencyProperty.Register(nameof(Options), typeof(DiffusionInputOptions), typeof(DiffusionInputControl));
3941
public static readonly DependencyProperty UpscaleOptionsProperty = DependencyProperty.Register(nameof(UpscaleOptions), typeof(UpscaleInputOptions), typeof(DiffusionInputControl));
4042
public static readonly DependencyProperty ExtractOptionsProperty = DependencyProperty.Register(nameof(ExtractOptions), typeof(ExtractInputOptions), typeof(DiffusionInputControl));
4143

@@ -146,58 +148,77 @@ public bool IsSchedulerThresholding
146148
set { SetProperty(ref _isSchedulerThresholding, value); }
147149
}
148150

151+
public bool IsSteps2Enabled
152+
{
153+
get { return _isSteps2Enabled; }
154+
set { SetProperty(ref _isSteps2Enabled, value); }
155+
}
156+
157+
public bool IsGuidance2Enabled
158+
{
159+
get { return _isGuidance2Enabled; }
160+
set { SetProperty(ref _isGuidance2Enabled, value); }
161+
}
162+
149163

150164
private Task OnPipelineChanged(PipelineModel oldPipeline, PipelineModel newPipeline)
151165
{
152166
if (newPipeline is null || newPipeline.DiffusionModel is null)
153167
return Task.CompletedTask;
154168

155169
var oldModel = oldPipeline?.DiffusionModel;
170+
var oldOptions = oldModel?.DefaultOptions;
156171
var newModel = newPipeline?.DiffusionModel;
172+
var newOptions = newModel?.DefaultOptions;
157173

158-
if (oldModel is not null && oldModel.Pipeline == newModel.Pipeline)
159-
return Task.CompletedTask;
160-
161-
IsModelOptionsVisible = newPipeline.UpscaleModel is not null || newPipeline.ExtractModel is not null;
162-
return Task.CompletedTask;
163-
}
164-
165-
166-
private Task OnOptionsChanged(DiffusionInputOptions oldOptions, DiffusionInputOptions newOptions)
167-
{
168-
if (newOptions is null)
174+
if (oldModel == newModel)
169175
return Task.CompletedTask;
170176

171-
if (oldOptions != null)
177+
var previousOptions = Options;
178+
Options = new DiffusionInputOptions
172179
{
173-
newOptions.Seed = oldOptions.Seed;
174-
newOptions.Prompt = oldOptions.Prompt;
175-
newOptions.NegativePrompt = oldOptions.NegativePrompt;
176-
177-
newOptions.Steps = oldOptions.Steps;
178-
newOptions.Steps2 = oldOptions.Steps2;
179-
newOptions.GuidanceScale = oldOptions.GuidanceScale;
180-
newOptions.GuidanceScale2 = oldOptions.GuidanceScale2;
181-
newOptions.InputImageCount = oldOptions.InputImageCount;
182-
183-
newOptions.Strength = oldOptions.Strength;
184-
newOptions.LoraStrength = oldOptions.LoraStrength;
185-
newOptions.ControlNetStrength = oldOptions.ControlNetStrength;
186-
187-
if (Pipeline.DiffusionModel.DefaultOptions.Schedulers.Contains(oldOptions.Scheduler))
180+
// Keep
181+
Prompt = previousOptions?.Prompt,
182+
NegativePrompt = previousOptions?.NegativePrompt,
183+
Seed = previousOptions?.Seed ?? 0,
184+
LoraStrength = previousOptions?.LoraStrength ?? 1f,
185+
InputImageCount = ProcessType == ProcessType.ImageEdit ? (previousOptions?.InputImageCount ?? 1) : 0,
186+
187+
// Update
188+
Strength = ProcessType == ProcessType.ImageToImage || ProcessType == ProcessType.ControlNetImageToImage ? (previousOptions?.Strength ?? 0.7f) : 1f,
189+
ControlNetStrength = ProcessType == ProcessType.ControlNetImage || ProcessType == ProcessType.ControlNetImageToImage ? (previousOptions?.ControlNetStrength ?? 0.7f) : 1f,
190+
191+
Steps = newOptions.Steps,
192+
Steps2 = newOptions.Steps2,
193+
Scheduler = newOptions.Scheduler,
194+
GuidanceScale = newOptions.GuidanceScale,
195+
GuidanceScale2 = newOptions.GuidanceScale2,
196+
SchedulerOptions = new SchedulerInputOptions
188197
{
189-
newOptions.Scheduler = oldOptions.Scheduler;
190-
newOptions.SchedulerOptions = oldOptions.SchedulerOptions;
198+
Shift = newOptions.Shift,
199+
SolverType = newOptions.SolverType,
200+
PredictionType = newOptions.PredictionType,
201+
BaseShift = newOptions.BaseShift,
202+
BetaEnd = newOptions.BetaEnd,
203+
BetaSchedule = newOptions.BetaSchedule,
204+
BetaStart = newOptions.BetaStart,
205+
MaxShift = newOptions.MaxShift,
206+
StepsOffset = newOptions.StepsOffset,
207+
TimestepSpacing = newOptions.TimestepSpacing,
208+
BaseImageSeqLen = newOptions.BaseImageSeqLen,
209+
MaxImageSeqLen = newOptions.MaxImageSeqLen,
210+
UseDynamicShifting = newOptions.UseDynamicShifting,
191211
}
212+
};
192213

193-
SelectedResolution = Pipeline?.DiffusionModel.Resolutions.FirstOrDefault(x => x.Width == _selectedResolution?.Width && x.Height == _selectedResolution?.Height)
194-
?? Pipeline?.DiffusionModel.Resolutions.FirstOrDefault(x => x.IsDefault);
195-
}
196-
else
197-
{
198-
SelectedResolution = Pipeline?.DiffusionModel.Resolutions.FirstOrDefault(x => x.IsDefault);
199-
}
214+
//Resolution
215+
SelectedResolution = newModel?.Resolutions.FirstOrDefault(x => x.Width == _selectedResolution?.Width && x.Height == _selectedResolution?.Height)
216+
?? newModel?.Resolutions.FirstOrDefault(x => x.IsDefault);
200217

218+
// UI Flags
219+
IsSteps2Enabled = newPipeline.DiffusionModel.DefaultOptions.Steps2 > 0;
220+
IsGuidance2Enabled = newPipeline.DiffusionModel.DefaultOptions.GuidanceScale2 > 0;
221+
IsModelOptionsVisible = newPipeline.UpscaleModel is not null || newPipeline.ExtractModel is not null;
201222
return Task.CompletedTask;
202223
}
203224

0 commit comments

Comments
 (0)