Flux.jl/src/compiler/interp.jl

37 lines
948 B
Julia
Raw Normal View History

2016-12-21 13:05:18 +00:00
using DataFlow: interpret, interpv, interptuple, interplambda, interpconst, Context
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-11-17 11:26:58 +00:00
function interpmap(cb)
function interp(ctx, ::typeof(map), f, xs...)
2016-12-21 13:05:18 +00:00
f, xs = interpv(ctx, (f, xs))
2016-11-17 11:26:58 +00:00
xs = astuples(xs)
xs nothing ?
group(map(f, xs...)...) :
cb(ctx, map, constant(f), xs...)
end
interp(args...) = cb(args...)
2016-11-15 20:46:01 +00:00
end
2016-11-15 20:01:56 +00:00
function interp(ctx, model, xs...)
g = graph(model)
2016-12-21 13:05:18 +00:00
g == nothing && return vertex(model, map(constant, interpv(ctx, xs))...)
2016-11-15 20:01:56 +00:00
interpret(ctx, g, xs...)
end
expand(graph, xs...) =
2016-11-17 11:26:58 +00:00
interp(Context(interplambda(interpmap(interpconst(interptuple(interp))))), graph, xs...)