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: str | list[str])
| 544 | self.set_adapter(adapter_name) |
| 545 | |
| 546 | def set_adapter(self, adapter_name: str | list[str]) -> None: |
| 547 | """ |
| 548 | Sets a specific adapter by forcing the model to only use that adapter and disables the other adapters. |
| 549 | |
| 550 | If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT |
| 551 | [documentation](https://huggingface.co/docs/peft). |
| 552 | |
| 553 | Args: |
| 554 | adapter_name (str | list[str])): |
| 555 | The list of adapters to set or the adapter name in the case of a single adapter. |
| 556 | """ |
| 557 | check_peft_version(min_version=MIN_PEFT_VERSION) |
| 558 | |
| 559 | if not self._hf_peft_config_loaded: |
| 560 | raise ValueError("No adapter loaded. Please load an adapter first.") |
| 561 | |
| 562 | if isinstance(adapter_name, str): |
| 563 | adapter_name = [adapter_name] |
| 564 | |
| 565 | missing = set(adapter_name) - set(self.peft_config) |
| 566 | if len(missing) > 0: |
| 567 | raise ValueError( |
| 568 | f"Following adapter(s) could not be found: {', '.join(missing)}. Make sure you are passing the correct adapter name(s)." |
| 569 | f" current loaded adapters are: {list(self.peft_config.keys())}" |
| 570 | ) |
| 571 | |
| 572 | from peft.tuners.tuners_utils import BaseTunerLayer |
| 573 | |
| 574 | _adapters_has_been_set = False |
| 575 | |
| 576 | for _, module in self.named_modules(): |
| 577 | if isinstance(module, BaseTunerLayer): |
| 578 | if hasattr(module, "set_adapter"): |
| 579 | module.set_adapter(adapter_name) |
| 580 | # Previous versions of PEFT does not support multi-adapter inference |
| 581 | elif not hasattr(module, "set_adapter") and len(adapter_name) != 1: |
| 582 | raise ValueError( |
| 583 | "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." |
| 584 | " `pip install -U peft` or `pip install -U git+https://github.com/huggingface/peft.git`" |
| 585 | ) |
| 586 | else: |
| 587 | module.active_adapter = adapter_name |
| 588 | _adapters_has_been_set = True |
| 589 | |
| 590 | if not _adapters_has_been_set: |
| 591 | raise ValueError( |
| 592 | "Did not succeeded in setting the adapter. Please make sure you are using a model that supports adapters." |
| 593 | ) |
| 594 | |
| 595 | def disable_adapters(self) -> None: |
| 596 | r""" |