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

extract_lora : support tied embeddings #483

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions mergekit/scripts/extract_lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ def validate_and_combine_details(
return module_details, base_model_embedding_size, finetuned_model_embedding_size


def get_model_tensor(loader: LazyTensorLoader, name: str) -> torch.Tensor:
try:
return loader.get_tensor(name)
except KeyError as e:
# Some models like Llama 3.2 1B have tied embedding and output layers, so we need to retry
if name == "lm_head.weight":
return loader.get_tensor("model.embed_tokens.weight")

def extract_lora(
module_details: List[Tuple[str, str]],
base_model_ref: ModelReference,
Expand Down Expand Up @@ -241,8 +249,8 @@ 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")
base_weight = get_model_tensor(base_loader, f"{module_name}.weight")
finetuned_weight = get_model_tensor(finetuned_loader, f"{module_name}.weight")

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