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

Method set_adapter

diffusers/src/diffusers/loaders/peft.py:150–197  ·  view source on GitHub ↗

Sets a specific adapter by forcing the model to only use that adapter and disables the other adapters. If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT [documentation](https://huggingface.co/docs/peft). Args:

(self, adapter_name: Union[str, List[str]])

Source from the content-addressed store, hash-verified

148 self.set_adapter(adapter_name)
149
150 def set_adapter(self, adapter_name: Union[str, List[str]]) -> None:
151 """
152 Sets a specific adapter by forcing the model to only use that adapter and disables the other adapters.
153
154 If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT
155 [documentation](https://huggingface.co/docs/peft).
156
157 Args:
158 adapter_name (Union[str, List[str]])):
159 The list of adapters to set or the adapter name in the case of a single adapter.
160 """
161 check_peft_version(min_version=MIN_PEFT_VERSION)
162
163 if not self._hf_peft_config_loaded:
164 raise ValueError("No adapter loaded. Please load an adapter first.")
165
166 if isinstance(adapter_name, str):
167 adapter_name = [adapter_name]
168
169 missing = set(adapter_name) - set(self.peft_config)
170 if len(missing) > 0:
171 raise ValueError(
172 f"Following adapter(s) could not be found: {', '.join(missing)}. Make sure you are passing the correct adapter name(s)."
173 f" current loaded adapters are: {list(self.peft_config.keys())}"
174 )
175
176 from peft.tuners.tuners_utils import BaseTunerLayer
177
178 _adapters_has_been_set = False
179
180 for _, module in self.named_modules():
181 if isinstance(module, BaseTunerLayer):
182 if hasattr(module, "set_adapter"):
183 module.set_adapter(adapter_name)
184 # Previous versions of PEFT does not support multi-adapter inference
185 elif not hasattr(module, "set_adapter") and len(adapter_name) != 1:
186 raise ValueError(
187 "You are trying to set multiple adapters and you have a PEFT version that does not support multi-adapter inference. Please upgrade to the latest version of PEFT."
188 " `pip install -U peft` or `pip install -U git+https://github.com/huggingface/peft.git`"
189 )
190 else:
191 module.active_adapter = adapter_name
192 _adapters_has_been_set = True
193
194 if not _adapters_has_been_set:
195 raise ValueError(
196 "Did not succeeded in setting the adapter. Please make sure you are using a model that supports adapters."
197 )
198
199 def disable_adapters(self) -> None:
200 r"""

Callers 2

add_adapterMethod · 0.95

Calls 1

check_peft_versionFunction · 0.85

Tested by

no test coverage detected