Flux.jl/src/compiler/interp.jl
2017-03-14 15:21:18 +00:00

43 lines
975 B
Julia
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DataFlow.Interpreter
function astuple(xs::Vertex)
isconstant(xs) && value(xs).value isa Tuple ? value(xs).value :
xs isa Vertex && value(xs) == tuple ? inputs(xs) :
nothing
end
astuple(xs::Tuple) = xs
astuple(xs) = nothing
function astuples(xs)
xs = [astuple(x) for x in xs]
all(x->!(x==nothing), xs) ? xs : nothing
end
function imap(cb, ctx, ::typeof(map), f, xs...)
f, xs = interpv(ctx, (f, xs))
xs = astuples(xs)
xs nothing ?
group(map(f, xs...)...) :
cb(ctx, map, constant(f), xs...)
end
imap(f, args...) = f(args...)
function interp(ctx, f, xs...)
g = graph(f)
@icatch(ctx, g nothing ?
interpret(ctx, reifyparams(g), xs...) :
f(xs...))
end
# TODO: batching should be secondary
function interpmodel_(m, args...)
ctx = Context(mux(iline, ilambda, iconst, iargs, ituple, interp))
interp(ctx, m, args...)
end
interpmodel(m, args...) = @ithrow runrawbatched((xs...) -> interpmodel_(m, xs...), args...)