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 77 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 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
8495d80
test: remove Enzyme as required part of test suite
MilesCranmer May 30, 2025
0af1700
feat: fix children set in ParametricNode
MilesCranmer May 30, 2025
7d2a542
test: incorporate turbo and bumper in supposition test
MilesCranmer May 30, 2025
358d21f
test: refactor supposition test
MilesCranmer May 31, 2025
7a69a7b
fix: fix JET identified missing method
MilesCranmer May 31, 2025
7689999
chore: bump major version with n-ary nodes
MilesCranmer May 31, 2025
0514c90
refactor: more generic `set_node!` implementation
MilesCranmer May 31, 2025
f16af83
refactor: use generic getters and setters
MilesCranmer May 31, 2025
981e3b0
docs: describe generic getters and setters
MilesCranmer Jun 1, 2025
84f8121
docs: describe changelog
MilesCranmer Jun 1, 2025
caca487
docs: fix signatures
MilesCranmer Jun 1, 2025
acbef0f
docs: fix example with base operations
MilesCranmer Jun 1, 2025
fe1c9d7
merge branch 'master' into n-arity-v4
MilesCranmer Jun 1, 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
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# CHANGELOG

## 2.0.0

- Nodes can now have arbitrary numbers of children, not just binary trees. This was designed in such a way to have identical performance to the previous version. Essentially, the `Node{T,D}` type is a wrapper around a tuple of `D` children.
- Note that `Node{T}` automatically converts to `Node{T,2}` in many contexts, for backwards compatibility.
- Node type signature changed from `Node{T}` to `Node{T,D}`. Usually `D` is 2.
- Direct property access `.l` and `.r` is automatically forwarded to `get_child(tree, 1)` and `get_child(tree, 2)`,
but it is recommended to use the generic accessors instead.
- Similarly, `.l = child` should be replaced with `set_child!(tree, child, 1)` and similar for `.r`.
- All internal code migrated to use generic accessors.

### Backwards Compatibility

- Existing `.l` and `.r` access continues to work without warnings

### Breaking Changes

The only breaking change is if:

1. You have any types that are subtyped to `<:AbstractExpressionNode{T}` or `<:AbstractNode{T}`. These should now be subtyped to `<:AbstractExpressionNode{T,2}` or `<:AbstractNode{T,2}`. You may also allow a `D` parameter in case you want to support higher-arity trees.
2. You assume a tree has type, e.g., `=== Node{T}`, rather than `<: Node{T}`. So any methods dispatched to `::Type{Node{T}}` will also break. (To be safe you should always use a form `<: Node{T}` in case of future type changes - in any library.)
3. You assume `tree.degree <= 2` in conditional logic, and your code interacts with a tree that is _not_ a binary tree. For example, the following pattern was common before this change:

```julia
if tree.degree == 0
#= operations on leaf node =#
elseif tree.degree == 1
#= operations on unary node =#
else
# BAD: ASSUMED TO BE BINARY
#= operations on binary node, using `.l` and `.r` only =#
end
```

This will obviously break if you pass a tree that is not binary, such as `tree::Node{T,3}`.
- To fix this, you can use the `get_children` function to get the children of the tree as a tuple of `D` children, and then index up to `tree.degree`.
- Inside DynamicExpressions, we commonly use `Base.Cartesian.@nif` to generate code for different degrees, to avoid any unstable types.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DynamicExpressions"
uuid = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
authors = ["MilesCranmer <[email protected]>"]
version = "1.10.2"
version = "2.0.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
40 changes: 38 additions & 2 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ When using these node constructors, types will automatically be promoted.
You can convert the type of a node using `convert`:

```@docs
convert(::Type{AbstractExpressionNode{T1}}, tree::AbstractExpressionNode{T2}) where {T1, T2}
convert(::Type{N1}, tree::N2) where {T1,T2,D1,D2,N1<:AbstractExpressionNode{T1,D1},N2<:AbstractExpressionNode{T2,D2}}
```

You can set a `tree` (in-place) with `set_node!`:
Expand All @@ -75,6 +75,42 @@ You can create a copy of a node with `copy_node`:
copy_node
```

## Generic Node Accessors

For working with nodes of arbitrary arity:

```@docs
get_child
set_child!
get_children
set_children!
```

Examples:

```julia
# Define operators including ternary
my_ternary(x, y, z) = x + y * z
operators = OperatorEnum(((sin,), (+, *), (my_ternary,))) # (unary, binary, ternary)

tree = Node{Float64,3}(; op=1, children=(Node{Float64,3}(; val=1.0), Node{Float64,3}(; val=2.0)))
new_child = Node{Float64,3}(; val=3.0)

left_child = get_child(tree, 1)
right_child = get_child(tree, 2)

set_child!(tree, new_child, 1)

children = get_children(tree)
left, right = get_children(tree, Val(2)) # type stable

# Transform to ternary operation
child1, child2, child3 = Node{Float64,3}(; val=4.0), Node{Float64,3}(; val=5.0), Node{Float64,3}(; val=6.0)
set_children!(tree, (child1, child2, child3))
tree.op = 1 # my_ternary
tree.degree = 3
```

## Graph Nodes

You can describe an equation as a *graph* rather than a tree
Expand Down Expand Up @@ -109,7 +145,7 @@ This means that we only need to change it once
to have changes propagate across the expression:

```julia
julia> y.r.val *= 0.9
julia> get_child(y, 2).val *= 0.9
1.35

julia> z
Expand Down
1 change: 0 additions & 1 deletion docs/src/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ mapreduce(f::F, op::G, tree::AbstractNode; return_type, f_on_shared, break_shari
any(f::F, tree::AbstractNode) where {F<:Function}
all(f::F, tree::AbstractNode) where {F<:Function}
map(f::F, tree::AbstractNode, result_type::Type{RT}=Nothing; break_sharing::Val=Val(false)) where {F<:Function,RT}
convert(::Type{<:AbstractExpressionNode{T1}}, n::AbstractExpressionNode{T2}) where {T1,T2}
hash(tree::AbstractExpressionNode{T}, h::UInt; break_sharing::Val=Val(false)) where {T}
```

Expand Down
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
Loading
Loading