Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
polvalente
approved these changes
Aug 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #576.
Axon.Loop.train_step/4updated the model state in two different ways. The objective function that runs underNx.Defn.value_and_gradtook the differentiated variable (the trainable parameters) and spliced it back intomodel_state.databy hand, using a privatetree_merge/3that was a copy of the one inAxon.ModelState. The step function then applied the optimizer output with the real API,Axon.ModelState.update/3. The hand-rolled merge existed becauseAxon.ModelState.update/3used to be a plaindefand could not be called from the jitted step. Since #656 turned theAxon.ModelStateAPI into transforms, that restriction is gone.This PR makes the objective call
Axon.ModelState.update(model_state, trainable_parameters)directly and deletes the duplicatedtree_merge/3fromAxon.Loop, so both parameter merges intrain_step/4go through the same function:The behaviour is the same.
update/2replaces the leaves ofdatathat exist intrainable_parametersand keeps everything else; frozen parameters are never part of the gradient variable (trainable_parameters/1diffs againstfrozen_parameters), so they stay untouched, andAxon.ModelState.SharedParameterleaves are skipped, so tied weights stay tied.updated_statedefaults to%{}, which makes the state merge a no-op, and theparameters,stateandfrozen_parametersmetadata arekeep:fields of the container, so they survive the jitted step. Because the merge is a plain replacement, the sameNx.Defn.Exprtensors end up indataand gradients flow exactly as before. Layer state (batch-norm running statistics, etc.) is still produced by the forward pass and merged in the step function with the three-arityupdate/3call, which is unchanged.The only difference from the old private copy is that
Axon.ModelState.tree_mergetreatsAxon.Quantization.QTensoras a leaf instead of recursing into the struct. This cannot change results becauseAxon.Quantizationfreezes quantized kernels, so they never appear in the trainable parameters.Two tests were added to
test/axon/loop_test.exs:train_step/3 updates only trainable parametersfreezes one dense layer, runs a step, and checks that the frozen kernel and bias come back unchanged while the trainable layer's parameters move, and thatparametersandfrozen_parametersare preserved.train_step/3 updates nested parametersuses a reusedAxon.blockwith a batch norm and checks that the nested block parameters and the layer state are both updated through the single path, withparametersandstatepreserved.🤖 Generated with Claude Code