SymbolicML / DynamicExpressions.jl

Ridiculously fast symbolic expressions
https://symbolicml.org/DynamicExpressions.jl/dev
Apache License 2.0
90 stars 11 forks source link

[WIP] Hierarchical printing of graph-like expressions #59

Open MilesCranmer opened 5 months ago

MilesCranmer commented 5 months ago

Say we create an expression:

julia> using DynamicExpressions

julia> x1, x2, x3 = (GraphNode{Float64}(feature=i) for i=1:3);

julia> operators = OperatorEnum(
           binary_operators=[+, -, *, /, ^],
           unary_operators=[cos, sin, exp],
       );

julia> base_tree = cos(x1 - 3.2) * x2 - x3 * x3
(cos(x1 - 3.2) * x2) - (x3 * {x3})

julia> base_tree2 = exp(x3 * x2) - base_tree
exp(x3 * x2) - ((cos(x1 - 3.2) * {x2}) - ({x3} * {x3}))

julia> tree = cos(base_tree * base_tree2) - base_tree * base_tree2 ^ 2
cos(((cos(x1 - 3.2) * x2) - (x3 * {x3})) * (exp({x3} * {x2}) - {((cos(x1 - 3.2) * x2) - (x3 * {x3}))})) - ({((cos(x1 - 3.2) * x2) - (x3 * {x3}))} * ({(exp({x3} * {x2}) - {((cos(x1 - 3.2) * x2) - (x3 * {x3}))})} ^ 2.0))

Note that the normal behavior is to encapsulate with {} when a node is shared.

However, we can print this with less redundancy with pretty_string_graph:

julia> print(pretty_string_graph(tree, operators))
┌─── z1 = (cos(x1 - 3.2) * x2) - (x3 * x3)
├─── z2 = exp(x3 * x2) - z1
│
= cos(z1 * z2) - (z1 * (z2 ^ 2.0))

which finds the shared nodes, assigns variable names based on their depth, and then prints the program in this hierarchical fashion.

This is only 2x slower than the regular string_tree method, so I'm considering have this as the default for GraphNode types. For consistency I might need to prepend the = to regular print statements though...

Curious to hear your thoughts @AlCap23 @Vaibhavdixit02 @ChrisRackauckas! I played around with a few different ideas for how to visualize this in the REPL and this one seemed like a good option. But interested in more.

github-actions[bot] commented 5 months ago

Pull Request Test Coverage Report for Build 7344448443


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/Strings.jl 70 135 51.85%
<!-- Total: 70 135 51.85% -->
Totals Coverage Status
Change from base Build 7337169551: -4.2%
Covered Lines: 1269
Relevant Lines: 1409

💛 - Coveralls
github-actions[bot] commented 5 months ago

Benchmark Results

master 9cd35ffdf59628... t[master]/t[9cd35ffdf59628...]
eval/ComplexF32/evaluation 7.37 ± 0.52 ms 7.31 ± 0.46 ms 1.01
eval/ComplexF64/evaluation 9.58 ± 0.7 ms 9.5 ± 0.64 ms 1.01
eval/Float32/derivative 10.5 ± 1.3 ms 10.8 ± 1.3 ms 0.972
eval/Float32/derivative_turbo 12.1 ± 1.4 ms 12.3 ± 1.3 ms 0.986
eval/Float32/evaluation 2.65 ± 0.24 ms 2.62 ± 0.22 ms 1.01
eval/Float32/evaluation_turbo 0.618 ± 0.025 ms 0.603 ± 0.026 ms 1.02
eval/Float64/derivative 13.7 ± 0.54 ms 13.3 ± 0.55 ms 1.03
eval/Float64/derivative_turbo 14.5 ± 0.56 ms 14.6 ± 0.54 ms 0.992
eval/Float64/evaluation 2.82 ± 0.24 ms 2.82 ± 0.24 ms 0.999
eval/Float64/evaluation_turbo 1.12 ± 0.058 ms 1.11 ± 0.059 ms 1.01
utils/combine_operators/break_sharing 0.0415 ± 0.0028 ms 0.0409 ± 0.0026 ms 1.01
utils/convert/break_sharing 28.1 ± 0.56 μs 28.4 ± 0.72 μs 0.99
utils/convert/preserve_sharing 0.127 ± 0.0025 ms 0.128 ± 0.0026 ms 0.995
utils/copy/break_sharing 28.9 ± 0.58 μs 28.6 ± 0.63 μs 1.01
utils/copy/preserve_sharing 0.128 ± 0.0027 ms 0.127 ± 0.0025 ms 1
utils/count_constants/break_sharing 11.8 ± 0.22 μs 10.8 ± 0.17 μs 1.09
utils/count_constants/preserve_sharing 0.11 ± 0.0023 ms 0.111 ± 0.0023 ms 0.997
utils/count_depth/break_sharing 12.8 ± 0.22 μs 12.7 ± 0.21 μs 1
utils/count_nodes/break_sharing 10.5 ± 0.18 μs 10.1 ± 0.16 μs 1.03
utils/count_nodes/preserve_sharing 0.114 ± 0.0022 ms 0.114 ± 0.0022 ms 1
utils/get_set_constants!/break_sharing 0.0539 ± 0.00088 ms 0.0523 ± 0.00081 ms 1.03
utils/get_set_constants!/preserve_sharing 0.318 ± 0.0051 ms 0.32 ± 0.0048 ms 0.994
utils/has_constants/break_sharing 4.47 ± 0.22 μs 4.31 ± 0.21 μs 1.04
utils/has_operators/break_sharing 1.78 ± 0.017 μs 1.77 ± 0.016 μs 1
utils/hash/break_sharing 30.4 ± 0.46 μs 30 ± 0.48 μs 1.01
utils/hash/preserve_sharing 0.133 ± 0.0025 ms 0.133 ± 0.0025 ms 1
utils/index_constants/break_sharing 27.5 ± 0.68 μs 27.4 ± 0.76 μs 1
utils/index_constants/preserve_sharing 0.127 ± 0.0024 ms 0.127 ± 0.0024 ms 0.997
utils/is_constant/break_sharing 5.51 ± 0.21 μs 4.77 ± 0.22 μs 1.16
utils/simplify_tree/break_sharing 0.251 ± 0.02 ms 0.248 ± 0.02 ms 1.01
utils/simplify_tree/preserve_sharing 0.377 ± 0.022 ms 0.386 ± 0.022 ms 0.977
utils/string_tree/break_sharing 0.558 ± 0.011 ms 0.582 ± 0.015 ms 0.959
utils/string_tree/preserve_sharing 0.693 ± 0.013 ms 0.7 ± 0.022 ms 0.99
time_to_load 0.683 ± 0.0083 s 0.701 ± 0.0079 s 0.974