Nodes/MD Nodes/MD: APG Guider
ComfyUI Node

MD: APG Guider

Higher prompt adherence without the CFG burn

By MDMAchine·Created about a year ago·Updated 3 months ago· 15
MD: APG Guider
  • model
  • positive
  • negative
  • apg_guider
  • guidance_plot
  • config_summary
disable_apgfalse
apg_scale4.5
cfg_before4.0
cfg_after3.0
norm_threshold2.5
momentum0.75
start_sigma-1.00
end_sigma-1.00
eta0.0
dims-1, -2
predict_imagetrue
mode
yaml_parameters_opt
debug_mode0 - Silent
enable_profilingfalse
verbose_debugfalse

The standard way to force a model to follow your prompt is CFG, and the standard problem with CFG is that cranking it burns the image - oversaturated, blown-out, "overcooked" results. APG (Adaptive Projected Gradient) is a family of guidance techniques that tries to have it both ways: project the guidance vector onto a cleaner direction so the model follows the prompt hard without the artifacts. This node is MD's fork of that idea, and the pack credits blepping with the original concept.

It's a GUIDER node: feed it model, positive, and negative conditioning, and it returns a guider object you connect to a KSampler's guider input, replacing the built-in CFG guider. If you've ever run a distilled/turbo model and gotten CFG burn at low scale, this is aimed at exactly that failure mode.

How it works

Under the hood it's the classic CFG formula - uncond + (cond - uncond) * scale - but with three corrections on top:

  • Orthogonal projection - the guidance correction is projected onto a direction orthogonal to the current prediction, which is the "adaptive projected gradient" trick that stops oversteering. The mode dropdown picks the variant: pure_apg (standard projection) is the recommended default, pre_cfg applies it before CFG scaling, and alt1/alt2 are alternative momentum blends.
  • Momentum (momentum, default 0.75) - a running average across steps that smooths the guidance vectors. Positive values stabilize; the source warns negative values oscillate.
  • Norm thresholding (norm_threshold, default 2.5) - caps the guidance vector's magnitude so it can't blow out. Lower = safer, weaker.
  • Sigma scheduling - start_sigma / end_sigma control when APG is active. The neat part: it's a two-stage CFG. cfg_before applies during the high-sigma active phase (4.0–7.0 recommended), then when APG deactivates at end_sigma, cfg_after takes over (2.0–3.5) so the low-noise detail phase runs gentler. That handoff is the whole "steer hard early, refine softly late" philosophy.

apg_scale (default 4.5) is the strength of the orthogonal correction - 3.0–6.0 is the band; higher enforces adherence but risks burning, same trade as CFG, just wider. predict_image (default True) targets the x0/v-prediction domain rather than epsilon, which the tooltip says works better for modern models. disable_apg turns the whole thing into a plain CFG guider for A/B testing without rewiring - genuinely useful.

The eta input is explicitly marked DEPRECATED - leave it at 0, this fork doesn't use it.

Outputs

apg_guider (wire into KSampler), plus two debug outputs: guidance_plot (a rendered image showing the guidance schedule - you can see where APG is active and where it hands off) and config_summary (a string). There's also an optional yaml_parameters_opt input for advanced multi-stage schedules - a list of dicts with start_sigma/cfg/apg_scale - which overrides the sliders if valid.

The honest take

APG-style guidance is real research (the projection idea is well established), and the sigma-scheduled two-CFG structure is a thoughtful design. But it's a fiddly node from a niche pack - you'll be tuning apg_scale, momentum, and thresholds against your specific model, and the community footprint is small enough that you're mostly on your own. Start with defaults, watch guidance_plot to see the schedule, and only reach for it if plain CFG burn is a problem you actually have.

Installing

It's part of MD Nodes:

cd path/to/ComfyUI/custom_nodes
git clone https://github.com/MDMAchine/ComfyUI_MD_Nodes.git
cd ComfyUI_MD_Nodes && pip install -r requirements.txt

Or via ComfyUI Manager (search MD Nodes), then restart. Heavy requirements (audio libs, matplotlib, client SDKs) come with the pack.

CategoryMD_Nodes/Guidance

Inputs (19)

NameTypeDefaultDescription
modelMODELMODEL INPUT • Purpose: The diffusion model to apply APG guidance to. • Requirement: Any standard diffusion model (SD1.5, SDXL, Flux). • Processing: Wraps model in custom CFGGuider.
positiveCONDITIONINGPOSITIVE COND • Purpose: Your main prompt conditioning (what you want). • Usage: Standard positive prompt input.
negativeCONDITIONINGNEGATIVE COND • Purpose: Negative prompt conditioning (what you avoid). • Trade-off: APG uses this heavily for orthogonal projection math. ⭐ Essential for APG logic.
disable_apgBOOLEANfalseDISABLE APG • Purpose: Bypass APG entirely for A/B testing. • Effect: If True, acts as standard CFGGuider. ⭐ Use to compare results without rewiring.
apg_scaleFLOAT4.50–1000APG SCALE • Purpose: Strength of orthogonal correction (how much to fix the path). • Range: 0.0 (Off) to 20.0 (Extreme). • Trade-offs: Higher values enforce prompt adherence but may 'burn' the image. • Recommended: 3.0 - 6.0. ⭐ Start with 4.5 for balanced results.
cfg_beforeFLOAT4.01–1000CFG (BEFORE) • Purpose: Static CFG applied *during* the APG active phase (High Sigma). • Range: 1.0 to 30.0. • Trade-offs: Lower values allow more creativity; higher values enforce strictness. ⭐ Recommended: 4.0 - 7.0.
cfg_afterFLOAT3.01–1000CFG (AFTER) • Purpose: Static CFG applied *after* APG deactivates (Low Sigma). • Range: 1.0 to 30.0. • Trade-offs: Lower values here improve fine details and texture. ⭐ Recommended: 2.0 - 3.5.
norm_thresholdFLOAT2.50–1000NORM THRESHOLD • Purpose: Cap the guidance vector magnitude to prevent artifacts. • Range: 0.0 (No Cap) to 10.0. • Trade-offs: Lower values prevent burning but reduce guidance strength. ⭐ Recommended: 2.5.
momentumFLOAT0.75-1000–1000MOMENTUM • Purpose: Smooths guidance vectors across steps using a running average. • Range: -1.0 to 1.0. • Effect: Positive values stabilize; negative values oscillate. ⭐ Recommended: 0.5 - 0.75.
start_sigmaFLOAT-1.00-1–10000START SIGMA • Purpose: Noise level where APG activates. • Options: -1.0 (Infinity/Always On) or specific sigma (e.g., 15.0). • Logic: Activates when current sigma <= start_sigma. ⭐ Recommended: -1.0 (or match your scheduler's max sigma).
end_sigmaFLOAT-1.00-1–10000END SIGMA • Purpose: Noise level where APG deactivates (switching to 'cfg_after'). • Options: -1.0 (Never disable) or specific sigma (e.g., 1.0). • Logic: Deactivates when current sigma < end_sigma. ⭐ Recommended: 1.0 to 3.0 (let the model refine details freely).
etaFLOAT0.0-1000–1000ETA (DEPRECATED) • Purpose: Legacy parameter from original APG, no longer used in this fork. • Recommendation: Leave at 0.0.
dimsSTRING-1, -2DIMS • Purpose: Dimensions for normalization and projection. • Format: Comma-separated integers (e.g., '-1, -2'). • Meaning: -1,-2 corresponds to Height/Width spatial dimensions. ⭐ Recommended: '-1, -2'.
predict_imageBOOLEANtruePREDICT IMAGE • Purpose: Defines target domain for guidance. • Options: True (v-prediction/x0), False (epsilon/noise). • Trade-offs: True usually works better for modern models. ⭐ Recommended: True.
modeCOMBOAPG MODE • Purpose: Algorithm variant selection. • Options: - pure_apg: Standard orthogonal projection. - pre_cfg: Applies projection before CFG scaling. - alt1/alt2: Alternative momentum blending strategies. ⭐ Recommended: 'pure_apg'.
yaml_parameters_optoptSTRINGYAML OVERRIDE • Purpose: Define complex multi-stage schedules. • Format: List of dicts with 'start_sigma', 'cfg', 'apg_scale', etc. • Priority: Overrides slider inputs if valid. ⭐ Advanced users only.
debug_modeoptCOMBO0 - SilentLOGGING VERBOSITY • Purpose: Controls console output details. • Options: - 0: Minimal output. - 1: Basic info + Profiler report. - 2: Step-by-step guidance logs. ⭐ Recommended: 0 for production, 1 for tuning.
enable_profilingoptBOOLEANfalseENABLE PROFILING • Purpose: Measure timing of rule building and plotting. • Automatic: Enabled if Debug Mode >= 1. ⭐ Recommended: False.
verbose_debugoptBOOLEANfalseLEGACY VERBOSE • Deprecated. Use 'debug_mode' instead.

Outputs (3)

NameTypeDescription
apg_guiderGUIDER
guidance_plotIMAGE
config_summarySTRING