-
-
Notifications
You must be signed in to change notification settings - Fork 238
Fix inference issue in Rosenbrock DAE test #2931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix inference issue in Rosenbrock DAE test #2931
Conversation
The alg_autodiff function was type-unstable because it used runtime
conditionals (if/elseif/else) that could return different types:
- AutoForwardDiff() for Val{true}
- AutoFiniteDiff() for Val{false}
- The original autodiff value otherwise
This caused downstream type inference failures in build_jac_config and
build_grad_config, which in turn caused the cache type and solution type
to be uninferred in the integrator construction.
The fix uses dispatch-based helper functions (_unwrap_autodiff) instead
of runtime conditionals, which allows Julia to resolve the return type
at compile time based on the input type.
This is the first step toward fixing inference for Rosenbrock DAE tests.
| if autodiff == Val(true) | ||
| return AutoForwardDiff() | ||
| elseif autodiff == Val(false) | ||
| return AutoFiniteDiff() | ||
| else | ||
| return autodiff | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternatively, this could be
if autodiff isa Val{true}
return AutoForwardDiff()
elseif autodiff isa Val{false}
return AutoFiniteDiff()
else
return autodiff
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any true advantage of using branching over multiple dispatch for distinguishing executable code by types? I know this is very widely used in the SciML ecosystem, but this if-else by type design pattern causes a lot of trouble when trying to extend codes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can do splits to multiple dispatch, it's just a code structure thing.
Use dispatch-based patterns instead of runtime conditionals to enable
better type inference. Both functions now dispatch on Val{true/false}
based on whether config computation is needed, which allows Julia's
compiler to specialize each branch separately.
This helps the cache construction in Rosenbrock methods infer concrete
types for jac_config and grad_config fields.
Refactor build_jac_config and build_grad_config to dispatch on the actual field values (f.jac, f.Wfact_t, f.tgrad) rather than using Val(boolean_condition). This allows Julia to specialize based on whether these fields have type Nothing vs a function type. Also add function barrier _make_rosenbrock_cache to help ensure cache construction sees concrete types.
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
Convert remaining runtime conditional to type-based dispatch: - _build_jac_config_alg(::Nothing, ::Nothing) -> compute config - _build_jac_config_alg(::Nothing, linsolve) -> check needs_concrete_A - _build_jac_config_alg(::Bool, linsolve) -> check bool value For default Rodas5P (concrete_jac=nothing, linsolve=nothing), this dispatches directly to the config computation without any runtime conditionals, enabling full type inference.
|
accidentally merged and reverted |
The alg_autodiff function was type-unstable because it used runtime conditionals (if/elseif/else) that could return different types:
This caused downstream type inference failures in build_jac_config and build_grad_config, which in turn caused the cache type and solution type to be uninferred in the integrator construction.
The fix uses dispatch-based helper functions (_unwrap_autodiff) instead of runtime conditionals, which allows Julia to resolve the return type at compile time based on the input type.
This is the first step toward fixing inference for Rosenbrock DAE tests.