File size: 10,063 Bytes
34a4bcb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
#!/usr/bin/env python3
"""
LoRA (Low-Rank Adaptation) 模块用于SAM3微调
参考: https://arxiv.org/abs/2106.09685
"""
import math
from typing import Dict, List, Optional, Tuple, Union
import torch
import torch.nn as nn
import torch.nn.functional as F
class LoRALinear(nn.Module):
"""
LoRA线性层
将原始线性层分解为: W = W_0 + BA
其中 B ∈ R^{d×r}, A ∈ R^{r×k}, r << min(d, k)
"""
def __init__(
self,
original_layer: nn.Linear,
rank: int = 4,
alpha: float = 1.0,
dropout: float = 0.0,
):
super().__init__()
self.original_layer = original_layer
self.rank = rank
self.alpha = alpha
self.scaling = alpha / rank
in_features = original_layer.in_features
out_features = original_layer.out_features
# LoRA参数
self.lora_A = nn.Parameter(torch.zeros(rank, in_features))
self.lora_B = nn.Parameter(torch.zeros(out_features, rank))
# 初始化
nn.init.kaiming_uniform_(self.lora_A, a=math.sqrt(5))
nn.init.zeros_(self.lora_B)
# Dropout
self.dropout = nn.Dropout(dropout) if dropout > 0 else nn.Identity()
# 冻结原始层
for param in self.original_layer.parameters():
param.requires_grad = False
def forward(self, x: torch.Tensor) -> torch.Tensor:
# 原始前向传播
result = self.original_layer(x)
# 确保LoRA参数在正确的设备上
if self.lora_A.device != x.device:
self.lora_A = nn.Parameter(self.lora_A.to(x.device))
self.lora_B = nn.Parameter(self.lora_B.to(x.device))
# LoRA分支
x_dropout = self.dropout(x)
with torch.amp.autocast('cuda', enabled=False):
x_fp32 = x_dropout.float()
lora_A = self.lora_A.float()
lora_B = self.lora_B.float()
lora_out = x_fp32 @ lora_A.T @ lora_B.T
lora_out = lora_out.to(result.dtype)
return result + lora_out * self.scaling
def merge_weights(self):
"""将LoRA权重合并到原始层"""
if self.rank > 0:
self.original_layer.weight.data += (
self.lora_B @ self.lora_A * self.scaling
)
def unmerge_weights(self):
"""从原始层移除LoRA权重"""
if self.rank > 0:
self.original_layer.weight.data -= (
self.lora_B @ self.lora_A * self.scaling
)
class LoRAConv2d(nn.Module):
"""
LoRA卷积层
使用与原始卷积相同的stride和padding
"""
def __init__(
self,
original_layer: nn.Conv2d,
rank: int = 4,
alpha: float = 1.0,
dropout: float = 0.0,
):
super().__init__()
self.original_layer = original_layer
self.rank = rank
self.alpha = alpha
self.scaling = alpha / rank
in_channels = original_layer.in_channels
out_channels = original_layer.out_channels
kernel_size = original_layer.kernel_size
stride = original_layer.stride
padding = original_layer.padding
# LoRA参数
# lora_A: 降维 (in_ch -> rank),使用1x1卷积
# lora_B: 升维 (rank -> out_ch),使用原始kernel_size保持空间维度一致
self.lora_A = nn.Conv2d(
in_channels, rank, kernel_size=1, stride=1, padding=0, bias=False
)
self.lora_B = nn.Conv2d(
rank, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=False
)
# 初始化
nn.init.kaiming_uniform_(self.lora_A.weight, a=math.sqrt(5))
nn.init.zeros_(self.lora_B.weight)
self.dropout = nn.Dropout(dropout) if dropout > 0 else nn.Identity()
# 冻结原始层
for param in self.original_layer.parameters():
param.requires_grad = False
def forward(self, x: torch.Tensor) -> torch.Tensor:
result = self.original_layer(x)
# 确保LoRA层在正确的设备上
if self.lora_A.weight.device != x.device:
self.lora_A = self.lora_A.to(x.device)
self.lora_B = self.lora_B.to(x.device)
x_dropout = self.dropout(x)
# 使用与输入相同的dtype
with torch.amp.autocast('cuda', enabled=False):
x_fp32 = x_dropout.float()
h = self.lora_A(x_fp32)
lora_out = self.lora_B(h)
lora_out = lora_out.to(result.dtype)
return result + lora_out * self.scaling
def apply_lora_to_model(
model: nn.Module,
rank: int = 4,
alpha: float = 1.0,
dropout: float = 0.0,
target_modules: Optional[List[str]] = None,
exclude_modules: Optional[List[str]] = None,
) -> nn.Module:
"""
将LoRA应用到模型的指定层
Args:
model: 原始模型
rank: LoRA秩
alpha: LoRA缩放因子
dropout: LoRA dropout
target_modules: 目标模块名称列表 (如 ['q_proj', 'v_proj'])
exclude_modules: 排除的模块名称列表
Returns:
应用了LoRA的模型
"""
if target_modules is None:
# 默认目标: attention层的QKV投影
target_modules = [
'q_proj', 'k_proj', 'v_proj',
'qkv', 'to_q', 'to_k', 'to_v',
'query', 'key', 'value',
]
if exclude_modules is None:
exclude_modules = []
lora_count = 0
for name, module in model.named_modules():
# 检查是否应该排除
should_exclude = any(ex in name for ex in exclude_modules)
if should_exclude:
continue
# 检查是否是目标模块
is_target = any(target in name for target in target_modules)
if is_target:
if isinstance(module, nn.Linear):
# 获取父模块和属性名
parent_name = '.'.join(name.split('.')[:-1])
attr_name = name.split('.')[-1]
parent = model
if parent_name:
for part in parent_name.split('.'):
parent = getattr(parent, part)
# 替换为LoRA层
lora_layer = LoRALinear(module, rank=rank, alpha=alpha, dropout=dropout)
setattr(parent, attr_name, lora_layer)
lora_count += 1
elif isinstance(module, nn.Conv2d):
parent_name = '.'.join(name.split('.')[:-1])
attr_name = name.split('.')[-1]
parent = model
if parent_name:
for part in parent_name.split('.'):
parent = getattr(parent, part)
lora_layer = LoRAConv2d(module, rank=rank, alpha=alpha, dropout=dropout)
setattr(parent, attr_name, lora_layer)
lora_count += 1
print(f"Applied LoRA to {lora_count} layers")
return model
def get_lora_params(model: nn.Module) -> List[nn.Parameter]:
"""获取所有LoRA参数"""
lora_params = []
for name, param in model.named_parameters():
if 'lora_' in name:
lora_params.append(param)
return lora_params
def get_trainable_params(model: nn.Module) -> Tuple[List[nn.Parameter], List[str]]:
"""获取所有可训练参数"""
trainable_params = []
trainable_names = []
for name, param in model.named_parameters():
if param.requires_grad:
trainable_params.append(param)
trainable_names.append(name)
return trainable_params, trainable_names
def freeze_model_except_lora(model: nn.Module):
"""冻结除LoRA外的所有参数"""
for name, param in model.named_parameters():
if 'lora_' not in name:
param.requires_grad = False
else:
param.requires_grad = True
def count_parameters(model: nn.Module) -> Dict[str, int]:
"""统计参数数量"""
total = sum(p.numel() for p in model.parameters())
trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
frozen = total - trainable
return {
'total': total,
'trainable': trainable,
'frozen': frozen,
'trainable_ratio': trainable / total if total > 0 else 0,
}
def save_lora_weights(model: nn.Module, path: str):
"""只保存LoRA权重"""
lora_state_dict = {}
for name, param in model.named_parameters():
if 'lora_' in name:
lora_state_dict[name] = param.data
torch.save(lora_state_dict, path)
print(f"Saved LoRA weights to {path}")
def load_lora_weights(model: nn.Module, path: str):
"""加载LoRA权重"""
lora_state_dict = torch.load(path, map_location='cpu')
model_state_dict = model.state_dict()
for name, param in lora_state_dict.items():
if name in model_state_dict:
model_state_dict[name] = param
model.load_state_dict(model_state_dict, strict=False)
print(f"Loaded LoRA weights from {path}")
if __name__ == "__main__":
# 测试
print("Testing LoRA module...")
# 测试线性层
linear = nn.Linear(768, 768)
lora_linear = LoRALinear(linear, rank=8, alpha=16)
x = torch.randn(2, 768)
y = lora_linear(x)
print(f"LoRA Linear output shape: {y.shape}")
# 测试卷积层
conv = nn.Conv2d(256, 256, 3, padding=1)
lora_conv = LoRAConv2d(conv, rank=8, alpha=16)
x = torch.randn(2, 256, 32, 32)
y = lora_conv(x)
print(f"LoRA Conv output shape: {y.shape}")
# 测试参数统计
model = nn.Sequential(
lora_linear,
nn.ReLU(),
nn.Linear(768, 10)
)
stats = count_parameters(model)
print(f"Parameters: {stats}")
|