You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For the life of me I cannot get velo working using pytorch or jax. I am curious what happens if we whiten the gradient before giving it to VeLo. I was wondering if you might be interested to compare.
defzeropower_via_newtonschulz5(G, steps=10, eps=1e-7):
""" Newton-Schulz iteration to compute the zeroth power / orthogonalization of G. We opt to use a quintic iteration whose coefficients are selected to maximize the slope at zero. For the purpose of minimizing steps, it turns out to be empirically effective to keep increasing the slope at zero even beyond the point where the iteration no longer converges all the way to one everywhere on the interval. This iteration therefore does not produce UV^T but rather something like US'V^T where S' is diagonal with S_{ii}' ~ Uniform(0.5, 1.5), which turns out not to hurt model performance at all relative to UV^T, where USV^T = G is the SVD. """assertlen(G.shape) ==2a, b, c= (3.4445, -4.7750, 2.0315)
X=G.bfloat16()
X/= (X.norm() +eps) # ensure top singular value <= 1ifG.size(0) >G.size(1):
X=X.Tfor_inrange(steps):
A=X @ X.TB=b*A+c*A @ AX=a*X+B @ XifG.size(0) >G.size(1):
X=X.TreturnX@th.no_grad()defstep(self, closure: LossClosure) ->Union[th.Tensor, float, None]:
withth.enable_grad():
closure_result=closure()
ifisinstance(closure_result, tuple):
assertlen(closure_result) ==2loss, model_state=closure_resultelifisinstance(closure_result, th.Tensor):
loss=closure_resultassertloss.numel() ==1model_state=Noneelse:
raiseTypeError('closure returned unexpected type: '+str(type(closure_result)))
# Collect all gradients into a single matrixall_grads= []
forgroupinself.param_groups:
forpingroup['params']:
ifp.gradisnotNone:
all_grads.append(p.grad.view(-1))
# Stack all gradients into a matrixcombined_grads=th.cat(all_grads)
n=combined_grads.shape[0]
grad_matrix=combined_grads.view(n, 1) # Make it 2D for zeropower# Apply zeropowerprocessed_grads=zeropower_via_newtonschulz5(grad_matrix)
# Reshape and redistribute the processed gradientsprocessed_grads=processed_grads.view(-1)
start_idx=0forgroupinself.param_groups:
forpingroup['params']:
ifp.gradisnotNone:
numel=p.grad.numel()
p.grad.copy_(processed_grads[start_idx:start_idx+numel].view_as(p.grad))
start_idx+=numel# Continue with normal VeLO updatejax_grad= {
str(i): [_th_to_jax(p.grad.ravel()) forpingroup['params']]
for (i, group) inenumerate(self.param_groups)
}
jax_model_state= (
_th_to_jax(model_state.ravel())
ifmodel_stateisnotNoneelsemodel_state
)
self.state['rng_key'], opt_key=jax.random.split(self.state['rng_key'])
self.state['opt_state'] =self.opt.update(
self.state['opt_state'],
jax_grad,
model_state=jax_model_state,
loss=_th_to_jax(loss),
key=opt_key,
)
for (i, group) inenumerate(self.param_groups):
for (param, jax_param) inzip(
group['params'],
self.opt.get_params(self.state['opt_state'])[str(i)],
):
param.data[:] =_jax_to_th(jax_param).reshape(param.shape)
returnloss```
The text was updated successfully, but these errors were encountered:
Hey! Love that the Newton-Schulz iterations are getting more attention! Sadly, this code base has probably become vastly out-of-date with developments on the JAX side of VeLO. From my side, the code is just way too slow, which is why I ended up not using it in practice. I believe the only way to properly speed it up would be to convert the JAX model to PyTorch.
I'll try to remember to run some tests with the recent PyTorch version next week. Feel free to ping again if I don't reply again on Wednesday. :)
For the life of me I cannot get velo working using pytorch or jax. I am curious what happens if we whiten the gradient before giving it to VeLo. I was wondering if you might be interested to compare.
The text was updated successfully, but these errors were encountered: