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
{{ message }}
Repository navigation
nnx.Optimizer.update should return the updates #5182
At https://github.com/google/flax/blob/main/flax/nnx/training/optimizer.py#L221 it would be nice if nnx.Optimizer.update could return the underlying updates PyTree from optax. All existing callers are free to ignore it, but this would allow logging and diagnostics on the weight update in a straightforward way without the caller needing to drop down to pure optax.
@am001122 what exactly you would like to return from update?
I think user can easily inspect opt_state and model. If you need to return new_params or new_opt_state or some other intermediate object, I think the most simple is to make a derived class from nnx.Optmizer and override update method and return desired objects. What do you think?
Hi @vfdev-5 , sorry, I should have been more precise: I meant to literally return the variable updates containing the updates to the weights. As you say, its easy to access nnx.Optimizer.opt_state, or the state of the optimized nnx.Module after the update. But to get the change in weights requires some external bookkeeping to save the previous weights, take the new ones, and do an extra subtraction operation, all of which could be skipped if updates was returned.
Definitely an option---I didn't take it because it involves almost completely re-implementing nnx.Optimizer in my code, and I didn't want the risk of drifting out of sync with upstream. My sense was that since nnx.Optimizer.update currently doesn't return anything this would be a non-breaking change, and a change that nicely mirrors the underlying optax API that nnx.Optimizer thinly wraps to handle state updates. But certainly there are at least these two acceptable workarounds, so it's not really an issue.
Activity
vfdev-5 commentedon Jan 12, 2026
@am001122 what exactly you would like to return from
update?I think user can easily inspect
opt_stateand model. If you need to returnnew_paramsornew_opt_stateor some other intermediate object, I think the most simple is to make a derived class fromnnx.Optmizerand overrideupdatemethod and return desired objects. What do you think?am001122 commentedon Jan 13, 2026
Hi @vfdev-5 , sorry, I should have been more precise: I meant to literally return the variable
updatescontaining the updates to the weights. As you say, its easy to accessnnx.Optimizer.opt_state, or the state of the optimizednnx.Moduleafter the update. But to get the change in weights requires some external bookkeeping to save the previous weights, take the new ones, and do an extra subtraction operation, all of which could be skipped ifupdateswas returned.vfdev-5 commentedon Jan 14, 2026
@am001122 thanks for the context. I was thinking that users can make a derived class of
nnx.Optimizerwith customizedupdatemethod:I agree that ideally this could be done in few copied lines.
am001122 commentedon Jan 14, 2026
Definitely an option---I didn't take it because it involves almost completely re-implementing
nnx.Optimizerin my code, and I didn't want the risk of drifting out of sync with upstream. My sense was that sincennx.Optimizer.updatecurrently doesn't return anything this would be a non-breaking change, and a change that nicely mirrors the underlyingoptaxAPI thatnnx.Optimizerthinly wraps to handle state updates. But certainly there are at least these two acceptable workarounds, so it's not really an issue.qGentry commentedon Jan 22, 2026
+1, would love to see the nnx.Optimizer returning updates without having to copy-paste source code from flax to implement it