[go: nahoru, domu]

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

How to finetune embeddings and LM head as a single layer when they are tied? #1750

Closed
GokulNC opened this issue May 21, 2024 · 2 comments
Closed

Comments

@GokulNC
Copy link
GokulNC commented May 21, 2024

I am looking to LoRA-finetune models like Gemma, which have tied embeddings.
But, I would also like to have the shared embeddings as trainable (the common embedding table corresponding to both input and output embeddings of the network).

How do I achieve this?


Note: Passing both ["embed_tokens","lm_head"] to modules_to_save will result in untying them, because PEFT will create separate tensor copies. Passing only ["embed_tokens"] will result in only the input embeddings trainable (by making a separate PEFT copy), while the output embeddings being as it is (the original tensor).

@BenjaminBossan
Copy link
Member

One possibility that you could try is to not add the embeddings to modules_to_save but instead just LoRA-tune them by adding them to target_modules. This could be especially useful for Gemma models, since they have huge embedding layers, so fully fine-tuning them pushes the number of trainable parameters up by a lot.

Another possibility (untested) is to try to manually tie the weights after initializing the PEFT models. So something along the lines of:

config = LoraConfig(..., modules_to_save=["embed_tokens", "lm_head"])
model = get_peft_model(model, config)
# exact names depend on architecture:
model.base_model.model.model.decoder.embed_tokens.modules_to_save["default"].weight = model.base_model.model.lm_head.modules_to_save["default"].weight

Copy link

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants