replace model unloading and peft loading mechanism for compare feature

This commit is contained in:
Roland Tannous 2026-02-14 19:18:49 +00:00
commit 3ff3def555

View file

@ -214,37 +214,29 @@ class InferenceBackend:
def activate_lora_adapter(self, base_model_name: str, lora_path: str) -> Tuple[bool, Optional[str]]:
"""
Activates a specific LoRA adapter on what is assumed to be a clean base model.
Uses PeftModel.from_pretrained() which correctly wraps the base model.
"""
model = self.models[base_model_name].get("model")
adapter_name_to_load = lora_path.split("/")[-1].replace(".", "_")
print(f"[DEBUG] activate_lora_adapter called. base_model_name='{base_model_name}', lora_path='{lora_path}'")
print(f"[DEBUG] adapter_name_to_load='{adapter_name_to_load}'")
print(f"[DEBUG] Model type BEFORE load_adapter: {model.__class__.__name__}")
print(f"[DEBUG] Has peft_config? {hasattr(model, 'peft_config')}, keys={list(getattr(model, 'peft_config', {}).keys())}")
print(f"[DEBUG] Model type BEFORE: {model.__class__.__name__}")
try:
# At this point, the model should be clean thanks to revert_to_base_model.
# We can now safely load and set the new adapter.
# Step 3: Load the new adapter.
print(f"[DEBUG] Calling model.load_adapter('{lora_path}', adapter_name='{adapter_name_to_load}')...")
model.load_adapter(lora_path, adapter_name=adapter_name_to_load)
print(f"[DEBUG] Model type AFTER load_adapter: {model.__class__.__name__}")
print(f"[DEBUG] peft_config keys AFTER load: {list(getattr(model, 'peft_config', {}).keys())}")
# Step 4: Set the new adapter as active.
print(f"[DEBUG] Calling model.set_adapter('{adapter_name_to_load}')...")
model.set_adapter(adapter_name_to_load)
print(f"[DEBUG] activate_lora_adapter SUCCESS. Model type: {model.__class__.__name__}")
# Use PeftModel.from_pretrained to wrap the clean base model with the adapter.
# This is the correct approach after model.unload() + del peft_config.
print(f"[DEBUG] Calling PeftModel.from_pretrained(model, '{lora_path}', adapter_name='{adapter_name_to_load}')...")
model = PeftModel.from_pretrained(model, lora_path, adapter_name=adapter_name_to_load)
self.models[base_model_name]["model"] = model
print(f"[DEBUG] Model type AFTER: {model.__class__.__name__}")
print(f"[DEBUG] activate_lora_adapter SUCCESS.")
return True, adapter_name_to_load
except Exception as e:
# This will catch the "already exists" error if revert_to_base_model failed.
print(f"[DEBUG] activate_lora_adapter FAILED: {e}")
import traceback
traceback.print_exc()
return False, None
pass
def load_adapter(self, base_model_name: str, adapter_path: str, adapter_name: str = None) -> bool:
"""