BOFT Conv2d forward ignores dilation, 2D stride/padding and padding_mode
System Info
peft main (0.21.1.dev0), torch 2.14.0, CPU, Python 3.12, macOS arm64.
Reproduction
BOFT's unmerged Conv2d.forward recomputes the convolution with F.conv2d(x, rotated_weight, bias, padding=self.base_layer.padding[0], stride=self.base_layer.stride[0]). That drops dilation, the second stride/padding dimension, and padding_mode. With identity initialization, the adapted model therefore no longer matches the base model for such layers:
import copy, torch
from torch import nn
from peft import get_peft_model, BOFTConfig
class M(nn.Module):
def __init__(self, **kw):
super().__init__()
self.conv = nn.Conv2d(4, 8, 3, **kw)
def forward(self, x):
return self.conv(x)
x = torch.randn(2, 4, 16, 16)
for kw in [dict(padding=1), dict(padding=2, dilation=2), dict(padding=(1, 0), stride=(1, 2)), dict(padding=1, padding_mode="reflect")]:
base = M(**kw).eval()
peft_model = get_peft_model(copy.deepcopy(base), BOFTConfig(boft_block_size=2, target_modules=["conv"])).eval()
out, expected = peft_model(x), base(x)
print(kw, out.shape == expected.shape and torch.allclose(out, expected, atol=1e-5), tuple(out.shape), tuple(expected.shape)){'padding': 1} True (2, 8, 16, 16) (2, 8, 16, 16)
{'padding': 2, 'dilation': 2} False (2, 8, 18, 18) (2, 8, 16, 16)
{'padding': (1, 0), 'stride': (1, 2)} False (2, 8, 16, 16) (2, 8, 16, 7)
{'padding': 1, 'padding_mode': 'reflect'} False (2, 8, 16, 16) (2, 8, 16, 16)The merged model is correct in all four cases, because merging writes the rotated weight into the base layer. So unmerged and merged outputs disagree.
Expected behavior
The unmerged forward pass should apply the base layer's own stride, padding, dilation and padding_mode, like the merged model and like the LoRA fix in #3758.
HRA has the same pattern (padding[0], stride[0] in hra/layer.py), but #3185 already rewrites that code path, so I'd leave HRA to that PR.
I have a small fix for BOFT ready. It calls self.base_layer._conv_forward(x, rotated_weight, bias), which is exactly what nn.Conv2d.forward does, plus a parametrized regression test. For a standard conv (square stride/padding, dilation 1, zero padding), the outputs are bit-identical before and after in fp32, fp16 and bf16. pytest tests/regression/test_state_dict.py --regression -k boft passes. I'd be happy to open the PR if this sounds good to you.
Source: huggingface/peft