peft icon indicating copy to clipboard operation
peft copied to clipboard

[Q] Does support for embedding and conv2d layers in bnb.py at tuners/lora make sense?

Open GM-git-dotcom opened this issue 2 years ago • 8 comments

Feature request

Currently, bnb.py at peft/tuners/lora has classes for Linear4bit and Linear8bit. But layers.py at peft/tuners/lora defines implementations for LoRA in Linear, Embedding, and Conv2d (I believe in the future, it would also include Conv3d as per #1193).

I was wondering if it makes sense to extend quantization support for Embedding and Conv2d layers? Could they be a thing in bnb.py? (possibly in other tuners as well). Sorry if this is obvious.

Motivation

I was just curious about bnb.py only having support for Linear when LoRA can be implemented through other layer types (please correct me if I'm wrong).

Your contribution

With some help, I would be happy to contribute a PR towards this, starting with bnb for Embedding.

GM-git-dotcom avatar Jan 24 '24 16:01 GM-git-dotcom

Yes, in principle, it should be possible add support for the Embedding layer from bnb to PEFT. Feel free to open a PR and ping us so that we can give feedback and help with open questions.

Regarding Conv2d, I don't think it's supported by bnb and thus could not be easily added to PEFT.

BenjaminBossan avatar Feb 05 '24 13:02 BenjaminBossan

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.

github-actions[bot] avatar Feb 29 '24 15:02 github-actions[bot]

Thank you very much for making such a great project open source, I learned a lot of new things by using PEFT, thank you again. I have a puzzle of class Linear8bitLt in peft/src/peft/tuners/lora/bnb.py. By reading the code I understand that it is different from the definition in bitsanbytes because the latter uses int8 precision during calculation. I used PEFT in Alpaca-lora finetune. In the transfoemer model obtained with is_load_8bit, the parts (q, v) replaced by lora still use Linear8bitLt on the surface, but actually use torch.nn.Linear in peft/src/peft/tuners/lora/layer.py update_layer(). Lora layer does not use int8 to participate in the calculation, it is for the sake of accuracy. Is that right, or did I not understand the whole calling program correctly?

asr-sheep1 avatar Mar 08 '24 13:03 asr-sheep1

Hi @asr-sheep1

Below is what happens under the hood if you initialize a Linear8bitLt class from peft

lora_animated

In blue you have the original linear layer (which is in 8bit) and we simply attach the LoRA adapters next to it and calculate the sum of both hidden states.

The original Linear8bit layer performs some part of the matmul in 8-bit and other parts (outliers) in 16-bit but the resulting hidden states will be in 16-bit precision. See https://huggingface.co/blog/hf-bitsandbytes-integration for more details

younesbelkada avatar Mar 11 '24 09:03 younesbelkada

Hi @asr-sheep1

Below is what happens under the hood if you initialize a Linear8bitLt class from peft

lora_animated lora_animated

In blue you have the original linear layer (which is in 8bit) and we simply attach the LoRA adapters next to it and calculate the sum of both hidden states.

The original Linear8bit layer performs some part of the matmul in 8-bit and other parts (outliers) in 16-bit but the resulting hidden states will be in 16-bit precision. See https://huggingface.co/blog/hf-bitsandbytes-integration for more details

Thanks! I have reviewed this material. So, during the calculation process, Lora_A and Lora_B, these two layers of networks, did not use int8 matmul, right?

asr-sheep1 avatar Mar 11 '24 12:03 asr-sheep1

Correct yes, Lora_A and Lora_B do not use int8 matmul in this case

younesbelkada avatar Mar 12 '24 07:03 younesbelkada

Correct yes, Lora_A and Lora_B do not use int8 matmul in this case

Thank you very much for your reply, helping me resolve a confusion. I will further study the excellent work on PEFT. Salute to you all.

asr-sheep1 avatar Mar 12 '24 08:03 asr-sheep1

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.

github-actions[bot] avatar Apr 05 '24 15:04 github-actions[bot]