Flux.jl/src/compiler/interp.jl
2016-11-15 21:09:58 +00:00

29 lines
820 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: interpret, interpret, interptuple, interplambda, interpconst, Context
function astuple(xs)
isconstant(xs) && isa(value(xs).value, Tuple) ? value(xs).value :
isa(xs, Vertex) && value(xs) == tuple ? inputs(xs) :
nothing
end
function astuples(xs)
xs = [astuple(x) for x in xs]
all(x->!(x==nothing), xs) ? xs : nothing
end
function interp(ctx, ::typeof(map), f, xs...)
f = interpret(ctx, f)
xs = interpret(ctx, xs)
xs = astuples(xs)
xs == nothing ? vertex(map, constant(f), xs...) : group(map(f, xs...)...)
end
function interp(ctx, model, xs...)
g = graph(model)
g == nothing && return vertex(model, map(constant, interpret(ctx, xs))...)
interpret(ctx, g, xs...)
end
expand(graph, xs...) =
interp(Context(interplambda(interpconst(interptuple(interp)))), graph, xs...)