-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add: inference-time-scaling #1270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ | |
| from ..models.step1x_text_encoder import Step1xEditEmbedder | ||
| from ..core.vram.layers import AutoWrappedLinear | ||
|
|
||
| from ..utils.inference_time_scaling.ses import run_ses_cem, SESRewardScorer | ||
|
|
||
| class MultiControlNet(torch.nn.Module): | ||
| def __init__(self, models: list[torch.nn.Module]): | ||
| super().__init__() | ||
|
|
@@ -240,6 +242,11 @@ def __call__( | |
| tile_stride: int = 64, | ||
| # Progress bar | ||
| progress_bar_cmd = tqdm, | ||
| # SES | ||
| enable_ses: bool = False, | ||
| ses_reward_model: str = "pick", | ||
| ses_eval_budget: int = 50, | ||
| ses_inference_steps: int = 10, | ||
| ): | ||
| # Scheduler | ||
| self.scheduler.set_timesteps(num_inference_steps, denoising_strength=denoising_strength, shift=sigma_shift) | ||
|
|
@@ -274,6 +281,49 @@ def __call__( | |
| for unit in self.units: | ||
| inputs_shared, inputs_posi, inputs_nega = self.unit_runner(unit, self, inputs_shared, inputs_posi, inputs_nega) | ||
|
|
||
| # Inference-Time Scaling (SES) | ||
| 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() | ||
|
Comment on lines
+285
to
+325
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
|
|
||
| # Denoise | ||
| self.load_models_to_device(self.in_iteration_models) | ||
| models = {name: getattr(self, name) for name in self.in_iteration_models} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| from ..models.dinov3_image_encoder import DINOv3ImageEncoder | ||
| from ..models.qwen_image_image2lora import QwenImageImage2LoRAModel | ||
|
|
||
| from ..utils.inference_time_scaling.ses import run_ses_cem, SESRewardScorer | ||
|
|
||
| class QwenImagePipeline(BasePipeline): | ||
|
|
||
|
|
@@ -141,6 +142,11 @@ def __call__( | |
| tile_stride: int = 64, | ||
| # Progress bar | ||
| progress_bar_cmd = tqdm, | ||
| # SES | ||
| enable_ses: bool = False, | ||
| ses_reward_model: str = "pick", | ||
| ses_eval_budget: int = 50, | ||
| ses_inference_steps: int = 10, | ||
| ): | ||
| # Scheduler | ||
| self.scheduler.set_timesteps(num_inference_steps, denoising_strength=denoising_strength, dynamic_shift_len=(height // 16) * (width // 16), exponential_shift_mu=exponential_shift_mu) | ||
|
|
@@ -171,6 +177,51 @@ def __call__( | |
| for unit in self.units: | ||
| inputs_shared, inputs_posi, inputs_nega = self.unit_runner(unit, self, inputs_shared, inputs_posi, inputs_nega) | ||
|
|
||
| # Inference-Time Scaling (SES) | ||
| 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() | ||
|
Comment on lines
+181
to
+223
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
|
|
||
| # Denoise | ||
| self.load_models_to_device(self.in_iteration_models) | ||
| models = {name: getattr(self, name) for name in self.in_iteration_models} | ||
|
|
@@ -182,7 +233,7 @@ def __call__( | |
| **models, timestep=timestep, progress_id=progress_id | ||
| ) | ||
| inputs_shared["latents"] = self.step(self.scheduler, progress_id=progress_id, noise_pred=noise_pred, **inputs_shared) | ||
|
|
||
| # Decode | ||
| self.load_models_to_device(['vae']) | ||
| image = self.vae.decode(inputs_shared["latents"], device=self.device, tiled=tiled, tile_size=tile_size, tile_stride=tile_stride) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| from ..models.dinov3_image_encoder import DINOv3ImageEncoder | ||
| from ..models.z_image_image2lora import ZImageImage2LoRAModel | ||
|
|
||
| from ..utils.inference_time_scaling.ses import run_ses_cem, SESRewardScorer | ||
|
|
||
| class ZImagePipeline(BasePipeline): | ||
|
|
||
|
|
@@ -116,6 +117,11 @@ def __call__( | |
| positive_only_lora: Dict[str, torch.Tensor] = None, | ||
| # Progress bar | ||
| progress_bar_cmd = tqdm, | ||
| # SES | ||
| enable_ses: bool = False, | ||
| ses_reward_model: str = "pick", | ||
| ses_eval_budget: int = 50, | ||
| ses_inference_steps: int = 10, | ||
| ): | ||
| # Scheduler | ||
| self.scheduler.set_timesteps(num_inference_steps, denoising_strength=denoising_strength, shift=sigma_shift) | ||
|
|
@@ -140,6 +146,46 @@ def __call__( | |
| for unit in self.units: | ||
| inputs_shared, inputs_posi, inputs_nega = self.unit_runner(unit, self, inputs_shared, inputs_posi, inputs_nega) | ||
|
|
||
| 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() | ||
|
Comment on lines
+149
to
+187
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
|
|
||
| # Denoise | ||
| self.load_models_to_device(self.in_iteration_models) | ||
| models = {name: getattr(self, name) for name in self.in_iteration_models} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 theBasePipelineclass. 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.