Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
125 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
5772472
docs: clean up
MilesCranmer Jun 7, 2025
a25db43
wip: new operator enum construction syntax
MilesCranmer Jun 7, 2025
6fbc5ea
feat: new operator enum construction and `@extend_operators`
MilesCranmer Jun 8, 2025
4cead25
refactor: remove duplicate code
MilesCranmer Jun 8, 2025
01ef42a
feat: allow vector of functions to OperatorEnum
MilesCranmer Jun 8, 2025
f313563
docs: update OperatorEnum syntax
MilesCranmer Jun 8, 2025
6c9d345
fix: operator conversion
MilesCranmer Jun 8, 2025
e0af4ce
style: remove old comment
MilesCranmer Jun 8, 2025
eef67f0
fix: edge case with chainable operators
MilesCranmer Jun 8, 2025
af76a5c
test: expand coverage
MilesCranmer Jun 8, 2025
585bbc0
style: remove old compat branches
MilesCranmer Jun 8, 2025
471b1fa
test: expand coverage
MilesCranmer Jun 8, 2025
5d39efc
refactor: remove ReadOnlyNode and simplify interface
MilesCranmer Jun 8, 2025
35e253c
test: expand coverage
MilesCranmer Jun 8, 2025
26ce63b
test: fix type instability on 1.10 tests
MilesCranmer Jun 8, 2025
02c5f10
refactor: avoid creating separate function for standard overloads
MilesCranmer Jun 8, 2025
edc1981
style: more obvious error in grad eval
MilesCranmer Jun 8, 2025
4d945ac
docs: improve description of `get_children`
MilesCranmer Jun 8, 2025
6a00ffa
test: non-Val get_children
MilesCranmer Jun 8, 2025
67248f7
fix: bad edit on get_child
MilesCranmer Jun 8, 2025
0798f57
test: graph compat with 3-arity nodes
MilesCranmer Jun 8, 2025
3da2fae
fix: correctly mark unstable
MilesCranmer Jun 8, 2025
938b335
chore: remove old code
MilesCranmer Jun 8, 2025
cdc980d
feat: guard undefined children behind Nullable
MilesCranmer Jun 9, 2025
59c41fd
fix: `ntuple` from `UInt8`
MilesCranmer Jun 9, 2025
d11ec78
test: fix ParametricExpression test
MilesCranmer Jun 9, 2025
757fb0a
style: prevent symbol conflicts in macro
MilesCranmer Jun 9, 2025
11f4465
test: fix access error
MilesCranmer Jun 9, 2025
f1d3a83
style: clean up macro generation in operator construction
MilesCranmer Jun 9, 2025
f1e92b9
docs: get building
MilesCranmer Jun 9, 2025
37d30bc
docs: work on upgrade guide
MilesCranmer Jun 9, 2025
4afa1a1
docs: describe OperatorEnum changes
MilesCranmer Jun 9, 2025
2b7d655
docs: work on upgrade guide
MilesCranmer Jun 9, 2025
dfdec54
docs: update upgrade guide
MilesCranmer Jun 9, 2025
67471f9
refactor: add an inline
MilesCranmer Jun 14, 2025
d0917c3
refactor: reduce compilation
MilesCranmer Jun 14, 2025
f948ddb
fix: mutation error for Zygote
MilesCranmer Jun 14, 2025
eeafb34
style: cleaner test code
MilesCranmer Jun 14, 2025
b4d71b0
style: consistent usage of max_degree
MilesCranmer Jun 14, 2025
83f0558
Merge branch 'master' into n-arity-v4
MilesCranmer Jun 14, 2025
4889408
Merge branch 'master' into n-arity-v4
MilesCranmer Jun 14, 2025
1c0d019
Merge branch 'master' into n-arity-v4
MilesCranmer Jun 14, 2025
3034262
ci: hide misses from coverage
MilesCranmer Jun 15, 2025
9c7e658
ci: prevent codecov from failing ci
MilesCranmer Jun 15, 2025
564d3b3
ci: more masking for codecov
MilesCranmer Jun 15, 2025
a89d22f
ci: more test coverage cleanup
MilesCranmer Jun 15, 2025
921c591
test: improve coverage
MilesCranmer Jun 15, 2025
73ad287
docs: fix docstring for `unsafe_get_children`
MilesCranmer Jun 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
5 changes: 5 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage:
status:
patch:
default:
informational: true
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "DynamicExpressions"
uuid = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
authors = ["MilesCranmer <[email protected]>"]
version = "1.10.3"
version = "2.0.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
DispatchDoctor = "8d63f2c5-f18a-4cf2-ba9d-b3f60fc568c8"
Interfaces = "85a1e053-f937-4924-92a5-1367d23b7b87"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Expand All @@ -30,6 +31,7 @@ DynamicExpressionsZygoteExt = "Zygote"
[compat]
Bumper = "0.6"
ChainRulesCore = "1"
Compat = "4.16"
DispatchDoctor = "0.4"
Interfaces = "0.3"
LoopVectorization = "0.12"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ A dynamic expression is a snippet of code that can change throughout runtime - c
```julia
using DynamicExpressions

operators = OperatorEnum(; binary_operators=[+, -, *], unary_operators=[cos])
operators = OperatorEnum(1 => (cos,), 2 => (+, -, *))
variable_names = ["x1", "x2"]

x1 = Expression(Node{Float64}(feature=1); operators, variable_names)
Expand Down Expand Up @@ -98,7 +98,7 @@ We can also compute gradients with the same speed:
```julia
using Zygote # trigger extension

operators = OperatorEnum(; binary_operators=[+, -, *], unary_operators=[cos])
operators = OperatorEnum(1 => (cos,), 2 => (+, -, *))
variable_names = ["x1", "x2"]
x1, x2 = (Expression(Node{Float64}(feature=i); operators, variable_names) for i in 1:2)

Expand Down Expand Up @@ -149,7 +149,7 @@ using DynamicExpressions: @declare_expression_operator
my_string_func(x::String) = "ello $x"
@declare_expression_operator(my_string_func, 1)

operators = GenericOperatorEnum(; binary_operators=[*], unary_operators=[my_string_func])
operators = GenericOperatorEnum(1 => (my_string_func,), 2 => (*,))

x1 = Expression(_x1; operators, variable_names)
```
Expand Down Expand Up @@ -192,7 +192,7 @@ vec_square(x) = x .* x
@declare_expression_operator(vec_square, 1)

# Set up an operator enum:
operators = GenericOperatorEnum(;binary_operators=[vec_add], unary_operators=[vec_square])
operators = GenericOperatorEnum(1 => (vec_square,), 2 => (vec_add,))

# Construct the expression:
variable_names = ["x1"]
Expand Down
3 changes: 3 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ DynamicExpressions = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
Interfaces = "85a1e053-f937-4924-92a5-1367d23b7b87"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[sources]
DynamicExpressions = { path = "../" }
51 changes: 39 additions & 12 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@ This `enum` is defined as follows:
OperatorEnum
```

Construct this operator specification as follows:

```@docs
OperatorEnum(; binary_operators=[], unary_operators=[], define_helper_functions::Bool=true)
```

This is just for scalar operators. However, you can use
the following for more general operators:

```@docs
GenericOperatorEnum(; binary_operators=[], unary_operators=[], define_helper_functions::Bool=true)
GenericOperatorEnum
```

By default, these operators will define helper functions for constructing trees,
Expand Down Expand Up @@ -60,7 +54,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 +69,41 @@ 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)

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 All @@ -88,9 +117,7 @@ This makes it so you can have multiple parents for a given node,
and share parts of an expression. For example:

```julia
julia> operators = OperatorEnum(;
binary_operators=[+, -, *], unary_operators=[cos, sin, exp]
);
julia> operators = OperatorEnum(1 => (cos, sin, exp), 2 => (+, -, *));

julia> x1, x2 = GraphNode(feature=1), GraphNode(feature=2)
(x1, x2)
Expand All @@ -109,7 +136,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
4 changes: 2 additions & 2 deletions docs/src/eval.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ For example,
```@example
using DynamicExpressions

operators = OperatorEnum(; binary_operators=[+, -, *], unary_operators=[cos])
operators = OperatorEnum(1 => (cos, sin), 2 => (+, -, *, /))
tree = Node(; feature=1) * cos(Node(; feature=2) - 3.2)

tree([1 2 3; 4 5 6.], operators)
Expand Down Expand Up @@ -155,7 +155,7 @@ Let's look at an example. First, let's create a tree:
```julia
using DynamicExpressions

operators = OperatorEnum(binary_operators=(+, -, *, /), unary_operators=(cos, sin))
operators = OperatorEnum(1 => (cos, sin), 2 => (+, -, *, /))

x1 = Node{Float64}(feature=1)
x2 = Node{Float64}(feature=2)
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 @@
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 @@
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 @@
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)

Check warning on line 77 in ext/DynamicExpressionsBumperExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DynamicExpressionsBumperExt.jl#L77

Added line #L77 was not covered by tests
)
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