Spaces:
Running on Zero
Running on Zero
Update pk_workflow.py
Browse files- pk_workflow.py +19 -14
pk_workflow.py
CHANGED
|
@@ -179,29 +179,35 @@ SCHEDULE_SIGMA_FUNCS = {
|
|
| 179 |
class use_schedule:
|
| 180 |
"""Set each scheduler's shift for one request, and — for anything but `native` — force its sigma grid onto
|
| 181 |
one of `SCHEDULE_SIGMA_FUNCS`'s named schedules.
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
`
|
| 185 |
-
|
|
|
|
|
|
|
| 186 |
"""
|
| 187 |
|
| 188 |
def __init__(self, pipe, steps: int, schedule_name: str, video_shift: float, audio_shift: float, threshold_noise: float = 0.025):
|
| 189 |
-
self.
|
|
|
|
| 190 |
self.shifts = [float(video_shift), float(audio_shift)]
|
| 191 |
self.schedule_name = schedule_name
|
| 192 |
self.steps = int(steps)
|
| 193 |
self.threshold_noise = float(threshold_noise)
|
| 194 |
-
self._originals =
|
| 195 |
|
| 196 |
def __enter__(self):
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
| 200 |
|
| 201 |
if self.schedule_name != "native":
|
| 202 |
sigma_func = SCHEDULE_SIGMA_FUNCS[self.schedule_name]
|
| 203 |
base = sigma_func(self.steps, self.threshold_noise) if sigma_func is linear_quadratic_sigmas else sigma_func(self.steps)
|
| 204 |
-
for
|
|
|
|
| 205 |
sigmas = time_shift_sigma(base, 1.0, float(scheduler.shift))
|
| 206 |
unbound = type(scheduler).set_timesteps
|
| 207 |
|
|
@@ -212,10 +218,9 @@ class use_schedule:
|
|
| 212 |
return self
|
| 213 |
|
| 214 |
def __exit__(self, *_):
|
| 215 |
-
for
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
scheduler.shift = original_shift
|
| 219 |
return False
|
| 220 |
|
| 221 |
|
|
|
|
| 179 |
class use_schedule:
|
| 180 |
"""Set each scheduler's shift for one request, and — for anything but `native` — force its sigma grid onto
|
| 181 |
one of `SCHEDULE_SIGMA_FUNCS`'s named schedules.
|
| 182 |
+
`MiniMaxH3Scheduler.shift` is a read-only property, so a different shift means swapping in a freshly built
|
| 183 |
+
scheduler via `from_config(..., shift=...)` rather than mutating one in place — the standard diffusers idiom
|
| 184 |
+
for changing a `ConfigMixin` parameter after construction, and correct regardless of exactly how `shift` is
|
| 185 |
+
stored internally. Applied unconditionally, including under `native`, so the shift sliders affect the
|
| 186 |
+
pipeline's own default schedule too — and always restored on exit, since `pipe.scheduler`/`pipe.audio_scheduler`
|
| 187 |
+
are shared, request-spanning objects that must not carry one request's shift into the next.
|
| 188 |
"""
|
| 189 |
|
| 190 |
def __init__(self, pipe, steps: int, schedule_name: str, video_shift: float, audio_shift: float, threshold_noise: float = 0.025):
|
| 191 |
+
self.pipe = pipe
|
| 192 |
+
self.attr_names = ["scheduler", "audio_scheduler"]
|
| 193 |
self.shifts = [float(video_shift), float(audio_shift)]
|
| 194 |
self.schedule_name = schedule_name
|
| 195 |
self.steps = int(steps)
|
| 196 |
self.threshold_noise = float(threshold_noise)
|
| 197 |
+
self._originals: dict = {}
|
| 198 |
|
| 199 |
def __enter__(self):
|
| 200 |
+
for attr_name, shift in zip(self.attr_names, self.shifts):
|
| 201 |
+
original = getattr(self.pipe, attr_name)
|
| 202 |
+
self._originals[attr_name] = original
|
| 203 |
+
if float(original.shift) != shift:
|
| 204 |
+
setattr(self.pipe, attr_name, type(original).from_config(original.config, shift=shift))
|
| 205 |
|
| 206 |
if self.schedule_name != "native":
|
| 207 |
sigma_func = SCHEDULE_SIGMA_FUNCS[self.schedule_name]
|
| 208 |
base = sigma_func(self.steps, self.threshold_noise) if sigma_func is linear_quadratic_sigmas else sigma_func(self.steps)
|
| 209 |
+
for attr_name in self.attr_names:
|
| 210 |
+
scheduler = getattr(self.pipe, attr_name)
|
| 211 |
sigmas = time_shift_sigma(base, 1.0, float(scheduler.shift))
|
| 212 |
unbound = type(scheduler).set_timesteps
|
| 213 |
|
|
|
|
| 218 |
return self
|
| 219 |
|
| 220 |
def __exit__(self, *_):
|
| 221 |
+
for attr_name, original in self._originals.items():
|
| 222 |
+
getattr(self.pipe, attr_name).__dict__.pop("set_timesteps", None)
|
| 223 |
+
setattr(self.pipe, attr_name, original)
|
|
|
|
| 224 |
return False
|
| 225 |
|
| 226 |
|