Skip to content

Commit aa2545d

Browse files
Document Val approach and add deprecation warnings
This commit makes Val{(...)} the documented and recommended way to specify accepted_kwargs for zero-allocation kwarg filtering. Changes: - Added deprecation warning in preprocess_update_func for plain tuple usage - Updated all operator docstrings to recommend Val((:kwarg,)) syntax - Updated examples to use Val approach - Plain tuples still work but show a deprecation warning (maxlog=1) The deprecation message clearly explains the migration path and only shows once per session to avoid spam. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 1dfd9bd commit aa2545d

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/func.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ uniform across `op`, `op_adjoint`, `op_inverse`, `op_adjoint_inverse`.
204204
- `u` - Prototype of the state struct passed to the operator during evaluation, i.e. `L(u, p, t)`. `u` is set to `nothing` if no value is provided.
205205
- `p` - Prototype of parameter struct passed to the operator during evaluation, i.e. `L(u, p, t)`. `p` is set to `nothing` if no value is provided.
206206
- `t` - Protype of scalar time variable passed to the operator during evaluation. `t` is set to `zero(T)` if no value is provided.
207-
- `accepted_kwargs` - `Tuple` of `Symbol`s corresponding to the keyword arguments accepted by `op*`, and `update_coefficients[!]`. For example, if `op` accepts kwarg `scale`, as in `op(u, p, t; scale)`, then `accepted_kwargs = (:scale,)`.
207+
- `accepted_kwargs` - `Val` of a `Tuple` of `Symbol`s for zero-allocation kwarg filtering. Corresponds to the keyword arguments accepted by `op*`, and `update_coefficients[!]`. For example, if `op` accepts kwarg `scale`, as in `op(u, p, t; scale)`, then `accepted_kwargs = Val((:scale,))`. Plain tuples like `(:scale,)` are deprecated but still supported.
208208
- `T` - `eltype` of the operator. If no value is provided, the constructor inferrs the value from types of `input`, and `output`
209209
- `isinplace` - `true` if the operator can be used is a mutating way with in-place allocations. This trait is inferred if no value is provided.
210210
- `outofplace` - `true` if the operator can be used is a non-mutating way with in-place allocations. This trait is inferred if no value is provided.
@@ -451,7 +451,7 @@ function Base.copy(L::FunctionOperator)
451451
isdefined(L, :p) && L.p !== nothing ? deepcopy(L.p) : nothing,
452452
L.t,
453453
L.cache === nothing ? nothing : deepcopy(L.cache),
454-
typeof(L).parameters[end-1], # iType
454+
typeof(L).parameters[end - 1], # iType
455455
typeof(L).parameters[end] # oType
456456
)
457457
end

src/matrix.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ or
1414
1515
update_func!(A::AbstractMatrix, u, p, t; <accepted kwargs>) -> [modifies A]
1616
17-
The set of keyword-arguments accepted by `update_func[!]` must be provided
18-
to `MatrixOperator` via the kwarg `accepted_kwargs` as a tuple of `Symbol`s.
17+
The set of keyword-arguments accepted by `update_func[!]` should be provided
18+
to `MatrixOperator` via the kwarg `accepted_kwargs` as a `Val` of a tuple of `Symbol`s
19+
for zero-allocation kwarg filtering. For example, `accepted_kwargs = Val((:dtgamma,))`.
20+
Plain tuples like `(:dtgamma,)` are deprecated but still supported.
1921
`kwargs` cannot be passed down to `update_func[!]` if `accepted_kwargs`
2022
are not provided.
2123
@@ -38,7 +40,7 @@ p = rand(4, 4)
3840
t = rand()
3941
4042
mat_update = (A, u, p, t; scale = 0.0) -> t * p
41-
M = MatrixOperator(0.0; update_func = mat_update, accepted_kwargs = (:scale,))
43+
M = MatrixOperator(0.0; update_func = mat_update, accepted_kwargs = Val((:scale,)))
4244
4345
L = M * M + 3I
4446
L = cache_operator(L, v)
@@ -65,7 +67,7 @@ p = rand(4) # Must be non-nothing
6567
t = rand()
6668
6769
mat_update! = (A, u, p, t; scale = 0.0) -> (A .= t * p * u' * scale)
68-
M = MatrixOperator(zeros(4, 4); update_func! = mat_update!, accepted_kwargs = (:scale,))
70+
M = MatrixOperator(zeros(4, 4); update_func! = mat_update!, accepted_kwargs = Val((:scale,)))
6971
L = M * M + 3I
7072
L = cache_operator(L, v)
7173
@@ -291,8 +293,10 @@ or
291293
292294
update_func!(diag::AbstractVecOrMat, u, p, t; <accepted kwargs>) -> [modifies diag]
293295
294-
The set of keyword-arguments accepted by `update_func[!]` must be provided
295-
to `MatrixOperator` via the kwarg `accepted_kwargs` as a tuple of `Symbol`s.
296+
The set of keyword-arguments accepted by `update_func[!]` should be provided
297+
to `DiagonalOperator` via the kwarg `accepted_kwargs` as a `Val` of a tuple of `Symbol`s
298+
for zero-allocation kwarg filtering. For example, `accepted_kwargs = Val((:dtgamma,))`.
299+
Plain tuples like `(:dtgamma,)` are deprecated but still supported.
296300
`kwargs` cannot be passed down to `update_func[!]` if `accepted_kwargs`
297301
are not provided.
298302
@@ -501,8 +505,10 @@ and `B`, `b` are expected to have an appropriate size so that
501505
`A * v + B * b` makes sense. Specifically, `size(A, 1) == size(B, 1)`, and
502506
`size(v, 2) == size(b, 2)`.
503507
504-
The set of keyword-arguments accepted by `update_func[!]` must be provided
505-
to `AffineOperator` via the kwarg `accepted_kwargs` as a tuple of `Symbol`s.
508+
The set of keyword-arguments accepted by `update_func[!]` should be provided
509+
to `AffineOperator` via the kwarg `accepted_kwargs` as a `Val` of a tuple of `Symbol`s
510+
for zero-allocation kwarg filtering. For example, `accepted_kwargs = Val((:dtgamma,))`.
511+
Plain tuples like `(:dtgamma,)` are deprecated but still supported.
506512
`kwargs` cannot be passed down to `update_func[!]` if `accepted_kwargs`
507513
are not provided.
508514

src/utils.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ function preprocess_update_func(update_func, accepted_kwargs)
1818
_accepted_kwargs = if accepted_kwargs === nothing
1919
Val(())
2020
elseif accepted_kwargs isa Tuple
21+
# Deprecation: Encourage users to use Val((...)) directly for better performance
22+
@warn """Passing accepted_kwargs as a plain Tuple is deprecated and will be removed in a future version.
23+
Please use Val((...)) instead for zero-allocation kwarg filtering.
24+
Example: accepted_kwargs = Val((:dtgamma,)) instead of accepted_kwargs = (:dtgamma,)
25+
This message will only be shown once per session.""" maxlog=1
2126
Val(accepted_kwargs)
2227
else
2328
accepted_kwargs # Already a Val or NoKwargFilter

0 commit comments

Comments
 (0)