@@ -683,9 +683,10 @@ def get_guider(self, model, positive, negative, cfg):
683683 return (guider ,)
684684
685685class Guider_DualCFG (comfy .samplers .CFGGuider ):
686- def set_cfg (self , cfg1 , cfg2 ):
686+ def set_cfg (self , cfg1 , cfg2 , nested = False ):
687687 self .cfg1 = cfg1
688688 self .cfg2 = cfg2
689+ self .nested = nested
689690
690691 def set_conds (self , positive , middle , negative ):
691692 middle = node_helpers .conditioning_set_values (middle , {"prompt_type" : "negative" })
@@ -695,14 +696,20 @@ def predict_noise(self, x, timestep, model_options={}, seed=None):
695696 negative_cond = self .conds .get ("negative" , None )
696697 middle_cond = self .conds .get ("middle" , None )
697698 positive_cond = self .conds .get ("positive" , None )
698- if model_options .get ("disable_cfg1_optimization" , False ) == False :
699- if math .isclose (self .cfg2 , 1.0 ):
700- negative_cond = None
701- if math .isclose (self .cfg1 , 1.0 ):
702- middle_cond = None
703699
704- out = comfy .samplers .calc_cond_batch (self .inner_model , [negative_cond , middle_cond , positive_cond ], x , timestep , model_options )
705- return comfy .samplers .cfg_function (self .inner_model , out [1 ], out [0 ], self .cfg2 , x , timestep , model_options = model_options , cond = middle_cond , uncond = negative_cond ) + (out [2 ] - out [1 ]) * self .cfg1
700+ if self .nested :
701+ out = comfy .samplers .calc_cond_batch (self .inner_model , [negative_cond , middle_cond , positive_cond ], x , timestep , model_options )
702+ pred_text = comfy .samplers .cfg_function (self .inner_model , out [2 ], out [1 ], self .cfg1 , x , timestep , model_options = model_options , cond = positive_cond , uncond = middle_cond )
703+ return out [0 ] + self .cfg2 * (pred_text - out [0 ])
704+ else :
705+ if model_options .get ("disable_cfg1_optimization" , False ) == False :
706+ if math .isclose (self .cfg2 , 1.0 ):
707+ negative_cond = None
708+ if math .isclose (self .cfg1 , 1.0 ):
709+ middle_cond = None
710+
711+ out = comfy .samplers .calc_cond_batch (self .inner_model , [negative_cond , middle_cond , positive_cond ], x , timestep , model_options )
712+ return comfy .samplers .cfg_function (self .inner_model , out [1 ], out [0 ], self .cfg2 , x , timestep , model_options = model_options , cond = middle_cond , uncond = negative_cond ) + (out [2 ] - out [1 ]) * self .cfg1
706713
707714class DualCFGGuider :
708715 @classmethod
@@ -714,6 +721,7 @@ def INPUT_TYPES(s):
714721 "negative" : ("CONDITIONING" , ),
715722 "cfg_conds" : ("FLOAT" , {"default" : 8.0 , "min" : 0.0 , "max" : 100.0 , "step" :0.1 , "round" : 0.01 }),
716723 "cfg_cond2_negative" : ("FLOAT" , {"default" : 8.0 , "min" : 0.0 , "max" : 100.0 , "step" :0.1 , "round" : 0.01 }),
724+ "style" : (["regular" , "nested" ],),
717725 }
718726 }
719727
@@ -722,10 +730,10 @@ def INPUT_TYPES(s):
722730 FUNCTION = "get_guider"
723731 CATEGORY = "sampling/custom_sampling/guiders"
724732
725- def get_guider (self , model , cond1 , cond2 , negative , cfg_conds , cfg_cond2_negative ):
733+ def get_guider (self , model , cond1 , cond2 , negative , cfg_conds , cfg_cond2_negative , style ):
726734 guider = Guider_DualCFG (model )
727735 guider .set_conds (cond1 , cond2 , negative )
728- guider .set_cfg (cfg_conds , cfg_cond2_negative )
736+ guider .set_cfg (cfg_conds , cfg_cond2_negative , nested = ( style == "nested" ) )
729737 return (guider ,)
730738
731739class DisableNoise :
0 commit comments