Skip to main content

Adapter-LoRa for Quantization

LoRa-Logo

Made With Love GitHub issues GitHub forks GitHub stars GitHub license

Comparative Features of "loralib" and "loratorch" Implementations

Distinguishing the "loralib" and "loratorch" Approaches for Implementation

The implementations of "loralib" and "loratorch" exhibit distinct methodologies, particularly when using the example of nn.Linear. The underlying mathematical representations are as follows:

    • loralib Approach

The computation is defined as:

[ h = x W_0^\top + \frac{\alpha}{r} x(BA)^\top, ]

where:

  • x is an input matrix of dimensions (k \times n),
  • W_0 is a pre-trained weight matrix of dimensions (m \times n),
  • r is a predefined LoRA rank,
  • B and A are LoRA matrices of dimensions (m \times r) and (r \times n) respectively,
  • \alpha is a hyper-parameter.
  1. For loralib, $h = x W_0^\top + \frac{\alpha}{r} x(BA)^\top,$

where $x\in\mathbb{R}^{k\times n}$ is the input matrix, $W_0\in\mathbb{R}^{m\times n}$ is the pre-trained weight matrix, $r$ is the predefined LoRA rank, $B\in\mathbb{R}^{m\times r}$ and $A\in \mathbb{R}^{r\times n}$ are the LoRA matrixes, and $\alpha$ is a hyper-parameter.

  1. For loratorch, $h = x (W_0 + \frac{\alpha}{r} BA)^\top.$

loralib computes $xW_0^\top$ and $x(BA)^\top$ respectively and then merges the results. While loratorch merges pre-trained weight $W_0$ and its LoRA weight $BA$ and then computes the results by simply using nn.Linear.forward(). There is no difference between loralib and loratorch in the linear layers. But in some no-linear or complex layers, we are no sure whether this layer satisfies $L(x, W_0)+L(x, BA) = L(x, W_0+BA)$. Hence, it is difficult to extend LoRA to some complex layers by using loralib. On the contrary, the idea of merging weights first in loratorch is more general and extensible. You just call merge_lora_param() in loratorch to merge weights and then call forward() in the original layer to compute the results. With the help of loratorch, you can easily implement LoRA to any type of layer of torch.nn.

Supported Layers

loralib loratorch
nn.Linear ✓ ✓ linear.ipynb
nn.Embedding ✓ ✓ embedding.ipynb
nn.Conv1d ✓ ✓
nn.Conv2d ✓ ✓
nn.Conv3d ✓ ✓
nn.MultiheadAttention ✘ ✓
MergedLinear ✓ (Error) ✓ mergedlinear.ipynb
$\cdots$ hard to extend easy to extend

We compare the results of loralib and loratorch in examples to demonstrate the correctness of the implementation in loratorch.

Quick Start

The usage of AdapterLoRa

  1. Install AdapterLoRa.

    pip install git+https://github.com/Baijiong-Lin/LoRA-Torch
    
pip install AdapterLoRa

Usage Tool AdpaterLoRa

import torch.nn as nn
import torch
from core.Quantized import AdapterLoRa

model = nn.TransformerEncoderLayer(d_model=512, nhead=8)

Adpate_model = AdapterLoRa(model , method="LoRa", Rank=4)

"""
adding Linear Layer built Self.attention 
Replace the layers where you would like to use AdapterLoRa by using  add_layer function.
"""

Adpate_model.add_layer("self_attn") 
Adpate_model.add_layer("linear1")
Adpate_model.add_layer("linear2")

# reconstruct model Quantized 
Adpate_model.reconstruct_model()

# Iplmented LoRa Method
model = Adpate_model.implement_lora(verbose=True)
# Total trainable parameters before LoRA: 3176960
# Total trainable parameters after LoRA: 24576

# This sets requires_grad to False for all parameters without the string "lora_" in their names

# Training loop
for batch in dataloader:
    model.train()

Saving Wieghts model

  • Save LoRA model (only the LoRA matrixes will be saved).
import loralib as lora 
# ===== Before =====
# torch.save(model.state_dict(), checkpoint_path)
# ===== After =====
torch.save(lora.lora_state_dict(model), checkpoint_path)

Loading the Pre-Trained Model

  • Load LoRA model (need to load the pre-trained model first).
import loralib as lora 
# Load the pre-trained checkpoint first
model.load_state_dict(torch.load('ckpt_pretrained.pt'), strict=False)
# Then load the LoRA checkpoint
model.load_state_dict(torch.load('ckpt_lora.pt'), strict=False)
  • Quantized Model

  • Time to Train

  • Cost to Train

What's in it for you?

For each of the above four pillars, we are sharing our codebase and insights to:

  • Assist you to leverage Transfomer-Based Model for your machines needs and challenges

  • Boost reproducibility efforts which are becoming increasingly difficult with Transfomers

i am providing Tool that are ready-to-use for Quantize the model:

  • Finetuning Transfomer-Based on your proprietary dataset via PeFT methodologies such as LoRA and QLoRa

  • Performing hyperparameter optimization to get the maximum performance out of these models

What's the best way to use this repository?

Go over to the Transfomer-Based-specific directory that you are interested in, and open the README.md. We have included details about the LLMs, followed by performance results on open-source datasets!

Roadmap

Our plan is to perform these experiments on all the Transformer-Based model below. To that end, this is a tentative roadmap of the LLMs that we aim to cover:

  • TransfomerEncoder
  • TransfomerDecoder
  • Vision-Transfomer
  • minGPT
  • OpenAI GPT-2
  • Inflection Pi Under Progress

Correspondence

Contributor

AdapterLoRa is developed and maintained by ''Youness ELbrag'' (Email | LinkedIn)

Metadata

Release files for AdapterLoRa 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for AdapterLoRa 2.0.0
File Size Uploaded Provenance
AdapterLoRa-2.0.0.tar.gz 7.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for AdapterLoRa 2.0.0
File Interpreter ABI Platform
AdapterLoRa-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / AdapterLoRa-2.0.0.tar.gz

Download URL AdapterLoRa-2.0.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
142f3f17d480b4541a95dfae133a590c25e5212152fa92adf48f77f4be558a12
BLAKE2b-256 checksum
How to use checksums
af396921f288b74d1ae8ddfd7f4b81ab290f75e029935bb00f73b9a700dd3cf1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release files / AdapterLoRa-2.0.0-py3-none-any.whl

Download URL AdapterLoRa-2.0.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
112d00ae96f710d2c900cbe3c2223ed349e3619aa75dbf01d334726e2225596b
BLAKE2b-256 checksum
How to use checksums
3a4753a777a92eab68ab8b7519de5a36734477871a631c08d52d0338251bc16f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Security for AdapterLoRa 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page