Skip to content

Commit c56e65a

Browse files
authored
Merge pull request #74 from DarthAffe/v6
Updated sd.cpp to 5792c66
2 parents 8fbbfde + e8349b8 commit c56e65a

12 files changed

Lines changed: 47 additions & 20 deletions

Header/stable-diffusion.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ enum sample_method_t {
4848
LCM_SAMPLE_METHOD,
4949
DDIM_TRAILING_SAMPLE_METHOD,
5050
TCD_SAMPLE_METHOD,
51+
RES_MULTISTEP_SAMPLE_METHOD,
52+
RES_2S_SAMPLE_METHOD,
5153
SAMPLE_METHOD_COUNT
5254
};
5355

@@ -62,6 +64,7 @@ enum scheduler_t {
6264
SMOOTHSTEP_SCHEDULER,
6365
KL_OPTIMAL_SCHEDULER,
6466
LCM_SCHEDULER,
67+
BONG_TANGENT_SCHEDULER,
6568
SCHEDULER_COUNT
6669
};
6770

@@ -186,6 +189,7 @@ typedef struct {
186189
bool keep_clip_on_cpu;
187190
bool keep_control_net_on_cpu;
188191
bool keep_vae_on_cpu;
192+
bool flash_attn;
189193
bool diffusion_flash_attn;
190194
bool tae_preview_only;
191195
bool diffusion_conv_direct;
@@ -197,7 +201,7 @@ typedef struct {
197201
bool chroma_use_t5_mask;
198202
int chroma_t5_mask_pad;
199203
bool qwen_image_zero_cond_t;
200-
float flow_shift;
204+
201205
} sd_ctx_params_t;
202206

203207
typedef struct {
@@ -231,6 +235,7 @@ typedef struct {
231235
int shifted_timestep;
232236
float* custom_sigmas;
233237
int custom_sigmas_count;
238+
float flow_shift;
234239
} sd_sample_params_t;
235240

236241
typedef struct {

StableDiffusion.NET/Enums/Sampler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ public enum Sampler
1414
LCM,
1515
DDIM_Trailing,
1616
TCD,
17+
ResMultistep,
18+
Res2S,
1719
Default
1820
}

StableDiffusion.NET/Enums/Schedule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public enum Scheduler
1212
Smoothstep,
1313
KlOptimal,
1414
LCM,
15+
BongTangent,
1516
Default
1617
}

StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ public sealed class DiffusionModelParameter
6060
/// </summary>
6161
public bool KeepVaeOnCPU { get; set; } = false;
6262

63+
public bool FlashAttention { get; set; } = false;
64+
6365
/// <summary>
6466
/// use flash attention in the diffusion model (for low vram)
6567
/// Might lower quality, since it implies converting k and v to f16.
6668
/// This might crash if it is not supported by the backend.
6769
/// </summary>
68-
public bool FlashAttention { get; set; } = false;
70+
public bool DiffusionFlashAttention { get; set; } = false;
6971

7072
public bool TaePreviewOnly { get; set; } = false;
7173

@@ -101,8 +103,6 @@ public sealed class DiffusionModelParameter
101103
/// </summary>
102104
public Quantization Quantization { get; set; } = Quantization.Unspecified;
103105

104-
public float FlowShift { get; set; } = float.PositiveInfinity;
105-
106106
public bool ForceSdxlVaeConvScale { get; set; } = false;
107107

108108
/// <summary>

StableDiffusion.NET/Models/Parameter/Extensions/DiffusionModelBuilderExtension.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ public static DiffusionModelParameter WithFlashAttention(this DiffusionModelPara
120120
return parameter;
121121
}
122122

123+
public static DiffusionModelParameter WithDiffusionFlashAttention(this DiffusionModelParameter parameter, bool flashAttention = true)
124+
{
125+
parameter.DiffusionFlashAttention = flashAttention;
126+
127+
return parameter;
128+
}
129+
123130
public static DiffusionModelParameter WithDiffusionConvDirect(this DiffusionModelParameter parameter, bool diffusionConvDirect = true)
124131
{
125132
parameter.DiffusionConvDirect = diffusionConvDirect;
@@ -159,13 +166,6 @@ public static DiffusionModelParameter WithQuantization(this DiffusionModelParame
159166
return parameter;
160167
}
161168

162-
public static DiffusionModelParameter WithFlowShift(this DiffusionModelParameter parameter, float flowShift)
163-
{
164-
parameter.FlowShift = flowShift;
165-
166-
return parameter;
167-
}
168-
169169
public static DiffusionModelParameter WithForcedSdxlVaeConvScale(this DiffusionModelParameter parameter, bool forcedScale = true)
170170
{
171171
parameter.ForceSdxlVaeConvScale = forcedScale;

StableDiffusion.NET/Models/Parameter/Extensions/ImageGenerationParameterExtension.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ public static ImageGenerationParameter WithShiftedTimestep(this ImageGenerationP
175175
return parameter;
176176
}
177177

178+
public static ImageGenerationParameter WithFlowShift(this ImageGenerationParameter parameter, float flowShift)
179+
{
180+
parameter.SampleParameter.FlowShift = flowShift;
181+
182+
return parameter;
183+
}
184+
178185
#endregion
179186

180187
public static ImageGenerationParameter WithStrength(this ImageGenerationParameter parameter, float strength)

StableDiffusion.NET/Models/Parameter/Extensions/VideoGenerationParameterExtension.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ public static VideoGenerationParameter WithShiftedTimestep(this VideoGenerationP
161161
return parameter;
162162
}
163163

164+
public static VideoGenerationParameter WithFlowShift(this VideoGenerationParameter parameter, float flowShift)
165+
{
166+
parameter.SampleParameter.FlowShift = flowShift;
167+
168+
return parameter;
169+
}
170+
164171
#endregion
165172

166173
#region HighNoiseSampleParameter

StableDiffusion.NET/Models/Parameter/SampleParameter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public sealed class SampleParameter
2828

2929
public float[] CustomSigmas { get; set; } = [];
3030

31+
public float FlowShift { get; set; } = float.PositiveInfinity;
32+
3133
internal SampleParameter() { }
3234
}

StableDiffusion.NET/Native/Marshaller/DiffusionModelParameterMarshaller.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public static DiffusionModelParameter ConvertToManaged(Native.Types.sd_ctx_param
3939
KeepClipOnCPU = unmanaged.keep_clip_on_cpu == 1,
4040
KeepControlNetOnCPU = unmanaged.keep_control_net_on_cpu == 1,
4141
KeepVaeOnCPU = unmanaged.keep_vae_on_cpu == 1,
42-
FlashAttention = unmanaged.diffusion_flash_attn == 1,
42+
DiffusionFlashAttention = unmanaged.diffusion_flash_attn == 1,
43+
FlashAttention = unmanaged.flash_attn == 1,
4344
TaePreviewOnly = unmanaged.tae_preview_only == 1,
4445
DiffusionConvDirect = unmanaged.diffusion_conv_direct == 1,
4546
VaeConvDirect = unmanaged.vae_conv_direct == 1,
@@ -50,7 +51,6 @@ public static DiffusionModelParameter ConvertToManaged(Native.Types.sd_ctx_param
5051
ChromaEnableT5Map = unmanaged.chroma_use_t5_mask == 1,
5152
ChromaT5MaskPad = unmanaged.chroma_t5_mask_pad,
5253
QwenImageZeroCondT = unmanaged.qwen_image_zero_cond_t == 1,
53-
FlowShift = unmanaged.flow_shift
5454
};
5555

5656
for (int i = 0; i < unmanaged.embedding_count; i++)
@@ -120,7 +120,8 @@ public void FromManaged(DiffusionModelParameter managed)
120120
keep_clip_on_cpu = (sbyte)(managed.KeepClipOnCPU ? 1 : 0),
121121
keep_control_net_on_cpu = (sbyte)(managed.KeepControlNetOnCPU ? 1 : 0),
122122
keep_vae_on_cpu = (sbyte)(managed.KeepVaeOnCPU ? 1 : 0),
123-
diffusion_flash_attn = (sbyte)(managed.FlashAttention ? 1 : 0),
123+
diffusion_flash_attn = (sbyte)(managed.DiffusionFlashAttention ? 1 : 0),
124+
flash_attn = (sbyte)(managed.FlashAttention ? 1 : 0),
124125
tae_preview_only = (sbyte)(managed.TaePreviewOnly ? 1 : 0),
125126
diffusion_conv_direct = (sbyte)(managed.DiffusionConvDirect ? 1 : 0),
126127
vae_conv_direct = (sbyte)(managed.VaeConvDirect ? 1 : 0),
@@ -130,8 +131,7 @@ public void FromManaged(DiffusionModelParameter managed)
130131
chroma_use_dit_mask = (sbyte)(managed.ChromaUseDitMap ? 1 : 0),
131132
chroma_use_t5_mask = (sbyte)(managed.ChromaEnableT5Map ? 1 : 0),
132133
chroma_t5_mask_pad = managed.ChromaT5MaskPad,
133-
qwen_image_zero_cond_t = (sbyte)(managed.QwenImageZeroCondT ? 1 : 0),
134-
flow_shift = managed.FlowShift
134+
qwen_image_zero_cond_t = (sbyte)(managed.QwenImageZeroCondT ? 1 : 0)
135135
};
136136
}
137137

StableDiffusion.NET/Native/Marshaller/SampleParameterMarshaller.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public static SampleParameter ConvertToManaged(Native.Types.sd_sample_params_t u
3333
SampleSteps = unmanaged.sample_steps,
3434
Eta = unmanaged.eta,
3535
ShiftedTimestep = unmanaged.shifted_timestep,
36-
CustomSigmas = new float[unmanaged.custom_sigmas_count]
36+
CustomSigmas = new float[unmanaged.custom_sigmas_count],
37+
FlowShift = unmanaged.flow_shift
3738
};
3839

3940
if (unmanaged.guidance.slg.layers != null)
@@ -86,7 +87,8 @@ public void FromManaged(SampleParameter managed)
8687
eta = managed.Eta,
8788
shifted_timestep = managed.ShiftedTimestep,
8889
custom_sigmas = _customSigmas,
89-
custom_sigmas_count = managed.CustomSigmas.Length
90+
custom_sigmas_count = managed.CustomSigmas.Length,
91+
flow_shift = managed.FlowShift
9092
};
9193
}
9294

0 commit comments

Comments
 (0)