Sign In

Revolutionizing CFG: Unlocking Flux Distilled Models with Advanced Guidance Algorithms

12
Revolutionizing CFG: Unlocking Flux Distilled Models with Advanced Guidance Algorithms

Revolutionizing CFG: Unlocking Flux Distilled Models with Advanced Guidance Algorithms

In the realm of AI image generation, control is key. The ability to fine-tune and steer model outputs transforms ordinary results into extraordinary creations. Inspired by the work of @Abstract—whose article on CivitAI explores Flux-distilled models and CFG customization—I sought to extend these ideas into practical tools for the community. This project does not introduce new algorithms but instead focuses on packaging Abstract's Forge extension into a convenient custom node for ComfyUI.

This article serves as both an introduction to this tool and an accessible guide for bringing these advanced techniques to your workflow.


Building on Abstract’s Foundation

First and foremost, credit goes to @Abstract for developing the foundation of this work. The Forge extension laid out many of the techniques used here, and my contribution is simply an adaptation to make them accessible as a custom node. If you're interested in diving deeper into Abstract's original insights, I highly recommend checking out his article here.

This tool bridges that conceptual foundation into ComfyUI, creating a seamless integration for the Flux model with enhanced CFG methods.


What Does the Translate Guidance Node Do?

This node introduces 10 advanced guidance methods to the Flux model, allowing users to push the creative limits of CFG (Classifier-Free Guidance) in ways that were not previously possible. Here’s what it brings to the table:

  • Extended Methods: Smooth, dynamic, and random transformations like cosine, ripsaw, and bubble.

  • Enhanced Control: Adjust CFG with fine-grained precision or wild experimental randomness.

  • Creative Freedom: Generate images with unique and tailored aesthetic effects.


How It Works

The Translate Guidance Node enables dynamic transformations to your CFG during image generation by enhancing how conditioning is handled. This functionality leverages the distilled classifier-free guidance (DCFG) mechanism unique to Flux. Here’s a breakdown of how it works and what makes it powerful:

Understanding Guidance in Flux

  1. Individual Guidance Per Conditioning:

    • Each conditioning node—whether for the positive or negative prompt—has its own distinct guidance value.

    • These guidance values are critical in determining how the weights of embeddings, vectors, and other elements affect the generation process.

  2. Default Functionality in Flux:

    • By default, Flux employs the following functionality to process guidance for each conditioning. It directly adjusts the vec (latent vector) by adding the timestep embedding for the guidance value:

    if self.params.guidance_embed:
        if guidance is None:
            raise ValueError("Didn't get guidance strength for guidance distilled model.")
        vec = vec + self.guidance_in(timestep_embedding(guidance, 256).to(img.dtype))
    • This ensures that the guidance values influence the latent vector dynamically at each timestep.

Enhanced Functionality with the Translate Guidance Node

The new implementation allows for more advanced control by introducing dynamic guidance methods. It works as follows:

  1. Set to Default or Custom:

    • The guidance_method can be left as None to use the default behavior, or it can be adjusted to apply custom transformations on a per-conditioning line basis.

  2. Dynamic Adjustment of Guidance:

    • The code dynamically calculates a translated guidance value based on the selected method and the current timestep, producing a higher-quality adjustment to the vector:

    if current_guidance_method and current_guidance_method != "None":
        print(f"Using guidance method: {current_guidance_method} for guidance: {guidance}")
        translated_guidance = translate_guidance(timesteps, guidance, img.device, current_guidance_method)
        output_guidance = translated_guidance
        vec = vec + m.model.diffusion_model.guidance_in(timestep_embedding(translated_guidance, 256).to(img.dtype))
    else:
        output_guidance = timestep_embedding(guidance, 256)
        vec = vec + m.model.diffusion_model.guidance_in(timestep_embedding(guidance, 256).to(img.dtype))

Why Does This Matter?

  • Dynamic Transformations:

    • Unlike the static guidance adjustment in the original implementation, this node enables real-time transformations that consider both timestep progression and guidance value dynamics.

  • Higher-Quality Results:

    • By dynamically calculating a translated guidance value, the model achieves a finer level of control over how each conditioning affects the latent vector.

  • Per-Conditioning Customization:

    • You can set unique guidance methods for the positive and negative prompts, offering unparalleled flexibility for balancing or amplifying specific features in the final image.

Why Use the Translate Guidance Node?

The Translate Guidance Node is not about introducing new algorithms but about enabling the Flux distilled model to reach its full potential by expanding its control and usability. Here’s why this tool is a game-changer:


Broader Control Over CFG in Distilled Models

  • Community Consensus on CFG Limitations:

    • When using distilled models, the CFG (Classifier-Free Guidance) field on the sampler is typically limited to a value of 1, as agreed upon by the community.

    • It’s widely accepted that values above 1.8 often produce degraded quality, with values like 3+ being practically unusable.

  • My Testing Results:

    • Adjusting CFG from 1 to 1.8 generally maintains decent quality and coherence.

    • However, values above 1.8 usually result in poor-quality outputs, making the effective CFG range extremely narrow.


How Translate Guidance Expands CFG Control

By integrating the Translate Guidance Node into your workflow:

  • CFG Range Broadens:

    • The node demonstrates coherent results even at CFG 10, dramatically expanding the usable range of CFG for distilled models.

    • This broader range feels akin to "fixing" whatever limitations were preventing higher CFG values from functioning properly.

  • Behaves Like a De-Distilled Model:

    • While not perfectly matching a de-distilled model (a model where distillation has been undone through additional training), this enhancement gives distilled models a broader range and greater flexibility.

What is De-Distillation? De-distillation refers to retraining a distilled model to remove the constraints of its distillation process, effectively restoring its ability to handle higher CFG values with better coherence and detail.


Improved Results and Creative Freedom

  • Higher Base Quality:

    • With the Translate Guidance Node, you’ll notice better-looking images using the same settings compared to the default setup.

    • CFG values in the expanded range produce cleaner and more coherent outputs compared to what was previously achievable.

  • Independent Control of Positive and Negative Guidance:

    • By allowing you to set unique guidance methods for the positive and negative prompts, the node provides even more creative flexibility.

    • This opens up new possibilities for experimentation, with combinations of guidance methods yielding novel effects.

  • More Creative Freedom:

    • The broader CFG range, combined with the ability to dynamically adjust the guidance method, means you’re no longer restricted to the limited 1–1.8 CFG range.

    • You now have a powerful tool to explore creative ideas with configurations that were previously impossible.


A Step Into Unexplored Territory

  • Innovative Approach:

    • This level of customization is largely unexplored, even within the ComfyUI ecosystem. There are no existing hooks in model_patcher to modify this functionality.

  • Clever Solutions:

    • To implement this, I had to employ creative techniques to override how the model operates, enabling dynamic adjustments to CFG and guidance methods.

  • Transformative Potential:

    • This node turns a distilled model into something that behaves closer to a de-distilled model, offering both higher quality and broader configurability.


By adding the Translate Guidance Node to your workflow, you’re not just enhancing your model’s capabilities—you’re stepping into new creative possibilities. Whether you’re an artist pushing for stylized outputs or a developer exploring the edges of what Flux can do, this tool offers the freedom to create, experiment, and innovate.


Here’s a quick rundown of the most interesting methods you can explore with the Translate Guidance Node:

  • Sin (Top Recommendation):
    This algorithm stands out above all others. Unlike other methods, the sin function produces outputs where the average guidance value is lower than the input guidance. This unique behavior seems to result in dramatically improved quality, with significantly enhanced detail, coherence, and sharpness in the generated images. If you’re looking to get the best results, this should be the first method you try.

  • Inverted Cosine:
    Offers smooth and consistent outputs, making it ideal for clean compositions where subtlety and balance are required.

  • Ripsaw:
    Produces dramatic, spiked peaks in guidance, resulting in attention-grabbing visuals that are bold and dynamic.

  • Bubble:
    Adds dynamic contrast and creates highly stylized results. This method is great for experimental, high-impact imagery.

  • Random Extreme:
    Injects chaotic energy into the guidance values, leading to pseudo-SDE-like images that are unpredictable and creative.


If you’re new to the Translate Guidance Node, start with sin to experience the full potential of this tool. From there, experiment with the others to find the method that aligns best with your creative vision.


Available Guidance Methods

Method Description

cosine Smooth wave-like transitions using cosine function.

inverted_cosine Reverse cosine for inverted, smooth transitions.

sin Periodic sine wave adjustments.

linear_increase Gradual linear increase in guidance strength.

linear_decrease Gradual linear decrease in guidance strength.

random_noise Adds uniform random noise for slight variation.

random_gaussian Adds Gaussian noise for natural randomness.

random_extreme Adds extreme random noise for dramatic variations.

ripsaw Random bit-shift followed by inverted cosine for sharp peaks.

bubble Dual-channel sharp peaks with alternating shifts for dynamic outputs.


Final Thoughts

The Translate Guidance Node was created with one goal: to make Abstract's work more accessible. If you’ve ever wanted to explore the cutting edge of CFG customization in the Flux model, this tool is for you.

I invite you to try it out and share your feedback. Let’s push the boundaries of creativity together.

GitHub Repository: TranslateGuidance

Follow up article with lots of images: https://civitai.com/articles/10128/seeing-the-difference-how-translate-guidance-transforms-image-quality-with-flux

Happy creating!

12

Comments