#447 · Deep Learning · Medium
⊣ Solve on deep-ml.comCalculate 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.
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)
}total_steps.cfg_start_steps and last cfg_end_steps steps where guidance matters most.