Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 0 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ on:
- '.github/workflows/**'
- 'Project.toml'
pull_request:
branches:
- master
paths:
- 'test/**'
- 'src/**'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/check-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
push:
branches:
- 'master'
- 'release-'
- 'release-*'
tags: '*'
pull_request:

Expand Down
2 changes: 1 addition & 1 deletion benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function benchmark_utilities()
[get_set_constants!(ex) for ex in exs],
seconds = 10.0,
setup = (
operators = $operators;
operators = ($operators);
ntrees = 100;
n = 20;
n_features = 5;
Expand Down
4 changes: 2 additions & 2 deletions ext/DynamicExpressionsSymbolicUtilsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function split_eq(
op,
args,
operators::AbstractOperatorEnum,
::Type{N}=Node;
(::Type{N})=Node;
variable_names::Union{AbstractVector{<:AbstractString},Nothing}=nothing,
# Deprecated:
varMap=nothing,
Expand Down Expand Up @@ -253,7 +253,7 @@ end
function symbolic_to_node(
eqn::BasicSymbolic,
operators::AbstractOperatorEnum,
::Type{N}=Node;
(::Type{N})=Node;
variable_names::Union{AbstractVector{<:AbstractString},Nothing}=nothing,
# Deprecated:
varMap=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/DynamicExpressionsZygoteExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DynamicExpressions.ExtensionInterfaceModule: _zygote_gradient, ZygoteGrad
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}
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)
Expand Down
5 changes: 3 additions & 2 deletions src/EvaluationHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ to every constant in the expression.
- `(evaluation, gradient, complete)::Tuple{AbstractVector{T}, AbstractMatrix{T}, Bool}`: the normal evaluation,
the gradient, and whether the evaluation completed as normal (or encountered a nan or inf).
"""
Base.adjoint(tree::AbstractExpressionNode) =
((args...; kws...) -> _grad_evaluator(tree, args...; kws...))
function Base.adjoint(tree::AbstractExpressionNode)
return ((args...; kws...) -> _grad_evaluator(tree, args...; kws...))
end

end
2 changes: 1 addition & 1 deletion src/Expression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ end
function copy_into!(::Nothing, src::AbstractExpression)
return copy(src)
end
function allocate_container(::AbstractExpression, ::Union{Nothing,Integer}=nothing)
function allocate_container(::AbstractExpression, (::Union{Nothing,Integer})=nothing)
return nothing
end
# COV_EXCL_STOP
Expand Down
4 changes: 2 additions & 2 deletions src/NodeUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ given the output of this function.
Also return metadata that can will be used in the `set_scalar_constants!` function.
"""
function get_scalar_constants(
tree::AbstractExpressionNode{T}, ::Type{BT}=get_number_type(T)
tree::AbstractExpressionNode{T}, (::Type{BT})=get_number_type(T)
) where {T,BT}
refs = filter_map(
is_node_constant, node -> Ref(node), tree, Base.RefValue{typeof(tree)}
Expand Down Expand Up @@ -160,7 +160,7 @@ end
# as we trace over the node we are indexing on.
preserve_sharing(::Union{Type{<:NodeIndex},NodeIndex}) = false

function index_constant_nodes(tree::AbstractExpressionNode, ::Type{T}=UInt16) where {T}
function index_constant_nodes(tree::AbstractExpressionNode, (::Type{T})=UInt16) where {T}
# Essentially we copy the tree, replacing the values
# with indices
constant_index = Ref(T(0))
Expand Down
129 changes: 69 additions & 60 deletions src/OperatorEnumConstruction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,74 +293,83 @@ function _extend_operators(operators, skip_user_operators, kws, __module__::Modu
unary_ex = _extend_unary_operator(f_inside, f_outside, type_requirements, internal)
#! format: off
return quote
local $type_requirements, $build_converters, $binary_exists, $unary_exists
# Initialize locals so static analyzers (JET) don't treat them as undefined
# when control-flow goes through closures/locks.
local $type_requirements = Any
local $build_converters = false
local $binary_exists = Dict{Function,Bool}()
local $unary_exists = Dict{Function,Bool}()

$(_validate_no_ambiguous_broadcasts)($operators)
lock($LATEST_LOCK) do
if isa($operators, $OperatorEnum)
$type_requirements = $(on_type == nothing ? Number : on_type)
$build_converters = $(on_type == nothing)
if !haskey($(ALREADY_DEFINED_BINARY_OPERATORS).operator_enum, $type_requirements)
$(ALREADY_DEFINED_BINARY_OPERATORS).operator_enum[$type_requirements] = Dict{Function,Bool}()
end
if !haskey($(ALREADY_DEFINED_UNARY_OPERATORS).operator_enum, $type_requirements)
$(ALREADY_DEFINED_UNARY_OPERATORS).operator_enum[$type_requirements] = Dict{Function,Bool}()
end
$binary_exists = $(ALREADY_DEFINED_BINARY_OPERATORS).operator_enum[$type_requirements]
$unary_exists = $(ALREADY_DEFINED_UNARY_OPERATORS).operator_enum[$type_requirements]
else
$type_requirements = $(on_type == nothing ? Any : on_type)
$build_converters = false
if !haskey($(ALREADY_DEFINED_BINARY_OPERATORS).generic_operator_enum, $type_requirements)
$(ALREADY_DEFINED_BINARY_OPERATORS).generic_operator_enum[$type_requirements] = Dict{Function,Bool}()
end
if !haskey($(ALREADY_DEFINED_UNARY_OPERATORS).generic_operator_enum, $type_requirements)
$(ALREADY_DEFINED_UNARY_OPERATORS).generic_operator_enum[$type_requirements] = Dict{Function,Bool}()
end
$binary_exists = $(ALREADY_DEFINED_BINARY_OPERATORS).generic_operator_enum[$type_requirements]
$unary_exists = $(ALREADY_DEFINED_UNARY_OPERATORS).generic_operator_enum[$type_requirements]
end
if $(empty_old_operators)
# Trigger errors if operators are not yet defined:
empty!($(LATEST_BINARY_OPERATOR_MAPPING))
empty!($(LATEST_UNARY_OPERATOR_MAPPING))
end
for (op, func) in enumerate($(operators).binops)
local ($f_outside, $f_inside) = $(_unpack_broadcast_function)(func)
local $skip = false
if isdefined(Base, $f_outside)
$f_outside = :(Base.$($f_outside))
elseif $(skip_user_operators)
$skip = true
if isa($operators, $OperatorEnum)
$type_requirements = $(on_type == nothing ? Number : on_type)
$build_converters = $(on_type == nothing)
if !haskey($(ALREADY_DEFINED_BINARY_OPERATORS).operator_enum, $type_requirements)
$(ALREADY_DEFINED_BINARY_OPERATORS).operator_enum[$type_requirements] = Dict{Function,Bool}()
end
if !haskey($(ALREADY_DEFINED_UNARY_OPERATORS).operator_enum, $type_requirements)
$(ALREADY_DEFINED_UNARY_OPERATORS).operator_enum[$type_requirements] = Dict{Function,Bool}()
end
$binary_exists = $(ALREADY_DEFINED_BINARY_OPERATORS).operator_enum[$type_requirements]
$unary_exists = $(ALREADY_DEFINED_UNARY_OPERATORS).operator_enum[$type_requirements]
else
$f_outside = :($($__module__).$($f_outside))
$type_requirements = $(on_type == nothing ? Any : on_type)
$build_converters = false
if !haskey($(ALREADY_DEFINED_BINARY_OPERATORS).generic_operator_enum, $type_requirements)
$(ALREADY_DEFINED_BINARY_OPERATORS).generic_operator_enum[$type_requirements] = Dict{Function,Bool}()
end
if !haskey($(ALREADY_DEFINED_UNARY_OPERATORS).generic_operator_enum, $type_requirements)
$(ALREADY_DEFINED_UNARY_OPERATORS).generic_operator_enum[$type_requirements] = Dict{Function,Bool}()
end
$binary_exists = $(ALREADY_DEFINED_BINARY_OPERATORS).generic_operator_enum[$type_requirements]
$unary_exists = $(ALREADY_DEFINED_UNARY_OPERATORS).generic_operator_enum[$type_requirements]
end
$(LATEST_BINARY_OPERATOR_MAPPING)[func] = op
$skip && continue
# Avoid redefining methods:
if !haskey($unary_exists, func)
eval($binary_ex)
$(unary_exists)[func] = true

if $(empty_old_operators)
# Trigger errors if operators are not yet defined:
empty!($(LATEST_BINARY_OPERATOR_MAPPING))
empty!($(LATEST_UNARY_OPERATOR_MAPPING))
end
end
for (op, func) in enumerate($(operators).unaops)
local ($f_outside, $f_inside) = $(_unpack_broadcast_function)(func)
local $skip = false
if isdefined(Base, $f_outside)
$f_outside = :(Base.$($f_outside))
elseif $(skip_user_operators)
$skip = true
else
$f_outside = :($($__module__).$($f_outside))

for (op, func) in enumerate($(operators).binops)
local ($f_outside, $f_inside) = $(_unpack_broadcast_function)(func)
local $skip = false
if isdefined(Base, $f_outside)
$f_outside = :(Base.$($f_outside))
elseif $(skip_user_operators)
$skip = true
else
$f_outside = :($($__module__).$($f_outside))
end
$(LATEST_BINARY_OPERATOR_MAPPING)[func] = op
$skip && continue
# Avoid redefining methods:
if !haskey($unary_exists, func)
eval($binary_ex)
$(unary_exists)[func] = true
end
end
$(LATEST_UNARY_OPERATOR_MAPPING)[func] = op
$skip && continue
# Avoid redefining methods:
if !haskey($binary_exists, func)
eval($unary_ex)
$(binary_exists)[func] = true

for (op, func) in enumerate($(operators).unaops)
local ($f_outside, $f_inside) = $(_unpack_broadcast_function)(func)
local $skip = false
if isdefined(Base, $f_outside)
$f_outside = :(Base.$($f_outside))
elseif $(skip_user_operators)
$skip = true
else
$f_outside = :($($__module__).$($f_outside))
end
$(LATEST_UNARY_OPERATOR_MAPPING)[func] = op
$skip && continue
# Avoid redefining methods:
if !haskey($binary_exists, func)
eval($unary_ex)
$(binary_exists)[func] = true
end
end
end
end
end
#! format: on
end
Expand Down
2 changes: 1 addition & 1 deletion src/ParametricExpression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function Base.convert(::Type{Node}, ex::ParametricExpression{T}) where {T}
elseif leaf.is_parameter
Node(T; feature=leaf.parameter)
else
Node(T; feature=leaf.feature + num_params)
Node(T; feature=(leaf.feature + num_params))
end,
branch -> branch.op,
(op, children...) -> Node(; op, children),
Expand Down
14 changes: 7 additions & 7 deletions src/Parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ macro parse_expression(ex, kws...)
return esc(
:($(parse_expression)(
$(Meta.quot(ex));
operators=$(parsed_kws.operators),
operators=($(parsed_kws.operators)),
binary_operators=nothing,
unary_operators=nothing,
variable_names=$(parsed_kws.variable_names),
node_type=$(parsed_kws.node_type),
expression_type=$(parsed_kws.expression_type),
evaluate_on=$(parsed_kws.evaluate_on),
variable_names=($(parsed_kws.variable_names)),
node_type=($(parsed_kws.node_type)),
expression_type=($(parsed_kws.expression_type)),
evaluate_on=($(parsed_kws.evaluate_on)),
$(parsed_kws.extra_metadata)...,
)),
)
Expand Down Expand Up @@ -188,8 +188,8 @@ end
"You must specify the operators using either `operators`, or `binary_operators` and `unary_operators`"
)
operators = :($(OperatorEnum)(;
binary_operators=$(binops === nothing ? :(Function[]) : binops),
unary_operators=$(unaops === nothing ? :(Function[]) : unaops),
binary_operators=($(binops === nothing ? :(Function[]) : binops)),
unary_operators=($(unaops === nothing ? :(Function[]) : unaops)),
))
else
@assert (binops === nothing && unaops === nothing)
Expand Down
5 changes: 3 additions & 2 deletions src/Random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ end

Sample a node from a tree according to the default sampler `NodeSampler(; tree)`.
"""
rand(rng::AbstractRNG, tree::Union{AbstractNode,AbstractExpression}) =
rand(rng, NodeSampler(; tree))
function rand(rng::AbstractRNG, tree::Union{AbstractNode,AbstractExpression})
return rand(rng, NodeSampler(; tree))
end

"""
rand(rng::AbstractRNG, sampler::NodeSampler)
Expand Down
42 changes: 32 additions & 10 deletions src/ValueInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,46 @@ end
function _check_is_valid_array(x)
return is_valid_array([x]) isa Bool && is_valid_array([x]) == is_valid(x)
end
function _check_get_number_type(x)
function _check_get_number_type(x)::Bool
try
get_number_type(typeof(x)) <: Number
catch e
@error e
return get_number_type(typeof(x)) <: Number
catch
return false
end
end
function _check_pack_scalar_constants!(x)
packed_x = Vector{get_number_type(typeof(x))}(undef, count_scalar_constants(x))
function _check_pack_scalar_constants!(x)::Bool
T = try
get_number_type(typeof(x))
catch
return false
end

n = count_scalar_constants(x)
packed_x = Vector{T}(undef, n)

applicable(pack_scalar_constants!, packed_x, 1, x) || return false

new_idx = pack_scalar_constants!(packed_x, 1, x)
return new_idx == 1 + count_scalar_constants(x)
return (new_idx isa Integer) && (new_idx == 1 + n)
end
function _check_unpack_scalar_constants(x)
packed_x = Vector{get_number_type(typeof(x))}(undef, count_scalar_constants(x))

function _check_unpack_scalar_constants(x)::Bool
T = try
get_number_type(typeof(x))
catch
return false
end

n = count_scalar_constants(x)
packed_x = Vector{T}(undef, n)

applicable(pack_scalar_constants!, packed_x, 1, x) || return false
applicable(unpack_scalar_constants, packed_x, 1, x) || return false

pack_scalar_constants!(packed_x, 1, x)
new_idx, x2 = unpack_scalar_constants(packed_x, 1, x)
return new_idx == 1 + count_scalar_constants(x) && x2 == x

return (new_idx isa Integer) && (new_idx == 1 + n) && (x2 == x)
end
function _check_count_scalar_constants(x)
return count_scalar_constants(x) isa Int &&
Expand Down
4 changes: 3 additions & 1 deletion src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ function test_all_combinations(; binary_operators, unary_operators, turbo, types
end

function test_functions_on_trees(::Type{T}, operators) where {T}
local x, c, tree
local x, c
# Initialize `tree` so static analyzers (JET) don't think it might be undefined.
tree = Node(Float64; val=0.0)
num_unaops = length(operators.unaops)
num_binops = length(operators.binops)
@assert num_unaops > 0 && num_binops > 0
Expand Down
Loading
Loading