| 14 | |
| 15 | |
| 16 | def gdupdateWexact(K, V, Ktarget1, Vtarget1, W, device='cuda'): |
| 17 | input_ = K |
| 18 | output = V |
| 19 | C = input_.T@input_ |
| 20 | d = [] |
| 21 | lu, piv = lu_factor(C.cpu().numpy()) |
| 22 | for i in range(Ktarget1.size(0)): |
| 23 | sol = lu_solve((lu, piv), Ktarget1[i].reshape(-1, 1).cpu().numpy()) |
| 24 | d.append(torch.from_numpy(sol).to(K.device)) |
| 25 | |
| 26 | d = torch.cat(d, 1).T |
| 27 | |
| 28 | e2 = d@Ktarget1.T |
| 29 | e1 = (Vtarget1.T - W@Ktarget1.T) |
| 30 | delta = e1@torch.linalg.inv(e2) |
| 31 | |
| 32 | Wnew = W + delta@d |
| 33 | lambda_split1 = Vtarget1.size(0) |
| 34 | |
| 35 | input_ = torch.cat([Ktarget1.T, K.T], dim=1) |
| 36 | output = torch.cat([Vtarget1, V], dim=0) |
| 37 | |
| 38 | loss = torch.norm((Wnew@input_).T - output, 2, dim=1) |
| 39 | print(loss[:lambda_split1].mean().item(), loss[lambda_split1:].mean().item()) |
| 40 | |
| 41 | return Wnew |
| 42 | |
| 43 | |
| 44 | def compose(paths, category, outpath, pretrained_model_path, regularization_prompt, prompts, save_path, device='cuda'): |