Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle weight aliases #490

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions mergekit/scripts/extract_lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from mergekit.card import generate_card_lora
from mergekit.common import ModelReference
from mergekit.io import LazyTensorLoader
from mergekit.architecture import get_architecture_info


def low_rank_decomposition(
Expand Down Expand Up @@ -230,6 +231,8 @@ def extract_lora(
:return: A tuple containing LoRA weights dictionary and ranks dictionary.
"""

base_architecture = get_architecture_info(base_model_ref.config())

base_loader = LazyTensorLoader(
base_model_ref.tensor_index(), lazy_unpickle=(not no_lazy_unpickle)
)
Expand All @@ -241,8 +244,18 @@ def extract_lora(
ranks = {}

for module_type, module_name in tqdm(module_details):
base_weight = base_loader.get_tensor(f"{module_name}.weight")
finetuned_weight = finetuned_loader.get_tensor(f"{module_name}.weight")

module_weight_name = f"{module_name}.weight"

aliases = []
for weight_info in base_architecture.all_weights(base_model_ref.config()):
all_names = [weight_info.name] + list(weight_info.aliases or []) + list(weight_info.tied_names or [])
if module_weight_name in all_names:
aliases = all_names
break

base_weight = base_loader.get_tensor(module_weight_name, aliases=aliases)
finetuned_weight = finetuned_loader.get_tensor(module_weight_name, aliases=aliases)

if module_type == "to_save":
lora_weights[
Expand Down