feat(Utilities): add safe_call for type-inferred conditional fallbacks#746
Closed
haakon-e wants to merge 1 commit into
Closed
feat(Utilities): add safe_call for type-inferred conditional fallbacks#746haakon-e wants to merge 1 commit into
haakon-e wants to merge 1 commit into
Conversation
Member
Author
|
This change is part of the following stack: Change managed by git-spice. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## he/ad-compat-2m-p3 #746 +/- ##
===================================================
Coverage 92.96% 92.97%
===================================================
Files 56 56
Lines 2859 2862 +3
===================================================
+ Hits 2658 2661 +3
Misses 201 201
🚀 New features to boost your workflow:
|
haakon-e
force-pushed
the
he/ad-compat-2m-p3
branch
from
June 25, 2026 18:01
6436c12 to
710d1ce
Compare
haakon-e
force-pushed
the
he/ad-compat-2m-p3
branch
from
June 26, 2026 00:38
710d1ce to
ee98c2d
Compare
haakon-e
force-pushed
the
he/ad-compat-2m-p3
branch
from
June 26, 2026 01:22
ee98c2d to
98e82b8
Compare
haakon-e
force-pushed
the
he/ad-compat-2m-p3
branch
from
June 26, 2026 01:31
98e82b8 to
6878dd5
Compare
haakon-e
force-pushed
the
he/safe-when
branch
2 times, most recently
from
June 26, 2026 01:38
8e79cdd to
435f982
Compare
haakon-e
force-pushed
the
he/ad-compat-2m-p3
branch
3 times, most recently
from
July 1, 2026 06:24
11eb605 to
ddd64f0
Compare
`safe_call(f, cond; otherwise = zero)` returns `f()` when `cond` holds and
`otherwise(FT)` when it does not, with `FT = Core.Compiler.return_type(f, Tuple{})`
— the element type the computation itself produces. This supersedes the
`zero(promote_typeof(args...))` plus `ifelse(cond, value, zero)` pattern: the
fallback type comes from the function's range rather than an enumeration of its
domain, so it cannot miss an argument, and the closure is evaluated only when
`cond` holds, which avoids running it on inputs outside its domain.
Condition on values (`x > ϵ`) rather than on `iszero` of a differentiated
argument: a value threshold fires at the degenerate point and gives a clean
zero-derivative fallback, whereas `iszero(Dual(0, ∂))` is false and would
evaluate the closure.
Member
Author
|
Closing in favor of branch-free patterns: rate functions are now evaluated unconditionally, with ifelse selecting the result, rather than short-circuiting on a condition (see e.g. #757). This removes the use case for safe_call. |
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.
Summary
Add
Utilities.safe_call, a higher-order helper for conditional values whose fallback type is inferred from the computation rather than from its inputs:It returns the
doblock whencondistrueandotherwise(FT)when it isfalse, whereFT = Core.Compiler.return_type(f, Tuple{}).Motivation
The recurring
zero(promote_typeof(args...))idiom has a weakness thatsafe_callremoves:promote_typeoftypes the fallback from the inputs, and every derivative-carrying argument must be listed by hand; omitting one returns aUnion, which is non-concrete when usingForwardDiffon the GPU.safe_calltakes the type from the function's range, so it cannot omit an argument.Notes for review
Core.Compiler.return_typeis internal and inference-directed: the fallback type is correct only where the computation is type-stable; this is required for the code to run on GPUs anyways. Where inference widens (Union type), this will fail._regularised_ratioin the same module. With that, it is type-stable and allocation-free throughForwardDiffon CUDA, for the default and overriddenotherwise.