Conversation
Summary of ChangesHello @yjy415, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces the Spectral Evolution Search (SES) algorithm, a novel inference-time scaling technique designed to enhance the quality of generated images without requiring model retraining. It integrates SES into several existing image generation pipelines, allowing users to optimize initial noise through an evolutionary search guided by reward models. This feature provides a trade-off between computational cost and image quality, offering greater control over the generation process and improving the fidelity of generated outputs. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces an exciting new feature: Inference-Time Scaling using Spectral Evolution Search (SES). The implementation looks solid and is accompanied by new documentation and examples, which is great. However, there is a significant amount of code duplication across the different pipeline files (flux_image.py, flux2_image.py, qwen_image.py, z_image.py). This duplicated logic for SES should be refactored into a shared helper function or a method in the BasePipeline to improve maintainability. I've also noted a few minor areas for improvement in the ses.py utility file regarding logging and exception handling, as well as some broken links and typos in the new documentation files.
| if enable_ses: | ||
| print(f"[SES] Starting optimization with budget={ses_eval_budget}, steps={ses_inference_steps}") | ||
| scorer = SESRewardScorer(ses_reward_model, device=self.device, dtype=self.torch_dtype) | ||
| self.load_models_to_device(list(self.in_iteration_models) + ['vae']) | ||
| models = {name: getattr(self, name) for name in self.in_iteration_models} | ||
|
|
||
| h_latent = height // 16 | ||
| w_latent = width // 16 | ||
|
|
||
| def ses_generate_callback(trial_latents_spatial): | ||
| trial_inputs = inputs_shared.copy() | ||
|
|
||
| self.scheduler.set_timesteps(ses_inference_steps, denoising_strength=denoising_strength, dynamic_shift_len=h_latent*w_latent) | ||
| eval_timesteps = self.scheduler.timesteps | ||
|
|
||
| curr_latents_seq = rearrange(trial_latents_spatial, "b c h w -> b (h w) c") | ||
|
|
||
| for progress_id, timestep in enumerate(eval_timesteps): | ||
| timestep = timestep.unsqueeze(0).to(dtype=self.torch_dtype, device=self.device) | ||
|
|
||
| trial_inputs["latents"] = curr_latents_seq | ||
|
|
||
| noise_pred = self.cfg_guided_model_fn( | ||
| self.model_fn, cfg_scale, | ||
| trial_inputs, inputs_posi, inputs_nega, | ||
| **models, timestep=timestep, progress_id=progress_id | ||
| ) | ||
| curr_latents_seq = self.step(self.scheduler, progress_id=progress_id, noise_pred=noise_pred, **trial_inputs) | ||
|
|
||
| curr_latents_spatial = rearrange(curr_latents_seq, "b (h w) c -> b c h w", h=h_latent, w=w_latent) | ||
|
|
||
| decoded_img = self.vae.decode(curr_latents_spatial) | ||
| return self.vae_output_to_image(decoded_img) | ||
| initial_noise_seq = inputs_shared["latents"] | ||
| initial_noise_spatial = rearrange(initial_noise_seq, "b (h w) c -> b c h w", h=h_latent, w=w_latent) | ||
|
|
||
| optimized_latents_spatial = run_ses_cem( | ||
| base_latents=initial_noise_spatial, | ||
| pipeline_callback=ses_generate_callback, | ||
| prompt=prompt, | ||
| scorer=scorer, | ||
| total_eval_budget=ses_eval_budget, | ||
| popsize=10, | ||
| k_elites=5 | ||
| ) | ||
| optimized_latents_seq = rearrange(optimized_latents_spatial, "b c h w -> b (h w) c") | ||
| inputs_shared["latents"] = optimized_latents_seq | ||
| self.scheduler.set_timesteps(num_inference_steps, denoising_strength=denoising_strength, dynamic_shift_len=h_latent*w_latent) | ||
| del scorer | ||
| torch.cuda.empty_cache() |
There was a problem hiding this comment.
This block of code for Inference-Time Scaling (SES) is largely duplicated across multiple pipeline files (flux_image.py, flux2_image.py, qwen_image.py, z_image.py). This duplication makes the code harder to maintain and update. Consider refactoring this logic into a shared helper function or a method in the BasePipeline class. This would centralize the SES implementation, making it easier to manage and reducing the risk of inconsistencies between pipelines. A base method could accept pipeline-specific parameters (like scheduler settings, VAE model name, and latent shape handling) to accommodate the variations between models.
| if enable_ses: | ||
| print(f"[SES] Starting optimization with budget={ses_eval_budget}, steps={ses_inference_steps}") | ||
| scorer = SESRewardScorer(ses_reward_model, device=self.device, dtype=self.torch_dtype) | ||
| self.load_models_to_device(list(self.in_iteration_models) + ['vae_decoder']) | ||
| models = {name: getattr(self, name) for name in self.in_iteration_models} | ||
|
|
||
| def ses_generate_callback(trial_latents): | ||
| trial_inputs = inputs_shared.copy() | ||
|
|
||
| self.scheduler.set_timesteps(ses_inference_steps, denoising_strength=denoising_strength, shift=sigma_shift) | ||
| eval_timesteps = self.scheduler.timesteps | ||
| curr_latents = trial_latents | ||
|
|
||
| for progress_id, timestep in enumerate(eval_timesteps): | ||
| timestep = timestep.unsqueeze(0).to(dtype=self.torch_dtype, device=self.device) | ||
|
|
||
| trial_inputs["latents"] = curr_latents | ||
| noise_pred = self.cfg_guided_model_fn( | ||
| self.model_fn, cfg_scale, | ||
| trial_inputs, inputs_posi, inputs_nega, | ||
| **models, timestep=timestep, progress_id=progress_id | ||
| ) | ||
| curr_latents = self.step(self.scheduler, progress_id=progress_id, noise_pred=noise_pred, **trial_inputs) | ||
|
|
||
| decoded_img = self.vae_decoder(curr_latents, device=self.device, tiled=tiled, tile_size=tile_size, tile_stride=tile_stride) | ||
| return self.vae_output_to_image(decoded_img) | ||
|
|
||
| initial_noise = inputs_shared["latents"] | ||
| optimized_latents = run_ses_cem( | ||
| base_latents=initial_noise, | ||
| pipeline_callback=ses_generate_callback, | ||
| prompt=prompt, | ||
| scorer=scorer, | ||
| total_eval_budget=ses_eval_budget, | ||
| popsize=10, | ||
| k_elites=5 | ||
| ) | ||
| inputs_shared["latents"] = optimized_latents | ||
| self.scheduler.set_timesteps(num_inference_steps, denoising_strength=denoising_strength, shift=sigma_shift) | ||
| del scorer | ||
| torch.cuda.empty_cache() |
There was a problem hiding this comment.
This block of code for Inference-Time Scaling (SES) is largely duplicated across multiple pipeline files. This duplication makes the code harder to maintain and update. Consider refactoring this logic into a shared helper function or a method in the BasePipeline class. This would centralize the SES implementation, making it easier to manage and reducing the risk of inconsistencies between pipelines.
| if enable_ses: | ||
| print(f"[SES] Starting optimization with budget={ses_eval_budget}, steps={ses_inference_steps}") | ||
| scorer = SESRewardScorer(ses_reward_model, device=self.device, dtype=self.torch_dtype) | ||
|
|
||
| self.load_models_to_device(list(self.in_iteration_models) + ['vae']) | ||
| models = {name: getattr(self, name) for name in self.in_iteration_models} | ||
|
|
||
| def ses_generate_callback(trial_latents): | ||
| trial_inputs = inputs_shared.copy() | ||
| self.scheduler.set_timesteps(ses_inference_steps, denoising_strength=denoising_strength, dynamic_shift_len=(height // 16) * (width // 16), exponential_shift_mu=exponential_shift_mu) | ||
| eval_timesteps = self.scheduler.timesteps | ||
| curr_latents = trial_latents | ||
|
|
||
| for progress_id, timestep in enumerate(eval_timesteps): | ||
| timestep = timestep.unsqueeze(0).to(dtype=self.torch_dtype, device=self.device) | ||
|
|
||
| trial_inputs["latents"] = curr_latents | ||
|
|
||
| noise_pred = self.cfg_guided_model_fn( | ||
| self.model_fn, cfg_scale, | ||
| trial_inputs, inputs_posi, inputs_nega, | ||
| **models, timestep=timestep, progress_id=progress_id | ||
| ) | ||
| curr_latents = self.step(self.scheduler, progress_id=progress_id, noise_pred=noise_pred, **trial_inputs) | ||
|
|
||
| decoded_img = self.vae.decode(curr_latents, device=self.device, tiled=tiled, tile_size=tile_size, tile_stride=tile_stride) | ||
| return self.vae_output_to_image(decoded_img) | ||
|
|
||
| initial_noise = inputs_shared["latents"] | ||
|
|
||
| optimized_latents = run_ses_cem( | ||
| base_latents=initial_noise, | ||
| pipeline_callback=ses_generate_callback, | ||
| prompt=prompt, | ||
| scorer=scorer, | ||
| total_eval_budget=ses_eval_budget, | ||
| popsize=10, | ||
| k_elites=5 | ||
| ) | ||
| inputs_shared["latents"] = optimized_latents | ||
| self.scheduler.set_timesteps(num_inference_steps, denoising_strength=denoising_strength, dynamic_shift_len=(height // 16) * (width // 16), exponential_shift_mu=exponential_shift_mu) | ||
| del scorer | ||
| torch.cuda.empty_cache() |
There was a problem hiding this comment.
This block of code for Inference-Time Scaling (SES) is largely duplicated across multiple pipeline files. This duplication makes the code harder to maintain and update. Consider refactoring this logic into a shared helper function or a method in the BasePipeline class. This would centralize the SES implementation, making it easier to manage and reducing the risk of inconsistencies between pipelines.
| if enable_ses: | ||
| print(f"[SES] Starting optimization with budget={ses_eval_budget}, steps={ses_inference_steps}") | ||
| scorer = SESRewardScorer(ses_reward_model, device=self.device, dtype=self.torch_dtype) | ||
| self.load_models_to_device(list(self.in_iteration_models) + ['vae_decoder']) | ||
| models = {name: getattr(self, name) for name in self.in_iteration_models} | ||
| def ses_generate_callback(trial_latents): | ||
| trial_inputs = inputs_shared.copy() | ||
| self.scheduler.set_timesteps(ses_inference_steps, denoising_strength=denoising_strength, shift=sigma_shift) | ||
| eval_timesteps = self.scheduler.timesteps | ||
| curr_latents = trial_latents | ||
|
|
||
| for progress_id, timestep in enumerate(eval_timesteps): | ||
| timestep = timestep.unsqueeze(0).to(dtype=self.torch_dtype, device=self.device) | ||
| trial_inputs["latents"] = curr_latents | ||
| noise_pred = self.cfg_guided_model_fn( | ||
| self.model_fn, cfg_scale, | ||
| trial_inputs, inputs_posi, inputs_nega, | ||
| **models, timestep=timestep, progress_id=progress_id | ||
| ) | ||
| curr_latents = self.step(self.scheduler, progress_id=progress_id, noise_pred=noise_pred, **trial_inputs) | ||
| decoded_img = self.vae_decoder(curr_latents) | ||
| return self.vae_output_to_image(decoded_img) | ||
|
|
||
| initial_noise = inputs_shared["latents"] | ||
|
|
||
| optimized_latents = run_ses_cem( | ||
| base_latents=initial_noise, | ||
| pipeline_callback=ses_generate_callback, | ||
| prompt=prompt, | ||
| scorer=scorer, | ||
| total_eval_budget=ses_eval_budget, | ||
| popsize=10, | ||
| k_elites=5 | ||
| ) | ||
| inputs_shared["latents"] = optimized_latents | ||
| self.scheduler.set_timesteps(num_inference_steps, denoising_strength=denoising_strength, shift=sigma_shift) | ||
|
|
||
| del scorer | ||
| torch.cuda.empty_cache() |
There was a problem hiding this comment.
This block of code for Inference-Time Scaling (SES) is largely duplicated across multiple pipeline files. This duplication makes the code harder to maintain and update. Consider refactoring this logic into a shared helper function or a method in the BasePipeline class. This would centralize the SES implementation, making it easier to manage and reducing the risk of inconsistencies between pipelines.
| self._load_model() | ||
|
|
||
| def _load_model(self): | ||
| print(f"[SES] Loading Reward Model: {self.reward_name}...") |
There was a problem hiding this comment.
Using print for logging is generally discouraged in library code as it offers less control over verbosity and output streams. It's recommended to use Python's standard logging module instead. This would allow consumers of your library to configure logging as needed. This applies to all print statements in this file.
To do this, add import logging and logger = logging.getLogger(__name__) at the top of the file, then replace the print statements with logger.info, logger.warning, etc.
| print(f"[SES] Loading Reward Model: {self.reward_name}...") | |
| logger.info(f"[SES] Loading Reward Model: {self.reward_name}...") |
| except Exception as e: | ||
| print(f"Error computing score: {e}") | ||
| return 0.0 |
There was a problem hiding this comment.
Catching a broad Exception and simply printing the message can hide important errors and make debugging difficult. It's better to log the full traceback to get more context on what went wrong.
except Exception:
import logging
logger = logging.getLogger(__name__)
logger.error("Error computing score", exc_info=True)
return 0.0|
|
||
| This method essentially **trades inference computation time for generation quality**. | ||
|
|
||
| For more technical details on this method, please refer to the paper: **[Spectral Evolution Search: Efficient Inference-Time Scaling for Reward-Aligned Image Generation](https://arxiv.org/abs/2602.03208)**. |
There was a problem hiding this comment.
There seems to be a typo in the arXiv link. The year 2602 should likely be 2402.
| For more technical details on this method, please refer to the paper: **[Spectral Evolution Search: Efficient Inference-Time Scaling for Reward-Aligned Image Generation](https://arxiv.org/abs/2602.03208)**. | |
| For more technical details on this method, please refer to the paper: **[Spectral Evolution Search: Efficient Inference-Time Scaling for Reward-Aligned Image Generation](https://arxiv.org/abs/2402.03208)**. |
|
|
||
| In DiffSynth-Studio, SES has been integrated into the pipelines of mainstream text-to-image models. You only need to set `enable_ses=True` when calling `pipe()` to enable it. | ||
|
|
||
| The following is [quick start code](https://www.google.com/search?q=../../../examples/z_image/model_inference/Z-Image-Turbo-SES.py) using **Z-Image-Turbo** as an example: |
There was a problem hiding this comment.
The link to the quick start code appears to be broken. It points to a Google search query instead of the example file. It should be a relative link to the file within the repository.
| The following is [quick start code](https://www.google.com/search?q=../../../examples/z_image/model_inference/Z-Image-Turbo-SES.py) using **Z-Image-Turbo** as an example: | |
| The following is [quick start code](../../../examples/z_image/model_inference/Z-Image-Turbo-SES.py) using **Z-Image-Turbo** as an example: |
|
|
||
| 这种方法本质上是用**推理计算时间换取生成质量**。 | ||
|
|
||
| 关于该方法的更多技术细节,请参考论文:**[Spectral Evolution Search: Efficient Inference-Time Scaling for Reward-Aligned Image Generation](https://arxiv.org/abs/2602.03208)**。 |
There was a problem hiding this comment.
The year 2602 in the arXiv link seems to be a typo. It should probably be 2402.
| 关于该方法的更多技术细节,请参考论文:**[Spectral Evolution Search: Efficient Inference-Time Scaling for Reward-Aligned Image Generation](https://arxiv.org/abs/2602.03208)**。 | |
| 关于该方法的更多技术细节,请参考论文:**[Spectral Evolution Search: Efficient Inference-Time Scaling for Reward-Aligned Image Generation](https://arxiv.org/abs/2402.03208)**。 |
No description provided.