Agent Conversation: qwen/qwen3.6-plus
Task: cv-diffusion-cfg
Diffusion Model: Classifier-Free Guidance Optimization
Objective
Improve text-to-image generation quality by designing a better classifier-free guidance formula. Your contribution should be a new way to combine noise_uc and noise_c (and optionally their history / derivatives) into a guided noise prediction — not a new numerical sampler.
The evaluation metric is FID (lower is better). CLIP score is logged for diagnostic purposes but is not part of the evaluation — a solution that improves CLIP at the cost of FID is a regression and will not be accepted.
Background
Classifier-free guidance (CFG) is a fundamental technique in diffusion models for text-guided generation. The standard CFG formula is:
noise_pred = noise_uc + cfg_guidance * (noise_c - noise_uc)
where noise_uc is the unconditional noise prediction and noise_c is the conditional (text-guided) noise prediction.
Standard CFG has known limitations:
- Requires high guidance scales (typically 7.5-12.5)
- Can cause mode collapse and saturation
- Results in curved, unnatural sampling trajectories
- Poor invertibility
Recent work (CFG++, Rescaled CFG, Perp-Neg, Interval CFG, Dynamic CFG, etc.) has shown that rethinking the guidance mechanism — while keeping the underlying sampler fixed — can substantially improve text-image alignment without regressing on sample quality.
Task
Redesign the guidance mechanism inside BaseDDIMCFGpp.sample() (SD v1.5)
and BaseDDIMCFGpp.reverse_process() (SDXL). Your contribution must be a
new way to compute the guided noise prediction from noise_uc and noise_c,
not a new way to integrate the diffusion ODE.
Hard rules — read carefully
-
The sampler stays DDIM. Every step must be a first-order DDIM update: one
predict_noisecall → Tweedie's formula → standard DDIM update rule (zt_next = alpha_next.sqrt() * z0t + (1 - alpha_next).sqrt() * noise_for_renoise). You may not:- switch to Euler / Heun / RK / DPM-Solver / DPM++ / UniPC / any higher-order or multistep numerical integrator
- introduce history buffers of past
denoised/noise_predpredictions to do polynomial extrapolation - add SDE Langevin noise terms
- use adaptive step sizing This is not a sampler efficiency task — see cv-diffusion-efficiency if you want to design solvers.
-
No image post-processing tricks. You may not apply dynamic thresholding, percentile clipping, amplitude clamping, momentum / EMA smoothing, or any per-sample normalization to
z0t/zt. These change the output distribution but do not improve the guidance formula. -
Your contribution lives in the noise combination. Legal moves include (but are not limited to):
- New functional forms for how
noise_ucandnoise_care combined (e.g. rescaled CFG, perpendicular projection, std normalization) - Timestep-dependent guidance schedules (interval CFG,
- New functional forms for how
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_diffusion.py",
4 "start_line": 621,
5 "end_line": 682,
6 "content": "@register_solver(\"ddim_cfg++\")\nclass BaseDDIMCFGpp(StableDiffusion):\n \"\"\"\n Smooth Manifold-Constrained CFG (SMC-CFG).\n \n Novel guidance formula combining three insights:\n 1. Cosine-guidance schedule: guidance ramps smoothly from 0→1, avoiding\n the abrupt onset of zeroinit. Early timesteps use near-zero guidance\n (latent is mostly noise), late timesteps use full guidance.\n 2. Manifold-constrained renoising: always renoise with noise_uc to keep\n the trajectory on the data manifold (CFG++ principle).\n 3. Per-channel normalization: normalize the guidance direction (noise_c -\n noise_uc) by its per-channel std to prevent over-amplification of any\n particular semantic direction, improving sample diversity and FID.\n \n Natural scale: cfg_guidance=4.5 (moderate base scale since the cosine\n schedule provides its own effective ramping).\n \"\"\"\n\n def __init__(self,\n solver_config: Dict,\n model_key:str=\"runwayml/stable-diffusion-v1-5\",\n device: Optional[torch.device]=None,\n **kwargs):\n super().__init__(solver_config, model_key, device, **kwargs)\n\n @torch.autocast(device_type='cuda', dtype=torch.float16)\n def sample(self,\n cfg_guidance=7.5,\n prompt=[\"\",\"\"],\n callback_fn=None,\n **kwargs):\n # SMC-CFG natural scale: moderate guidance since cosine schedule\n # provides smooth ramping from zero\n cfg_guidance = 4.5\n\n # Text embedding\n uc, c = self.get_text_embed(null_prompt=prompt[0], prompt=prompt[1])\n\n # Initialize zT\n zt = self.initialize_latent()\n zt = zt.requires_grad_()\n\n # Total number of steps for scheduling\n num_steps = len(self.scheduler.timesteps)\n\n # Sampling\n pbar = tqdm(self.scheduler.timesteps, desc=\"SD\")\n for step, t in enumerate(pbar):\n at = self.alpha(t)\n at_prev = self.alpha(t - self.skip)\n\n with torch.no_grad():\n noise_uc, noise_c = self.predict_noise(zt, t, uc, c)\n \n # Cosine guidance schedule: smoothly ramps from 0 to 1\n # over the course of generation\n progress = step / max(num_steps - 1, 1)\n # Half-cosine: starts at 0, reaches 1 at midpoint, stays at 1\n if progress < 0.5:\n guidance_factor = 0.5 * (1.0 - torch.cos(torch.tensor(progress * 2.0 * 3.14159265)).item())\n else:\n guidance_factor = 1.0\n \n # Guidance direction with per-channel std normalization\n # to prevent over-amplification\n guidance_dir = noise_c - noise_uc\n eps = 1e-6\n guidance_std = guidance_dir.std(dim=list(range(1, guidance_dir.ndim)), keepdim=True)\n noise_dir_normalized = guidance_dir / (guidance_std + eps)\n \n # Apply normalized guidance\n noise_pred = noise_uc + cfg_guidance * guidance_factor * noise_dir_normalized\n\n # Imagen Rescaled CFG (Lin et al 2024)\n rescale_phi = 0.7\n std_c = noise_c.std(dim=list(range(1, noise_c.ndim)), keepdim=True)\n std_pred = noise_pred.std(dim=list(range(1, noise_pred.ndim)), keepdim=True)\n noise_pred_rescaled = noise_pred * (std_c / std_pred)\n noise_pred = rescale_phi * noise_pred_rescaled + (1 - rescale_phi) * noise_pred\n\n # tweedie: use guided prediction for denoised estimate\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # add noise - MANIFOLD-CONSTRAINED: use noise_uc to stay on manifold\n zt = at_prev.sqrt() * z0t + (1-at_prev).sqrt() * noise_uc\n\n if callback_fn is not None:\n callback_kwargs = {'z0t': z0t.detach(),\n 'zt': zt.detach(),\n 'decode': self.decode}\n callback_kwargs = callback_fn(step, t, callback_kwargs)\n z0t = callback_kwargs[\"z0t\"]\n zt = callback_kwargs[\"zt\"]\n\n # for the last step, do not add noise\n img = self.decode(z0t)\n img = (img / 2 + 0.5).clamp(0, 1)\n return img.detach().cpu()"
7}ERROR: Lines 621..682 exceed the editable range. You may only edit lines 624–682.
[Current file: CFGpp-main/latent_diffusion.py | editable: 624–682 | total: 1021 lines]
621| @register_solver("ddim_cfg++")
622| class BaseDDIMCFGpp(StableDiffusion):
623| # TODO: Implement your improved method here.
624 #
625 # Your goal is to improve the CFG mechanism to achieve better text-image
626 # alignment (measured by CLIP score) while maintaining or improving sample quality.
627 #
628 # Key methods you need to implement:
629 # - __init__: Initialize the solver
630 # - sample: Main sampling function that generates images
631 #
632 # Available helper methods from parent class:
633 # - self.get_text_embed(null_prompt, prompt): Get text embeddings
634 # - self.initialize_latent(): Initialize latent variable zT
635 # - self.predict_noise(zt, t, uc, c): Predict noise at timestep t
636 #
Tool Input
1{
2 "end_line": 682,
3 "filename": "CFGpp-main/latent_diffusion.py",
4 "op": "replace",
5 "start_line": 624,
6 "content": " \"\"\"\n Smooth Manifold-Constrained CFG (SMC-CFG).\n \n Novel guidance formula combining three insights:\n 1. Cosine-guidance schedule: guidance ramps smoothly from 0->1, avoiding\n the abrupt onset of zeroinit. Early timesteps use near-zero guidance\n (latent is mostly noise), late timesteps use full guidance.\n 2. Manifold-constrained renoising: always renoise with noise_uc to keep\n the trajectory on the data manifold (CFG++ principle).\n 3. Per-channel normalization: normalize the guidance direction (noise_c -\n noise_uc) by its per-channel std to prevent over-amplification of any\n particular semantic direction, improving sample diversity and FID.\n \n Natural scale: cfg_guidance=4.5 (moderate base scale since the cosine\n schedule provides its own effective ramping).\n \"\"\"\n\n def __init__(self,\n solver_config: Dict,\n model_key:str=\"runwayml/stable-diffusion-v1-5\",\n device: Optional[torch.device]=None,\n **kwargs):\n super().__init__(solver_config, model_key, device, **kwargs)\n\n @torch.autocast(device_type='cuda', dtype=torch.float16)\n def sample(self,\n cfg_guidance=7.5,\n prompt=[\"\",\"\"],\n callback_fn=None,\n **kwargs):\n # SMC-CFG natural scale: moderate guidance since cosine schedule\n # provides smooth ramping from zero\n cfg_guidance = 4.5\n\n # Text embedding\n uc, c = self.get_text_embed(null_prompt=prompt[0], prompt=prompt[1])\n\n # Initialize zT\n zt = self.initialize_latent()\n zt = zt.requires_grad_()\n\n # Total number of steps for scheduling\n num_steps = len(self.scheduler.timesteps)\n\n # Sampling\n pbar = tqdm(self.scheduler.timesteps, desc=\"SD\")\n for step, t in enumerate(pbar):\n at = self.alpha(t)\n at_prev = self.alpha(t - self.skip)\n\n with torch.no_grad():\n noise_uc, noise_c = self.predict_noise(zt, t, uc, c)\n \n # Cosine guidance schedule: smoothly ramps from 0 to 1\n # over the course of generation\n progress = step / max(num_steps - 1, 1)\n # Half-cosine: starts at 0, reaches 1 at midpoint, stays at 1\n if progress < 0.5:\n guidance_factor = 0.5 * (1.0 - torch.cos(torch.tensor(progress * 2.0 * 3.14159265)).item())\n else:\n guidance_factor = 1.0\n \n # Guidance direction with per-channel std normalization\n # to prevent over-amplification\n guidance_dir = noise_c - noise_uc\n eps = 1e-6\n guidance_std = guidance_dir.std(dim=list(range(1, guidance_dir.ndim)), keepdim=True)\n noise_dir_normalized = guidance_dir / (guidance_std + eps)\n \n # Apply normalized guidance\n noise_pred = noise_uc + cfg_guidance * guidance_factor * noise_dir_normalized\n\n # Imagen Rescaled CFG (Lin et al 2024)\n rescale_phi = 0.7\n std_c = noise_c.std(dim=list(range(1, noise_c.ndim)), keepdim=True)\n std_pred = noise_pred.std(dim=list(range(1, noise_pred.ndim)), keepdim=True)\n noise_pred_rescaled = noise_pred * (std_c / std_pred)\n noise_pred = rescale_phi * noise_pred_rescaled + (1 - rescale_phi) * noise_pred\n\n # tweedie: use guided prediction for denoised estimate\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # add noise - MANIFOLD-CONSTRAINED: use noise_uc to stay on manifold\n zt = at_prev.sqrt() * z0t + (1-at_prev).sqrt() * noise_uc\n\n if callback_fn is not None:\n callback_kwargs = {'z0t': z0t.detach(),\n 'zt': zt.detach(),\n 'decode': self.decode}\n callback_kwargs = callback_fn(step, t, callback_kwargs)\n z0t = callback_kwargs[\"z0t\"]\n zt = callback_kwargs[\"zt\"]\n\n # for the last step, do not add noise\n img = self.decode(z0t)\n img = (img / 2 + 0.5).clamp(0, 1)\n return img.detach().cpu()"
7}OK: Replaced lines 624..682 with 97 line(s) in CFGpp-main/latent_diffusion.py. Editable range: 624–720.
[Current file: CFGpp-main/latent_diffusion.py | editable: 624–720 | total: 1059 lines]
621| @register_solver("ddim_cfg++")
622| class BaseDDIMCFGpp(StableDiffusion):
623| # TODO: Implement your improved method here.
624 """
625 Smooth Manifold-Constrained CFG (SMC-CFG).
626
627 Novel guidance formula combining three insights:
628 1. Cosine-guidance schedule: guidance ramps smoothly from 0->1, avoiding
629 the abrupt onset of zeroinit. Early timesteps use near-zero guidance
630 (latent is mostly noise), late timesteps use full guidance.
631 2. Manifold-constrained renoising: always renoise with noise_uc to keep
632 the trajectory on the data manifold (CFG++ principle).
633 3. Per-channel normalization: normalize the guidance direction (noise_c -
634 noise_uc) by its per-c
Tool Input
1{
2 "end_line": 755,
3 "filename": "CFGpp-main/latent_sdxl.py",
4 "op": "replace",
5 "start_line": 714,
6 "content": "@register_solver(\"ddim_cfg++\")\nclass BaseDDIMCFGpp(SDXL):\n def reverse_process(self,\n null_prompt_embeds,\n prompt_embeds,\n cfg_guidance,\n add_cond_kwargs,\n shape=(1024, 1024),\n callback_fn=None,\n **kwargs):\n # SMC-CFG natural scale — moderate guidance with cosine schedule\n cfg_guidance = 4.5\n zt = self.initialize_latent(size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor))\n\n num_steps = len(self.scheduler.timesteps)\n\n pbar = tqdm(self.scheduler.timesteps.int(), desc='SDXL')\n for step, t in enumerate(pbar):\n next_t = t - self.skip\n at = self.scheduler.alphas_cumprod[t]\n at_next = self.scheduler.alphas_cumprod[next_t]\n\n with torch.no_grad():\n noise_uc, noise_c = self.predict_noise(zt, t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)\n \n # Cosine guidance schedule: smoothly ramps from 0 to 1\n progress = step / max(num_steps - 1, 1)\n if progress < 0.5:\n guidance_factor = 0.5 * (1.0 - torch.cos(torch.tensor(progress * 2.0 * 3.14159265)).item())\n else:\n guidance_factor = 1.0\n \n # Guidance direction with per-channel std normalization\n guidance_dir = noise_c - noise_uc\n eps = 1e-6\n guidance_std = guidance_dir.std(dim=list(range(1, guidance_dir.ndim)), keepdim=True)\n noise_dir_normalized = guidance_dir / (guidance_std + eps)\n \n # Apply normalized guidance\n noise_pred = noise_uc + cfg_guidance * guidance_factor * noise_dir_normalized\n\n # Imagen Rescaled CFG (Lin et al 2024)\n rescale_phi = 0.7\n std_c = noise_c.std(dim=list(range(1, noise_c.ndim)), keepdim=True)\n std_pred = noise_pred.std(dim=list(range(1, noise_pred.ndim)), keepdim=True)\n noise_pred_rescaled = noise_pred * (std_c / std_pred)\n noise_pred = rescale_phi * noise_pred_rescaled + (1 - rescale_phi) * noise_pred\n\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # MANIFOLD-CONSTRAINED: use noise_uc to stay on manifold\n zt = at_next.sqrt() * z0t + (1-at_next).sqrt() * noise_uc\n\n if callback_fn is not None:\n callback_kwargs = {'z0t': z0t.detach(),\n 'zt': zt.detach(),\n 'decode': self.decode}\n callback_kwargs = callback_fn(step, t, callback_kwargs)\n z0t = callback_kwargs[\"z0t\"]\n zt = callback_kwargs[\"zt\"]\n\n return z0t"
7}OK: Replaced lines 714..755 with 62 line(s) in CFGpp-main/latent_sdxl.py. Editable range: 713–775.
[Current file: CFGpp-main/latent_sdxl.py | editable: 713–775 | total: 1051 lines]
710| # CFG++ version
711| ###########################################
712|
713 @register_solver("ddim_cfg++")
714 @register_solver("ddim_cfg++")
715 class BaseDDIMCFGpp(SDXL):
716 def reverse_process(self,
717 null_prompt_embeds,
718 prompt_embeds,
719 cfg_guidance,
720 add_cond_kwargs,
721 shape=(1024, 1024),
722 callback_fn=None,
723 **kwargs):
724 # SMC-CFG natural scale — moderate guidance with cosine schedule
725 cfg_guidance = 4.5
726 zt = self.initialize_latent(size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor))
Tool Input
1{
2 "filename": "CFGpp-main/latent_sdxl.py",
3 "op": "replace",
4 "start_line": 713,
5 "end_line": 714,
6 "content": "@register_solver(\"ddim_cfg++\")"
7}OK: Replaced lines 713..714 with 1 line(s) in CFGpp-main/latent_sdxl.py. Editable range: 713–774.
[Current file: CFGpp-main/latent_sdxl.py | editable: 713–774 | total: 1050 lines]
710| # CFG++ version
711| ###########################################
712|
713 @register_solver("ddim_cfg++")
714 class BaseDDIMCFGpp(SDXL):
715 def reverse_process(self,
716 null_prompt_embeds,
717 prompt_embeds,
718 cfg_guidance,
719 add_cond_kwargs,
720 shape=(1024, 1024),
721 callback_fn=None,
722 **kwargs):
723 # SMC-CFG natural scale — moderate guidance with cosine schedule
724 cfg_guidance = 4.5
725 zt = self.initialize_latent(size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor))
726
727 num_steps = len
Tool Input
1{
2 "content": " \"\"\"\n Smooth Manifold-Constrained CFG (SMC-CFG).\n \n Novel guidance formula combining two complementary ideas:\n 1. Cosine-guidance schedule: guidance ramps smoothly from 0 to 1 using a \n half-cosine curve. This avoids the harsh discontinuity of zeroinit's\n step function, providing a more natural transition from noise-only to \n fully guided generation.\n 2. Manifold-constrained renoising: always renoise with noise_uc to keep\n the trajectory on the data manifold (CFG++ principle), producing \n sharper samples with fewer artifacts.\n \n The Imagen Rescaled CFG (Lin et al 2024, phi=0.7) is applied to the \n noise_pred for the Tweedie denoised estimate, but renoising uses noise_uc.\n \n Natural scale: cfg_guidance=7.5 (same as standard CFG/zeroinit since\n the cosine schedule only modulates the effective guidance, not the peak).\n \"\"\"\n\n def __init__(self,\n solver_config: Dict,\n model_key:str=\"runwayml/stable-diffusion-v1-5\",\n device: Optional[torch.device]=None,\n **kwargs):\n super().__init__(solver_config, model_key, device, **kwargs)\n\n @torch.autocast(device_type='cuda', dtype=torch.float16)\n def sample(self,\n cfg_guidance=7.5,\n prompt=[\"\",\"\"],\n callback_fn=None,\n **kwargs):\n # SMC-CFG natural scale\n cfg_guidance = 7.5\n\n # Text embedding\n uc, c = self.get_text_embed(null_prompt=prompt[0], prompt=prompt[1])\n\n # Initialize zT\n zt = self.initialize_latent()\n zt = zt.requires_grad_()\n\n # Total number of steps for scheduling\n num_steps = len(self.scheduler.timesteps)\n\n # Sampling\n pbar = tqdm(self.scheduler.timesteps, desc=\"SD\")\n for step, t in enumerate(pbar):\n at = self.alpha(t)\n at_prev = self.alpha(t - self.skip)\n\n with torch.no_grad():\n noise_uc, noise_c = self.predict_noise(zt, t, uc, c)\n \n # Cosine guidance schedule: smoothly ramps from 0 to 1\n # Half-cosine: starts at 0, reaches 1 at midpoint, stays at 1\n progress = step / max(num_steps - 1, 1)\n if progress < 0.5:\n guidance_factor = 0.5 * (1.0 - torch.cos(torch.tensor(progress * 2.0 * 3.14159265)).item())\n else:\n guidance_factor = 1.0\n \n # Standard CFG with cosine-modulated guidance\n noise_pred = noise_uc + cfg_guidance * guidance_factor * (noise_c - noise_uc)\n\n # Imagen Rescaled CFG (Lin et al 2024)\n rescale_phi = 0.7\n std_c = noise_c.std(dim=list(range(1, noise_c.ndim)), keepdim=True)\n std_pred = noise_pred.std(dim=list(range(1, noise_pred.ndim)), keepdim=True)\n noise_pred_rescaled = noise_pred * (std_c / std_pred)\n noise_pred = rescale_phi * noise_pred_rescaled + (1 - rescale_phi) * noise_pred\n\n # tweedie: use guided+rescaled prediction for denoised estimate\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # add noise - MANIFOLD-CONSTRAINED: use noise_uc to stay on manifold\n zt = at_prev.sqrt() * z0t + (1-at_prev).sqrt() * noise_uc\n\n if callback_fn is not None:\n callback_kwargs = {'z0t': z0t.detach(),\n 'zt': zt.detach(),\n 'decode': self.decode}\n callback_kwargs = callback_fn(step, t, callback_kwargs)\n z0t = callback_kwargs[\"z0t\"]\n zt = callback_kwargs[\"zt\"]\n\n # for the last step, do not add noise\n img = self.decode(z0t)\n img = (img / 2 + 0.5).clamp(0, 1)\n return img.detach().cpu()",
3 "end_line": 720,
4 "filename": "CFGpp-main/latent_diffusion.py",
5 "start_line": 624
6}ERROR: Invalid arguments for 'edit': WorkspaceTools.edit() missing 1 required positional argument: 'op'
Tool Input
1{
2 "content": " \"\"\"\n Smooth Manifold-Constrained CFG (SMC-CFG).\n \n Novel guidance formula combining two complementary ideas:\n 1. Cosine-guidance schedule: guidance ramps smoothly from 0 to 1 using a \n half-cosine curve. This avoids the harsh discontinuity of zeroinit's\n step function, providing a more natural transition from noise-only to \n fully guided generation.\n 2. Manifold-constrained renoising: always renoise with noise_uc to keep\n the trajectory on the data manifold (CFG++ principle), producing \n sharper samples with fewer artifacts.\n \n The Imagen Rescaled CFG (Lin et al 2024, phi=0.7) is applied to the \n noise_pred for the Tweedie denoised estimate, but renoising uses noise_uc.\n \n Natural scale: cfg_guidance=7.5 (same as standard CFG/zeroinit since\n the cosine schedule only modulates the effective guidance, not the peak).\n \"\"\"\n\n def __init__(self,\n solver_config: Dict,\n model_key:str=\"runwayml/stable-diffusion-v1-5\",\n device: Optional[torch.device]=None,\n **kwargs):\n super().__init__(solver_config, model_key, device, **kwargs)\n\n @torch.autocast(device_type='cuda', dtype=torch.float16)\n def sample(self,\n cfg_guidance=7.5,\n prompt=[\"\",\"\"],\n callback_fn=None,\n **kwargs):\n # SMC-CFG natural scale\n cfg_guidance = 7.5\n\n # Text embedding\n uc, c = self.get_text_embed(null_prompt=prompt[0], prompt=prompt[1])\n\n # Initialize zT\n zt = self.initialize_latent()\n zt = zt.requires_grad_()\n\n # Total number of steps for scheduling\n num_steps = len(self.scheduler.timesteps)\n\n # Sampling\n pbar = tqdm(self.scheduler.timesteps, desc=\"SD\")\n for step, t in enumerate(pbar):\n at = self.alpha(t)\n at_prev = self.alpha(t - self.skip)\n\n with torch.no_grad():\n noise_uc, noise_c = self.predict_noise(zt, t, uc, c)\n \n # Cosine guidance schedule: smoothly ramps from 0 to 1\n # Half-cosine: starts at 0, reaches 1 at midpoint, stays at 1\n progress = step / max(num_steps - 1, 1)\n if progress < 0.5:\n guidance_factor = 0.5 * (1.0 - torch.cos(torch.tensor(progress * 2.0 * 3.14159265)).item())\n else:\n guidance_factor = 1.0\n \n # Standard CFG with cosine-modulated guidance\n noise_pred = noise_uc + cfg_guidance * guidance_factor * (noise_c - noise_uc)\n\n # Imagen Rescaled CFG (Lin et al 2024)\n rescale_phi = 0.7\n std_c = noise_c.std(dim=list(range(1, noise_c.ndim)), keepdim=True)\n std_pred = noise_pred.std(dim=list(range(1, noise_pred.ndim)), keepdim=True)\n noise_pred_rescaled = noise_pred * (std_c / std_pred)\n noise_pred = rescale_phi * noise_pred_rescaled + (1 - rescale_phi) * noise_pred\n\n # tweedie: use guided+rescaled prediction for denoised estimate\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # add noise - MANIFOL-CONSTRAINED: use noise_uc to stay on manifold\n zt = at_prev.sqrt() * z0t + (1-at_prev).sqrt() * noise_uc\n\n if callback_fn is not None:\n callback_kwargs = {'z0t': z0t.detach(),\n 'zt': zt.detach(),\n 'decode': self.decode}\n callback_kwargs = callback_fn(step, t, callback_kwargs)\n z0t = callback_kwargs[\"z0t\"]\n zt = callback_kwargs[\"zt\"]\n\n # for the last step, do not add noise\n img = self.decode(z0t)\n img = (img / 2 + 0.5).clamp(0, 1)\n return img.detach().cpu()",
3 "end_line": 720,
4 "filename": "CFGpp-main/latent_diffusion.py",
5 "start_line": 624
6}ERROR: Invalid arguments for 'edit': WorkspaceTools.edit() missing 1 required positional argument: 'op'
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_diffusion.py",
4 "start_line": 624,
5 "end_line": 720,
6 "content": " \"\"\"\n Smooth Manifold-Constrained CFG (SMC-CFG).\n \n Novel guidance formula combining two complementary ideas:\n 1. Cosine-guidance schedule: guidance ramps smoothly from 0 to 1 using a \n half-cosine curve. This avoids the harsh discontinuity of zeroinit's\n step function, providing a more natural transition from noise-only to \n fully guided generation.\n 2. Manifold-constrained renoising: always renoise with noise_uc to keep\n the trajectory on the data manifold (CFG++ principle), producing \n sharper samples with fewer artifacts.\n \n The Imagen Rescaled CFG (Lin et al 2024, phi=0.7) is applied to the \n noise_pred for the Tweedie denoised estimate, but renoising uses noise_uc.\n \n Natural scale: cfg_guidance=7.5 (same as standard CFG/zeroinit since\n the cosine schedule only modulates the effective guidance, not the peak).\n \"\"\"\n\n def __init__(self,\n solver_config: Dict,\n model_key:str=\"runwayml/stable-diffusion-v1-5\",\n device: Optional[torch.device]=None,\n **kwargs):\n super().__init__(solver_config, model_key, device, **kwargs)\n\n @torch.autocast(device_type='cuda', dtype=torch.float16)\n def sample(self,\n cfg_guidance=7.5,\n prompt=[\"\",\"\"],\n callback_fn=None,\n **kwargs):\n # SMC-CFG natural scale\n cfg_guidance = 7.5\n\n # Text embedding\n uc, c = self.get_text_embed(null_prompt=prompt[0], prompt=prompt[1])\n\n # Initialize zT\n zt = self.initialize_latent()\n zt = zt.requires_grad_()\n\n # Total number of steps for scheduling\n num_steps = len(self.scheduler.timesteps)\n\n # Sampling\n pbar = tqdm(self.scheduler.timesteps, desc=\"SD\")\n for step, t in enumerate(pbar):\n at = self.alpha(t)\n at_prev = self.alpha(t - self.skip)\n\n with torch.no_grad():\n noise_uc, noise_c = self.predict_noise(zt, t, uc, c)\n \n # Cosine guidance schedule: smoothly ramps from 0 to 1\n # Half-cosine: starts at 0, reaches 1 at midpoint, stays at 1\n progress = step / max(num_steps - 1, 1)\n if progress < 0.5:\n guidance_factor = 0.5 * (1.0 - torch.cos(torch.tensor(progress * 2.0 * 3.14159265)).item())\n else:\n guidance_factor = 1.0\n \n # Standard CFG with cosine-modulated guidance\n noise_pred = noise_uc + cfg_guidance * guidance_factor * (noise_c - noise_uc)\n\n # Imagen Rescaled CFG (Lin et al 2024)\n rescale_phi = 0.7\n std_c = noise_c.std(dim=list(range(1, noise_c.ndim)), keepdim=True)\n std_pred = noise_pred.std(dim=list(range(1, noise_pred.ndim)), keepdim=True)\n noise_pred_rescaled = noise_pred * (std_c / std_pred)\n noise_pred = rescale_phi * noise_pred_rescaled + (1 - rescale_phi) * noise_pred\n\n # tweedie: use guided+rescaled prediction for denoised estimate\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # add noise - MANIFOLD-CONSTRAINED: use noise_uc to stay on manifold\n zt = at_prev.sqrt() * z0t + (1-at_prev).sqrt() * noise_uc\n\n if callback_fn is not None:\n callback_kwargs = {'z0t': z0t.detach(),\n 'zt': zt.detach(),\n 'decode': self.decode}\n callback_kwargs = callback_fn(step, t, callback_kwargs)\n z0t = callback_kwargs[\"z0t\"]\n zt = callback_kwargs[\"zt\"]\n\n # for the last step, do not add noise\n img = self.decode(z0t)\n img = (img / 2 + 0.5).clamp(0, 1)\n return img.detach().cpu()"
7}OK: Replaced lines 624..720 with 90 line(s) in CFGpp-main/latent_diffusion.py. Editable range: 624–713.
[Current file: CFGpp-main/latent_diffusion.py | editable: 624–713 | total: 1052 lines]
621| @register_solver("ddim_cfg++")
622| class BaseDDIMCFGpp(StableDiffusion):
623| # TODO: Implement your improved method here.
624 """
625 Smooth Manifold-Constrained CFG (SMC-CFG).
626
627 Novel guidance formula combining two complementary ideas:
628 1. Cosine-guidance schedule: guidance ramps smoothly from 0 to 1 using a
629 half-cosine curve. This avoids the harsh discontinuity of zeroinit's
630 step function, providing a more natural transition from noise-only to
631 fully guided generation.
632 2. Manifold-constrained renoising: always renoise with noise_uc to keep
633 the trajectory on the data manifold (CFG++ principle), producing
634 sharper samples with fewer artifacts
[Test #1] (2 tests remaining; call submit(n=1) to submit this result)
### sd15 (scripts/eval_sd15.sh)
Generation results:
GENERATION_METRICS model=sd15 method=ddim_cfg++ cfg_guidance=7.5 NFE=50 seed=42 fid=53.3756
sd15: FID=53.3756
Average FID: 53.3756
### sd20 (scripts/eval_sd20.sh)
Generation results:
GENERATION_METRICS model=sd20 method=ddim_cfg++ cfg_guidance=7.5 NFE=50 seed=42 fid=48.6751
sd20: FID=48.6751
Average FID: 48.6751
[Leaderboard] Results saved: {'fid_sd15': 53.3756, 'fid': 48.6751, 'elapsed_sd15': 935, 'fid_sd20': 48.6751, 'elapsed_sd20': 914}Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_diffusion.py",
4 "start_line": 624,
5 "end_line": 713,
6 "content": " \"\"\"\n Smooth Adaptive CFG (SA-CFG).\n \n Novel guidance formula with exponential onset schedule:\n \n The key insight is that zeroinit's step-function zero-init (K=2) is\n suboptimal. Early timesteps should have gradually increasing guidance\n rather than an abrupt on/off switch. We use an exponential ramp:\n \n w(step) = cfg_guidance * (1 - exp(-step / tau))\n \n This provides a smooth transition from noise-only (step 0) to full\n guidance (later steps), avoiding the discontinuity of zeroinit while\n maintaining its core benefit of not corrupting early latent evolution.\n \n Standard CFG renoising (noise_pred) + Imagen Rescaled CFG (phi=0.7)\n are applied as in the zeroinit baseline.\n \n Natural scale: cfg_guidance=7.5, tau=3.0\n \"\"\"\n\n def __init__(self,\n solver_config: Dict,\n model_key:str=\"runwayml/stable-diffusion-v1-5\",\n device: Optional[torch.device]=None,\n **kwargs):\n super().__init__(solver_config, model_key, device, **kwargs)\n\n @torch.autocast(device_type='cuda', dtype=torch.float16)\n def sample(self,\n cfg_guidance=7.5,\n prompt=[\"\",\"\"],\n callback_fn=None,\n **kwargs):\n # SA-CFG natural scale: same as standard CFG/zeroinit\n cfg_guidance = 7.5\n \n # Exponential ramp time constant\n tau = 3.0\n\n # Text embedding\n uc, c = self.get_text_embed(null_prompt=prompt[0], prompt=prompt[1])\n\n # Initialize zT\n zt = self.initialize_latent()\n zt = zt.requires_grad_()\n\n # Sampling\n pbar = tqdm(self.scheduler.timesteps, desc=\"SD\")\n for step, t in enumerate(pbar):\n at = self.alpha(t)\n at_prev = self.alpha(t - self.skip)\n\n with torch.no_grad():\n noise_uc, noise_c = self.predict_noise(zt, t, uc, c)\n \n # Exponential guidance onset: smooth ramp from 0 to full\n guidance_factor = 1.0 - torch.exp(torch.tensor(-step / tau)).item()\n \n # Standard CFG with smooth guidance\n noise_pred = noise_uc + cfg_guidance * guidance_factor * (noise_c - noise_uc)\n\n # Imagen Rescaled CFG (Lin et al 2024)\n rescale_phi = 0.7\n std_c = noise_c.std(dim=list(range(1, noise_c.ndim)), keepdim=True)\n std_pred = noise_pred.std(dim=list(range(1, noise_pred.ndim)), keepdim=True)\n noise_pred_rescaled = noise_pred * (std_c / std_pred)\n noise_pred = rescale_phi * noise_pred_rescaled + (1 - rescale_phi) * noise_pred\n\n # tweedie\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # Standard CFG renoising\n zt = at_prev.sqrt() * z0t + (1-at_prev).sqrt() * noise_pred\n\n if callback_fn is not None:\n callback_kwargs = {'z0t': z0t.detach(),\n 'zt': zt.detach(),\n 'decode': self.decode}\n callback_kwargs = callback_fn(step, t, callback_kwargs)\n z0t = callback_kwargs[\"z0t\"]\n zt = callback_kwargs[\"zt\"]\n\n # for the last step, do not add noise\n img = self.decode(z0t)\n img = (img / 2 + 0.5).clamp(0, 1)\n return img.detach().cpu()"
7}OK: Replaced lines 624..713 with 87 line(s) in CFGpp-main/latent_diffusion.py. Editable range: 624–710.
[Current file: CFGpp-main/latent_diffusion.py | editable: 624–710 | total: 1049 lines]
621| @register_solver("ddim_cfg++")
622| class BaseDDIMCFGpp(StableDiffusion):
623| # TODO: Implement your improved method here.
624 """
625 Smooth Adaptive CFG (SA-CFG).
626
627 Novel guidance formula with exponential onset schedule:
628
629 The key insight is that zeroinit's step-function zero-init (K=2) is
630 suboptimal. Early timesteps should have gradually increasing guidance
631 rather than an abrupt on/off switch. We use an exponential ramp:
632
633 w(step) = cfg_guidance * (1 - exp(-step / tau))
634
635 This provides a smooth transition from noise-only (step 0) to full
636 guidance (later steps), avoiding the discontinuity of zeroinit while
637 maintaining its