Agent Conversation: deepseek-reasoner
Task: cv-diffusion-prediction
Diffusion Prediction Parameterization
Background
In DDPM training, the model predicts a target from noisy input x_t. The noisy sample is constructed as:
x_t = sqrt(alpha_t) * x_0 + sqrt(1 - alpha_t) * epsilon
There are three standard parameterizations for what the model predicts:
- Epsilon prediction (Ho et al., 2020): predict the noise epsilon
- X0 prediction: directly predict the clean image x_0
- V-prediction (Salimans & Ho, 2022): predict the velocity v = sqrt(alpha_t) * epsilon - sqrt(1 - alpha_t) * x_0
These are mathematically equivalent (one can be converted to any other), but they result in different loss landscapes and training dynamics, leading to different FID scores under the same training budget.
Research Question
Can we design a prediction parameterization that achieves better FID than the standard epsilon, v-prediction, and x0-prediction baselines?
Task
You are given custom_train.py, a self-contained training script that trains
an unconditional UNet2DModel (google/ddpm-cifar10-32 architecture) on CIFAR-10.
The editable region contains two functions:
-
compute_training_target(x_0, noise, timesteps, schedule)— defines what the model should predict during training. -
predict_x0(model_output, x_t, timesteps, schedule)— recovers the predicted clean image from the model's output (used during DDIM sampling).
These two functions must be consistent: the sampling procedure must correctly invert the training parameterization.
The schedule dict provides precomputed noise schedule tensors:
alphas_cumprod: cumulative product of (1 - beta)sqrt_alpha: sqrt(alphas_cumprod)sqrt_one_minus_alpha: sqrt(1 - alphas_cumprod)
Evaluation
- Dataset: CIFAR-10 (32x32)
- Model: UNet2DModel (diffusers backbone) at three scales:
- Small: block_out_channels=(64,128,128,128), ~9M params, batch 128
- Medium: block_out_channels=(128,256,256,256), ~36M params, batch 128
- Large: block_out_channels=(256,512,512,512), ~140M params, batch 64
- Training: 35000 steps per scale, AdamW lr=2e-4, EMA rate 0.9995, 8-GPU DDP
- Inference: 50-step DDIM sampler
- Metric: FID (lower is better), computed with clean-fid against CIFAR-10 train set (50k samples)
diffusers-main/custom_train.py [EDITABLE — lines 83–118 only]
1: """Unconditional DDPM Training on CIFAR-10 with configurable prediction target.
2:
3: Uses diffusers UNet2DModel (google/ddpm-cifar10-32 architecture).
4: Only the prediction parameterization (training target + x0 recovery) is editable.
5: """
6:
7: import copy
8: import math
9: import os
10: import sys
11: import time
12: from datetime import timedelta
13:
14: import numpy as np
15: import torch
16: import torch.distributed as dist
17: import torch.nn.functional as F
18: from PIL import Image
19: from torch.nn.parallel import DistributedDatTool Input
1{
2 "op": "replace",
3 "filename": "diffusers-main/custom_train.py",
4 "start_line": 83,
5 "end_line": 118,
6 "content": "def compute_training_target(x_0, noise, timesteps, schedule):\n \"\"\"Compute normalized v-prediction target with unit variance across timesteps.\n \n Target = (sqrt(alpha) * noise - sqrt(1-alpha) * x_0) / sqrt(alpha + (1-alpha)*sigma_x2)\n where sigma_x2 = variance of x_0 (estimated as 0.25 for CIFAR-10 normalized to [-1,1]).\n This ensures the target has approximately unit variance independent of timestep.\n \"\"\"\n sqrt_alpha = schedule[\"sqrt_alpha\"][timesteps].view(-1, 1, 1, 1)\n sqrt_one_minus_alpha = schedule[\"sqrt_one_minus_alpha\"][timesteps].view(-1, 1, 1, 1)\n # Variance of x_0 (estimated from CIFAR-10 normalized to [-1,1])\n sigma_x2 = 0.25\n # Denominator: sqrt(alpha + (1-alpha)*sigma_x2)\n alpha = schedule[\"alphas_cumprod\"][timesteps].view(-1, 1, 1, 1)\n denom = torch.sqrt(alpha + (1 - alpha) * sigma_x2 + 1e-8)\n target = (sqrt_alpha * noise - sqrt_one_minus_alpha * x_0) / denom\n return target\n\n\ndef predict_x0(model_output, x_t, timesteps, schedule):\n \"\"\"Recover predicted x_0 from normalized v-prediction.\n \n Inverse of compute_training_target:\n x_0 = sqrt(alpha) * x_t - sqrt(1-alpha) * sqrt(alpha + (1-alpha)*sigma_x2) * model_output\n \"\"\"\n sqrt_alpha = schedule[\"sqrt_alpha\"][timesteps].view(-1, 1, 1, 1)\n sqrt_one_minus_alpha = schedule[\"sqrt_one_minus_alpha\"][timesteps].view(-1, 1, 1, 1)\n sigma_x2 = 0.25\n alpha = schedule[\"alphas_cumprod\"][timesteps].view(-1, 1, 1, 1)\n denom = torch.sqrt(alpha + (1 - alpha) * sigma_x2 + 1e-8)\n pred_x0 = sqrt_alpha * x_t - sqrt_one_minus_alpha * denom * model_output\n return pred_x0"
7}OK: Replaced lines 83..118 with 31 line(s) in diffusers-main/custom_train.py. Editable range: 83–113.
[Current file: diffusers-main/custom_train.py | editable: 83–113 | total: 487 lines]
80| # ============================================================================
81| # Prediction parameterization (EDITABLE REGION)
82| # ============================================================================
83 def compute_training_target(x_0, noise, timesteps, schedule):
84 """Compute normalized v-prediction target with unit variance across timesteps.
85
86 Target = (sqrt(alpha) * noise - sqrt(1-alpha) * x_0) / sqrt(alpha + (1-alpha)*sigma_x2)
87 where sigma_x2 = variance of x_0 (estimated as 0.25 for CIFAR-10 normalized to [-1,1]).
88 This ensures the target has approximately unit variance independent of timestep.
89 """
90 sqrt_alpha = schedule["sqrt_alpha"][timesteps].view(-1, 1, 1, 1)
91 sqrt_one_m
Tool Input
1{
2 "op": "replace",
3 "filename": "diffusers-main/custom_train.py",
4 "start_line": 114,
5 "end_line": 114,
6 "content": ""
7}ERROR: Lines 114..114 exceed the editable range. You may only edit lines 83–113.
[Current file: diffusers-main/custom_train.py | editable: 83–113 | total: 487 lines]
80| # ============================================================================
81| # Prediction parameterization (EDITABLE REGION)
82| # ============================================================================
83 def compute_training_target(x_0, noise, timesteps, schedule):
84 """Compute normalized v-prediction target with unit variance across timesteps.
85
86 Target = (sqrt(alpha) * noise - sqrt(1-alpha) * x_0) / sqrt(alpha + (1-alpha)*sigma_x2)
87 where sigma_x2 = variance of x_0 (estimated as 0.25 for CIFAR-10 normalized to [-1,1]).
88 This ensures the target has approximately unit variance independent of timestep.
89 """
90 sqrt_alpha = schedule["sqrt_alpha"][timesteps].view(-1, 1, 1, 1)
91 sqrt_one_minus_alpha = schedule
[Test #1] (2 tests remaining; call submit(n=1) to submit this result) ### train_small (scripts/train_small.sh) Training results: Model parameters: 9.0M | GPUs: 8 step 200/35000 | loss 0.5056 | 17.2s step 400/35000 | loss 0.5697 | 16.4s step 600/35000 | loss 0.4915 | 16.5s step 800/35000 | loss 0.4824 | 16.2s step 1000/35000 | loss 0.4088 | 16.2s step 1200/35000 | loss 0.4589 | 16.2s step 1400/35000 | loss 0.4900 | 16.5s step 1600/35000 | loss 0.5504 | 16.2s step 1800/35000 | loss 0.4537 | 16.6s step 2000/35000 | loss 0.4131 | 16.4s step 2200/35000 | loss 0.4448 | 16.4s step 2400/35000 | loss 0.5198 | 16.2s step 2600/35000 | loss 0.4392 | 16.5s step 2800/35000 | loss 0.4639 | 16.4s step 3000/35000 | loss 0.4449 | 16.4s step 3200/35000 | loss 0.4786 | 16.2s step 3400/35000 | loss 0.4998 | 16.1s step 3600/35000 | loss 0.4835 | 16.5s step 3800/35000 | loss 0.4666 | 16.2s step 4000/35000 | loss 0.4961 | 16.4s step 4200/35000 | loss 0.4711 | 16.4s step 4400/35000 | loss 0.5233 | 16.7s step
Tool Input
1{
2 "op": "replace",
3 "filename": "diffusers-main/custom_train.py",
4 "start_line": 83,
5 "end_line": 113,
6 "content": "def compute_training_target(x_0, noise, timesteps, schedule):\n \"\"\"Score prediction: predict epsilon / sqrt(1-alpha).\n \n This target amplifies the noise at high noise levels (small alpha),\n emphasizing learning in early diffusion steps.\n \"\"\"\n sqrt_one_minus_alpha = schedule[\"sqrt_one_minus_alpha\"][timesteps].view(-1, 1, 1, 1)\n # Avoid division by zero\n denom = sqrt_one_minus_alpha.clamp(min=1e-4)\n target = noise / denom\n return target\n\n\ndef predict_x0(model_output, x_t, timesteps, schedule):\n \"\"\"Recover x_0 from score prediction.\n \n Given model_output = epsilon / sqrt(1-alpha),\n we have epsilon = model_output * sqrt_one_minus_alpha.\n Then x_0 = (x_t - sqrt_one_minus_alpha * epsilon) / sqrt(alpha)\n = (x_t - (1-alpha) * model_output) / sqrt(alpha)\n \"\"\"\n sqrt_alpha = schedule[\"sqrt_alpha\"][timesteps].view(-1, 1, 1, 1)\n sqrt_one_minus_alpha = schedule[\"sqrt_one_minus_alpha\"][timesteps].view(-1, 1, 1, 1)\n # Compute (1-alpha) = sqrt_one_minus_alpha^2\n one_minus_alpha = sqrt_one_minus_alpha ** 2\n pred_x0 = (x_t - one_minus_alpha * model_output) / sqrt_alpha.clamp(min=1e-8)\n return pred_x0"
7}OK: Replaced lines 83..113 with 27 line(s) in diffusers-main/custom_train.py. Editable range: 83–109.
[Current file: diffusers-main/custom_train.py | editable: 83–109 | total: 483 lines]
80| # ============================================================================
81| # Prediction parameterization (EDITABLE REGION)
82| # ============================================================================
83 def compute_training_target(x_0, noise, timesteps, schedule):
84 """Score prediction: predict epsilon / sqrt(1-alpha).
85
86 This target amplifies the noise at high noise levels (small alpha),
87 emphasizing learning in early diffusion steps.
88 """
89 sqrt_one_minus_alpha = schedule["sqrt_one_minus_alpha"][timesteps].view(-1, 1, 1, 1)
90 # Avoid division by zero
91 denom = sqrt_one_minus_alpha.clamp(min=1e-4)
92 target = noise / denom
93 return target
94
95
96
[Test #2] (1 test remaining; call submit(n=2) to submit this result) ### train_small (scripts/train_small.sh) Training results: Model parameters: 9.0M | GPUs: 8 step 200/35000 | loss 1.1264 | 17.1s step 400/35000 | loss 3.9692 | 16.3s step 600/35000 | loss 32.7717 | 16.7s step 800/35000 | loss 1.3466 | 16.1s step 1000/35000 | loss 2.8519 | 16.1s step 1200/35000 | loss 5.3287 | 16.4s step 1400/35000 | loss 6.0700 | 16.7s step 1600/35000 | loss 4.1971 | 16.3s step 1800/35000 | loss 3.0981 | 16.5s step 2000/35000 | loss 33.1923 | 16.1s step 2200/35000 | loss 37.6316 | 16.2s step 2400/35000 | loss 7.5083 | 16.1s step 2600/35000 | loss 31.0953 | 16.4s step 2800/35000 | loss 0.9344 | 16.3s step 3000/35000 | loss 62.1059 | 16.8s step 3200/35000 | loss 4.9542 | 16.2s step 3400/35000 | loss 7.8856 | 16.1s step 3600/35000 | loss 2.4277 | 16.3s step 3800/35000 | loss 16.5612 | 16.6s step 4000/35000 | loss 3.7723 | 16.1s step 4200/35000 | loss 3.2776 | 16.1s step 4400/35000 | loss 0.5875 | 15.9s
Tool Input
1{
2 "op": "replace",
3 "filename": "diffusers-main/custom_train.py",
4 "start_line": 83,
5 "end_line": 109,
6 "content": "def compute_training_target(x_0, noise, timesteps, schedule):\n \"\"\"Epsilon prediction: model learns to predict the added noise.\"\"\"\n return noise\n\n\ndef predict_x0(model_output, x_t, timesteps, schedule):\n \"\"\"Recover x_0 from epsilon prediction:\n x_t = sqrt(alpha) * x_0 + sqrt(1-alpha) * eps\n => x_0 = (x_t - sqrt(1-alpha) * eps) / sqrt(alpha)\n \"\"\"\n sqrt_alpha = schedule[\"sqrt_alpha\"][timesteps].view(-1, 1, 1, 1)\n sqrt_one_minus_alpha = schedule[\"sqrt_one_minus_alpha\"][timesteps].view(-1, 1, 1, 1)\n return (x_t - sqrt_one_minus_alpha * model_output) / sqrt_alpha.clamp(min=1e-8)"
7}OK: Replaced lines 83..109 with 13 line(s) in diffusers-main/custom_train.py. Editable range: 83–95.
[Current file: diffusers-main/custom_train.py | editable: 83–95 | total: 469 lines]
80| # ============================================================================
81| # Prediction parameterization (EDITABLE REGION)
82| # ============================================================================
83 def compute_training_target(x_0, noise, timesteps, schedule):
84 """Epsilon prediction: model learns to predict the added noise."""
85 return noise
86
87
88 def predict_x0(model_output, x_t, timesteps, schedule):
89 """Recover x_0 from epsilon prediction:
90 x_t = sqrt(alpha) * x_0 + sqrt(1-alpha) * eps
91 => x_0 = (x_t - sqrt(1-alpha) * eps) / sqrt(alpha)
92 """
93 sqrt_alpha = schedule["sqrt_alpha"][timesteps].view(-1, 1, 1, 1)
94 sqrt_one_minus_alpha = schedule["sqrt_one_minus_al
[Test #3] (0 tests remaining) ### train_small (scripts/train_small.sh) Training results: Model parameters: 9.0M | GPUs: 8 step 200/35000 | loss 0.0349 | 17.4s step 400/35000 | loss 0.0323 | 16.4s step 600/35000 | loss 0.0382 | 16.2s step 800/35000 | loss 0.0272 | 16.2s step 1000/35000 | loss 0.0404 | 16.1s step 1200/35000 | loss 0.0335 | 16.2s step 1400/35000 | loss 0.0318 | 16.4s step 1600/35000 | loss 0.0227 | 16.2s step 1800/35000 | loss 0.0319 | 16.2s step 2000/35000 | loss 0.0435 | 16.4s step 2200/35000 | loss 0.0429 | 16.0s step 2400/35000 | loss 0.0277 | 16.1s step 2600/35000 | loss 0.0476 | 16.3s step 2800/35000 | loss 0.0282 | 16.2s step 3000/35000 | loss 0.0348 | 16.0s step 3200/35000 | loss 0.0351 | 15.8s step 3400/35000 | loss 0.0327 | 16.1s step 3600/35000 | loss 0.0301 | 16.2s step 3800/35000 | loss 0.0356 | 16.4s step 4000/35000 | loss 0.0232 | 16.2s step 4200/35000 | loss 0.0352 | 16.2s step 4400/35000 | loss 0.0207 | 16.1s step 4600/35000 | loss 0.0356 | 16.2s step 48
Tool Input
1{
2 "op": "replace",
3 "filename": "diffusers-main/custom_train.py",
4 "start_line": 83,
5 "end_line": 118,
6 "content": "def compute_training_target(x_0, noise, timesteps, schedule):\n \"\"\"Compute the training target: epsilon + lambda * sigma * x_0.\n \n Adds a small amount of x_0 signal to the standard epsilon prediction,\n which may help the model learn cleaner x_0 estimates while retaining\n the stable training dynamics of epsilon prediction.\n \"\"\"\n sqrt_alpha = schedule[\"sqrt_alpha\"][timesteps].view(-1, 1, 1, 1)\n sqrt_one_minus_alpha = schedule[\"sqrt_one_minus_alpha\"][timesteps].view(-1, 1, 1, 1)\n # Hyperparameter lambda: weight of x_0 correction term\n lambda_val = 0.1\n # target = epsilon + lambda * sigma * x_0\n target = noise + lambda_val * sqrt_one_minus_alpha * x_0\n return target\n\n\ndef predict_x0(model_output, x_t, timesteps, schedule):\n \"\"\"Recover predicted x_0 from model output.\n \n Inverse of compute_training_target with same lambda.\n \"\"\"\n sqrt_alpha = schedule[\"sqrt_alpha\"][timesteps].view(-1, 1, 1, 1)\n sqrt_one_minus_alpha = schedule[\"sqrt_one_minus_alpha\"][timesteps].view(-1, 1, 1, 1)\n lambda_val = 0.1\n # x_0 = (x_t - sigma * model_output) / (alpha - lambda * sigma^2)\n denominator = sqrt_alpha - lambda_val * sqrt_one_minus_alpha * sqrt_one_minus_alpha\n # clamp to avoid division by zero (should be positive)\n denominator = denominator.clamp(min=1e-8)\n pred_x0 = (x_t - sqrt_one_minus_alpha * model_output) / denominator\n return pred_x0"
7}OK: Replaced lines 83..118 with 30 line(s) in diffusers-main/custom_train.py. Editable range: 83–112.
[Current file: diffusers-main/custom_train.py | editable: 83–112 | total: 486 lines]
80| # ============================================================================
81| # Prediction parameterization (EDITABLE REGION)
82| # ============================================================================
83 def compute_training_target(x_0, noise, timesteps, schedule):
84 """Compute the training target: epsilon + lambda * sigma * x_0.
85
86 Adds a small amount of x_0 signal to the standard epsilon prediction,
87 which may help the model learn cleaner x_0 estimates while retaining
88 the stable training dynamics of epsilon prediction.
89 """
90 sqrt_alpha = schedule["sqrt_alpha"][timesteps].view(-1, 1, 1, 1)
91 sqrt_one_minus_alpha = schedule["sqrt_one_minus_alpha"][timesteps].view(-1, 1, 1, 1)
92
Tool Input
1{
2 "op": "replace",
3 "filename": "diffusers-main/custom_train.py",
4 "start_line": 83,
5 "end_line": 118,
6 "content": "def compute_training_target(x_0, noise, timesteps, schedule):\n \"\"\"Compute the training target: epsilon + lambda * sigma * x_0.\n \n Adds a small amount of x_0 signal to the standard epsilon prediction,\n which may help the model learn cleaner x_0 estimates while retaining\n the stable training dynamics of epsilon prediction.\n \"\"\"\n sqrt_alpha = schedule[\"sqrt_alpha\"][timesteps].view(-1, 1, 1, 1)\n sqrt_one_minus_alpha = schedule[\"sqrt_one_minus_alpha\"][timesteps].view(-1, 1, 1, 1)\n # Hyperparameter lambda: weight of x_0 correction term\n lambda_val = 0.1\n # target = epsilon + lambda * sigma * x_0\n target = noise + lambda_val * sqrt_one_minus_alpha * x_0\n return target\n\n\ndef predict_x0(model_output, x_t, timesteps, schedule):\n \"\"\"Recover predicted x_0 from model output.\n \n Inverse of compute_training_target with same lambda.\n \"\"\"\n sqrt_alpha = schedule[\"sqrt_alpha\"][timesteps].view(-1, 1, 1, 1)\n sqrt_one_minus_alpha = schedule[\"sqrt_one_minus_alpha\"][timesteps].view(-1, 1, 1, 1)\n lambda_val = 0.1\n # x_0 = (x_t - sigma * model_output) / (alpha - lambda * sigma^2)\n denominator = sqrt_alpha - lambda_val * sqrt_one_minus_alpha * sqrt_one_minus_alpha\n # clamp to avoid division by zero (should be positive)\n denominator = denominator.clamp(min=1e-8)\n pred_x0 = (x_t - sqrt_one_minus_alpha * model_output) / denominator\n return pred_x0\n\n\n# ============================================================================\n# Sampling — uses diffusers DDIMScheduler with predict_x0 bridge\n# ============================================================================"
7}ERROR: Lines 83..118 exceed the editable range. You may only edit lines 83–112.
[Current file: diffusers-main/custom_train.py | editable: 83–112 | total: 486 lines]
80| # ============================================================================
81| # Prediction parameterization (EDITABLE REGION)
82| # ============================================================================
83 def compute_training_target(x_0, noise, timesteps, schedule):
84 """Compute the training target: epsilon + lambda * sigma * x_0.
85
86 Adds a small amount of x_0 signal to the standard epsilon prediction,
87 which may help the model learn cleaner x_0 estimates while retaining
88 the stable training dynamics of epsilon prediction.
89 """
90 sqrt_alpha = schedule["sqrt_alpha"][timesteps].view(-1, 1, 1, 1)
91 sqrt_one_minus_alpha = schedule["sqrt_one_minus_alpha"][timesteps].view(-1, 1, 1, 1)
92 # Hyperparameter