Agent Conversation: deepseek-reasoner
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.
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, linear / cosine /
piecewise schedules over
t) - Using different predictions for the Tweedie step vs. the renoise step (CFG++ style asymmetry)
- Multiple forward passes per step with different guidance scales, as
- New functional forms for how
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_diffusion.py",
4 "start_line": 652,
5 "end_line": 679,
6 "content": " @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 # 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 # Compute guidance direction\n delta = noise_c - noise_uc\n \n # Perpendicular projection: remove component parallel to noise_uc\n eps = 1e-8\n # Compute dot product\n dot = torch.sum(noise_uc * delta, dim=(1,2,3), keepdim=True)\n norm_sq = torch.sum(noise_uc * noise_uc, dim=(1,2,3), keepdim=True) + eps\n delta_parallel = dot / norm_sq * noise_uc\n delta_perp = delta - delta_parallel\n \n # Rescale to maintain original magnitude\n delta_norm = torch.norm(delta.view(delta.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n delta_perp_norm = torch.norm(delta_perp.view(delta_perp.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1) + eps\n delta_rescaled = delta_perp * (delta_norm / delta_perp_norm)\n \n # Timestep-dependent guidance scaling\n # alpha_t is between 0 and 1, higher at low noise (t small)\n # We want guidance stronger when noise is high (alpha_t low)\n # Use linear schedule: w_t = cfg_guidance * (1 - at)\n # Add minimum guidance of 0.1 to avoid zero guidance at end\n w_t = cfg_guidance * (1 - at) + 0.1\n \n # Combined noise prediction for Tweedie step\n noise_pred = noise_uc + w_t * delta_rescaled\n\n # Tweedie denoising step\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # Renoising step: use noise_uc to stay on manifold (CFG++ style)\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 652..679 with 66 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 # 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 ti
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_diffusion.py",
4 "start_line": 651,
5 "end_line": 720,
6 "content": " @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 # 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 # Compute guidance direction\n delta = noise_c - noise_uc\n \n # Perpendicular projection: remove component parallel to noise_uc\n eps = 1e-8\n # Compute dot product\n dot = torch.sum(noise_uc * delta, dim=(1,2,3), keepdim=True)\n norm_sq = torch.sum(noise_uc * noise_uc, dim=(1,2,3), keepdim=True) + eps\n delta_parallel = dot / norm_sq * noise_uc\n delta_perp = delta - delta_parallel\n \n # Rescale to maintain original magnitude\n delta_norm = torch.norm(delta.view(delta.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n delta_perp_norm = torch.norm(delta_perp.view(delta_perp.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1) + eps\n delta_rescaled = delta_perp * (delta_norm / delta_perp_norm)\n \n # Timestep-dependent guidance scaling\n # alpha_t is between 0 and 1, higher at low noise (t small)\n # We want guidance stronger when noise is high (alpha_t low)\n # Use linear schedule: w_t = cfg_guidance * (1 - at)\n # Add minimum guidance of 0.1 to avoid zero guidance at end\n w_t = cfg_guidance * (1 - at) + 0.1\n \n # Combined noise prediction for Tweedie step\n noise_pred = noise_uc + w_t * delta_rescaled\n\n # Tweedie denoising step\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # Renoising step: use noise_uc to stay on manifold (CFG++ style)\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 651..720 with 66 line(s) in CFGpp-main/latent_diffusion.py. Editable range: 624–716.
[Current file: CFGpp-main/latent_diffusion.py | editable: 624–716 | total: 1055 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 ti
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_sdxl.py",
4 "start_line": 734,
5 "end_line": 757,
6 "content": " 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 zt = self.initialize_latent(size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor))\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.alpha(t)\n at_next = self.alpha(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 # Compute guidance direction\n delta = noise_c - noise_uc\n \n # Perpendicular projection: remove component parallel to noise_uc\n eps = 1e-8\n # Compute dot product\n dot = torch.sum(noise_uc * delta, dim=(1,2,3), keepdim=True)\n norm_sq = torch.sum(noise_uc * noise_uc, dim=(1,2,3), keepdim=True) + eps\n delta_parallel = dot / norm_sq * noise_uc\n delta_perp = delta - delta_parallel\n \n # Rescale to maintain original magnitude\n delta_norm = torch.norm(delta.view(delta.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n delta_perp_norm = torch.norm(delta_perp.view(delta_perp.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1) + eps\n delta_rescaled = delta_perp * (delta_norm / delta_perp_norm)\n \n # Timestep-dependent guidance scaling\n # alpha_t is between 0 and 1, higher at low noise (t small)\n # We want guidance stronger when noise is high (alpha_t low)\n # Use linear schedule: w_t = cfg_guidance * (1 - at)\n # Add minimum guidance of 0.1 to avoid zero guidance at end\n w_t = cfg_guidance * (1 - at) + 0.1\n \n # Combined noise prediction for Tweedie step\n noise_pred = noise_uc + w_t * delta_rescaled\n\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # Renoising step: use noise_uc to stay on manifold (CFG++ style)\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}ERROR: Lines 734..757 exceed the editable range. You may only edit lines 713–755.
[Current file: CFGpp-main/latent_sdxl.py | editable: 713–755 | total: 1031 lines]
710| # CFG++ version
711| ###########################################
712|
713 @register_solver("ddim_cfg++")
714 class BaseDDIMCFGpp(SDXL):
715 # TODO: Implement your improved method here.
716 #
717 # Your goal is to improve the CFG mechanism for SDXL to achieve better
718 # text-image alignment (measured by CLIP score).
719 #
720 # Key methods you need to implement:
721 # - reverse_process: Main sampling function that generates latents
722 #
723 # Available helper methods from parent class:
724 # - self.initialize_latent(size=(1, 4, H//vae_scale, W//vae_scale))
725 # - self.predict_noise(zt, t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)
726 # - self.scheduler.alphas_cumprod[t]: Get alpha_t value
72
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_sdxl.py",
4 "start_line": 734,
5 "end_line": 748,
6 "content": " 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 zt = self.initialize_latent(size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor))\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.alpha(t)\n at_next = self.alpha(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 # Compute guidance direction\n delta = noise_c - noise_uc\n \n # Perpendicular projection: remove component parallel to noise_uc\n eps = 1e-8\n # Compute dot product\n dot = torch.sum(noise_uc * delta, dim=(1,2,3), keepdim=True)\n norm_sq = torch.sum(noise_uc * noise_uc, dim=(1,2,3), keepdim=True) + eps\n delta_parallel = dot / norm_sq * noise_uc\n delta_perp = delta - delta_parallel\n \n # Rescale to maintain original magnitude\n delta_norm = torch.norm(delta.view(delta.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n delta_perp_norm = torch.norm(delta_perp.view(delta_perp.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1) + eps\n delta_rescaled = delta_perp * (delta_norm / delta_perp_norm)\n \n # Timestep-dependent guidance scaling\n # alpha_t is between 0 and 1, higher at low noise (t small)\n # We want guidance stronger when noise is high (alpha_t low)\n # Use linear schedule: w_t = cfg_guidance * (1 - at)\n # Add minimum guidance of 0.1 to avoid zero guidance at end\n w_t = cfg_guidance * (1 - at) + 0.1\n \n # Combined noise prediction for Tweedie step\n noise_pred = noise_uc + w_t * delta_rescaled\n\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # Renoising step: use noise_uc to stay on manifold (CFG++ style)\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 734..748 with 59 line(s) in CFGpp-main/latent_sdxl.py. Editable range: 713–799.
[Current file: CFGpp-main/latent_sdxl.py | editable: 713–799 | total: 1075 lines]
710| # CFG++ version
711| ###########################################
712|
713 @register_solver("ddim_cfg++")
714 class BaseDDIMCFGpp(SDXL):
715 # TODO: Implement your improved method here.
716 #
717 # Your goal is to improve the CFG mechanism for SDXL to achieve better
718 # text-image alignment (measured by CLIP score).
719 #
720 # Key methods you need to implement:
721 # - reverse_process: Main sampling function that generates latents
722 #
723 # Available helper methods from parent class:
724 # - self.initialize_latent(size=(1, 4, H//vae_scale, W//vae_scale))
725 # - self.predict_noise(zt, t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)
726 # - self.scheduler.alphas_cumprod[t]: Get al
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_diffusion.py",
4 "start_line": 670,
5 "end_line": 698,
6 "content": " with torch.no_grad():\n noise_uc, noise_c = self.predict_noise(zt, t, uc, c)\n \n # Compute guidance direction\n delta = noise_c - noise_uc\n \n # Compute dot product with unconditional noise\n eps = 1e-8\n dot = torch.sum(noise_uc * delta, dim=(1,2,3), keepdim=True)\n norm_sq = torch.sum(noise_uc * noise_uc, dim=(1,2,3), keepdim=True) + eps\n \n # If dot positive, delta aligns with noise_uc -> project out parallel component\n # If dot negative, delta opposes noise_uc -> keep as is (strong guidance)\n delta_processed = delta.clone()\n mask = dot > 0\n if mask.any():\n delta_parallel = dot / norm_sq * noise_uc\n delta_perp = delta - delta_parallel\n # Rescale to maintain original magnitude\n delta_norm = torch.norm(delta.view(delta.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n delta_perp_norm = torch.norm(delta_perp.view(delta_perp.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1) + eps\n delta_rescaled = delta_perp * (delta_norm / delta_perp_norm)\n delta_processed = torch.where(mask, delta_rescaled, delta)\n \n # Timestep-dependent guidance scaling\n # alpha_t is between 0 and 1, higher at low noise (t small)\n # We want guidance stronger when noise is high (alpha_t low)\n # Use linear schedule: w_t = cfg_guidance * (1 - at)\n # Add minimum guidance of 0.1 to avoid zero guidance at end\n w_t = cfg_guidance * (1 - at) + 0.1\n \n # Combined noise prediction for Tweedie step\n noise_pred = noise_uc + w_t * delta_processed"
7}OK: Replaced lines 670..698 with 33 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 # 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 ti
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_sdxl.py",
4 "start_line": 750,
5 "end_line": 777,
6 "content": " with torch.no_grad():\n noise_uc, noise_c = self.predict_noise(zt, t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)\n \n # Compute guidance direction\n delta = noise_c - noise_uc\n \n # Compute dot product with unconditional noise\n eps = 1e-8\n dot = torch.sum(noise_uc * delta, dim=(1,2,3), keepdim=True)\n norm_sq = torch.sum(noise_uc * noise_uc, dim=(1,2,3), keepdim=True) + eps\n \n # If dot positive, delta aligns with noise_uc -> project out parallel component\n # If dot negative, delta opposes noise_uc -> keep as is (strong guidance)\n delta_processed = delta.clone()\n mask = dot > 0\n if mask.any():\n delta_parallel = dot / norm_sq * noise_uc\n delta_perp = delta - delta_parallel\n # Rescale to maintain original magnitude\n delta_norm = torch.norm(delta.view(delta.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n delta_perp_norm = torch.norm(delta_perp.view(delta_perp.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1) + eps\n delta_rescaled = delta_perp * (delta_norm / delta_perp_norm)\n delta_processed = torch.where(mask, delta_rescaled, delta)\n \n # Timestep-dependent guidance scaling\n # alpha_t is between 0 and 1, higher at low noise (t small)\n # We want guidance stronger when noise is high (alpha_t low)\n # Use linear schedule: w_t = cfg_guidance * (1 - at)\n # Add minimum guidance of 0.1 to avoid zero guidance at end\n w_t = cfg_guidance * (1 - at) + 0.1\n \n # Combined noise prediction for Tweedie step\n noise_pred = noise_uc + w_t * delta_processed"
7}OK: Replaced lines 750..777 with 33 line(s) in CFGpp-main/latent_sdxl.py. Editable range: 713–804.
[Current file: CFGpp-main/latent_sdxl.py | editable: 713–804 | total: 1080 lines]
710| # CFG++ version
711| ###########################################
712|
713 @register_solver("ddim_cfg++")
714 class BaseDDIMCFGpp(SDXL):
715 # TODO: Implement your improved method here.
716 #
717 # Your goal is to improve the CFG mechanism for SDXL to achieve better
718 # text-image alignment (measured by CLIP score).
719 #
720 # Key methods you need to implement:
721 # - reverse_process: Main sampling function that generates latents
722 #
723 # Available helper methods from parent class:
724 # - self.initialize_latent(size=(1, 4, H//vae_scale, W//vae_scale))
725 # - self.predict_noise(zt, t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)
726 # - self.scheduler.alphas_cumprod[t]: Get al
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_diffusion.py",
4 "start_line": 651,
5 "end_line": 679,
6 "content": " @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 # 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 # Adaptive Rescaled CFG with Perpendicular Projection\n delta = noise_c - noise_uc\n \n # Compute projection of delta onto noise_uc\n # proj = (delta·noise_uc) / (noise_uc·noise_uc) * noise_uc\n dot_du = torch.sum(delta * noise_uc, dim=(1,2,3), keepdim=True)\n dot_uu = torch.sum(noise_uc * noise_uc, dim=(1,2,3), keepdim=True)\n # Avoid division by zero\n dot_uu = torch.where(dot_uu == 0, torch.ones_like(dot_uu), dot_uu)\n proj = (dot_du / dot_uu) * noise_uc\n \n # Parallel component (along noise_uc)\n delta_para = proj\n # Perpendicular component\n delta_perp = delta - proj\n \n # Timestep-dependent mixing weights\n # Use cosine schedule: early steps emphasize parallel, later steps emphasize perpendicular\n # t_idx is step index, total_steps is len(timesteps)\n total_steps = len(self.scheduler.timesteps)\n progress = step / max(total_steps - 1, 1)\n # alpha for parallel, beta for perpendicular\n alpha = 0.8 + 0.2 * torch.cos(torch.pi * progress) # decrease parallel over time\n beta = 1.0 - alpha # increase perpendicular over time\n \n # Mixed delta\n delta_mix = alpha * delta_para + beta * delta_perp\n \n # Rescaling factor: prevent large deviations\n norm_uc = torch.norm(noise_uc.view(noise_uc.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n norm_delta = torch.norm(delta_mix.view(delta_mix.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n scale = norm_uc / (norm_uc + norm_delta + 1e-8)\n \n # Final guided noise prediction\n noise_pred = noise_uc + cfg_guidance * scale * delta_mix\n\n # tweedie\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # add noise - use unconditional noise for renoising (CFG++ style)\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 651..679 with 76 line(s) in CFGpp-main/latent_diffusion.py. Editable range: 624–729.
[Current file: CFGpp-main/latent_diffusion.py | editable: 624–729 | total: 1068 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 ti
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_sdxl.py",
4 "start_line": 734,
5 "end_line": 757,
6 "content": " 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 zt = self.initialize_latent(size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor))\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 # Adaptive Rescaled CFG with Perpendicular Projection\n delta = noise_c - noise_uc\n \n # Compute projection of delta onto noise_uc\n dot_du = torch.sum(delta * noise_uc, dim=(1,2,3), keepdim=True)\n dot_uu = torch.sum(noise_uc * noise_uc, dim=(1,2,3), keepdim=True)\n dot_uu = torch.where(dot_uu == 0, torch.ones_like(dot_uu), dot_uu)\n proj = (dot_du / dot_uu) * noise_uc\n \n # Parallel component (along noise_uc)\n delta_para = proj\n # Perpendicular component\n delta_perp = delta - proj\n \n # Timestep-dependent mixing weights\n total_steps = len(self.scheduler.timesteps)\n progress = step / max(total_steps - 1, 1)\n alpha = 0.8 + 0.2 * torch.cos(torch.pi * progress) # decrease parallel over time\n beta = 1.0 - alpha # increase perpendicular over time\n \n # Mixed delta\n delta_mix = alpha * delta_para + beta * delta_perp\n \n # Rescaling factor: prevent large deviations\n norm_uc = torch.norm(noise_uc.view(noise_uc.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n norm_delta = torch.norm(delta_mix.view(delta_mix.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n scale = norm_uc / (norm_uc + norm_delta + 1e-8)\n \n # Final guided noise prediction\n noise_pred = noise_uc + cfg_guidance * scale * delta_mix\n\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # add noise - use unconditional noise for renoising (CFG++ style)\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}ERROR: Lines 734..757 exceed the editable range. You may only edit lines 713–755.
[Current file: CFGpp-main/latent_sdxl.py | editable: 713–755 | total: 1031 lines]
710| # CFG++ version
711| ###########################################
712|
713 @register_solver("ddim_cfg++")
714 class BaseDDIMCFGpp(SDXL):
715 # TODO: Implement your improved method here.
716 #
717 # Your goal is to improve the CFG mechanism for SDXL to achieve better
718 # text-image alignment (measured by CLIP score).
719 #
720 # Key methods you need to implement:
721 # - reverse_process: Main sampling function that generates latents
722 #
723 # Available helper methods from parent class:
724 # - self.initialize_latent(size=(1, 4, H//vae_scale, W//vae_scale))
725 # - self.predict_noise(zt, t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)
726 # - self.scheduler.alphas_cumprod[t]: Get alpha_t value
72
Tool Input
1{
2 "op": "replace",
3 "filename": "CFGpp-main/latent_sdxl.py",
4 "start_line": 734,
5 "end_line": 748,
6 "content": " 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 zt = self.initialize_latent(size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor))\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 # Adaptive Rescaled CFG with Perpendicular Projection\n delta = noise_c - noise_uc\n \n # Compute projection of delta onto noise_uc\n dot_du = torch.sum(delta * noise_uc, dim=(1,2,3), keepdim=True)\n dot_uu = torch.sum(noise_uc * noise_uc, dim=(1,2,3), keepdim=True)\n dot_uu = torch.where(dot_uu == 0, torch.ones_like(dot_uu), dot_uu)\n proj = (dot_du / dot_uu) * noise_uc\n \n # Parallel component (along noise_uc)\n delta_para = proj\n # Perpendicular component\n delta_perp = delta - proj\n \n # Timestep-dependent mixing weights\n total_steps = len(self.scheduler.timesteps)\n progress = step / max(total_steps - 1, 1)\n alpha = 0.8 + 0.2 * torch.cos(torch.pi * progress) # decrease parallel over time\n beta = 1.0 - alpha # increase perpendicular over time\n \n # Mixed delta\n delta_mix = alpha * delta_para + beta * delta_perp\n \n # Rescaling factor: prevent large deviations\n norm_uc = torch.norm(noise_uc.view(noise_uc.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n norm_delta = torch.norm(delta_mix.view(delta_mix.shape[0], -1), dim=1, keepdim=True).view(-1,1,1,1)\n scale = norm_uc / (norm_uc + norm_delta + 1e-8)\n \n # Final guided noise prediction\n noise_pred = noise_uc + cfg_guidance * scale * delta_mix\n\n z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()\n\n # add noise - use unconditional noise for renoising (CFG++ style)\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 734..748 with 64 line(s) in CFGpp-main/latent_sdxl.py. Editable range: 713–804.
[Current file: CFGpp-main/latent_sdxl.py | editable: 713–804 | total: 1080 lines]
710| # CFG++ version
711| ###########################################
712|
713 @register_solver("ddim_cfg++")
714 class BaseDDIMCFGpp(SDXL):
715 # TODO: Implement your improved method here.
716 #
717 # Your goal is to improve the CFG mechanism for SDXL to achieve better
718 # text-image alignment (measured by CLIP score).
719 #
720 # Key methods you need to implement:
721 # - reverse_process: Main sampling function that generates latents
722 #
723 # Available helper methods from parent class:
724 # - self.initialize_latent(size=(1, 4, H//vae_scale, W//vae_scale))
725 # - self.predict_noise(zt, t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)
726 # - self.scheduler.alphas_cumprod[t]: Get al