├── .gitignore ├── LICENSE ├── README.md ├── dora.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Chris Taylor 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DoRA 2 | 3 | Implementation of "DoRA: Weight-Decomposed Low-Rank Adaptation" (Liu et al, 2024) https://arxiv.org/pdf/2402.09353.pdf 4 | 5 | ## Demo 6 | 7 | Install conda: https://docs.conda.io/projects/miniconda/en/latest/index.html 8 | 9 | ```bash 10 | git clone https://github.com/catid/dora.git 11 | cd dora 12 | 13 | conda create -n dora python=3.10 -y && conda activate dora 14 | 15 | pip install -U -r requirements.txt 16 | 17 | python dora.py 18 | ``` 19 | 20 | ## Output 21 | 22 | ```bash 23 | (dora) ➜ dora git:(main) python dora.py 24 | Total Parameters: 11 25 | Trainable Parameters: 11 26 | Final Evaluation Loss: 0.13415579497814178 27 | Total Parameters: 65 28 | Trainable Parameters: 54 29 | Continuing training with DoRA layers... 30 | Final (DoRA) Evaluation Loss: 0.06080308556556702 31 | ``` 32 | -------------------------------------------------------------------------------- /dora.py: -------------------------------------------------------------------------------- 1 | import torch.optim as optim 2 | from torch.utils.data import DataLoader, TensorDataset 3 | 4 | import torch 5 | import torch.nn as nn 6 | import torch.nn.functional as F 7 | 8 | torch.manual_seed(0) 9 | 10 | # This layer is dropped into your pre-trained PyTorch model where nn.Linear is used 11 | class DoRALayer(nn.Module): 12 | def __init__(self, d_in, d_out, rank=4, weight=None, bias=None): 13 | super().__init__() 14 | 15 | if weight is not None: 16 | self.weight = nn.Parameter(weight, requires_grad=False) 17 | else: 18 | self.weight = nn.Parameter(torch.Tensor(d_out, d_in), requires_grad=False) 19 | 20 | if bias is not None: 21 | self.bias = nn.Parameter(bias, requires_grad=False) 22 | else: 23 | self.bias = nn.Parameter(torch.Tensor(d_out), requires_grad=False) 24 | 25 | # m = Magnitude column-wise across output dimension 26 | self.m = nn.Parameter(self.weight.norm(p=2, dim=0, keepdim=True)) 27 | 28 | std_dev = 1 / torch.sqrt(torch.tensor(rank).float()) 29 | self.lora_A = nn.Parameter(torch.randn(d_out, rank)*std_dev) 30 | self.lora_B = nn.Parameter(torch.zeros(rank, d_in)) 31 | 32 | def forward(self, x): 33 | lora = torch.matmul(self.lora_A, self.lora_B) 34 | adapted = self.weight + lora 35 | column_norm = adapted.norm(p=2, dim=0, keepdim=True) 36 | norm_adapted = adapted / column_norm 37 | calc_weights = self.m * norm_adapted 38 | return F.linear(x, calc_weights, self.bias) 39 | 40 | 41 | class SimpleModel(nn.Module): 42 | def __init__(self, input_dim, output_dim): 43 | super(SimpleModel, self).__init__() 44 | self.layer1 = nn.Linear(input_dim, output_dim) 45 | 46 | def forward(self, x): 47 | x = self.layer1(x) 48 | return x 49 | 50 | # Generating synthetic data 51 | def generate_data(num_samples=100, input_dim=10): 52 | X = torch.randn(num_samples, input_dim) 53 | y = torch.sum(X, dim=1, keepdim=True) # Simple relationship for demonstration 54 | return X, y 55 | 56 | # Training function 57 | def train(model, criterion, optimizer, data_loader, epochs=5): 58 | model.train() 59 | for epoch in range(epochs): 60 | for inputs, targets in data_loader: 61 | optimizer.zero_grad() 62 | outputs = model(inputs) 63 | loss = criterion(outputs, targets) 64 | loss.backward() 65 | optimizer.step() 66 | #print(f"Epoch {epoch+1}, Loss: {loss.item()}") 67 | 68 | 69 | 70 | def replace_linear_with_dora(model): 71 | for name, module in model.named_children(): 72 | if isinstance(module, nn.Linear): 73 | # Get the input and output dimensions of the current nn.Linear layer 74 | d_in = module.in_features 75 | d_out = module.out_features 76 | 77 | # Create a new DoRALayer with the same dimensions 78 | setattr(model, name, DoRALayer(d_out=d_out, d_in=d_in, weight=module.weight.data.clone(), bias=module.bias.data.clone())) 79 | else: 80 | # Recursively apply this function to submodules 81 | replace_linear_with_dora(module) 82 | 83 | def print_model_parameters(model): 84 | total_params = sum(p.numel() for p in model.parameters()) 85 | trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad) 86 | 87 | print(f"Total Parameters: {total_params}") 88 | print(f"Trainable Parameters: {trainable_params}") 89 | 90 | # Main script 91 | if __name__ == "__main__": 92 | input_dim, output_dim = 10, 1 93 | model = SimpleModel(input_dim, output_dim) 94 | criterion = nn.MSELoss() 95 | optimizer = optim.AdamW(model.parameters(), lr=0.001) 96 | 97 | X, y = generate_data(num_samples=1000, input_dim=input_dim) 98 | dataset = TensorDataset(X, y) 99 | data_loader = DataLoader(dataset, batch_size=64, shuffle=True) 100 | 101 | print_model_parameters(model) 102 | 103 | train(model, criterion, optimizer, data_loader, epochs=100) 104 | 105 | # Evaluate the model 106 | model.eval() 107 | with torch.no_grad(): 108 | inputs, targets = next(iter(data_loader)) 109 | predictions = model(inputs) 110 | loss = criterion(predictions, targets) 111 | print(f"Final Evaluation Loss: {loss.item()}") 112 | 113 | replace_linear_with_dora(model) 114 | 115 | print_model_parameters(model) 116 | 117 | # Continue training with the Dora model 118 | optimizer = optim.AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=0.001) 119 | print("Continuing training with DoRA layers...") 120 | train(model, criterion, optimizer, data_loader, epochs=5) # Continue training 121 | 122 | # Evaluate the model 123 | model.eval() 124 | with torch.no_grad(): 125 | inputs, targets = next(iter(data_loader)) 126 | predictions = model(inputs) 127 | loss = criterion(predictions, targets) 128 | print(f"Final (DoRA) Evaluation Loss: {loss.item()}") 129 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | torch 2 | --------------------------------------------------------------------------------