-
Notifications
You must be signed in to change notification settings - Fork 21
WIP: Front end for EnzymeMLIR ProbProg pass #1236
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
Draft
sbrantq
wants to merge
13
commits into
main
Choose a base branch
from
probprog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
902ced9
generate
sbrantq e2c77e4
refactor
sbrantq e204d13
Merge branch 'main' of https://github.com/EnzymeAD/Reactant.jl into p…
sbrantq 327b10a
Merge branch 'main' of https://github.com/EnzymeAD/Reactant.jl into p…
sbrantq d611ae4
add probprog pass to :all
sbrantq 3672d83
improve test
sbrantq b70843e
only probprog opt mode
sbrantq 597fa89
fix up test
sbrantq e6c2c0a
move
sbrantq 9b9395e
simplify
sbrantq b3ba477
fix up
sbrantq 47e9fe3
saving changes
sbrantq 982b2bf
Merge branch 'main' of https://github.com/EnzymeAD/Reactant.jl into p…
sbrantq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,131 @@ | ||
module ProbProg | ||
|
||
using ..Reactant: Reactant, XLA, MLIR, TracedUtils | ||
using ReactantCore: ReactantCore | ||
|
||
using Enzyme | ||
|
||
@noinline function generate(f::Function, args::Vararg{Any,Nargs}) where {Nargs} | ||
argprefix::Symbol = gensym("generatearg") | ||
resprefix::Symbol = gensym("generateresult") | ||
resargprefix::Symbol = gensym("generateresarg") | ||
|
||
mlir_fn_res = TracedUtils.make_mlir_fn( | ||
f, | ||
args, | ||
(), | ||
string(f), | ||
false; | ||
args_in_result=:result, | ||
argprefix, | ||
resprefix, | ||
resargprefix, | ||
) | ||
(; result, linear_args, in_tys, linear_results) = mlir_fn_res | ||
fnwrap = mlir_fn_res.fnwrapped | ||
func2 = mlir_fn_res.f | ||
|
||
out_tys = [MLIR.IR.type(TracedUtils.get_mlir_data(res)) for res in linear_results] | ||
fname = TracedUtils.get_attribute_by_name(func2, "sym_name") | ||
fname = MLIR.IR.FlatSymbolRefAttribute(Base.String(fname)) | ||
|
||
batch_inputs = MLIR.IR.Value[] | ||
for a in linear_args | ||
idx, path = TracedUtils.get_argidx(a, argprefix) | ||
if idx == 1 && fnwrap | ||
TracedUtils.push_val!(batch_inputs, f, path[3:end]) | ||
else | ||
if fnwrap | ||
idx -= 1 | ||
end | ||
TracedUtils.push_val!(batch_inputs, args[idx], path[3:end]) | ||
end | ||
end | ||
|
||
gen_op = MLIR.Dialects.enzyme.generate(batch_inputs; outputs=out_tys, fn=fname) | ||
|
||
residx = 1 | ||
for a in linear_results | ||
resv = MLIR.IR.result(gen_op, residx) | ||
residx += 1 | ||
for path in a.paths | ||
if length(path) == 0 | ||
continue | ||
end | ||
if path[1] == resprefix | ||
TracedUtils.set!(result, path[2:end], resv) | ||
elseif path[1] == argprefix | ||
idx = path[2]::Int | ||
if idx == 1 && fnwrap | ||
TracedUtils.set!(f, path[3:end], resv) | ||
else | ||
if fnwrap | ||
idx -= 1 | ||
end | ||
TracedUtils.set!(args[idx], path[3:end], resv) | ||
end | ||
end | ||
end | ||
end | ||
|
||
return result | ||
end | ||
|
||
function sample(f::Function, args::Vararg{Any,Nargs}) where {Nargs} | ||
argprefix::Symbol = gensym("samplearg") | ||
resprefix::Symbol = gensym("sampleresult") | ||
resargprefix::Symbol = gensym("sampleresarg") | ||
|
||
mlir_fn_res = TracedUtils.make_mlir_fn( | ||
f, | ||
args, | ||
(), | ||
string(f), | ||
false; | ||
args_in_result=:result, | ||
argprefix, | ||
resprefix, | ||
resargprefix, | ||
) | ||
(; result, linear_args, in_tys, linear_results) = mlir_fn_res | ||
fnwrap = mlir_fn_res.fnwrapped | ||
func2 = mlir_fn_res.f | ||
|
||
batch_inputs = MLIR.IR.Value[] | ||
for a in linear_args | ||
idx, path = TracedUtils.get_argidx(a, argprefix) | ||
if idx == 1 && fnwrap | ||
TracedUtils.push_val!(batch_inputs, f, path[3:end]) | ||
else | ||
idx -= fnwrap ? 1 : 0 | ||
TracedUtils.push_val!(batch_inputs, args[idx], path[3:end]) | ||
end | ||
end | ||
|
||
out_tys = [MLIR.IR.type(TracedUtils.get_mlir_data(res)) for res in linear_results] | ||
|
||
sym = TracedUtils.get_attribute_by_name(func2, "sym_name") | ||
fn_attr = MLIR.IR.FlatSymbolRefAttribute(Base.String(sym)) | ||
|
||
sample_op = MLIR.Dialects.enzyme.sample(batch_inputs; outputs=out_tys, fn=fn_attr) | ||
|
||
ridx = 1 | ||
for a in linear_results | ||
val = MLIR.IR.result(sample_op, ridx) | ||
ridx += 1 | ||
|
||
for path in a.paths | ||
isempty(path) && continue | ||
if path[1] == resprefix | ||
TracedUtils.set!(result, path[2:end], val) | ||
elseif path[1] == argprefix | ||
idx = path[2]::Int - (fnwrap ? 1 : 0) | ||
TracedUtils.set!(args[idx], path[3:end], val) | ||
end | ||
end | ||
end | ||
|
||
return result | ||
end | ||
|
||
end | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[JuliaFormatter] reported by reviewdog 🐶