[diffusion] refactor: simplify sampling params' override logic (#14539)

This commit is contained in:
Mick
2025-12-06 20:23:49 +08:00
committed by GitHub
parent ea177372bd
commit 35a9a07370
2 changed files with 10 additions and 19 deletions
@@ -116,8 +116,8 @@ class SamplingParams:
fps: int = 24 fps: int = 24
# Denoising parameters # Denoising parameters
num_inference_steps: int = 50 num_inference_steps: int = None
guidance_scale: float = 1.0 guidance_scale: float = None
guidance_rescale: float = 0.0 guidance_rescale: float = 0.0
boundary_ratio: float | None = None boundary_ratio: float | None = None
@@ -559,34 +559,25 @@ class SamplingParams:
def _merge_with_user_params(self, user_params: "SamplingParams"): def _merge_with_user_params(self, user_params: "SamplingParams"):
""" """
Merges parameters from a user-provided SamplingParams object. Merges parameters from a user-provided SamplingParams object.
This method updates the current object with values from `user_params`,
but skips any fields that are explicitly defined in the current object's
subclass. This is to preserve model-specific optimal parameters.
It also skips fields that the user has not changed from the default
in `user_params`.
""" """
if user_params is None: if user_params is None:
return return
# user is not allowed to modify any param defined in the SamplingParams subclass predefined_fields = set(type(self).__annotations__.keys())
subclass_defined_fields = set(type(self).__annotations__.keys())
# global switch: if True, allow overriding protected fields # global switch: if True, allow overriding protected fields
allow_override_protected = not user_params.no_override_protected_fields allow_override_protected = not user_params.no_override_protected_fields
for field in dataclasses.fields(user_params): for field in dataclasses.fields(user_params):
field_name = field.name field_name = field.name
user_value = getattr(user_params, field_name) user_value = getattr(user_params, field_name)
default_value = getattr(self, field_name) default_class_value = getattr(SamplingParams, field_name)
# A field is considered user-modified if its value is different from # A field is considered user-modified if its value is different from the default
# the default is_user_modified = user_value != default_class_value
is_user_modified = user_value != default_value is_protected_field = field_name in predefined_fields
if is_user_modified and ( if is_user_modified and (
allow_override_protected or field_name not in subclass_defined_fields allow_override_protected or not is_protected_field
): ):
if hasattr(self, field_name):
setattr(self, field_name, user_value) setattr(self, field_name, user_value)
self.height_not_provided = user_params.height_not_provided self.height_not_provided = user_params.height_not_provided
self.width_not_provided = user_params.width_not_provided self.width_not_provided = user_params.width_not_provided
@@ -237,7 +237,7 @@
"LatentPreparationStage": 12.71, "LatentPreparationStage": 12.71,
"TimestepPreparationStage": 2.91, "TimestepPreparationStage": 2.91,
"DenoisingStage": 26403.1, "DenoisingStage": 26403.1,
"DecodingStage": 286.85 "DecodingStage": 489.8
}, },
"denoise_step_ms": { "denoise_step_ms": {
"0": 511.3, "0": 511.3,