Skip to content

Commit f777b9a

Browse files
authored
Merge pull request LykosAI#1441 from NeuralFault/comfyui-zluda-cudnnfix2
Inference module to utilize CFZ CUDNN Toggle Node in ComfyUI-Zluda
2 parents c7ccc59 + 4b04e84 commit f777b9a

File tree

9 files changed

+140
-9
lines changed

9 files changed

+140
-9
lines changed

StabilityMatrix.Avalonia/App.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<StyleInclude Source="Controls/Inference/WanModelCard.axaml" />
101101
<StyleInclude Source="Controls/Inference/PlasmaNoiseCard.axaml" />
102102
<StyleInclude Source="Controls/Inference/NrsCard.axaml" />
103+
<StyleInclude Source="Controls/Inference/CfzCudnnToggleCard.axaml" />
103104
<labs:ControlThemes />
104105

105106
<Style Selector="DockControl">
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Styles xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:controls="using:StabilityMatrix.Avalonia.Controls"
4+
xmlns:mocks="clr-namespace:StabilityMatrix.Avalonia.DesignData"
5+
xmlns:vmInference="clr-namespace:StabilityMatrix.Avalonia.ViewModels.Inference"
6+
x:DataType="vmInference:CfzCudnnToggleCardViewModel">
7+
<Design.PreviewWith>
8+
<controls:CfzCudnnToggleCard Width="400" />
9+
</Design.PreviewWith>
10+
11+
<Style Selector="controls|CfzCudnnToggleCard">
12+
<Setter Property="Template">
13+
<ControlTemplate>
14+
<controls:Card x:Name="PART_Card">
15+
<StackPanel Spacing="8">
16+
<TextBlock Text="Disable CUDNN" FontSize="14" FontWeight="Bold" />
17+
<ToggleSwitch
18+
IsChecked="{Binding DisableCudnn}"
19+
Margin="8,0,4,0"
20+
HorizontalAlignment="Stretch" />
21+
</StackPanel>
22+
</controls:Card>
23+
</ControlTemplate>
24+
</Setter>
25+
</Style>
26+
</Styles>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using Injectio.Attributes;
2+
3+
namespace StabilityMatrix.Avalonia.Controls;
4+
5+
[RegisterTransient<CfzCudnnToggleCard>]
6+
public class CfzCudnnToggleCard : TemplatedControlBase;

StabilityMatrix.Avalonia/ViewModels/Base/LoadableViewModelBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace StabilityMatrix.Avalonia.ViewModels.Base;
2626
[JsonDerivedType(typeof(RescaleCfgCardViewModel), RescaleCfgCardViewModel.ModuleKey)]
2727
[JsonDerivedType(typeof(PlasmaNoiseCardViewModel), PlasmaNoiseCardViewModel.ModuleKey)]
2828
[JsonDerivedType(typeof(NrsCardViewModel), NrsCardViewModel.ModuleKey)]
29+
[JsonDerivedType(typeof(CfzCudnnToggleCardViewModel), CfzCudnnToggleCardViewModel.ModuleKey)]
2930
[JsonDerivedType(typeof(FreeUModule))]
3031
[JsonDerivedType(typeof(HiresFixModule))]
3132
[JsonDerivedType(typeof(FluxHiresFixModule))]
@@ -41,6 +42,7 @@ namespace StabilityMatrix.Avalonia.ViewModels.Base;
4142
[JsonDerivedType(typeof(RescaleCfgModule))]
4243
[JsonDerivedType(typeof(PlasmaNoiseModule))]
4344
[JsonDerivedType(typeof(NRSModule))]
45+
[JsonDerivedType(typeof(CfzCudnnToggleModule))]
4446
public abstract class LoadableViewModelBase : ViewModelBase, IJsonLoadableState
4547
{
4648
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
using Injectio.Attributes;
3+
using StabilityMatrix.Avalonia.Controls;
4+
using StabilityMatrix.Avalonia.ViewModels.Base;
5+
using StabilityMatrix.Core.Attributes;
6+
7+
namespace StabilityMatrix.Avalonia.ViewModels.Inference;
8+
9+
[View(typeof(CfzCudnnToggleCard))]
10+
[ManagedService]
11+
[RegisterTransient<CfzCudnnToggleCardViewModel>]
12+
public partial class CfzCudnnToggleCardViewModel : LoadableViewModelBase
13+
{
14+
public const string ModuleKey = "CfzCudnnToggle";
15+
16+
[ObservableProperty]
17+
private bool disableCudnn = true;
18+
}

StabilityMatrix.Avalonia/ViewModels/Inference/InferenceFluxTextToImageViewModel.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ RunningPackageService runningPackageService
9393
{
9494
modulesCard.AvailableModules = new[]
9595
{
96+
typeof(CfzCudnnToggleModule),
97+
typeof(FaceDetailerModule),
9698
typeof(FluxHiresFixModule),
97-
typeof(UpscalerModule),
9899
typeof(SaveImageModule),
99-
typeof(FaceDetailerModule)
100+
typeof(UpscalerModule),
100101
};
101102
modulesCard.DefaultModules = new[] { typeof(FluxHiresFixModule), typeof(UpscalerModule) };
102103
modulesCard.InitializeDefaults();
@@ -121,7 +122,7 @@ protected override void BuildPrompt(BuildPromptEventArgs args)
121122
builder.Connections.Seed = args.SeedOverride switch
122123
{
123124
{ } seed => Convert.ToUInt64(seed),
124-
_ => Convert.ToUInt64(SeedCardViewModel.Seed)
125+
_ => Convert.ToUInt64(SeedCardViewModel.Seed),
125126
};
126127

127128
var applyArgs = args.ToModuleApplyStepEventArgs();
@@ -217,7 +218,7 @@ CancellationToken cancellationToken
217218
FilesToTransfer = buildPromptArgs.FilesToTransfer,
218219
BatchIndex = i,
219220
// Only clear output images on the first batch
220-
ClearOutputImages = i == 0
221+
ClearOutputImages = i == 0,
221222
};
222223

223224
batchArgs.Add(generationArgs);

StabilityMatrix.Avalonia/ViewModels/Inference/InferenceTextToImageViewModel.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ TabContext tabContext
100100
{
101101
modulesCard.AvailableModules = new[]
102102
{
103+
typeof(CfzCudnnToggleModule),
104+
typeof(FaceDetailerModule),
103105
typeof(HiresFixModule),
104-
typeof(UpscalerModule),
105106
typeof(SaveImageModule),
106-
typeof(FaceDetailerModule)
107+
typeof(UpscalerModule),
107108
};
108109
modulesCard.DefaultModules = new[] { typeof(HiresFixModule), typeof(UpscalerModule) };
109110
modulesCard.InitializeDefaults();
@@ -158,7 +159,7 @@ protected override void BuildPrompt(BuildPromptEventArgs args)
158159
builder.Connections.Seed = args.SeedOverride switch
159160
{
160161
{ } seed => Convert.ToUInt64(seed),
161-
_ => Convert.ToUInt64(SeedCardViewModel.Seed)
162+
_ => Convert.ToUInt64(SeedCardViewModel.Seed),
162163
};
163164

164165
var applyArgs = args.ToModuleApplyStepEventArgs();
@@ -319,13 +320,13 @@ CancellationToken cancellationToken
319320
OutputNodeNames = buildPromptArgs.Builder.Connections.OutputNodeNames.ToArray(),
320321
Parameters = SaveStateToParameters(new GenerationParameters()) with
321322
{
322-
Seed = Convert.ToUInt64(seed)
323+
Seed = Convert.ToUInt64(seed),
323324
},
324325
Project = inferenceProject,
325326
FilesToTransfer = buildPromptArgs.FilesToTransfer,
326327
BatchIndex = i,
327328
// Only clear output images on the first batch
328-
ClearOutputImages = i == 0
329+
ClearOutputImages = i == 0,
329330
};
330331

331332
batchArgs.Add(generationArgs);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Injectio.Attributes;
2+
using StabilityMatrix.Avalonia.Models.Inference;
3+
using StabilityMatrix.Avalonia.Services;
4+
using StabilityMatrix.Avalonia.ViewModels.Base;
5+
using StabilityMatrix.Avalonia.ViewModels.Inference;
6+
using StabilityMatrix.Core.Attributes;
7+
using StabilityMatrix.Core.Models.Api.Comfy.Nodes;
8+
using StabilityMatrix.Core.Models.Api.Comfy.NodeTypes;
9+
using StabilityMatrix.Core.Models.Inference;
10+
11+
namespace StabilityMatrix.Avalonia.ViewModels.Inference.Modules;
12+
13+
[ManagedService]
14+
[RegisterTransient<CfzCudnnToggleModule>]
15+
public class CfzCudnnToggleModule : ModuleBase
16+
{
17+
/// <inheritdoc />
18+
public CfzCudnnToggleModule(IServiceManager<ViewModelBase> vmFactory)
19+
: base(vmFactory)
20+
{
21+
Title = "CUDNN Toggle (ComfyUI-Zluda)";
22+
AddCards(vmFactory.Get<CfzCudnnToggleCardViewModel>());
23+
}
24+
25+
/// <summary>
26+
/// Applies CUDNN Toggle node between sampler latent output and VAE decode
27+
/// This prevents "GET was unable to find an engine" errors on AMD cards with Zluda
28+
/// </summary>
29+
protected override void OnApplyStep(ModuleApplyStepEventArgs e)
30+
{
31+
// Get the primary connection (can be latent or image)
32+
var primary = e.Builder.Connections.Primary;
33+
if (primary == null)
34+
{
35+
return; // No primary connection to process
36+
}
37+
38+
// Check if primary is a latent (from sampler output)
39+
if (primary.IsT0) // T0 is LatentNodeConnection
40+
{
41+
var card = GetCard<CfzCudnnToggleCardViewModel>();
42+
var latentConnection = primary.AsT0;
43+
44+
// Insert CUDNN toggle node between sampler and VAE decode
45+
var cudnnToggleOutput = e.Nodes.AddTypedNode(
46+
new ComfyNodeBuilder.CUDNNToggleAutoPassthrough
47+
{
48+
Name = e.Nodes.GetUniqueName("CUDNNToggle"),
49+
Model = null,
50+
Conditioning = null,
51+
Latent = latentConnection, // Pass through the latent from sampler
52+
EnableCudnn = !card.DisableCudnn,
53+
CudnnBenchmark = false,
54+
}
55+
);
56+
57+
// Update the primary connection to use the CUDNN toggle latent output (Output3)
58+
// This ensures VAE decode receives latent from CUDNN toggle instead of directly from sampler
59+
e.Builder.Connections.Primary = cudnnToggleOutput.Output3;
60+
}
61+
}
62+
}

StabilityMatrix.Core/Models/Api/Comfy/Nodes/ComfyNodeBuilder.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,20 @@ public record BrownNoise : ComfyTypedNodeBase<ImageNodeConnection>
997997
public required ulong Seed { get; init; } = 0;
998998
}
999999

1000+
/// <summary>
1001+
/// CUDNN Toggle node for controlling CUDA Deep Neural Network library settings (CUDNNToggleAutoPassthrough)
1002+
/// </summary>
1003+
[TypedNodeOptions(Name = "CUDNNToggleAutoPassthrough")]
1004+
public record CUDNNToggleAutoPassthrough
1005+
: ComfyTypedNodeBase<ModelNodeConnection, ConditioningNodeConnection, LatentNodeConnection>
1006+
{
1007+
public ModelNodeConnection? Model { get; init; }
1008+
public ConditioningNodeConnection? Conditioning { get; init; }
1009+
public LatentNodeConnection? Latent { get; init; }
1010+
public required bool EnableCudnn { get; init; } = false;
1011+
public required bool CudnnBenchmark { get; init; } = false;
1012+
}
1013+
10001014
/// <summary>
10011015
/// Custom KSampler node using alternative noise distribution (Lykos_JDC_PlasmaSampler)
10021016
/// </summary>

0 commit comments

Comments
 (0)