Lora训练的参数和性能

复制代码
主要为了测试模型增加Lora模块后,参数量和训练速度的变化情况。
结论:正常情况下,增加Lora模块是会增加参数量的,因此前向传播和反向传播的时间也会增加。
但是,在大语言模型训练的情况下,因为基础模型本身参数量非常大,Lora模块增加的参数量相对非常小。并且,基础模型不参与梯度更新,可以做模型量化,实际上是能减少模型训练时间和显存使用量的。
以下是实验脚本和运行结果:
复制代码
#部分参考https://zhuanlan.zhihu.com/p/666000885
import time
import torch
from torch import nn
from peft import LoraConfig, get_peft_model, PeftModel
from torchsummary import summary


x_train = torch.randn((1000, 10))
y_train = torch.randn((1000, 1))

net = nn.Sequential(
    nn.Linear(10,20),
    nn.Sigmoid(),
    nn.Linear(20,30),
    nn.Sigmoid(),
    nn.Linear(30,1)
)
summary(net, (1,10))

config = LoraConfig(target_modules=["0"], r=2)
model = get_peft_model(net, config)
criterion = torch.nn.MSELoss(reduction='mean')            # 定义损失函数,采用均方误差
optimizer = torch.optim.Adam(model.parameters(), lr=0.3)  # 定义优化器,采用Adam
summary(model, (1,10))


# base 前向计算时间
start = time.time()
for i in range(100000):
    y_pre = net(x_train)            # 前向传播
print("base 前向计算时间: ", time.time() - start)

# lora 前向计算时间
start = time.time()
for i in range(100000):
    y_pre = model(x_train)            # 前向传播
print("lora 前向计算时间", time.time() - start)

# base 反向传播时间
start = time.time()
for i in range(1000):
    y_pre = net(x_train)            # 前向传播
    loss = criterion(y_pre, y_train)  # 计算损失
    optimizer.zero_grad()             # 梯度清零
    loss.backward()                   # 反向传播
    optimizer.step()                  # 使用优化器更新梯度
print("base loss after training: ", loss.item())
print("base 反向计算时间", time.time() - start)

# lora 反向传播时间
start = time.time()
for i in range(1000):
    y_pre = model(x_train)            # 前向传播
    loss = criterion(y_pre, y_train)  # 计算损失
    optimizer.zero_grad()             # 梯度清零
    loss.backward()                   # 反向传播
    optimizer.step()                  # 使用优化器更新梯度
print("lora loss after training: ", loss.item())
print("lora 反向计算时间", time.time() - start)

  运行代码输出:

复制代码
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Linear-1                [-1, 1, 20]             220
           Sigmoid-2                [-1, 1, 20]               0
            Linear-3                [-1, 1, 30]             630
           Sigmoid-4                [-1, 1, 30]               0
            Linear-5                 [-1, 1, 1]              31
================================================================
Total params: 881
Trainable params: 881
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.00
Params size (MB): 0.00
Estimated Total Size (MB): 0.00
----------------------------------------------------------------
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Linear-1                [-1, 1, 20]             220
          Identity-2                [-1, 1, 10]               0
            Linear-3                 [-1, 1, 2]              20
            Linear-4                [-1, 1, 20]              40
            Linear-5                [-1, 1, 20]             220
           Sigmoid-6                [-1, 1, 20]               0
            Linear-7                [-1, 1, 30]             630
           Sigmoid-8                [-1, 1, 30]               0
            Linear-9                 [-1, 1, 1]              31
================================================================
Total params: 1,161
Trainable params: 60
Non-trainable params: 1,101
----------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.00
Params size (MB): 0.00
Estimated Total Size (MB): 0.01
----------------------------------------------------------------
base loss after training:  1.0724023580551147
base 反向计算时间 2.9570980072021484
lora loss after training:  1.0643658638000488
lora 反向计算时间 3.053032159805298
相关推荐
AI创界者4 小时前
Python 进阶实战(七十一章):研发效能度量、自动化质量门禁与工程规范落地实战
人工智能·python·aigc
yingjie1104 小时前
快榜GEO|官网 AI 可读性自查 12 项(附 Python 检测脚本)
开发语言·人工智能·python
tryCbest5 小时前
Python 爬虫扩展篇(二):验证码的检测与人工介入——把“拦路虎”当作信号,而不是要征服的对手
开发语言·爬虫·python
哥不是小萝莉7 小时前
多 Agent 何时比单 Agent 更高效
ai
@陈小鱼7 小时前
基于InceptionTime网络的PPG无创血压估计算法研究
人工智能·python·算法·回归·开源·ppg·血压估计
谙弆悕博士7 小时前
动态自适应多智能体架构的 LangGraph 实现:从元调度器到可运行原型
人工智能·python·神经网络·机器学习·架构·多智能体·智能体
SeanQhl11 小时前
用 Python 给长文做排版体检:图够不够、段落是不是太长
python·自动化·markdown·代码规范·写作工具
SeanQhl11 小时前
中文 Windows 上跑 Python 定时任务——编码、路径、控制台三个坑
windows·python·编码·效率工具·定时任务
积流成海13 小时前
Agent演化史(十):2027年它会消失吗?(终章)
python·agent·智能体·大模型应用
真上帝的左手13 小时前
4. 关系型数据库-MySQL-原生集成 AI能力
数据库·人工智能·mysql·ai