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

FIX for ConvNd layers using the groups argument. #2403

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/peft/tuners/lora/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,9 @@ def update_layer(
conv_layer = type(base_layer)
out_kernel = out_stride = (1,) * (self._kernel_dim - 2)
self.lora_A[adapter_name] = conv_layer(self.in_features, r, kernel_size, stride, padding, bias=False)
self.lora_B[adapter_name] = conv_layer(r, self.out_features, out_kernel, out_stride, bias=lora_bias)
self.lora_B[adapter_name] = conv_layer(
r, self.out_features // base_layer.groups, out_kernel, out_stride, bias=lora_bias
)
self.lora_bias[adapter_name] = lora_bias

if use_rslora:
Expand Down Expand Up @@ -1243,13 +1245,12 @@ def get_delta_weight(self, adapter) -> torch.Tensor:
3
) * self.scaling[adapter]
else:
output_tensor = (
self.conv_fn(
weight_A.transpose(0, 1),
weight_B,
).transpose(0, 1)
* self.scaling[adapter]
)
output_tensor = self.conv_fn(weight_A.transpose(0, 1), weight_B)

if self.get_base_layer().groups > 1:
output_tensor = output_tensor * self.scaling[adapter]
else:
output_tensor = output_tensor.transpose(0, 1) * self.scaling[adapter]

if cast_to_fp32:
output_tensor = output_tensor.to(dtype=dtype)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_custom_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
("Conv2d 2 LoRA", "Conv2d", LoraConfig, {"target_modules": ["conv2d", "lin0"]}),
("Conv2d 1 LoRA with DoRA", "Conv2d", LoraConfig, {"target_modules": ["conv2d"], "use_dora": True}),
("Conv2d 2 LoRA with DoRA", "Conv2d", LoraConfig, {"target_modules": ["conv2d", "lin0"], "use_dora": True}),
("Conv2d Groups LoRA", "Conv2dGroups", LoraConfig, {"target_modules": ["conv2d"]}),
("Conv2d Groups LoRA with DoRA", "Conv2dGroups", LoraConfig, {"target_modules": ["conv2d"], "use_dora": True}),
("Conv3d 1 LoRA", "Conv3d", LoraConfig, {"target_modules": ["conv3d"]}),
("Conv3d 2 LoRA", "Conv3d", LoraConfig, {"target_modules": ["conv3d", "lin0"]}),
("Conv3d 1 LoRA with DoRA", "Conv3d", LoraConfig, {"target_modules": ["conv3d"], "use_dora": True}),
Expand Down Expand Up @@ -903,6 +905,25 @@ def forward(self, X):
return X


class ModelConv2DGroups(nn.Module):
def __init__(self):
super().__init__()
self.conv2d = nn.Conv2d(5, 5, 3, groups=5)
self.relu = nn.ReLU()
self.flat = nn.Flatten()
self.lin0 = nn.Linear(5, 2)
self.sm = nn.LogSoftmax(dim=-1)

def forward(self, X):
X = X.float().reshape(-1, 5, 3, 3)
X = self.conv2d(X)
X = self.relu(X)
X = self.flat(X)
X = self.lin0(X)
X = self.sm(X)
return X


class ModelConv3D(nn.Module):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -967,6 +988,9 @@ def from_pretrained(cls, model_id, torch_dtype=None):
if model_id == "Conv2d":
return ModelConv2D().to(torch_dtype)

if model_id == "Conv2dGroups":
return ModelConv2DGroups().to(torch_dtype)

if model_id == "Conv3d":
return ModelConv3D().to(torch_dtype)

Expand Down
Loading