MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / set_adapters

Method set_adapters

diffusers/src/diffusers/loaders/lora_base.py:516–581  ·  view source on GitHub ↗
(
        self,
        adapter_names: Union[List[str], str],
        adapter_weights: Optional[Union[float, Dict, List[float], List[Dict]]] = None,
    )

Source from the content-addressed store, hash-verified

514 self.num_fused_loras -= 1
515
516 def set_adapters(
517 self,
518 adapter_names: Union[List[str], str],
519 adapter_weights: Optional[Union[float, Dict, List[float], List[Dict]]] = None,
520 ):
521 adapter_names = [adapter_names] if isinstance(adapter_names, str) else adapter_names
522
523 adapter_weights = copy.deepcopy(adapter_weights)
524
525 # Expand weights into a list, one entry per adapter
526 if not isinstance(adapter_weights, list):
527 adapter_weights = [adapter_weights] * len(adapter_names)
528
529 if len(adapter_names) != len(adapter_weights):
530 raise ValueError(
531 f"Length of adapter names {len(adapter_names)} is not equal to the length of the weights {len(adapter_weights)}"
532 )
533
534 list_adapters = self.get_list_adapters() # eg {"unet": ["adapter1", "adapter2"], "text_encoder": ["adapter2"]}
535 # eg ["adapter1", "adapter2"]
536 all_adapters = {adapter for adapters in list_adapters.values() for adapter in adapters}
537 missing_adapters = set(adapter_names) - all_adapters
538 if len(missing_adapters) > 0:
539 raise ValueError(
540 f"Adapter name(s) {missing_adapters} not in the list of present adapters: {all_adapters}."
541 )
542
543 # eg {"adapter1": ["unet"], "adapter2": ["unet", "text_encoder"]}
544 invert_list_adapters = {
545 adapter: [part for part, adapters in list_adapters.items() if adapter in adapters]
546 for adapter in all_adapters
547 }
548
549 # Decompose weights into weights for denoiser and text encoders.
550 _component_adapter_weights = {}
551 for component in self._lora_loadable_modules:
552 model = getattr(self, component)
553
554 for adapter_name, weights in zip(adapter_names, adapter_weights):
555 if isinstance(weights, dict):
556 component_adapter_weights = weights.pop(component, None)
557
558 if component_adapter_weights is not None and not hasattr(self, component):
559 logger.warning(
560 f"Lora weight dict contains {component} weights but will be ignored because pipeline does not have {component}."
561 )
562
563 if component_adapter_weights is not None and component not in invert_list_adapters[adapter_name]:
564 logger.warning(
565 (
566 f"Lora weight dict for adapter '{adapter_name}' contains {component},"
567 f"but this will be ignored because {adapter_name} does not contain weights for {component}."
568 f"Valid parts for {adapter_name} are: {invert_list_adapters[adapter_name]}."
569 )
570 )
571
572 else:
573 component_adapter_weights = weights

Callers

nothing calls this directly

Calls 4

get_list_adaptersMethod · 0.95
popMethod · 0.45
setdefaultMethod · 0.45

Tested by

no test coverage detected