feat: add non-mutating set_scalar_constants
This adds a non-mutating set_scalar_constants helper that returns a new tree with updated scalar constants.
Motivation (empirical): trying to run ForwardDiff.gradient through set_scalar_constants! on a Node{Float64} fails because constant node fields are concretely typed (e.g. cannot assign Dual into Float64). The new helper promotes the tree number type to accommodate eltype(constants) (so Duals work), sets the constants, and returns the new tree.
Changes:
- Add
set_scalar_constants(tree, constants)(non-mutating; promotes number type if needed). - Add
set_scalar_constants(ex::Expression, constants)wrapper. - Add a regression test demonstrating ForwardDiff gradients w.r.t. constants.
Tests:
Pkg.test()
Codecov Report
:x: Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 59.59%. Comparing base (efbc2aa) to head (b2ef7fc).
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/NodeUtils.jl | 88.88% | 1 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #165 +/- ##
==========================================
+ Coverage 59.43% 59.59% +0.16%
==========================================
Files 30 30
Lines 2682 2693 +11
==========================================
+ Hits 1594 1605 +11
Misses 1088 1088
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Benchmark Results (Julia v1)
Time benchmarks
| master | b2ef7fcc2da053... | master / b2ef7fcc2da053... | |
|---|---|---|---|
| eval/ComplexF32/evaluation | 6.92 ± 0.49 ms | 6.92 ± 0.53 ms | 1 ± 0.1 |
| eval/ComplexF64/evaluation | 10.2 ± 0.79 ms | 10.3 ± 0.86 ms | 0.991 ± 0.11 |
| eval/Float32/derivative | 11.1 ± 1.5 ms | 11.2 ± 0.9 ms | 0.996 ± 0.16 |
| eval/Float32/derivative_turbo | 11.1 ± 1.7 ms | 11.2 ± 1.3 ms | 0.99 ± 0.19 |
| eval/Float32/evaluation | 2.46 ± 0.26 ms | 2.46 ± 0.25 ms | 1 ± 0.14 |
| eval/Float32/evaluation_bumper | 0.604 ± 0.016 ms | 0.575 ± 0.017 ms | 1.05 ± 0.042 |
| eval/Float32/evaluation_turbo | 0.533 ± 0.04 ms | 0.527 ± 0.038 ms | 1.01 ± 0.11 |
| eval/Float32/evaluation_turbo_bumper | 0.602 ± 0.017 ms | 0.57 ± 0.016 ms | 1.05 ± 0.041 |
| eval/Float64/derivative | 14.6 ± 2.8 ms | 14.3 ± 3.9 ms | 1.02 ± 0.34 |
| eval/Float64/derivative_turbo | 14.4 ± 2.9 ms | 14.2 ± 3 ms | 1.01 ± 0.29 |
| eval/Float64/evaluation | 2.88 ± 0.29 ms | 2.88 ± 0.31 ms | 0.998 ± 0.15 |
| eval/Float64/evaluation_bumper | 1.27 ± 0.046 ms | 1.2 ± 0.048 ms | 1.06 ± 0.058 |
| eval/Float64/evaluation_turbo | 1.03 ± 0.069 ms | 1.02 ± 0.064 ms | 1.01 ± 0.092 |
| eval/Float64/evaluation_turbo_bumper | 1.27 ± 0.044 ms | 1.2 ± 0.049 ms | 1.06 ± 0.057 |
| utils/combine_operators/break_sharing | 0.0421 ± 0.0015 ms | 0.0409 ± 0.0011 ms | 1.03 ± 0.045 |
| utils/convert/break_sharing | 27.7 ± 3.6 μs | 27.4 ± 3.3 μs | 1.01 ± 0.18 |
| utils/convert/preserve_sharing | 0.0977 ± 0.0082 ms | 0.0988 ± 0.009 ms | 0.988 ± 0.12 |
| utils/copy/break_sharing | 28.1 ± 3.8 μs | 27.6 ± 3.4 μs | 1.02 ± 0.19 |
| utils/copy/preserve_sharing | 0.099 ± 0.0094 ms | 0.0991 ± 0.01 ms | 0.999 ± 0.14 |
| utils/count_constant_nodes/break_sharing | 12.7 ± 0.83 μs | 12.5 ± 1 μs | 1.02 ± 0.11 |
| utils/count_constant_nodes/preserve_sharing | 0.0841 ± 0.0055 ms | 0.085 ± 0.0065 ms | 0.989 ± 0.099 |
| utils/count_depth/break_sharing | 14.5 ± 1.1 μs | 13.5 ± 1 μs | 1.07 ± 0.12 |
| utils/count_nodes/break_sharing | 12.5 ± 0.73 μs | 12.2 ± 0.85 μs | 1.02 ± 0.093 |
| utils/count_nodes/preserve_sharing | 0.0855 ± 0.0061 ms | 0.0873 ± 0.0064 ms | 0.98 ± 0.1 |
| utils/get_set_constants!/break_sharing | 0.0328 ± 0.0034 ms | 0.0332 ± 0.0039 ms | 0.989 ± 0.16 |
| utils/get_set_constants!/preserve_sharing | 0.173 ± 0.011 ms | 0.172 ± 0.012 ms | 1 ± 0.093 |
| utils/get_set_constants_parametric | 0.045 ± 0.0047 ms | 0.0459 ± 0.0045 ms | 0.98 ± 0.14 |
| utils/has_constants/break_sharing | 7.58 ± 1 μs | 7.75 ± 1.4 μs | 0.979 ± 0.22 |
| utils/has_operators/break_sharing | 2.67 ± 0.26 μs | 2.54 ± 0.14 μs | 1.05 ± 0.12 |
| utils/hash/break_sharing | 24.2 ± 1.8 μs | 23.6 ± 1.5 μs | 1.03 ± 0.1 |
| utils/hash/preserve_sharing | 0.0988 ± 0.0059 ms | 0.101 ± 0.0088 ms | 0.975 ± 0.1 |
| utils/index_constant_nodes/break_sharing | 29.2 ± 2.1 μs | 28.6 ± 2.4 μs | 1.02 ± 0.11 |
| utils/index_constant_nodes/preserve_sharing | 0.0995 ± 0.0049 ms | 0.102 ± 0.0079 ms | 0.98 ± 0.09 |
| utils/is_constant/break_sharing | 8.47 ± 1.3 μs | 8.02 ± 1 μs | 1.06 ± 0.22 |
| utils/simplify_tree/break_sharing | 31.2 ± 1.3 μs | 30.7 ± 1.4 μs | 1.02 ± 0.064 |
| utils/simplify_tree/preserve_sharing | 0.112 ± 0.0064 ms | 0.112 ± 0.007 ms | 0.999 ± 0.085 |
| utils/string_tree/break_sharing | 0.443 ± 0.026 ms | 0.434 ± 0.02 ms | 1.02 ± 0.075 |
| utils/string_tree/preserve_sharing | 0.532 ± 0.02 ms | 0.535 ± 0.021 ms | 0.995 ± 0.054 |
| time_to_load | 0.168 ± 0.0021 s | 0.172 ± 0.006 s | 0.978 ± 0.036 |
Memory benchmarks
| master | b2ef7fcc2da053... | master / b2ef7fcc2da053... | |
|---|---|---|---|
| eval/ComplexF32/evaluation | 0.987 k allocs: 2.53 MB | 0.96 k allocs: 2.46 MB | 1.03 |
| eval/ComplexF64/evaluation | 0.975 k allocs: 4.97 MB | 0.987 k allocs: 5.03 MB | 0.988 |
| eval/Float32/derivative | 4.55 k allocs: 17.1 MB | 4.68 k allocs: 17.6 MB | 0.972 |
| eval/Float32/derivative_turbo | 4.67 k allocs: 17.6 MB | 4.66 k allocs: 17.5 MB | 1 |
| eval/Float32/evaluation | 0.987 k allocs: 1.29 MB | 0.987 k allocs: 1.29 MB | 1 |
| eval/Float32/evaluation_bumper | 0.303 k allocs: 0.393 MB | 0.303 k allocs: 0.393 MB | 1 |
| eval/Float32/evaluation_turbo | 0.966 k allocs: 1.26 MB | 0.969 k allocs: 1.26 MB | 0.997 |
| eval/Float32/evaluation_turbo_bumper | 0.303 k allocs: 0.393 MB | 0.303 k allocs: 0.393 MB | 1 |
| eval/Float64/derivative | 4.77 k allocs: 0.0349 GB | 4.83 k allocs: 0.0353 GB | 0.987 |
| eval/Float64/derivative_turbo | 4.77 k allocs: 0.0349 GB | 4.8 k allocs: 0.0351 GB | 0.992 |
| eval/Float64/evaluation | 1.01 k allocs: 2.58 MB | 0.999 k allocs: 2.56 MB | 1.01 |
| eval/Float64/evaluation_bumper | 0.303 k allocs: 0.771 MB | 0.303 k allocs: 0.771 MB | 1 |
| eval/Float64/evaluation_turbo | 0.99 k allocs: 2.53 MB | 0.975 k allocs: 2.5 MB | 1.02 |
| eval/Float64/evaluation_turbo_bumper | 0.303 k allocs: 0.771 MB | 0.303 k allocs: 0.771 MB | 1 |
| utils/combine_operators/break_sharing | 4 allocs: 0.953 kB | 4 allocs: 0.953 kB | 1 |
| utils/convert/break_sharing | 2 k allocs: 0.123 MB | 2 k allocs: 0.123 MB | 1 |
| utils/convert/preserve_sharing | 2.4 k allocs: 0.192 MB | 2.4 k allocs: 0.192 MB | 1 |
| utils/copy/break_sharing | 2 k allocs: 0.123 MB | 2 k allocs: 0.123 MB | 1 |
| utils/copy/preserve_sharing | 2.4 k allocs: 0.192 MB | 2.4 k allocs: 0.192 MB | 1 |
| utils/count_constant_nodes/break_sharing | 4 allocs: 0.953 kB | 4 allocs: 0.953 kB | 1 |
| utils/count_constant_nodes/preserve_sharing | 0.404 k allocs: 0.0696 MB | 0.404 k allocs: 0.0696 MB | 1 |
| utils/count_depth/break_sharing | 4 allocs: 0.953 kB | 4 allocs: 0.953 kB | 1 |
| utils/count_nodes/break_sharing | 4 allocs: 0.953 kB | 4 allocs: 0.953 kB | 1 |
| utils/count_nodes/preserve_sharing | 0.404 k allocs: 0.0696 MB | 0.404 k allocs: 0.0696 MB | 1 |
| utils/get_set_constants!/break_sharing | 0.898 k allocs: 25.2 kB | 0.898 k allocs: 25.2 kB | 1 |
| utils/get_set_constants!/preserve_sharing | 1.7 k allocs: 0.138 MB | 1.7 k allocs: 0.138 MB | 1 |
| utils/get_set_constants_parametric | 1.42 k allocs: 0.0663 MB | 1.42 k allocs: 0.0663 MB | 1 |
| utils/has_constants/break_sharing | 4 allocs: 0.203 kB | 4 allocs: 0.203 kB | 1 |
| utils/has_operators/break_sharing | 4 allocs: 0.203 kB | 4 allocs: 0.203 kB | 1 |
| utils/hash/break_sharing | 0.104 k allocs: 2.52 kB | 0.104 k allocs: 2.52 kB | 1 |
| utils/hash/preserve_sharing | 0.504 k allocs: 0.0711 MB | 0.504 k allocs: 0.0711 MB | 1 |
| utils/index_constant_nodes/break_sharing | 2.1 k allocs: 0.094 MB | 2.1 k allocs: 0.094 MB | 1 |
| utils/index_constant_nodes/preserve_sharing | 2.5 k allocs: 0.163 MB | 2.5 k allocs: 0.163 MB | 1 |
| utils/is_constant/break_sharing | 4 allocs: 0.203 kB | 4 allocs: 0.203 kB | 1 |
| utils/simplify_tree/break_sharing | 0.104 k allocs: 2.52 kB | 0.104 k allocs: 2.52 kB | 1 |
| utils/simplify_tree/preserve_sharing | 0.504 k allocs: 0.0711 MB | 0.504 k allocs: 0.0711 MB | 1 |
| utils/string_tree/break_sharing | 11.9 k allocs: 0.999 MB | 11.9 k allocs: 0.999 MB | 1 |
| utils/string_tree/preserve_sharing | 12.3 k allocs: 1.07 MB | 12.3 k allocs: 1.07 MB | 1 |
| time_to_load | 0.145 k allocs: 11 kB | 0.145 k allocs: 11 kB | 1 |