পাঠ ১৫ · ২৮-এর মধ্যে · মডিউল ৩
Home / AI Courses / Generative AI / DDIM sampling

DDIM — দ্রুত sampling

DDIM — fast deterministic sampling
৭ মিনিট পড়া মাঝারি+ · Intermediate+ 20× speedup

এই পাঠে যা শিখবেন

  • DDPM-এর slow sampling — কেন bottleneck
  • DDIM-এর non-Markovian formulation এবং একই training share করার যুক্তি
  • η parameter — stochastic থেকে deterministic continuum
  • DDIM inversion — real image ↔ noise round-trip

১ · DDPM-এর sampling সমস্যা

DDPM training fast (random $t$ pick), কিন্তু sampling — sequential ১০০০ U-Net call। একটি ছবি তৈরিতে A100 GPU-এ ~৫-১০ সেকেন্ড। ব্যাচ ৪-এ একটি batch মিনিট বা তার বেশি।

Production-এ এটি অগ্রহণযোগ্য। DALL·E 2 (২০২২), Stable Diffusion-এর সব কিছুই DDIM-নির্ভর।

২ · DDIM-এর key insight

Song, Meng, Ermon (ICLR ২০২১, "Denoising Diffusion Implicit Models") দেখালেন: DDPM-এর training objective $\mathcal{L}_{\text{simple}}$ শুধু marginal $q(\mathbf{x}_t \mid \mathbf{x}_0)$-এর উপর নির্ভর করে — full Markov chain-এর joint-এর উপর নয়।

মানে — অন্য একটি forward process ব্যবহার করুন যা একই marginal রাখে কিন্তু non-Markovian; sampling চলবে।

Mind-bending fact

একই trained network ($\epsilon_\theta$) — different sampling procedure use করুন। DDPM (1000 step), DDIM (50 step), DPM-Solver (20 step) — সব same model, different solver। Re-training লাগে না!

৩ · DDIM update rule

$\mathbf{x}_t \to \mathbf{x}_{t-1}$ generic update:

$$\mathbf{x}_{t-1} = \sqrt{\bar\alpha_{t-1}}\underbrace{\Big(\frac{\mathbf{x}_t - \sqrt{1-\bar\alpha_t}\,\epsilon_\theta(\mathbf{x}_t, t)}{\sqrt{\bar\alpha_t}}\Big)}_{\hat{\mathbf{x}}_0 \text{ prediction}} + \underbrace{\sqrt{1-\bar\alpha_{t-1} - \sigma_t^2}\,\epsilon_\theta(\mathbf{x}_t, t)}_{\text{direction toward } \mathbf{x}_t} + \underbrace{\sigma_t \boldsymbol\epsilon}_{\text{random noise}}$$

$\sigma_t = \eta \sqrt{(1-\bar\alpha_{t-1})/(1-\bar\alpha_t)} \sqrt{1 - \bar\alpha_t/\bar\alpha_{t-1}}$।

  • $\eta = 1$: $\sigma_t = $ DDPM-এর $\tilde\beta_t$ — exactly DDPM update।
  • $\eta = 0$: $\sigma_t = 0$ — deterministic, no random noise injection। PF-ODE-এর Euler step।
  • $\eta \in (0, 1)$: interpolated stochasticity।

৪ · Step skipping — কম step-এ sample

Update rule যেকোনো $\bar\alpha_{t-1}, \bar\alpha_t$ (যেকোনো two timesteps) এর জন্য কাজ করে — পাশাপাশি দুটি হতে হবে এমন না!

Sub-sequence $\tau_1 < \tau_2 < \ldots < \tau_S$ (যেমন $S = 50$, evenly spaced)। Reverse traverse: $\mathbf{x}_{\tau_S} \to \mathbf{x}_{\tau_{S-1}} \to \ldots \to \mathbf{x}_{\tau_1} \to \mathbf{x}_0$।

Quality vs speed: 1000 step (full DDPM) → 250 step (Improved DDPM) → 50 step (DDIM) → 20 step (DPM-Solver++) → 4 step (LCM/Turbo) → 1 step (Consistency Model)। Recent research এই trade-off-এর frontier।
DDPM (sequential, η=1) vs DDIM (skip, η=0) DDPM: 1000 sequential steps + random noise ···(950 more)··· x_T x_0 +ε +ε +ε +ε ~10 sec / image (A100) DDIM: 50 skipped steps, deterministic (η=0) ·· 45 more ·· x_τ_50 (=x_1000) x_0 skip 20 skip 20 skip 20 ~0.5 sec — 20× Same trained ε_θ! Different sampler. Skip 20 timesteps each step → similar quality, 20× faster. η=0: same noise → same image (deterministic). η=1: DDPM (stochastic).
DDPM প্রতি step ছোট, sequential, stochastic। DDIM 20 step skip — same trained model, deterministic update। Production-এ DDIM standard।

৫ · DDIM inversion — image ↔ noise

Deterministic DDIM ($\eta = 0$) — bijective mapping $\mathbf{x}_0 \leftrightarrow \mathbf{x}_T$।

Inversion: Real image $\mathbf{x}_0$ → forward DDIM steps → estimated $\mathbf{x}_T$।

Reverse from same $\mathbf{x}_T$ → approximately reconstruct $\mathbf{x}_0$।

Application:

  • SDEdit, prompt-to-prompt editing।
  • Real image-এ Stable Diffusion edit।
  • Style transfer, image interpolation।

৬ · PyTorch — DDIM sampler

Python · PyTorch
import torch

@torch.no_grad()
def ddim_sample(model, alpha_bar, shape, n_steps=50, eta=0.0, device='cuda'):
    """DDIM sampling — alpha_bar precomputed for full T=1000."""
    T = len(alpha_bar)
    # uniform sub-sequence: τ_1, ..., τ_S
    tau = torch.linspace(0, T - 1, n_steps + 1).long().to(device)

    x = torch.randn(shape, device=device)
    for i in reversed(range(n_steps)):
        t  = tau[i + 1]
        tp = tau[i]                       # t-prev (closer to 0)
        ab_t  = alpha_bar[t]
        ab_tp = alpha_bar[tp] if tp >= 0 else torch.tensor(1.0, device=device)

        eps = model(x, t.unsqueeze(0).expand(shape[0]))

        # predict x_0
        x0_pred = (x - (1 - ab_t).sqrt() * eps) / ab_t.sqrt()

        # variance σ_t (η controls stochasticity)
        sigma = eta * ((1 - ab_tp) / (1 - ab_t)).sqrt() * (1 - ab_t / ab_tp).sqrt()

        # direction term
        dir_xt = (1 - ab_tp - sigma ** 2).sqrt() * eps

        noise = sigma * torch.randn_like(x) if eta > 0 else 0.

        x = ab_tp.sqrt() * x0_pred + dir_xt + noise
    return x

    
n_steps=50, eta=0 — Stable Diffusion-এর default। eta=1 — DDPM equivalent। বিভিন্ন n_steps (10, 25, 50, 100) compare করুন।

৭ · Beyond DDIM — modern samplers

  • DPM-Solver (Lu et al. 2022): ODE-এর high-order solver। 10-20 step quality।
  • DPM-Solver++: Multi-step, stable for guidance scale বেশি।
  • UniPC (Zhao et al. 2023): Predictor-corrector, 5-10 step।
  • Heun (EDM): 2nd-order, 30-40 step optimal।
  • Consistency Models (Song 2023): 1-4 step generation।
  • LCM/Turbo: Distilled 1-4 step variant of SD/SDXL।
Practical: HuggingFace diffusers-এ ১০+ scheduler ready। Test multiple: DDIM, DPM-Solver++, Euler-A — visualize differences। Same prompt+seed-এ different sampler different aesthetic দেয়।

ভাবনার প্রশ্ন

প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।

প্র ০১ DDIM "একই training, ভিন্ন sampling" — এই trick গাণিতিকভাবে কীভাবে justify হয়? Non-Markovian forward process শুরুতে paradoxical লাগে — কেন কাজ করে?

DDIM-এর elegant insight — diffusion training-এর implicit assumption খতিয়ে দেখা।

DDPM training revisited:

  • Training loss: $\mathbb{E}_{t, \mathbf{x}_0, \epsilon} \|\epsilon - \epsilon_\theta(\mathbf{x}_t, t)\|^2$।
  • $\mathbf{x}_t$ generated via $\mathbf{x}_t = \sqrt{\bar\alpha_t} \mathbf{x}_0 + \sqrt{1-\bar\alpha_t}\epsilon$।
  • Loss only uses marginal $q(\mathbf{x}_t \mid \mathbf{x}_0)$ — joint $q(\mathbf{x}_{1:T} \mid \mathbf{x}_0)$ irrelevant।

DDIM observation:

  • Define new forward process $q_\sigma(\mathbf{x}_{1:T} \mid \mathbf{x}_0)$ — non-Markovian, parameterized by $\sigma_t$।
  • Constraint: same marginals $q_\sigma(\mathbf{x}_t \mid \mathbf{x}_0) = q(\mathbf{x}_t \mid \mathbf{x}_0)$ DDPM-এর।
  • Training objective unchanged — same $\epsilon_\theta$ valid।
  • But reverse process $q_\sigma(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0)$ different — gives different sampling rule।

Non-Markovian detail:

  • $q_\sigma(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0) = \mathcal{N}(\sqrt{\bar\alpha_{t-1}} \mathbf{x}_0 + \sqrt{1-\bar\alpha_{t-1} - \sigma_t^2} \cdot \frac{\mathbf{x}_t - \sqrt{\bar\alpha_t}\mathbf{x}_0}{\sqrt{1-\bar\alpha_t}}, \sigma_t^2 \mathbf{I})$।
  • Mean depends on both $\mathbf{x}_t$ এবং $\mathbf{x}_0$ — chain depends on origin, not just immediate predecessor।
  • $\sigma_t = 0$ — completely deterministic chain।
  • $\sigma_t = \sqrt{(1-\bar\alpha_{t-1})/(1-\bar\alpha_t)} \cdot \sqrt{\beta_t}$ — recovers DDPM's Markovian process।

Why "paradoxical":

  • Standard intuition: forward process determines what reverse can be।
  • DDIM: reverse process design choice with constraint (same marginals)।
  • One trained model — family of reverse processes।

Practical justification (empirical):

  • Same model checkpoint — DDPM, DDIM, DPM-Solver all give comparable FID scores।
  • η = 0 sometimes better quality at low step counts।
  • Higher η সমস্যায় more diversity, less prompt adherence।

Theoretical depth:

  • Yang Song's PF-ODE (পাঠ ১৪) generalizes — ODE deterministic, SDE stochastic, both same marginals।
  • DDIM ($\eta=0$) discrete-time PF-ODE Euler solver।
  • Modern samplers (DPM-Solver, UniPC) — higher-order ODE solvers।

Implication for research:

  • "Train once, sample many ways" — paradigm shift।
  • Sampler innovation independent of training innovation।
  • Pretrained Stable Diffusion + new sampler (Heun, UniPC) → instant quality boost।
  • Reduces compute waste — no retraining needed।

মূল উপলব্ধি: DDIM-এর contribution মূলত conceptual — "marginal preservation suffices"। এই idea-র উপর সমস্ত আধুনিক fast samplers, image inversion, editing — সব দাঁড়িয়ে। Theory simple, impact massive।

প্র ০২ $\eta$ parameter quality-diversity trade-off-কে কীভাবে control করে? কখন $\eta = 0$ ভাল, কখন $\eta > 0$? ImageNet vs face vs text rendering — different domain-এ optimal $\eta$ ভিন্ন কেন?

$\eta$ — DDIM-এর হৃদয়, কিন্তু optimal choice context-dependent।

$\eta$ semantics:

  • $\eta = 0$: Deterministic — same noise → same image। Predictable।
  • $\eta = 1$: DDPM stochasticity — even same noise → varied output।
  • $\eta \in (0, 1)$: Interpolated — controlled randomness।

$\eta = 0$ (deterministic) ভাল কখন:

  • Reproducibility critical: Production deployment — same prompt, same seed, same image।
  • Image inversion: Real image → noise → re-generate। Round-trip accuracy দরকার।
  • Few steps (5-20): Stochasticity-এর জন্য enough rooms নেই।
  • Editing applications: SDEdit, prompt-to-prompt — deterministic baseline।
  • Latent space arithmetic: Interpolation, slerp — bijective mapping দরকার।

$\eta > 0$ (stochastic) ভাল কখন:

  • Diversity-critical: Same prompt-এ varied creative output।
  • Many steps (100+): Stochasticity error correct করে।
  • Rare modes: Deterministic high-density region-এ stuck হতে পারে; stochastic explore।
  • High-resolution detail: Texture, fine detail-এ stochasticity natural।

Domain-specific observations:

  • ImageNet (object class): $\eta = 0$ adequate, structure dominant।
  • Face (FFHQ, CelebA): $\eta = 0.3-0.5$ — slight stochasticity helps skin texture।
  • Text rendering: $\eta = 0$ critical — text legibility deterministic chain-এ ভাল।
  • Painterly art: $\eta > 0$ — brushstrokes, randomness এর adverse না।
  • Photorealistic landscape: $\eta = 0.2-0.4$ optimal।

$\eta$ এর interaction with steps:

  • Few steps + high $\eta$ → noisy output (insufficient denoising)।
  • Many steps + $\eta = 0$ → "smooth" but possibly generic।
  • Many steps + $\eta = 1$ → DDPM equivalent — high quality but slow।

$\eta$ এর interaction with CFG:

  • High guidance scale (CFG-এ $w > 7$) — accumulated drift, $\eta = 0$ amplifies error।
  • $\eta > 0$ — error stochastically corrected।
  • SDXL-এর default Euler-A: $\eta$ implicit, CFG-friendly।

Modern samplers (post-DDIM):

  • Euler discrete: $\eta$ explicit।
  • Euler ancestral (Euler-A): $\eta$-like stochasticity, popular default।
  • DPM++ 2M Karras: low effective $\eta$, deterministic-leaning।
  • DPM++ 2S Karras: stochastic, high-quality, slower।
  • LCM, Turbo: $\eta$ irrelevant — distilled few-step।

Practical guidance:

  • Default: $\eta = 0$, 50 step DDIM।
  • Explore $\eta = 0.3-0.7$ creative output-এর জন্য।
  • A/B test domain-specific।
  • Editing/inversion task: $\eta = 0$ always।

মূল উপলব্ধি: $\eta$ "creativity knob" — প্রায়ই overlooked but powerful। SDXL/SD3-এর built-in samplers এই trade-off-কে abstract করে রেখেছে, কিন্তু নিচের mathematics একই। Domain understanding + experimentation = optimal choice।

প্র ০৩ DDIM inversion image editing-এ revolutionize করল। SDEdit, Prompt-to-Prompt, Null-text inversion, InstructPix2Pix — কী কী technique এসেছে এবং কোনটি কখন use করব?

DDIM inversion image editing landscape-কে fundamental change এনেছে। Real image-এ AI edit — যা GAN-এ অসম্ভব ছিল।

Core idea — DDIM Inversion:

  • Real image $\mathbf{x}_0$ + caption $c$ → forward DDIM steps with $\epsilon_\theta(\mathbf{x}_t, t, c)$ → $\mathbf{x}_T$ approx pure noise।
  • Reverse with same $c$ → reconstruct original।
  • Reverse with different $c'$ → edited image।

(১) SDEdit (Meng et al. 2021, "Stochastic Differential Editing"):

  • Real image + edit guide (rough sketch/painting)।
  • Forward partial steps (e.g., $t = 500$ out of 1000) — partial noise।
  • Reverse with target prompt → guided edit।
  • Use case: rough sketch → photo, color edit।
  • Strength = forward step count (low: subtle, high: aggressive)।

(২) Prompt-to-Prompt (Hertz et al. 2022):

  • Cross-attention map manipulation।
  • Same noise, slight prompt change ("a cat" → "a dog") → controlled edit।
  • Attention map of original word "cat" → injected for edit。
  • Local edit possible — keep background, change subject।
  • No inversion needed for synthesized images।

(৩) Null-text Inversion (Mokady et al. 2022):

  • SDXL/SD CFG-তে inversion accurate নয়।
  • Trick: optimize null-text embedding to make inversion exact।
  • Per-image fine-tuning (~1 min)।
  • Combined with Prompt-to-Prompt — real image edit highly accurate।

(৪) InstructPix2Pix (Brooks et al. 2023):

  • Different paradigm — finetune SD on (input, edit instruction, output) triples।
  • "Make it sunset", "add a hat" — natural language edit।
  • Synthesized training data via Prompt-to-Prompt + GPT-3।
  • No per-image inversion — fast inference।

(৫) ControlNet (পাঠ ১৮):

  • Different but related — structure preservation via conditioning।
  • Canny edge, depth, pose — frozen content, change style।

(৬) MasaCtrl, Cross-Image Attention:

  • Subject preservation across edits।
  • "Same character, different action"।

(৭) Latent Blend (Avrahami et al. 2022):

  • Mask-guided edit — local replacement।
  • Inpaint specific region with prompt।

কোনটি কখন:

  • Subtle stylistic edit: SDEdit (strength 0.3-0.6)।
  • Word swap (cat→dog): Prompt-to-Prompt + Null-text inversion।
  • Natural language instruction: InstructPix2Pix বা latest GPT-4o image edit।
  • Local edit (specific region): Inpainting + mask।
  • Subject preservation: DreamBooth (পাঠ ১৯) + edit।
  • Structure-preserving style change: ControlNet।

Production tools (২০২৪-২৫):

  • Adobe Firefly: Generative Fill — inpainting-based।
  • Photoshop Generative Expand: Outpainting।
  • Krea, Runway: Real-time edit, motion।
  • FLUX-Kontext, Qwen-Image-Edit (2024): Native edit models।

Open challenges:

  • Identity preservation (face) consistent edit।
  • Multi-subject scene complex edit।
  • Text-in-image edit — unsolved।
  • Video edit temporal consistency।

মূল উপলব্ধি: Image editing-এ ৩-বছরে যা ঘটেছে — DDIM inversion + cross-attention manipulation এর গণিত — Photoshop ১৫ বছরে যা পারেনি। আজকের designer-এর hybrid workflow: Photoshop precision + diffusion creativity। বাংলাদেশের designer/agency-দের এই tool-গুলো শিখে দ্রুত international market-এ entry possible।

প্র ০৪ Consistency Models (Song 2023) ও LCM/Turbo এক step-এই ছবি তৈরি করে। DDIM/DDPM এর সাথে এদের relation কী এবং কেন distillation এত effective? quality-speed trade-off কোথায় পৌঁছেছে?

২০২৩-২৪-এ "few-step diffusion" এক revolutionary breakthrough — real-time image generation possible।

Background — কেন many-step needed ছিল:

  • Diffusion-এ noise level $\sigma_T \to \sigma_0$ traverse করতে হয়।
  • প্রতি step এ small ODE/SDE update — accumulated trajectory দরকার।
  • Big jump = network must learn complex multimodal mapping।

Consistency Models (Song, Dhariwal, Chen, Sutskever 2023):

  • Idea: train network $f_\theta(\mathbf{x}_t, t)$ to predict $\mathbf{x}_0$ directly from any $t$।
  • Consistency property: $f(\mathbf{x}_t, t) = f(\mathbf{x}_{t'}, t')$ for any $t, t'$ on same PF-ODE trajectory।
  • Two training modes:
    • Consistency Distillation (CD): from pretrained diffusion model।
    • Consistency Training (CT): from scratch।
  • One forward pass → image। 1-step generation।
  • Multi-step refinement possible (2-4 step)।

LCM (Latent Consistency Models, Luo et al. 2023):

  • Apply Consistency Distillation to Stable Diffusion-এর latent space।
  • Result: 4-step generation, comparable quality to SD's 50-step।
  • LCM-LoRA: distill into LoRA adapter — apply to any SD checkpoint।
  • Real-time generation possible (10+ FPS on consumer GPU)।

SDXL Turbo (Stability AI 2023):

  • Adversarial Diffusion Distillation (ADD) — combine consistency + GAN loss।
  • 1-4 step inference at SDXL quality।
  • Released as open weights।

SD3 Turbo, Flux Schnell:

  • SD3-distilled 4-step variant।
  • Flux.1 Schnell: 1-4 step at near-Flux-Pro quality।
  • License: Schnell Apache-2.0 (commercial OK)।

Why distillation works so well:

  • Teacher provides trajectory: Pretrained diffusion's PF-ODE trajectory — student learns shortcut।
  • Inductive bias: Diffusion already learned data distribution — distillation只 changes sampling speed।
  • Model capacity: Same architecture — sufficient capacity for direct mapping।
  • Pixel-precise supervision: Teacher output as target — strong signal।

Quality-speed Pareto frontier:

  • 1 step: Best quality currently FID ~5-8 (vs ~3 for 50-step)। Quality "good enough" for most।
  • 4 step: Near-parity with 50-step। Practical sweet spot।
  • 20 step: SOTA quality, modest speedup।
  • 50+ step: Marginal gains, mostly legacy।

Trade-offs:

  • Diversity loss: Distilled models slightly less diverse — mode coverage narrows।
  • Editability: DDIM inversion-based editing harder with few-step models।
  • Fine detail: Texture, hands, text — multi-step still slightly better।
  • CFG fragility: High guidance scale-এ artifacts বেশি।

Production implications:

  • Real-time interactive AI canvas (Krea, Runway)।
  • Mobile generation (Apple, Google on-device)।
  • Cost reduction — 50× cheaper API serving।
  • Live-stream generation for video games, social filter।

Recent developments (২০২৪-২৫):

  • Hyper-SD: 1-step distillation, near-multistep quality।
  • SDXL Lightning: 2-8 step variants।
  • InstaFlow: Rectified Flow + distillation।
  • StreamDiffusion: pipeline parallelism for video।

Theoretical questions:

  • Is 1-step lower bound? Or can we go below the diffusion trajectory?
  • Can we train direct-1-step from scratch (no diffusion teacher)?
  • Quality-speed Pareto fundamental limit?

মূল উপলব্ধি: ৩ বছরে diffusion sampling 1000-step → 1-step। GAN-এর 1-step + diffusion-এর quality — best of both worlds। ২০২৫-এ practical generative AI mostly 4-step models। Performance bottleneck shift হয়েছে inference থেকে training & data curation-এ। বাংলাদেশে real-time AI feature build করতে — LCM/Turbo learn করা must।

অনুশীলন

  1. Speed estimate: SD1.5-এ একটি ছবি 50-step DDIM-এ 5 সেকেন্ড লাগে। 1000-step DDPM-এ আনুমানিক কত? 4-step LCM-এ?
    • 1000-step DDPM: $5 \times (1000/50) = 100$ সেকেন্ড।
    • 4-step LCM: $5 \times (4/50) = 0.4$ সেকেন্ড।
    • Real-time interactive UX-এর জন্য LCM essential।
  2. $\eta = 0$ verify: উপরের code-এ eta=0 দিয়ে same seed দু'বার চালালে identical output পান কি? কেন?

    হ্যাঁ — $\eta = 0$ মানে $\sigma_t = 0$, কোনো random noise injection নেই। শুধু initial $\mathbf{x}_T$ random। Same torch.manual_seed() + same prompt + $\eta = 0$ → bit-exact same output।

    Production reproducibility-এর জন্য critical।

  3. HuggingFace explore: diffusers library-তে DDIMScheduler, EulerDiscreteScheduler, DPMSolverMultistepScheduler — তিনটি দিয়ে same prompt চালান। Visual difference লক্ষ্য করুন।
    from diffusers import StableDiffusionPipeline, DDIMScheduler, DPMSolverMultistepScheduler
    pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
    prompt = "a photo of a cat in cosmic space"
    
    for sched_cls in [DDIMScheduler, DPMSolverMultistepScheduler]:
        pipe.scheduler = sched_cls.from_config(pipe.scheduler.config)
        img = pipe(prompt, num_inference_steps=20, generator=torch.manual_seed(42)).images[0]
        img.save(f"{sched_cls.__name__}.png")

    DPM-Solver typically sharper at low steps; DDIM smoother।

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

Hands-on: Colab-এ diffusers + LCM-LoRA चालান — 4-step real-time generation-এর experience নিন।
পূর্ববর্তী পাঠ
পাঠ ১৪ · Score matching ও SDE