Skip to content

Commit 0770186

Browse files
committed
Add FluxGuidanceModule for using flux fp8 model with regular TextToImage inference project
1 parent 5cb21da commit 0770186

7 files changed

Lines changed: 79 additions & 20 deletions

File tree

StabilityMatrix.Avalonia/ViewModels/Base/LoadableViewModelBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace StabilityMatrix.Avalonia.ViewModels.Base;
3232
[JsonDerivedType(typeof(LoraModule))]
3333
[JsonDerivedType(typeof(LayerDiffuseModule))]
3434
[JsonDerivedType(typeof(FaceDetailerModule))]
35+
[JsonDerivedType(typeof(FluxGuidanceModule))]
3536
public abstract class LoadableViewModelBase : ViewModelBase, IJsonLoadableState
3637
{
3738
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ or nameof(SortConnectedModelsFirst)
318318
)
319319
.Subscribe();
320320

321+
// make filter go
322+
OnPropertyChanged(nameof(HideEmptyRootCategories));
323+
321324
settingsManager.RelayPropertyFor(
322325
this,
323326
vm => vm.IsImportAsConnectedEnabled,
@@ -380,8 +383,6 @@ or nameof(SortConnectedModelsFirst)
380383

381384
// make sure a sort happens
382385
OnPropertyChanged(nameof(SortConnectedModelsFirst));
383-
// gotta do this one too i guess
384-
OnPropertyChanged(nameof(HideEmptyRootCategories));
385386
}
386387

387388
public void ClearSearchQuery()
@@ -852,7 +853,8 @@ private void RefreshCategories()
852853
SelectedCategory =
853854
previouslySelectedCategory
854855
?? Categories.FirstOrDefault(x => x.Path == previouslySelectedCategory?.Path)
855-
?? Categories.First();
856+
?? Categories.FirstOrDefault()
857+
?? categoriesCache.Items[0];
856858

857859
var dirPath = new DirectoryPath(SelectedCategory.Path);
858860

@@ -868,12 +870,6 @@ private void RefreshCategories()
868870

869871
dirPath = dirPath.Parent;
870872
}
871-
872-
// one more time for good measure??
873-
SelectedCategory =
874-
previouslySelectedCategory
875-
?? Categories.FirstOrDefault(x => x.Path == previouslySelectedCategory?.Path)
876-
?? Categories.First();
877873
}
878874

879875
private ObservableCollection<CheckpointCategory> GetSubfolders(string strPath)

StabilityMatrix.Avalonia/ViewModels/Inference/InferenceTextToImageViewModel.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,26 @@ protected override void BuildPrompt(BuildPromptEventArgs args)
142142
// Load models
143143
ModelCardViewModel.ApplyStep(applyArgs);
144144

145-
// Setup empty latent
146-
builder.SetupEmptyLatentSource(
147-
SamplerCardViewModel.Width,
148-
SamplerCardViewModel.Height,
149-
BatchSizeCardViewModel.BatchSize,
150-
BatchSizeCardViewModel.IsBatchIndexEnabled ? BatchSizeCardViewModel.BatchIndex : null
151-
);
145+
if (SamplerCardViewModel.ModulesCardViewModel.IsModuleEnabled<FluxGuidanceModule>())
146+
{
147+
// need SD3Latent
148+
builder.SetupEmptySd3LatentSource(
149+
SamplerCardViewModel.Width,
150+
SamplerCardViewModel.Height,
151+
BatchSizeCardViewModel.BatchSize,
152+
BatchSizeCardViewModel.IsBatchIndexEnabled ? BatchSizeCardViewModel.BatchIndex : null
153+
);
154+
}
155+
else
156+
{
157+
// Setup empty latent
158+
builder.SetupEmptyLatentSource(
159+
SamplerCardViewModel.Width,
160+
SamplerCardViewModel.Height,
161+
BatchSizeCardViewModel.BatchSize,
162+
BatchSizeCardViewModel.IsBatchIndexEnabled ? BatchSizeCardViewModel.BatchIndex : null
163+
);
164+
}
152165

153166
// Prompts and loras
154167
PromptCardViewModel.ApplyStep(applyArgs);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using StabilityMatrix.Avalonia.Models.Inference;
2+
using StabilityMatrix.Avalonia.Services;
3+
using StabilityMatrix.Avalonia.ViewModels.Base;
4+
using StabilityMatrix.Core.Attributes;
5+
6+
namespace StabilityMatrix.Avalonia.ViewModels.Inference.Modules;
7+
8+
[ManagedService]
9+
[Transient]
10+
public class FluxGuidanceModule : ModuleBase
11+
{
12+
public FluxGuidanceModule(ServiceManager<ViewModelBase> vmFactory)
13+
: base(vmFactory)
14+
{
15+
Title = "Use Flux Guidance";
16+
}
17+
18+
protected override void OnApplyStep(ModuleApplyStepEventArgs e) { }
19+
}

StabilityMatrix.Avalonia/ViewModels/Inference/SamplerCardViewModel.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ ServiceManager<ViewModelBase> vmFactory
117117
[
118118
typeof(FreeUModule),
119119
typeof(ControlNetModule),
120-
typeof(LayerDiffuseModule)
120+
typeof(LayerDiffuseModule),
121+
typeof(FluxGuidanceModule)
121122
];
122123
});
123124
}
@@ -286,6 +287,23 @@ private void ApplyStepsInitialSampler(ModuleApplyStepEventArgs e)
286287
var conditioning = e.Temp.Base.Conditioning.Unwrap();
287288
var refinerConditioning = e.Temp.Refiner.Conditioning;
288289

290+
var useFluxGuidance = ModulesCardViewModel.IsModuleEnabled<FluxGuidanceModule>();
291+
292+
if (useFluxGuidance)
293+
{
294+
// Flux guidance
295+
var fluxGuidance = e.Nodes.AddTypedNode(
296+
new ComfyNodeBuilder.FluxGuidance
297+
{
298+
Name = e.Nodes.GetUniqueName("FluxGuidance"),
299+
Conditioning = conditioning.Positive,
300+
Guidance = CfgScale
301+
}
302+
);
303+
304+
conditioning = conditioning with { Positive = fluxGuidance.Output };
305+
}
306+
289307
// Use custom sampler if SDTurbo scheduler is selected
290308
if (e.Builder.Connections.PrimaryScheduler == ComfyScheduler.SDTurbo)
291309
{
@@ -320,7 +338,7 @@ private void ApplyStepsInitialSampler(ModuleApplyStepEventArgs e)
320338
Model = e.Builder.Connections.Base.Model,
321339
AddNoise = true,
322340
NoiseSeed = e.Builder.Connections.Seed,
323-
Cfg = CfgScale,
341+
Cfg = useFluxGuidance ? 1.0d : CfgScale,
324342
Positive = conditioning.Positive,
325343
Negative = conditioning.Negative,
326344
Sampler = kSamplerSelect.Output,
@@ -344,7 +362,7 @@ private void ApplyStepsInitialSampler(ModuleApplyStepEventArgs e)
344362
SamplerName = primarySampler.Name,
345363
Scheduler = primaryScheduler.Name,
346364
Steps = Steps,
347-
Cfg = CfgScale,
365+
Cfg = useFluxGuidance ? 1.0d : CfgScale,
348366
Positive = conditioning.Positive,
349367
Negative = conditioning.Negative,
350368
LatentImage = primaryLatent,
@@ -365,7 +383,7 @@ private void ApplyStepsInitialSampler(ModuleApplyStepEventArgs e)
365383
AddNoise = true,
366384
NoiseSeed = e.Builder.Connections.Seed,
367385
Steps = TotalSteps,
368-
Cfg = CfgScale,
386+
Cfg = useFluxGuidance ? 1.0d : CfgScale,
369387
SamplerName = primarySampler.Name,
370388
Scheduler = primaryScheduler.Name,
371389
Positive = conditioning.Positive,

StabilityMatrix.Avalonia/ViewModels/Inference/StackEditableCardViewModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ private void AddModule(Type type)
117117
AddCards(card!);
118118
}
119119

120+
public bool IsModuleEnabled<T>(int index = 0)
121+
where T : ModuleBase
122+
{
123+
var card = Cards.OfType<T>().ElementAtOrDefault(index);
124+
return card is { IsEnabled: true };
125+
}
126+
120127
/*/// <inheritdoc />
121128
public override void LoadStateFromJsonObject(JsonObject state)
122129
{

StabilityMatrix.Avalonia/Views/CheckpointsPage.axaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@
231231
IconSource="Filter"
232232
VerticalAlignment="Center"
233233
Label="Filter">
234+
<ui:CommandBarButton.Styles>
235+
<Style Selector="FlyoutPresenter">
236+
<Setter Property="MaxHeight" Value="1000"/>
237+
</Style>
238+
</ui:CommandBarButton.Styles>
234239
<ui:CommandBarButton.Flyout>
235240
<Flyout>
236241
<StackPanel Orientation="Vertical">

0 commit comments

Comments
 (0)