(model, rank=16)
| 19 | |
| 20 | |
| 21 | def apply_lora(model, rank=16): |
| 22 | for name, module in model.named_modules(): |
| 23 | if isinstance(module, nn.Linear) and module.in_features == module.out_features: |
| 24 | lora = LoRA(module.in_features, module.out_features, rank=rank).to(model.device) |
| 25 | setattr(module, "lora", lora) |
| 26 | original_forward = module.forward |
| 27 | |
| 28 | # 显式绑定 |
| 29 | def forward_with_lora(x, layer1=original_forward, layer2=lora): |
| 30 | return layer1(x) + layer2(x) |
| 31 | |
| 32 | module.forward = forward_with_lora |
| 33 | |
| 34 | |
| 35 | def load_lora(model, path): |
no test coverage detected