MD: APG Guider
Higher prompt adherence without the CFG burn
- model
- positive
- negative
- apg_guider
- guidance_plot
- config_summary
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
modedropdown picks the variant:pure_apg(standard projection) is the recommended default,pre_cfgapplies it before CFG scaling, andalt1/alt2are 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_sigmacontrol when APG is active. The neat part: it's a two-stage CFG.cfg_beforeapplies during the high-sigma active phase (4.0–7.0 recommended), then when APG deactivates atend_sigma,cfg_aftertakes 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.
Inputs (19)
| Name | Type | Default | Description |
|---|---|---|---|
| model | MODEL | MODEL 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. | |
| positive | CONDITIONING | POSITIVE COND • Purpose: Your main prompt conditioning (what you want). • Usage: Standard positive prompt input. | |
| negative | CONDITIONING | NEGATIVE COND • Purpose: Negative prompt conditioning (what you avoid). • Trade-off: APG uses this heavily for orthogonal projection math. ⭐ Essential for APG logic. | |
| disable_apg | BOOLEAN | false | DISABLE APG • Purpose: Bypass APG entirely for A/B testing. • Effect: If True, acts as standard CFGGuider. ⭐ Use to compare results without rewiring. |
| apg_scale | FLOAT | 4.50–1000 | APG 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_before | FLOAT | 4.01–1000 | CFG (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_after | FLOAT | 3.01–1000 | CFG (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_threshold | FLOAT | 2.50–1000 | NORM 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. |
| momentum | FLOAT | 0.75-1000–1000 | MOMENTUM • 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_sigma | FLOAT | -1.00-1–10000 | START 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_sigma | FLOAT | -1.00-1–10000 | END 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). |
| eta | FLOAT | 0.0-1000–1000 | ETA (DEPRECATED) • Purpose: Legacy parameter from original APG, no longer used in this fork. • Recommendation: Leave at 0.0. |
| dims | STRING | -1, -2 | DIMS • 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_image | BOOLEAN | true | PREDICT 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. |
| mode | COMBO | APG 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_optopt | STRING | YAML 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_modeopt | COMBO | 0 - Silent | LOGGING 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_profilingopt | BOOLEAN | false | ENABLE PROFILING • Purpose: Measure timing of rule building and plotting. • Automatic: Enabled if Debug Mode >= 1. ⭐ Recommended: False. |
| verbose_debugopt | BOOLEAN | false | LEGACY VERBOSE • Deprecated. Use 'debug_mode' instead. |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| apg_guider | GUIDER | — |
| guidance_plot | IMAGE | — |
| config_summary | STRING | — |