-
Notifications
You must be signed in to change notification settings - Fork 219
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
Refactoring of compiler #513
Conversation
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.
This looks great, much cleaner than before. The thing that I feel most strongly about regards the use of MacroTools
to make some code more robust and to avoid reinventing the wheel. Please see my specific comments on that.
src/core/compiler.jl
Outdated
|
||
Extract function name, arguments and body. | ||
""" | ||
function extractcomponents(fexpr::Expr) |
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.
Please consider using MacroTools.jl here. It's splitdef
function is designed precisely to do the job of this function.
src/core/compiler.jl
Outdated
|
||
function insdelim(c, deli=",") | ||
reduce((e, res) -> append!(e, [res, deli]), c; init = [])[1:end-1] | ||
reduce((e, res) -> append!(e, [res, deli]), c; init = [])[1:end-1] |
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.
return
statement please. Also, this function appears to only be used in is_inside.jl
, so perhaps we could move it to that file?
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.
Good point, I'll move it into is_inside.jl
src/core/compiler.jl
Outdated
while isa(curr, Expr) && curr.head == :ref | ||
curr = curr.args[1] | ||
end | ||
curr |
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.
return
src/core/compiler.jl
Outdated
else | ||
map(translate!, ex.args) | ||
end | ||
ex |
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.
return
src/core/compiler.jl
Outdated
fname_inner = Symbol("$(fname)_model") | ||
fname_inner_str = string(fname_inner) | ||
fdefn_inner_func = constructfunc( | ||
fname_inner, |
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.
Indentation (again, sorry)
src/core/compiler.jl
Outdated
push!(fargs_outer[1].args, Expr(:kw, :compiler, compiler)) | ||
|
||
# turn f(x;..) into f(x=nothing;..) | ||
for i = 2:length(fargs_outer) |
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.
(Personal preference) This is maybe a cleaner version:
fargs_outer = map(x -> x isa Symbol ? Expr(:kw, x, :nothing) : x, fargs_outer[2:end])
Thanks for reviewing, I'll consider using |
Regarding the make_varname(expr::Symbol) = (expr, (), gensym())
function make_varname(expr::Expr)
@assert expr.head == :ref "VarName: Malformed variable name $(expr)"
return _make_varname(expr, Vector{Union{Symbol, Expr}}())..., gensym()
end
function _make_varname(expr::Expr, indices::Vector{Union{Symbol, Expr}})
if expr.args[1] isa Symbol
return expr.args[1], vcat(Symbol(expr.args[2]), indices)
else
return _make_varname(expr.args[1], vcat(Symbol(expr.args[2]), indices))
end
end (named This doesn't quite work correctly, in particular the type of the second thing returned isn't quite the same as what |
There are cases where a function isn't enough, and a macro is required. Consider the following case x = 1
y = 2
a = Array(undef, 4,4)
a[x,y] ~ Normal(0,1) Here the indexing only becomes available at runtime. So we have to generate the variable name using a macro. |
@yebai I completely agree that we require a way to construct indices dynamically, but disagree that it necessitates the extra macro. I'll open a separate issue to discuss so that we can resolve this in a separate PR. |
Hi @trappmartin, here is an updated compiler design after removing # Compiler design: sample(f_compiletime(x,y), sampler)
# f_compiletime(x, y; compiler=compiler) = begin
# ex = quote
# f_runtime(vi::VarInfo, sampler::Sampler; x = x, y = y) = begin
# # pour model definition `fbody`, e.g.
# x ~ Normal(0,1)
# y ~ Normal(x, 1)
# end
# end
# Main.eval(ex)
# end |
3fcdb47
to
ee5c19c
Compare
…ing to guidelines
The current code breaks the
Does anybody have an idea why the ad tests break? |
Looks like const CHUNKSIZE = Ref(10) # default chunksize used by AD on line 51 of Turing.jl |
0c9402e
to
2881f04
Compare
2881f04
to
d3306d1
Compare
Thanks, @yebai! The |
This is a major refactoring of the compiler code. Currently, this PR is work in progress and should not be merged. The PR should be finished soon.
Todo:
return vi::VarInfo
. #483model_f(;data=Dict())
from compiler to utilities #475vi.logp
and initializevi.logp = zero(Real)