|
| 1 | +import math |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | +import torch.nn.functional as F |
| 5 | +from torch.optim import Optimizer |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +class oCoLAud(Optimizer): |
| 9 | + def __init__(self,params,device,Constrainedlist,lr=0.1,cgamma=0,dgamma=0,weight_decay=0): |
| 10 | + self.device = device |
| 11 | + self.Constrainedlist = Constrainedlist |
| 12 | + defaults = dict(lr=lr,cgamma=cgamma,dgamma=dgamma,weight_decay=weight_decay) |
| 13 | + super(oCoLAud,self).__init__(params,defaults) |
| 14 | + |
| 15 | + def __setstate__(self,state): |
| 16 | + super(oCoLAud,self).__setstate__(state) |
| 17 | + |
| 18 | + @torch.no_grad() |
| 19 | + def stepMom(self): |
| 20 | + for group in self.param_groups: |
| 21 | + |
| 22 | + for i,p in enumerate(group['params']): |
| 23 | + |
| 24 | + if p.grad is None: |
| 25 | + continue |
| 26 | + |
| 27 | + param_state =self.state[p] |
| 28 | + shapep = p.shape |
| 29 | + if self.Constrainedlist[i] == 1: |
| 30 | + shapep0 = shapep[0] |
| 31 | + if len(shapep) > 2: |
| 32 | + shapep1 = shapep[1]*shapep[2]*shapep[3] |
| 33 | + else: |
| 34 | + shapep1 = shapep[1] |
| 35 | + |
| 36 | + d_p = p.grad |
| 37 | + buf = param_state['momentum_buffer'] = -0.01*torch.clone(d_p).detach() |
| 38 | + buffy = torch.clone(buf).detach().reshape((shapep0,shapep1)) |
| 39 | + Weighty = torch.clone(p).detach().reshape((shapep0,shapep1)) |
| 40 | + |
| 41 | + if shapep0 >= shapep1: |
| 42 | + bufproj = -0.5*torch.matmul(Weighty,(torch.matmul(torch.transpose(buffy,0,1),Weighty)+torch.matmul(torch.transpose(Weighty,0,1),buffy))).reshape(*shapep) |
| 43 | + else: |
| 44 | + bufproj = -0.5*torch.transpose(torch.matmul(torch.transpose(Weighty,0,1),(torch.matmul(Weighty,torch.transpose(buffy,0,1))+torch.matmul(buffy,torch.transpose(Weighty,0,1)))),0,1).reshape(*shapep) |
| 45 | + |
| 46 | + buf.add_(bufproj) |
| 47 | + |
| 48 | + else: |
| 49 | + d_p = p.grad |
| 50 | + buf = param_state['momentum_buffer'] = -0.01*torch.clone(d_p).detach() |
| 51 | + |
| 52 | + |
| 53 | + @torch.no_grad() |
| 54 | + def step(self): |
| 55 | + |
| 56 | + for group in self.param_groups: |
| 57 | + cgamma = group['cgamma'] |
| 58 | + dgamma = group['dgamma'] |
| 59 | + weight_decay = group['weight_decay'] |
| 60 | + |
| 61 | + for i,p in enumerate(group['params']): |
| 62 | + |
| 63 | + if p.grad is None: |
| 64 | + continue |
| 65 | + |
| 66 | + param_state = self.state[p] |
| 67 | + shapep = p.shape |
| 68 | + |
| 69 | + if self.Constrainedlist[i] == 1: |
| 70 | + |
| 71 | + shapep0 = shapep[0] |
| 72 | + if len(shapep) > 2: |
| 73 | + shapep1 = shapep[1]*shapep[2]*shapep[3] |
| 74 | + else: |
| 75 | + shapep1 = shapep[1] |
| 76 | + |
| 77 | + if 'OldWeight' not in param_state: |
| 78 | + OldWeight = param_state['OldWeight'] = torch.clone(p).detach() |
| 79 | + OldWeight = OldWeight.reshape((shapep0,shapep1)) |
| 80 | + if shapep0 >= shapep1: |
| 81 | + prodis = torch.matmul(torch.transpose(OldWeight,0,1),OldWeight) |
| 82 | + else: |
| 83 | + prodis = torch.matmul(OldWeight,torch.transpose(OldWeight,0,1)) |
| 84 | + OldWeightT = torch.transpose(OldWeight,0,1) |
| 85 | + Id = param_state['Id'] = torch.eye(*prodis.shape).to(self.device) |
| 86 | + else: |
| 87 | + OldWeight = param_state['OldWeight'] |
| 88 | + OldWeight = torch.clone(p).detach() |
| 89 | + OldWeight = OldWeight.reshape((shapep0,shapep1)) |
| 90 | + if shapep0 < shapep1: |
| 91 | + OldWeightT = torch.transpose(OldWeight,0,1) |
| 92 | + Id = param_state['Id'] |
| 93 | + |
| 94 | + buf = param_state['momentum_buffer'] |
| 95 | + |
| 96 | + # O -step |
| 97 | + if dgamma == 0: |
| 98 | + buf.mul_(cgamma) |
| 99 | + else: |
| 100 | + buf.mul_(cgamma).add_(dgamma,torch.cuda.FloatTensor(*shapep).normal_()) |
| 101 | + buffy = torch.clone(buf).detach().reshape((shapep0,shapep1)) |
| 102 | + if shapep0 >= shapep1: |
| 103 | + bufproj = -0.5*torch.matmul(OldWeight,(torch.matmul(torch.transpose(buffy,0,1),OldWeight)+torch.matmul(torch.transpose(OldWeight,0,1),buffy))).reshape(*shapep) |
| 104 | + else: |
| 105 | + bufproj = -0.5*torch.transpose(torch.matmul(OldWeightT,(torch.matmul(OldWeight,torch.transpose(buffy,0,1))+torch.matmul(buffy,torch.transpose(OldWeight,0,1)))),0,1).reshape(*shapep) |
| 106 | + |
| 107 | + buf.add_(bufproj) |
| 108 | + |
| 109 | + # B-step |
| 110 | + d_p = p.grad |
| 111 | + if weight_decay != 0: |
| 112 | + d_p = d_p.add(p, alpha=weight_decay) |
| 113 | + |
| 114 | + buf.add_(-d_p) |
| 115 | + buffy = torch.clone(buf).detach().reshape((shapep0,shapep1)) |
| 116 | + if shapep0 >= shapep1: |
| 117 | + bufproj = -0.5*torch.matmul(OldWeight,(torch.matmul(torch.transpose(buffy,0,1),OldWeight)+torch.matmul(torch.transpose(OldWeight,0,1),buffy))).reshape(*shapep) |
| 118 | + else: |
| 119 | + bufproj = -0.5*torch.transpose(torch.matmul(OldWeightT,(torch.matmul(OldWeight,torch.transpose(buffy,0,1))+torch.matmul(buffy,torch.transpose(OldWeight,0,1)))),0,1).reshape(*shapep) |
| 120 | + |
| 121 | + |
| 122 | + buf.add_(bufproj) |
| 123 | + d_p = buf |
| 124 | + |
| 125 | + # A-step |
| 126 | + p.data.add_(d_p,alpha=group['lr']) |
| 127 | + p.data = p.reshape((shapep0,shapep1)) |
| 128 | + FirstStep = torch.clone(p).detach() |
| 129 | + |
| 130 | + if shapep0 >= shapep1: |
| 131 | + for ks in range(10): |
| 132 | + Lambda = torch.matmul(torch.transpose(p,0,1),p)-Id |
| 133 | + products = -0.5*torch.matmul(OldWeight,Lambda) |
| 134 | + p.add_(products) |
| 135 | + |
| 136 | + bufproj1 = ((p.data-FirstStep)/group['lr']).reshape(*shapep) |
| 137 | + buf.add_(bufproj1) |
| 138 | + else: |
| 139 | + for ks in range(10): |
| 140 | + Lambda = torch.matmul(p,torch.transpose(p,0,1))-Id |
| 141 | + products = -0.5*torch.transpose(torch.matmul(OldWeightT,Lambda),0,1) |
| 142 | + p.add_(products) |
| 143 | + |
| 144 | + bufproj1 = ((p.data-FirstStep)/group['lr']).reshape(*shapep) |
| 145 | + buf.add_(bufproj1) |
| 146 | + |
| 147 | + p.data = p.reshape(*shapep) |
| 148 | + |
| 149 | + OldWeight = torch.clone(p).detach() |
| 150 | + OldWeight = OldWeight.reshape((shapep0,shapep1)) |
| 151 | + |
| 152 | + buffy = torch.clone(buf).detach().reshape((shapep0,shapep1)) |
| 153 | + if shapep0 >= shapep1: |
| 154 | + bufproj = -0.5*torch.matmul(OldWeight,(torch.matmul(torch.transpose(buffy,0,1),OldWeight)+torch.matmul(torch.transpose(OldWeight,0,1),buffy))).reshape(*shapep) |
| 155 | + else: |
| 156 | + bufproj = -0.5*torch.transpose(torch.matmul(OldWeightT,(torch.matmul(OldWeight,torch.transpose(buffy,0,1))+torch.matmul(buffy,torch.transpose(OldWeight,0,1)))),0,1).reshape(*shapep) |
| 157 | + |
| 158 | + buf.add_(bufproj) |
| 159 | + else: |
| 160 | + buf = param_state['momentum_buffer'] |
| 161 | + |
| 162 | + if dgamma == 0: |
| 163 | + buf.mul_(cgamma) |
| 164 | + else: |
| 165 | + buf.mul_(cgamma).add_(dgamma,torch.cuda.FloatTensor(*shapep).normal_()) |
| 166 | + |
| 167 | + d_p = p.grad |
| 168 | + if weight_decay != 0: |
| 169 | + d_p = d_p.add(p, alpha=weight_decay) |
| 170 | + |
| 171 | + buf.add_(-d_p,alpha=1) |
| 172 | + d_p = buf |
| 173 | + p.data.add_(d_p,alpha=group['lr']) |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + |
0 commit comments