Diagnose partial initial model states and document Axon.build - #660
Open
seanmor5 wants to merge 2 commits into
Open
Diagnose partial initial model states and document Axon.build#660seanmor5 wants to merge 2 commits into
seanmor5 wants to merge 2 commits into
Conversation
Log the parameters that init_fn initialized from scratch because they were absent from a non-empty initial state (debug level), aggregate the unexpected initial-state keys into a single warning, and name every parameter by its full dotted path so blocks and composite parameters are unambiguous. Rewrite the Axon.build docs to explain what init_fn and predict_fn do, and update the guides to pass Axon.ModelState.empty() instead of the deprecated bare map. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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 #475.
Most of the concrete asks in that thread have already landed: the
Axon.CompileErrormessage now points atdebug: truefor the "layer was defined at" section,Axon.blockandAxon.namespace/2,3give parameter maps grouping/nesting, andAxon.elem/3/Axon.fetch/3destructure containers. The Livebook table truncation and the unpickler location were answered in the thread as not being Axon changes. Two asks were still open, and this PR resolves them.Initial-state diagnostics in
init_fnThe reporter asked for stricter modes that flag parameters that were missing from the supplied parameter map (and therefore silently initialized) and extra unused data in the map. The maintainer position in the thread was not to raise, because partially initializing a model is a normal training / transfer-learning workflow, but to do what Bumblebee's loader does and log the missing parameters at debug level, and to log unused parameters as well.
init_fnnow does both, and always names parameters by their full dotted path:Logger.debugline. WithAxon.ModelState.empty()nothing is logged, since every parameter is "missing" and the line would be noise.Logger.warningline with their full paths. Previously this was one warning per key with only the leaf name, so a naming mismatch in a large model produced hundreds offound unexpected key ...: "kernel"lines that did not say which layer was affected. An entirely unknown subtree is reported by its root ("dense_9") rather than by every leaf under it.Paths follow the structure of
Axon.ModelState.data, soAxon.blocksub-states appear as"block_0.dense_0.kernel"and composite parameters as"lstm.input_kernel.wii". Quantized tensors and shared parameters are structs and are treated as leaves.For example, loading a state that misspells one key and covers only the first layer:
logs
Tradeoffs: the missing-parameter report is at
:debugrather than:warningor an exception, following the maintainer's comment; users validating a ported checkpoint can raise their log level. The check lives ininit_fn(the natural point where the supplied state meets the model's declared parameters) rather than inpredict_fn, and theAxon.builddocs now recommend runninginit_fnwith loaded parameters as the way to validate them. No newAxon.buildoption was added. Becauseinit_fnis JIT-compiled and cached by argument signature, a repeated call with an identical signature may not re-trace and therefore may not re-log; this was already true of the existing warnings. Merge semantics are unchanged: this only changes what is logged.Axon.build/2docs and guidesThe reporter also asked whether
predict_fnmutates backend state or whetherinit_fncopies parameters to the backend, and proposed wording for the docs. Theinit_fn/predict_fnsections ofAxon.build/2now explain that both are pure functions of their arguments, that the model never carries parameters and nothing is cached on the backend, why a template is needed, whatinit_fndoes with an initial state (use, cast, initialize, ignore-with-warning), and whatpredict_fnreturns in:inferenceversus:trainmode. The old example passed a bare map toinit_fn, which now triggers a deprecation warning; it usesAxon.ModelState.new/1andAxon.ModelState.empty/0instead. The 22init_fn.(x, %{})call sites in the guides, the three in theAxonmoduledoc and the one in the README were updated the same way, and the signature description in the "Your first Axon model" guide now usesAxon.ModelState.t(). Livebook output blocks were left untouched.Out of scope
A scale-factor form of
Axon.resizefor inputs withnilspatial dimensions was raised late in the thread and never answered by the maintainers; it is a separate layer feature and not addressed here. A predict-time unused-key check is also not added, sinceinit_fnis where the supplied state is reconciled with the model.Tests
test/axon/compiler_test.exsgains an "initial state diagnostics" describe block covering: the debug line for a partial state (and that present parameters are not listed), no debug line for an empty state, a single aggregated warning with full paths for unexpected keys (including an unknown subtree reported by its root), nested paths throughAxon.block, nested paths through a composite LSTM parameter, and the full path in the type-cast warning. Five of the six fail onmain.mix test: 883 passed (200 doctests, 683 tests), 47 excluded.🤖 Generated with Claude Code