Skip to content

Commit

Permalink
🦆 bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2za committed Jun 18, 2023
1 parent d363816 commit 328c533
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Small Library of Torch Adaptation modules
- [X] LoRA
- [X] Prompt Tuning
- [X] Bottleneck Adapter
- [ ] Prefix Tuning
- [X] Prefix Tuning
- [ ] P-Tuning

# Installation
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "torch-adapters"
version = "0.0.5"
version = "0.0.6"
description = "Small Library of Torch Adaptation modules"
authors = ["ma2za <mazzapaolo2019@gmail.com>"]
license = "MIT"
Expand Down
8 changes: 4 additions & 4 deletions src/torch_adapters/adapters/lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LoRA(nn.Linear, AdapterMixin):
"""

def __init__(
self, src: nn.Linear, alpha: int = 8, r: int = 8, dropout: float = 0.0
self, src: nn.Linear, alpha: int = 8, r: int = 8, dropout: float = 0.0
):
super().__init__(src.in_features, src.out_features)

Expand All @@ -30,8 +30,8 @@ def __init__(
self.lora_weight = nn.Sequential(
OrderedDict(
[
("dropout", nn.Dropout(p=dropout)),
("A", nn.Linear(self.in_features, r, bias=False)),
("dropout", nn.Dropout(p=dropout)),
("B", nn.Linear(r, self.out_features, bias=False)),
]
)
Expand All @@ -48,13 +48,13 @@ def merge(self) -> nn.Linear:
# TODO check if matrix transpose is required
merged_layer = nn.Linear(self.in_features, self.out_features)
merged_weight = self.weight.data + (self.alpha / self.r) * (
self.lora_weight.B.weight.data @ self.lora_weight.A.weight.data
self.lora_weight.B.weight.data @ self.lora_weight.A.weight.data
)
merged_layer.weight.data = merged_weight.detach().clone().to(self.weight.device)
merged_layer.bias.data = self.bias.data.detach().clone().to(self.bias.device)
return merged_layer

def forward(self, input_ids: Tensor) -> Tensor:
return super().forward(input_ids) + self.lora_weight(input_ids) * (
self.alpha / self.r
self.alpha / self.r
)
11 changes: 7 additions & 4 deletions src/torch_adapters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def add_prefix_tuning(
model: nn.Module, layers_names: List[str], config: Dict
model: nn.Module, layers_names: List[str], config: Dict
) -> torch.nn.Module:
for name, module in model.named_modules():
if any([i in name for i in layers_names]):
Expand Down Expand Up @@ -48,7 +48,7 @@ def drop_prefix_tuning_reparametrization(model: nn.Module):


def add_adapter(
model: nn.Module, layers_names: List[str], config: Dict
model: nn.Module, layers_names: List[str], config: Dict
) -> torch.nn.Module:
"""
Expand All @@ -74,7 +74,7 @@ def add_adapter(


def add_lora(
model: nn.Module, layers_names: List[str], config: Dict
model: nn.Module, layers_names: List[str], config: Dict
) -> torch.nn.Module:
"""
Expand All @@ -100,7 +100,10 @@ def add_lora(

module.__setattr__(
attr_name,
LoRA(attr, alpha=config.get("alpha", 8), r=config.get("r", 8)),
LoRA(attr,
alpha=config.get("alpha", 8),
r=config.get("r", 8),
dropout=config.get("dropout", 0.0)),
)
return model

Expand Down

0 comments on commit 328c533

Please sign in to comment.