|
6 | 6 | from typing import Callable, Mapping, Optional, Sequence, Tuple |
7 | 7 |
|
8 | 8 | import torch |
| 9 | +from packaging.version import Version |
9 | 10 |
|
10 | 11 |
|
11 | 12 | try: |
@@ -368,6 +369,23 @@ def _rename_legacy_weights(self, k): |
368 | 369 | k = k.replace(old, new) |
369 | 370 | return k |
370 | 371 |
|
| 372 | + def _fix_backward_compat(self, config): |
| 373 | + # Fix error in previous versions for LoRA/ (IA)^3 |
| 374 | + ADAPTER_PREFIX = "adapters." |
| 375 | + MIN_VERSION = Version("1.1.0") |
| 376 | + |
| 377 | + version = config.get("version", "") |
| 378 | + if version.startswith(ADAPTER_PREFIX) and Version(version[len(ADAPTER_PREFIX) :]) < MIN_VERSION: |
| 379 | + if ( |
| 380 | + config["config"].get("architecture", None) == "lora" |
| 381 | + and config["config"]["r"] != config["config"]["alpha"] |
| 382 | + ): |
| 383 | + logger.warning( |
| 384 | + "Loading a LoRA trained using a faulty scaling implementation of a previous library version. Editing the configuration to make sure the adapter works as trained." |
| 385 | + "See https://github.com/adapter-hub/adapters/pull/770 for more." |
| 386 | + ) |
| 387 | + config["config"]["alpha"] = config["config"]["r"] |
| 388 | + |
371 | 389 | # This method is used to remove unnecessary invertible adapters from task adapters using the old format. |
372 | 390 | # In the old format, task adapters e.g. using seq_bn config specify inv. adapters but don't use them. |
373 | 391 | # As inv. adapters would be incorrectly used in the new implementation, |
@@ -560,6 +578,8 @@ def load( |
560 | 578 | # The conversion to a set and then back to a list removes all duplicates |
561 | 579 | leave_out = list(set(leave_out + config["config"]["leave_out"])) |
562 | 580 | config["config"]["leave_out"] = leave_out |
| 581 | + # Fix issues |
| 582 | + self._fix_backward_compat(config) |
563 | 583 |
|
564 | 584 | adapter_name = load_as or config["name"] |
565 | 585 | # If the adapter is not part of the model, add it |
|
0 commit comments