Repository navigation
Conversation
…type mismatch Fixes huggingface#14887 Fixes huggingface#14888 1. On the final denoising step under `final_sigmas_type="zero"` (the default), `sigma_t = 0` and `h = lambda_t - lambda_s0 = inf`: - In `multistep_uni_p_bh_update`, when `D1s is None` (first-order update), `pred_res = 0` was multiplied by `B_h = hh = -inf` under `solver_type="bh1"`, producing `-inf * 0 = nan`. Only subtract the `B_h * pred_res` residual term when `D1s is not None`. - In `step`, force `this_order = 1` when `self.config.final_sigmas_type == "zero"` on the final step (`self.step_index == len(self.timesteps) - 1`), matching `DPMSolverMultistepScheduler` so `lower_order_final=False` does not divide by `rk = (lambda_si - lambda_s0) / inf = 0` or build a singular `R` matrix. - In `__init__`, raise a `ValueError` when `not predict_x0 and final_sigmas_type == "zero"`, matching `DPMSolverMultistepScheduler`. 2. In `multistep_uni_p_bh_update` and `multistep_uni_c_bh_update`, replace `rks.append(torch.ones((), device=device))` with `rks.append(torch.ones_like(h))` so `R` preserves `h.dtype` instead of promoting to `float64` when `torch.set_default_dtype(torch.float64)` is active. Signed-off-by: Parth Vadera <parthvadera@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14887
Fixes #14888
Root Cause & Fixes
Final-step
NaN/inf/_LinAlgErrorwhenfinal_sigmas_type="zero"(UniPCMultistepScheduler returns nan with solver_type="bh1", and inf or nan with lower_order_final=False or predict_x0=False, on the final step to sigma = 0 #14887):Since
final_sigmas_typedefaults to"zero", the final step reachessigma_t = 0, makinglambda_t = infandh = lambda_t - lambda_s0 = inf:solver_type="bh1": On a 1st-order final step (D1s is None),multistep_uni_p_bh_updatesetpred_res = 0and evaluatedx_t_ - alpha_t * B_h * pred_res. Forbh1,B_h = hh = -inf, so-inf * 0 = nancorrupted the output even though no multistep residual exists whenD1s is None. Guard theB_h * pred_ressubtraction behindif D1s is not None:and setx_t = x_t_otherwise.lower_order_final=False: Attempting an order>= 2predictor update intosigma_t = 0givesrk = (lambda_si - lambda_s0) / inf = 0andD1s = (mi - m0) / 0 = inf(and a singularRmatrix fororder >= 3). Forcethis_order = 1on the last step whenself.config.final_sigmas_type == "zero", matchingDPMSolverMultistepScheduler.predict_x0=False: Withsigma_t = 0andhh = +inf, the noise-prediction branch multiplies0 * inf = nan. Raise aValueErrorin__init__whennot predict_x0 and final_sigmas_type == "zero", matchingDPMSolverMultistepScheduler.torch.linalg.solvedtype mismatch undertorch.set_default_dtype(torch.float64)(UniPCMultistepScheduler fails in torch.linalg.solve under a float64 default dtype: the unit entry of rks takes the default dtype #14888):In both
multistep_uni_p_bh_updateandmultistep_uni_c_bh_update, the firstorder - 1entries ofrksinherith.dtype(float32), whereasrks.append(torch.ones((), device=device))used the global default dtype. Undertorch.set_default_dtype(torch.float64),torch.stack(rks)promotedRtofloat64whilebremainedfloat32, raisingRuntimeError: linalg.solve: Expected A and B to have the same dtype. Replacetorch.ones((), device=device)withtorch.ones_like(h).Validation
test_last_step_to_sigma_zero_is_the_data_prediction,test_predict_x0_false_requires_final_sigmas_type_sigma_min, andtest_default_dtype_float64totests/schedulers/test_scheduler_unipc.py.tests/schedulers/test_scheduler_unipc.pypass.Credits & Acknowledgments
solver_type="bh1",lower_order_final=False,predict_x0=Falseatsigma = 0, andrksundertorch.set_default_dtype(torch.float64), and provided the proposed diffs and unit tests (test_last_step_to_sigma_zero_is_the_data_prediction,test_predict_x0_false_requires_final_sigmas_type_sigma_min, andtest_default_dtype_float64) directly in the issue descriptions.