Vlad Iliescu commited on
Commit ·
56efe4e
1
Parent(s): b990348
fixed loras
Browse files- lora_utils.py +26 -9
lora_utils.py
CHANGED
|
@@ -305,10 +305,17 @@ def _load_lora_with_peft(host, state_dict, adapter_name):
|
|
| 305 |
unexpected_keys = [
|
| 306 |
key
|
| 307 |
for key in getattr(result, "unexpected_keys", [])
|
| 308 |
-
if ".lora_" in key
|
| 309 |
]
|
| 310 |
if unexpected_keys:
|
| 311 |
raise ValueError(f"Unexpected LoRA keys while loading adapter: {unexpected_keys[:5]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
return result
|
| 313 |
|
| 314 |
|
|
@@ -321,6 +328,15 @@ def _iter_host_modules(host):
|
|
| 321 |
return []
|
| 322 |
|
| 323 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
def _peft_adapter_names_on_host(host):
|
| 325 |
adapter_names = set()
|
| 326 |
peft_config = getattr(host, "peft_config", None)
|
|
@@ -351,7 +367,7 @@ def _adapter_names_on_host(host):
|
|
| 351 |
|
| 352 |
def _delete_peft_adapter_on_host(host, adapter_name):
|
| 353 |
deleted = False
|
| 354 |
-
for target in (host
|
| 355 |
if not hasattr(target, "delete_adapter"):
|
| 356 |
continue
|
| 357 |
try:
|
|
@@ -369,7 +385,7 @@ def _delete_peft_adapter_on_host(host, adapter_name):
|
|
| 369 |
def _set_peft_adapters_on_host(host, adapter_names, adapter_weights):
|
| 370 |
changed = False
|
| 371 |
if not adapter_names:
|
| 372 |
-
for target in (host
|
| 373 |
if hasattr(target, "enable_adapters"):
|
| 374 |
try:
|
| 375 |
target.enable_adapters(False)
|
|
@@ -378,17 +394,18 @@ def _set_peft_adapters_on_host(host, adapter_names, adapter_weights):
|
|
| 378 |
pass
|
| 379 |
return changed
|
| 380 |
|
| 381 |
-
for target in (host
|
| 382 |
if hasattr(target, "set_adapter"):
|
| 383 |
try:
|
| 384 |
target.set_adapter(adapter_names)
|
| 385 |
changed = True
|
| 386 |
except TypeError:
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
|
|
|
| 392 |
except Exception:
|
| 393 |
pass
|
| 394 |
if hasattr(target, "enable_adapters"):
|
|
|
|
| 305 |
unexpected_keys = [
|
| 306 |
key
|
| 307 |
for key in getattr(result, "unexpected_keys", [])
|
| 308 |
+
if ".lora_" in key
|
| 309 |
]
|
| 310 |
if unexpected_keys:
|
| 311 |
raise ValueError(f"Unexpected LoRA keys while loading adapter: {unexpected_keys[:5]}")
|
| 312 |
+
missing_keys = [
|
| 313 |
+
key
|
| 314 |
+
for key in getattr(result, "missing_keys", [])
|
| 315 |
+
if ".lora_" in key and f".{adapter_name}." in key
|
| 316 |
+
]
|
| 317 |
+
if missing_keys:
|
| 318 |
+
raise ValueError(f"Missing LoRA keys while loading adapter: {missing_keys[:5]}")
|
| 319 |
return result
|
| 320 |
|
| 321 |
|
|
|
|
| 328 |
return []
|
| 329 |
|
| 330 |
|
| 331 |
+
def _iter_host_and_modules(host):
|
| 332 |
+
seen = set()
|
| 333 |
+
for target in (host, *_iter_host_modules(host)):
|
| 334 |
+
if id(target) in seen:
|
| 335 |
+
continue
|
| 336 |
+
seen.add(id(target))
|
| 337 |
+
yield target
|
| 338 |
+
|
| 339 |
+
|
| 340 |
def _peft_adapter_names_on_host(host):
|
| 341 |
adapter_names = set()
|
| 342 |
peft_config = getattr(host, "peft_config", None)
|
|
|
|
| 367 |
|
| 368 |
def _delete_peft_adapter_on_host(host, adapter_name):
|
| 369 |
deleted = False
|
| 370 |
+
for target in _iter_host_and_modules(host):
|
| 371 |
if not hasattr(target, "delete_adapter"):
|
| 372 |
continue
|
| 373 |
try:
|
|
|
|
| 385 |
def _set_peft_adapters_on_host(host, adapter_names, adapter_weights):
|
| 386 |
changed = False
|
| 387 |
if not adapter_names:
|
| 388 |
+
for target in _iter_host_and_modules(host):
|
| 389 |
if hasattr(target, "enable_adapters"):
|
| 390 |
try:
|
| 391 |
target.enable_adapters(False)
|
|
|
|
| 394 |
pass
|
| 395 |
return changed
|
| 396 |
|
| 397 |
+
for target in _iter_host_and_modules(host):
|
| 398 |
if hasattr(target, "set_adapter"):
|
| 399 |
try:
|
| 400 |
target.set_adapter(adapter_names)
|
| 401 |
changed = True
|
| 402 |
except TypeError:
|
| 403 |
+
if len(adapter_names) == 1:
|
| 404 |
+
try:
|
| 405 |
+
target.set_adapter(adapter_names[0])
|
| 406 |
+
changed = True
|
| 407 |
+
except Exception:
|
| 408 |
+
pass
|
| 409 |
except Exception:
|
| 410 |
pass
|
| 411 |
if hasattr(target, "enable_adapters"):
|