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

<wip> fix lora module add for transformers #19

Merged
merged 1 commit into from
Nov 24, 2023
Merged
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
14 changes: 13 additions & 1 deletion analog/lora/lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def find_parameter_sharing_group(
return found_groups[0]


def _get_submodules(model, key):
"""
Helper function to replace a module with transformers model
https://github.com/huggingface/peft/blob/c0dd27bc974e4a62c6072142146887b75bb2de6c/src/peft/utils/other.py#L251
"""
parent = model.get_submodule(".".join(key.split(".")[:-1]))
target_name = key.split(".")[-1]
target = model.get_submodule(key)
return parent, target, target_name


class LoRAHandler:
"""
Transforms a model into a Lora model.
Expand Down Expand Up @@ -94,4 +105,5 @@ def add_lora(
lora_module.init_weight(self.init_strategy, hessian_state[name])
lora_module.to(device)

setattr(model, name, lora_module)
parent, target, target_name = _get_submodules(model, name)
hwijeen marked this conversation as resolved.
Show resolved Hide resolved
setattr(parent, target_name, lora_module)
Loading