import torch
#这是输入数据,维度为100,长度为120
tensor = torch.rand(120, 100)
#这是一个线性层
matrix0 = torch.rand(100, 100)
# 这是一个lora
matrix1 = torch.rand(100, 10)
matrix2 = torch.rand(10, 100)
#1、120*100的矩阵与100*10相乘,输出再和10*100的矩阵相乘
ouput1= torch.mm(torch.mm(tensor, matrix1),matrix2)+torch.mm(tensor,matrix0)
#2、120*100的矩阵与100*10和10*100的矩阵先转换为100*100的低秩方阵相乘
ouput2= torch.mm(tensor, torch.mm(matrix1,matrix2))+torch.mm(tensor,matrix0)
#1和2结果相同
print(torch.allclose(ouput1, ouput2)) # True
#我们把低秩方阵提前与线性层相加
ouput3= torch.mm(tensor, torch.mm(matrix1,matrix2)+matrix0)
#发现提前相加的结果与分开运算结果相同,这就是为什么lora可以合并进原始模型
print(torch.allclose(ouput1, ouput3)) # True
lora为什么可以合并进原始模型?
最新推荐文章于 2025-03-26 00:24:27 发布