-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from SymbolicML:bump-alloc3
Bump allocator version of expression evaluation
- Loading branch information
Showing
18 changed files
with
562 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
[deps] | ||
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | ||
Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e" | ||
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
StrideArrays = "d1fa6d79-ef01-42a6-86c9-f7c551f8593b" | ||
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
module DynamicExpressionsBumperExt | ||
|
||
using Bumper: @no_escape, @alloc | ||
using DynamicExpressions: OperatorEnum, AbstractExpressionNode, tree_mapreduce | ||
using DynamicExpressions.UtilsModule: ResultOk, counttuple, is_bad_array | ||
|
||
import DynamicExpressions.ExtensionInterfaceModule: | ||
bumper_eval_tree_array, bumper_kern1!, bumper_kern2! | ||
|
||
function bumper_eval_tree_array( | ||
tree::AbstractExpressionNode{T}, | ||
cX::AbstractMatrix{T}, | ||
operators::OperatorEnum, | ||
::Val{turbo}, | ||
) where {T,turbo} | ||
result = similar(cX, axes(cX, 2)) | ||
n = size(cX, 2) | ||
all_ok = Ref(false) | ||
@no_escape begin | ||
_result_ok = tree_mapreduce( | ||
# Leaf nodes, we create an allocation and fill | ||
# it with the value of the leaf: | ||
leaf_node -> begin | ||
ar = @alloc(T, n) | ||
ok = if leaf_node.constant | ||
v = leaf_node.val::T | ||
ar .= v | ||
isfinite(v) | ||
else | ||
ar .= view(cX, leaf_node.feature, :) | ||
true | ||
end | ||
ResultOk(ar, ok) | ||
end, | ||
# Branch nodes, we simply pass them to the evaluation kernel: | ||
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..., Val(turbo)), | ||
tree; | ||
break_sharing=Val(true), | ||
) | ||
x = _result_ok.x | ||
result .= x | ||
all_ok[] = _result_ok.ok | ||
end | ||
return (result, all_ok[]) | ||
end | ||
|
||
function dispatch_kerns!(operators, branch_node, cumulator, ::Val{turbo}) where {turbo} | ||
cumulator.ok || return cumulator | ||
|
||
out = dispatch_kern1!(operators.unaops, branch_node.op, cumulator.x, Val(turbo)) | ||
return ResultOk(out, !is_bad_array(out)) | ||
end | ||
function dispatch_kerns!( | ||
operators, branch_node, cumulator1, cumulator2, ::Val{turbo} | ||
) where {turbo} | ||
cumulator1.ok || return cumulator1 | ||
cumulator2.ok || return cumulator2 | ||
|
||
out = dispatch_kern2!( | ||
operators.binops, branch_node.op, cumulator1.x, cumulator2.x, Val(turbo) | ||
) | ||
return ResultOk(out, !is_bad_array(out)) | ||
end | ||
|
||
@generated function dispatch_kern1!(unaops, op_idx, cumulator, ::Val{turbo}) where {turbo} | ||
nuna = counttuple(unaops) | ||
quote | ||
Base.@nif( | ||
$nuna, | ||
i -> i == op_idx, | ||
i -> let op = unaops[i] | ||
return bumper_kern1!(op, cumulator, Val(turbo)) | ||
end, | ||
) | ||
end | ||
end | ||
@generated function dispatch_kern2!( | ||
binops, op_idx, cumulator1, cumulator2, ::Val{turbo} | ||
) where {turbo} | ||
nbin = counttuple(binops) | ||
quote | ||
Base.@nif( | ||
$nbin, | ||
i -> i == op_idx, | ||
i -> let op = binops[i] | ||
return bumper_kern2!(op, cumulator1, cumulator2, Val(turbo)) | ||
end, | ||
) | ||
end | ||
end | ||
function bumper_kern1!(op::F, cumulator, ::Val{false}) where {F} | ||
@. cumulator = op(cumulator) | ||
return cumulator | ||
end | ||
function bumper_kern2!(op::F, cumulator1, cumulator2, ::Val{false}) where {F} | ||
@. cumulator1 = op(cumulator1, cumulator2) | ||
return cumulator1 | ||
end | ||
|
||
end |
Oops, something went wrong.
1f1ad6c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
1f1ad6c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/100215
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: