Flux.jl/src/compiler/interp.jl

43 lines
997 B
Julia
Raw Normal View History

2017-02-01 06:57:02 +00:00
using DataFlow.Interpreter
2016-11-15 20:01:56 +00:00
2016-11-17 11:27:17 +00:00
function astuple(xs::Vertex)
2016-11-15 20:01:56 +00:00
isconstant(xs) && isa(value(xs).value, Tuple) ? value(xs).value :
2016-11-15 21:09:58 +00:00
isa(xs, Vertex) && value(xs) == tuple ? inputs(xs) :
2016-11-15 20:01:56 +00:00
nothing
end
2016-11-17 11:27:17 +00:00
astuple(xs::Tuple) = xs
astuple(xs) = nothing
2016-11-15 20:01:56 +00:00
function astuples(xs)
xs = [astuple(x) for x in xs]
all(x->!(x==nothing), xs) ? xs : nothing
end
2016-12-26 13:42:12 +00:00
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...)
2016-11-15 20:46:01 +00:00
end
2016-11-15 20:01:56 +00:00
2016-12-26 13:42:12 +00:00
imap(f, args...) = f(args...)
2017-01-24 11:53:42 +00:00
2017-02-01 11:26:53 +00:00
function interp(ctx, f, xs...)
2017-02-01 11:36:42 +00:00
g = graph(f)
@ithrow(ctx, g nothing ?
interpret(ctx, reifyparams(g), xs...) :
f(xs...))
2017-02-01 11:26:53 +00:00
end
2017-01-25 10:03:58 +00:00
# TODO: batching should be secondary
2017-01-24 11:53:42 +00:00
function interpmodel(m, args::Batch...)
2017-02-01 11:26:53 +00:00
ctx = Context(mux(iline, ilambda, iconst, iargs, ituple, interp))
2017-02-01 11:36:42 +00:00
rebatch(@icatch interp(ctx, m, map(rawbatch, args)...))
2017-01-24 11:53:42 +00:00
end
interpmodel(m, args...) = unbatchone(interpmodel(m, batchone(args)...))