Skip to content

Permit n-argument operators #127

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

Open
wants to merge 64 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
1b542bc
testing out n-arity implementation with SizedVector
MilesCranmer Jun 27, 2024
6b90350
Merge branch 'master' into n-arity
MilesCranmer Jul 8, 2024
ab7c65c
wip
MilesCranmer Jul 8, 2024
3ed6b41
fix: various aspects of degree interface
MilesCranmer Jul 8, 2024
b5285f7
fix: segfault in NodeIndex
MilesCranmer Jul 8, 2024
8707d24
refactor: no more need for `memoize_on`
MilesCranmer Jul 8, 2024
2a0bd05
fix: various aspects of degree interface
MilesCranmer Jul 8, 2024
1e672bc
fix: some issues with D-degree ParametricNode
MilesCranmer Jul 9, 2024
df12857
Merge branch 'master' into n-arity-v4
MilesCranmer May 3, 2025
5505506
fix: a few merge issues
MilesCranmer May 3, 2025
0514598
fix: `setproperty!` for tuple children
MilesCranmer May 3, 2025
0079acf
fix: constructor from explicit eltype
MilesCranmer May 3, 2025
97c8868
fix: permit vector children to constructor
MilesCranmer May 3, 2025
33e5966
fix: other fixes for d-degree nodes
MilesCranmer May 4, 2025
24aec43
fix: type assertion issue in constructor
MilesCranmer May 4, 2025
5f647ff
test: fix interfaces
MilesCranmer May 6, 2025
867dab6
test: fix `children` call in ReadOnlyNode
MilesCranmer May 6, 2025
b6d187b
fix: read only nodes when given ref
MilesCranmer May 6, 2025
87102d8
feat: create n-arity operator enum
MilesCranmer May 6, 2025
cda0896
feat: add generic degree pathway in evaluation
MilesCranmer May 7, 2025
673146c
feat: n-arity strings
MilesCranmer May 7, 2025
9ccb46a
feat: get expression operators working with 3-arg input
MilesCranmer May 9, 2025
633cc94
feat: get expression algebra working
MilesCranmer May 10, 2025
38101d1
fix: node conversion changing degree
MilesCranmer May 10, 2025
22154d0
fix: type instabilities
MilesCranmer May 10, 2025
092b945
feat: n-arity LoopVectorization compat
MilesCranmer May 10, 2025
f7b49a5
refactor: cleaner `call_mapreducer` for n-arity
MilesCranmer May 10, 2025
560af07
refactor: simplify constant eval code for n-arity
MilesCranmer May 10, 2025
a51f1fb
refactor: avoid need for refs by setting tuple to self
MilesCranmer May 10, 2025
1d3d834
feat: `any` and `==` working with n-arity nodes
MilesCranmer May 11, 2025
d5a69b7
fix: various issues with n-arity parametric node
MilesCranmer May 11, 2025
90fe177
fix: fix degree in `NodeIndex`
MilesCranmer May 11, 2025
b4a5ba8
fix: `set_node!` should set to `children`
MilesCranmer May 11, 2025
3905fc8
fix: node preallocation for n-arity nodes
MilesCranmer May 11, 2025
59c0878
feat: complete node interface for n-arity
MilesCranmer May 11, 2025
b78097a
feat: add `children` to required interface
MilesCranmer May 11, 2025
008dfbc
feat: better interface for children
MilesCranmer May 11, 2025
97abbd0
feat: make differentiable eval work for n-arity
MilesCranmer May 11, 2025
067734a
feat: make generic eval allow n-arity nodes
MilesCranmer May 11, 2025
7b04a15
refactor: remove reference stuff from read only nodes
MilesCranmer May 11, 2025
83902e3
feat: make diff compatibility with n-arity
MilesCranmer May 11, 2025
3845539
refactor: simplify eval code
MilesCranmer May 11, 2025
5f977ab
feat: make grad compatible with n-arity
MilesCranmer May 11, 2025
7b51c06
feat: n-arity compat with simplification
MilesCranmer May 11, 2025
4f31a85
feat: n-arity compat with bumper
MilesCranmer May 12, 2025
f9d21c6
refactor: avoid `NTuple` typing
MilesCranmer May 12, 2025
69cd372
docs: tweak docstring
MilesCranmer May 12, 2025
422b5c5
feat: parsing for D-degree nodes
MilesCranmer May 12, 2025
fb248de
test: integrate Supposition testing
MilesCranmer May 12, 2025
e3da69b
refactor: better name for poison node
MilesCranmer May 12, 2025
4f2c117
refactor: fix type instability in parametric expression converter
MilesCranmer May 12, 2025
b309f52
refactor: reduce new type instabilities in generic eval
MilesCranmer May 13, 2025
b66b178
refactor: eliminate other type instabilities
MilesCranmer May 13, 2025
6e78b27
fix: attempt type stability fix for union of `Type{Union{}}` with oth…
MilesCranmer May 13, 2025
bae7063
test: move test to main file
MilesCranmer May 13, 2025
1d61192
fix: `convert` should check degree
MilesCranmer May 13, 2025
24b4ed0
test: make `FrozenNode` also overload `with_max_degree`
MilesCranmer May 13, 2025
befee81
feat: add `with_max_degree` to required interface
MilesCranmer May 13, 2025
0668a58
fix: missing import
MilesCranmer May 13, 2025
a960c27
test: move preferences to test/Project.toml
MilesCranmer May 13, 2025
7978292
refactor: additional tricks to try to improve type stability
MilesCranmer May 14, 2025
a623e8f
test: fix imaginary type instability from Interfaces.jl
MilesCranmer May 14, 2025
5472e8a
test: fix dd seting
MilesCranmer May 14, 2025
2bf20fa
feat: `set_children!` to work with vector
MilesCranmer May 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 30 additions & 51 deletions ext/DynamicExpressionsBumperExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ using DynamicExpressions:
OperatorEnum, AbstractExpressionNode, tree_mapreduce, is_valid_array, EvalOptions
using DynamicExpressions.UtilsModule: ResultOk, counttuple

import DynamicExpressions.ExtensionInterfaceModule:
bumper_eval_tree_array, bumper_kern1!, bumper_kern2!
import DynamicExpressions.ExtensionInterfaceModule: bumper_eval_tree_array, bumper_kern!

function bumper_eval_tree_array(
tree::AbstractExpressionNode{T},
Expand Down Expand Up @@ -37,8 +36,7 @@ function bumper_eval_tree_array(
branch_node -> branch_node,
# In the evaluation kernel, we combine the branch nodes
# with the arrays created by the leaf nodes:
((args::Vararg{Any,M}) where {M}) ->
dispatch_kerns!(operators, args..., eval_options),
KernelDispatcher(operators, eval_options),
tree;
break_sharing=Val(true),
)
Expand All @@ -49,63 +47,44 @@ function bumper_eval_tree_array(
return (result, all_ok[])
end

function dispatch_kerns!(
operators, branch_node, cumulator, eval_options::EvalOptions{<:Any,true,early_exit}
) where {early_exit}
cumulator.ok || return cumulator

out = dispatch_kern1!(operators.unaops, branch_node.op, cumulator.x, eval_options)
return ResultOk(out, early_exit ? is_valid_array(out) : true)
end
function dispatch_kerns!(
operators,
branch_node,
cumulator1,
cumulator2,
eval_options::EvalOptions{<:Any,true,early_exit},
) where {early_exit}
cumulator1.ok || return cumulator1
cumulator2.ok || return cumulator2

out = dispatch_kern2!(
operators.binops, branch_node.op, cumulator1.x, cumulator2.x, eval_options
)
return ResultOk(out, early_exit ? is_valid_array(out) : true)
struct KernelDispatcher{O<:OperatorEnum,E<:EvalOptions{<:Any,true,<:Any}} <: Function
operators::O
eval_options::E
end

@generated function dispatch_kern1!(unaops, op_idx, cumulator, eval_options::EvalOptions)
nuna = counttuple(unaops)
@generated function (kd::KernelDispatcher{<:Any,<:EvalOptions{<:Any,true,early_exit}})(
branch_node, inputs::Vararg{Any,degree}
) where {degree,early_exit}
quote
Base.@nif(
$nuna,
i -> i == op_idx,
i -> let op = unaops[i]
return bumper_kern1!(op, cumulator, eval_options)
end,
)
Base.Cartesian.@nexprs($degree, i -> inputs[i].ok || return inputs[i])
cumulators = Base.Cartesian.@ntuple($degree, i -> inputs[i].x)
out = dispatch_kerns!(kd.operators, branch_node, cumulators, kd.eval_options)
return ResultOk(out, early_exit ? is_valid_array(out) : true)
end
end
@generated function dispatch_kern2!(
binops, op_idx, cumulator1, cumulator2, eval_options::EvalOptions
)
nbin = counttuple(binops)
@generated function dispatch_kerns!(
operators::OperatorEnum{OPS},
branch_node,
cumulators::Tuple{Vararg{Any,degree}},
eval_options::EvalOptions,
) where {OPS,degree}
nops = length(OPS.types[degree].types)
quote
Base.@nif(
$nbin,
op_idx = branch_node.op
Base.Cartesian.@nif(
$nops,
i -> i == op_idx,
i -> let op = binops[i]
return bumper_kern2!(op, cumulator1, cumulator2, eval_options)
end,
i -> bumper_kern!(operators[$degree][i], cumulators, eval_options)
)
end
end
function bumper_kern1!(op::F, cumulator, ::EvalOptions{false,true}) where {F}
@. cumulator = op(cumulator)
return cumulator
end
function bumper_kern2!(op::F, cumulator1, cumulator2, ::EvalOptions{false,true}) where {F}
@. cumulator1 = op(cumulator1, cumulator2)
return cumulator1

function bumper_kern!(
op::F, cumulators::Tuple{Vararg{Any,degree}}, ::EvalOptions{false,true,early_exit}
) where {F,degree,early_exit}
cumulator_1 = first(cumulators)
@. cumulator_1 = op(cumulators...)
return cumulator_1
end

end
52 changes: 19 additions & 33 deletions ext/DynamicExpressionsLoopVectorizationExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,28 @@ using DynamicExpressions.EvaluateModule:
import DynamicExpressions.EvaluateModule:
deg1_eval,
deg2_eval,
degn_eval,
deg1_l2_ll0_lr0_eval,
deg1_l1_ll0_eval,
deg2_l0_r0_eval,
deg2_l0_eval,
deg2_r0_eval
import DynamicExpressions.ExtensionInterfaceModule:
_is_loopvectorization_loaded, bumper_kern1!, bumper_kern2!
_is_loopvectorization_loaded, bumper_kern!

_is_loopvectorization_loaded(::Int) = true

function deg2_eval(
cumulator_l::AbstractVector{T},
cumulator_r::AbstractVector{T},
op::F,
::EvalOptions{true},
)::ResultOk where {T<:Number,F}
@turbo for j in eachindex(cumulator_l)
x = op(cumulator_l[j], cumulator_r[j])
cumulator_l[j] = x
end
return ResultOk(cumulator_l, true)
end

function deg1_eval(
cumulator::AbstractVector{T}, op::F, ::EvalOptions{true}
)::ResultOk where {T<:Number,F}
@turbo for j in eachindex(cumulator)
x = op(cumulator[j])
cumulator[j] = x
@generated function degn_eval(
cumulators::NTuple{N,<:AbstractVector{T}}, op::F, ::EvalOptions{true}
)::ResultOk where {N,T,F}
# Fast general implementation of `cumulators[1] .= op.(cumulators[1], cumulators[2], ...)`
quote
Base.Cartesian.@nexprs($N, i -> cumulator_i = cumulators[i])
@turbo for j in eachindex(cumulator_1)
cumulator_1[j] = Base.Cartesian.@ncall($N, op, i -> cumulator_i[j])
end
return ResultOk(cumulator_1, true)
end
return ResultOk(cumulator, true)
end

function deg1_l2_ll0_lr0_eval(
Expand Down Expand Up @@ -217,18 +208,13 @@ function deg2_r0_eval(
end
end

## Interface with Bumper.jl
function bumper_kern1!(
op::F, cumulator, ::EvalOptions{true,true,early_exit}
) where {F,early_exit}
@turbo @. cumulator = op(cumulator)
return cumulator
end
function bumper_kern2!(
op::F, cumulator1, cumulator2, ::EvalOptions{true,true,early_exit}
) where {F,early_exit}
@turbo @. cumulator1 = op(cumulator1, cumulator2)
return cumulator1
# Interface with Bumper.jl
function bumper_kern!(
op::F, cumulators::Tuple{Vararg{Any,degree}}, ::EvalOptions{true,true,early_exit}
) where {F,degree,early_exit}
cumulator_1 = first(cumulators)
@turbo @. cumulator_1 = op(cumulators...)
return cumulator_1
end

end
10 changes: 7 additions & 3 deletions ext/DynamicExpressionsSymbolicUtilsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ function Base.convert(
findoperation(op, operators.unaops)
end

return constructorof(N)(;
op=ind, children=map(x -> convert(N, x, operators; variable_names), args)
)
if length(args) == 2
children = map(x -> convert(N, x, operators; variable_names), (args[1], args[2]))
return constructorof(N)(; op=ind, children)
else
children = map(x -> convert(N, x, operators; variable_names), (only(args),))
return constructorof(N)(; op=ind, children)
end
end

_node_type(::Type{<:AbstractExpression{T,N}}) where {T,N<:AbstractExpressionNode} = N
Expand Down
28 changes: 6 additions & 22 deletions ext/DynamicExpressionsZygoteExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,14 @@ module DynamicExpressionsZygoteExt
using Zygote: gradient
import DynamicExpressions.ExtensionInterfaceModule: _zygote_gradient, ZygoteGradient

function _zygote_gradient(op::F, ::Val{1}) where {F}
return ZygoteGradient{F,1,1}(op)
end
function _zygote_gradient(op::F, ::Val{2}, ::Val{side}=Val(nothing)) where {F,side}
# side should be either nothing (for both), 1, or 2
@assert side === nothing || side in (1, 2)
return ZygoteGradient{F,2,side}(op)
function _zygote_gradient(op::F, ::Val{degree}) where {F,degree}
return ZygoteGradient{F,degree}(op)
end

function (g::ZygoteGradient{F,1,1})(x) where {F}
out = only(gradient(g.op, x))
return out === nothing ? zero(x) : out
end
function (g::ZygoteGradient{F,2,nothing})(x, y) where {F}
(∂x, ∂y) = gradient(g.op, x, y)
return (∂x === nothing ? zero(x) : ∂x, ∂y === nothing ? zero(y) : ∂y)
end
function (g::ZygoteGradient{F,2,1})(x, y) where {F}
∂x = only(gradient(Base.Fix2(g.op, y), x))
return ∂x === nothing ? zero(x) : ∂x
end
function (g::ZygoteGradient{F,2,2})(x, y) where {F}
∂y = only(gradient(Base.Fix1(g.op, x), y))
return ∂y === nothing ? zero(y) : ∂y
# All this does is remove `nothing`, so that we get type stability
function (g::ZygoteGradient{F,degree})(args::Vararg{Any,degree}) where {F,degree}
partials = gradient(g.op, args...)
return ntuple(i -> @something(partials[i], zero(args[i])), Val(degree))
end

end
2 changes: 2 additions & 0 deletions src/DynamicExpressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import .NodeModule:
constructorof,
with_type_parameters,
preserve_sharing,
max_degree,
with_max_degree,
leaf_copy,
branch_copy,
leaf_hash,
Expand Down
Loading
Loading