Repository navigation
v3: NeuralNetworkBlock uses StatelessApplyWrapper; default chains are Enzyme-constant - #168
SebastianM-C wants to merge 12 commits into
Conversation
…Enzyme-constant NeuralNetworkBlock now stores the Lux model in a single callable parameter, `lux_apply`, like SymbolicNeuralNetwork. This removes the `lux_model` and `T` parameters (breaking, hence v3.0.0) and gives the block the device-generic ComponentArray reconstruction from #162. `get_nn_chain` now works on the block's `lux_apply` parameter instead of throwing a MethodError. The `multi_layer_feed_forward` init closure captured `initial_scaling_factor` as a Float64, which made the chain (stored in the model's parameter object) non-`guaranteed_const` for Enzyme, so Enzyme rebuilt its shadow on every `make_zero`. The closure is replaced by a `ScaledInit` callable struct marked with `EnzymeRules.inactive_type`; that is sound because it is only called by `initialparameters`, never in the differentiated forward pass. All fields of the default chains are now inactive, so the chain and its wrapper are constant for Enzyme without declaring the wrapper itself inactive (which would be unsound for user models holding data that feeds the forward pass). Initial weights are unchanged. EnzymeCore is added as a direct dependency; Lux already depends on it. The QA ExplicitImports checks ignore `EnzymeRules`/`inactive_type`, which are Enzyme's documented API but not declared public in EnzymeCore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The block now uses StatelessApplyWrapper, so check that its callable keeps CuArray/JLArray inputs and parameters on the device, like SymbolicNeuralNetwork. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lux's `outputsize` propagates nil arrays through the model, which fails for
layers that need real memory, such as `SimpleChainsLayer` from
`ToSimpleChainsAdaptor` ("conversion to pointer not defined for NilArray").
The size is only used for a clearer error message, so skip the check when it
cannot be computed instead of failing construction.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0cee56b to
9e95e0c
Compare
|
The bare Smaller things:
|
The bare catch also swallowed real model errors, so a wrong n_input (DimensionMismatch for Dense, ArgumentError for SimpleChains) only failed in solve instead of at construction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018GADJP8tjsBJuvPS8KpGin
SciMLTesting never runs QA as part of the All group, so QA needs its own group in test_groups.toml. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018GADJP8tjsBJuvPS8KpGin
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018GADJP8tjsBJuvPS8KpGin
|
addressed the comments, QA works now and added docs. The benchmark would be separate. |
…ns model Lux 1.31.5 is the first release whose compat admits SimpleChains 0.5 (LuxDL/Lux.jl#1748). SimpleChains 0.5 adds an Enzyme reverse rule for SimpleChain. With SimpleChains 0.5 as a test dependency, the downgrade job would not resolve with the old Lux 1.14 floor, which only allows SimpleChains 0.4. The new test builds a NeuralNetworkBlock from ToSimpleChainsAdaptor and checks it against the adapted Lux model. It also checks that a wrong n_input still fails at construction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PR: #168 Reviewed head: e12ba39 Risk assessment
Review reportVERDICT: CHANGES Riskhigh (H1: removes the documented Blocking findings
Non-blocking findings
What I ran
What I did not verify
AUTHOR_GRADE: {"task":"feature","score":2,"difficulty":3,"rounds":1,"author_harness":"unknown","author_model":"unknown","note":"The CPU suite and docs passed, and valid SimpleChains blocks solved. Invalid output declarations also returned Success and silently discarded a channel."} Head link: e12ba39 🤖 Posted by an AI agent — harness: codex-cli 0.170.0 · model: gpt-6.2-sol |
…utputsize fails Lux's outputsize cannot propagate nil arrays through layers that need real memory, such as SimpleChainsLayer, and the block used to skip the n_output check for them. A wrong n_output then solved silently with dropped outputs. Now any outputsize failure falls back to one evaluation of the network, so n_output is always checked at construction. n_input cannot be checked for arbitrary layers; document that as a limitation. A wrong n_input still errors when the network rejects it, but the error type depends on the matmul backend (LoopVectorization throws an ErrorException), so the test only checks that it errors. Also remove the positional NeuralNetworkBlock(n_input, n_output; kwargs...) method, since v3 is breaking anyway, and use keywords everywhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Apply `only` to the size from either path, so a network whose output has `n_output` elements but more than one dimension is rejected again, as on main. `outputsize` of a bare `Lux.Dense` does not depend on the input, so a wrong `n_input` is not caught for it. Say so in the docstring instead of naming `Dense` as an example that is caught. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…zyme-constant Add Enzyme as a test dependency (it is already installed through SciMLSensitivity) and differentiate the Lotka-Volterra loss with GaussAdjoint(autojacvec = EnzymeVJP()) explicitly. The Zygote comparison with ForwardDiff was marked broken, but only missed the default isapprox tolerance (relative error ~2e-8), so it now uses rtol = 1e-6. Also check that `Enzyme.make_zero` returns the block's wrapper itself, which holds as long as `ScaledInit` keeps the default chain constant for Enzyme. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review. Addressed in the latest commits:
posted by Claude claude-opus-5-5 on behalf of Sebastian |
Finite differences agree with ForwardDiff to a relative error of about 1.4e-4, just above the old rtol of 1e-4, so the test was marked broken (with a `broken -= true` typo). Use rtol = 1e-3 instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A DimensionMismatch from outputsize means a wrong n_input, so falling back to evaluating the network hides it whenever the evaluation does not check sizes. On Julia 1.10, LuxLib's LoopVectorization matmul (active once SimpleChains is loaded) skips that check, so a Chain of Dense layers with a wrong n_input was accepted. Other outputsize errors, such as the NilArray pointer error of SimpleChains models, still use the fallback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Breaking release (v3.0.0):
NeuralNetworkBlocknow stores the Lux model likeSymbolicNeuralNetworkdoes, in aStatelessApplyWrapperparameter, andmulti_layer_feed_forwardchains become constant for Enzyme.Breaking: the block's parameters are now
pandlux_apply, withoutputs ~ lux_apply(inputs, p). Thelux_modelandTparameters are removed. Usesol.ps[sys.nn.lux_apply](input, p)to evaluate the network, orget_nn_chain(sys.nn.lux_apply)to get the Lux model. The positionalNeuralNetworkBlock(n_input, n_output; ...)method is removed; pass both as keywords.Also:
multi_layer_feed_forward's init closure captured the scale as aFloat64, so Enzyme treated the whole chain as possibly active and rebuilt its shadow on everymake_zero. It is replaced by aScaledInitstruct markedEnzymeRules.inactive_type(sound: it is only called byinitialparameters). Every field of these chains is now inactive, so the chain and wrapper areguaranteed_constwithout declaring the wrapper itself inactive, which would be unsound for user models that hold data used in the forward pass. Initial weights are unchanged.get_nn_chainnow works on a block.outputsizefails, e.g. forToSimpleChainsAdaptormodels, the block evaluates the network once to checkn_output. A wrongn_inputcannot be detected in general; the docstring now says so.ToSimpleChainsAdaptormodel.Performance, for loss + gradient of a 68-parameter UDE (GaussAdjoint + EnzymeVJP, single thread, i9-14900K):
Tests: updated block metadata tests,
NeuralNetworkBlockdevice tests (JLArrays and CUDA), and regression tests for theoutputsizefallback, including wrongn_outputwith a real SimpleChains model, and an explicitEnzymeVJPUDE gradient check against ForwardDiff plus a check that the default chain stays constant for Enzyme (make_zero(wrapper) === wrapper). The All group passes locally on Julia 1.10, 1.12 and 1.13 at the current head; CUDA (RTX 4080 SUPER) passed before the last commits, which don't touch device code. The docs build is left to CI.Independent review: codex gpt-6.2-sol · 8 findings on the review reply and its benchmark · 2 fixed in code (bare
Densen_inputdocs, multidimensional outputs), 6 fixed in the reply wording🤖 Generated with Claude Code · model: claude-opus-5-5
Review: skimmed