MCPcopy Create free account
hub / github.com/Kosinkadink/ComfyUI-Advanced-ControlNet / SparseCtrlAdvanced

Class SparseCtrlAdvanced

adv_control/control.py:346–509  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

344 self.copy_to(c)
345 self.copy_to_advanced(c)
346 return c
347
348
349class SparseCtrlAdvanced(ControlNetAdvanced):
350 def __init__(self, control_model: SparseControlNet, motion_model: InterfaceAnimateDiffModel,
351 timestep_keyframes: TimestepKeyframeGroup, sparse_settings: SparseSettings=None, global_average_pooling=False, load_device=None, manual_cast_dtype=None):
352 super().__init__(control_model=None, timestep_keyframes=timestep_keyframes, global_average_pooling=global_average_pooling, load_device=load_device, manual_cast_dtype=manual_cast_dtype)
353 self.control_model = control_model
354 if control_model is not None:
355 self.control_model_wrapped: ModelPatcher = create_sparse_modelpatcher(self.control_model, motion_model, load_device=load_device, offload_device=comfy.model_management.unet_offload_device())
356 self.prepare_conditioning_info()
357 self.add_compatible_weight(ControlWeightType.SPARSECTRL)
358 self.postpone_condhint_latents_check = True
359 self.sparse_settings = sparse_settings if sparse_settings is not None else SparseSettings.default()
360 self.model_latent_format = None # latent format for active SD model, NOT controlnet
361 self.preprocessed = False
362
363 def prepare_conditioning_info(self):
364 if self.control_model.use_simplified_conditioning_embedding:
365 # TODO: allow vae_optional to be used instead of preprocessor
366 #self.require_vae = True
367 self.allow_condhint_latents = True
368
369 @property
370 def motion_model(self) -> InterfaceAnimateDiffModel:
371 motion_models = self.control_model_wrapped.get_additional_models_with_key(WrapperConsts.ACN)
372 if len(motion_models) == 0:
373 return None
374 return motion_models[0].model
375
376 def get_control_advanced(self, x_noisy: Tensor, t, cond, batched_number: int, transformer_options):
377 # normal ControlNet stuff
378 control_prev = None
379 if self.previous_controlnet is not None:
380 control_prev = self.previous_controlnet.get_control(x_noisy, t, cond, batched_number, transformer_options)
381
382 if self.timestep_range is not None:
383 if t[0] > self.timestep_range[0] or t[0] < self.timestep_range[1]:
384 if control_prev is not None:
385 return control_prev
386 else:
387 return None
388
389 dtype = self.control_model.dtype
390 if self.manual_cast_dtype is not None:
391 dtype = self.manual_cast_dtype
392 output_dtype = x_noisy.dtype
393 # set actual input length on motion model
394 actual_length = x_noisy.size(0)//batched_number
395 full_length = actual_length if self.sub_idxs is None else self.full_latent_length
396 if self.motion_model is not None:
397 self.motion_model.set_video_length(video_length=actual_length, full_length=full_length)
398 # prepare cond_hint, if needed
399 dim_mult = 1 if self.control_model.use_simplified_conditioning_embedding else 8
400 if self.sub_idxs is not None or self.cond_hint is None or x_noisy.shape[2]*dim_mult != self.cond_hint.shape[2] or x_noisy.shape[3]*dim_mult != self.cond_hint.shape[3]:
401 # clear out cond_hint and conditioning_mask
402 if self.cond_hint is not None:
403 del self.cond_hint

Callers 2

copyMethod · 0.85
load_sparsectrlFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected