← back

Classifier-Free Guidance Skip Speedup Calculator

#447 · Deep Learning · Medium

⊣ Solve on deep-ml.com

Problem

Calculate the inference speedup from applying classifier-free guidance (CFG) skip to a diffusion model. In standard CFG, each denoising step runs the model twice (conditional + unconditional). CFG skip applies full CFG only for the first and last k steps, skipping the unconditional pass for middle steps. Compute the number of model evaluations saved and the resulting speedup.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def cfg_skip_speedup(
    total_steps: int,
    cfg_start_steps: int,
    cfg_end_steps: int
) -> dict:
    cfg_end_steps = min(cfg_end_steps, total_steps - cfg_start_steps)

    full_cfg_steps = min(cfg_start_steps + cfg_end_steps, total_steps)
    skip_steps = total_steps - full_cfg_steps

    standard_evals = total_steps * 2

    skip_evals = full_cfg_steps * 2 + skip_steps * 1

    evals_saved = standard_evals - skip_evals

    speedup = standard_evals / skip_evals if skip_evals > 0 else 1.0

    fraction_saved = evals_saved / standard_evals if standard_evals > 0 else 0.0

    return {
        "total_steps": total_steps,
        "full_cfg_steps": full_cfg_steps,
        "skip_steps": skip_steps,
        "standard_evals": standard_evals,
        "skip_evals": skip_evals,
        "evals_saved": evals_saved,
        "speedup": round(speedup, 4),
        "fraction_saved": round(fraction_saved, 4)
    }

Explanation

  1. Standard CFG requires 2 forward passes per step (conditional + unconditional) for all total_steps.
  2. CFG skip applies double evaluation only during the first cfg_start_steps and last cfg_end_steps steps where guidance matters most.
  3. The middle steps use only the conditional pass (1 evaluation), saving one forward pass per skipped step.
  4. Speedup = standard evaluations / skip evaluations. With 50 steps and skipping 40, we go from 100 to 60 evaluations, a 1.67x speedup.
  5. This exploits the observation that unconditional guidance has minimal effect during mid-denoising, preserving generation quality.

Complexity

  • Time: O(1)
  • Space: O(1)