2016-10-31 12:38:18 +00:00
|
|
|
import DataFlow: mapconst, cse
|
2017-03-09 01:10:44 +00:00
|
|
|
using MacroTools: @q
|
2016-08-18 21:06:12 +00:00
|
|
|
|
2016-11-14 20:14:53 +00:00
|
|
|
export @net, @ml
|
2016-08-23 15:22:11 +00:00
|
|
|
|
|
|
|
function process_func(ex, params = [])
|
2016-04-01 21:11:42 +00:00
|
|
|
@capture(shortdef(ex), (args__,) -> body_)
|
2016-12-19 15:04:41 +00:00
|
|
|
body = @> body MacroTools.flatten liftloops graphm DataFlow.il
|
2016-08-18 21:06:12 +00:00
|
|
|
body = mapconst(x -> x in params ? :(self.$x) : x, body)
|
2016-04-01 21:11:42 +00:00
|
|
|
return args, body
|
|
|
|
end
|
|
|
|
|
2016-08-22 13:49:12 +00:00
|
|
|
function makegraph(graph, args)
|
2016-11-14 21:56:40 +00:00
|
|
|
graph = prewalk(graph) do v
|
2017-03-30 14:54:42 +00:00
|
|
|
value(v) isa Constant && (i = findfirst(x->x==value(v).value, args)) ≠ 0 ?
|
|
|
|
inputnode(i) :
|
2016-11-14 21:56:40 +00:00
|
|
|
v
|
|
|
|
end
|
|
|
|
graph = map(graph) do x
|
2017-03-14 15:21:18 +00:00
|
|
|
x isa Offset ?
|
2016-11-15 16:39:51 +00:00
|
|
|
:(Flux.Offset($(Expr(:quote, x.name)), $(x.n), self.$(x.name))) :
|
2016-11-14 21:56:40 +00:00
|
|
|
x
|
2016-08-22 13:49:12 +00:00
|
|
|
end
|
2016-12-26 12:11:24 +00:00
|
|
|
vertex(:(Flux.Frame(self)), graph)
|
2016-08-22 13:49:12 +00:00
|
|
|
end
|
|
|
|
|
2016-04-01 21:11:42 +00:00
|
|
|
function build_type(T, params)
|
2016-11-14 21:56:40 +00:00
|
|
|
@esc T
|
2016-08-23 13:14:20 +00:00
|
|
|
ex = quote
|
2016-08-22 13:49:23 +00:00
|
|
|
type $T <: Model
|
2016-04-01 21:11:42 +00:00
|
|
|
$(params...)
|
|
|
|
end
|
2016-08-23 11:38:58 +00:00
|
|
|
end
|
2016-08-23 13:14:20 +00:00
|
|
|
if any(x->isexpr(x, Symbol), params)
|
|
|
|
push!(ex.args,
|
|
|
|
:($T($(map(x->isexpr(x, Symbol) ? :($x::AArray) : x, params)...)) =
|
|
|
|
$T($(map(x->isexpr(x, Symbol) ? :(param($x)) : namify(x), params)...))))
|
|
|
|
end
|
|
|
|
ex
|
2016-08-23 11:38:58 +00:00
|
|
|
end
|
|
|
|
|
2017-02-24 14:38:17 +00:00
|
|
|
runmodel(f, xs...) = f(xs...)
|
|
|
|
|
|
|
|
function deref_params(v)
|
|
|
|
v = map(v) do x
|
2017-03-14 15:21:18 +00:00
|
|
|
x isa Constant && @capture(x.value, self.p_) ? Constant(:(Flux.state(self.$p))) : x
|
2017-02-24 14:38:17 +00:00
|
|
|
end
|
|
|
|
prewalk(v) do v
|
|
|
|
@capture(value(v), self.p_) ? vertex(:(Flux.runmodel), constant(:(self.$p)), inputs(v)...) : v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function build_forward(body, args)
|
|
|
|
iscyclic(body) && return :(error("Can't run forward pass on a cyclic graph"))
|
|
|
|
applylines(syntax(cse(deref_params(body))))
|
|
|
|
end
|
|
|
|
|
2016-10-29 22:36:39 +00:00
|
|
|
import Lazy: groupby
|
|
|
|
|
2017-03-14 15:21:18 +00:00
|
|
|
reifyparams(v::IVertex) = mapconst(x -> x isa Param ? x.x : x, v)
|
2016-11-13 20:46:35 +00:00
|
|
|
|
2017-03-09 00:12:49 +00:00
|
|
|
# TODO: type hints for parameters
|
|
|
|
|
2016-04-01 21:11:42 +00:00
|
|
|
function process_type(ex)
|
|
|
|
@capture(ex, type T_ fs__ end)
|
2016-08-23 11:38:58 +00:00
|
|
|
@destruct [params = false || [],
|
|
|
|
funcs = true || []] = groupby(x->isexpr(x, :->, :function), fs)
|
2016-04-01 21:11:42 +00:00
|
|
|
@assert length(funcs) == 1
|
2016-08-23 13:14:20 +00:00
|
|
|
pnames = namify.(params)
|
|
|
|
args, body = process_func(funcs[1], pnames)
|
2016-12-21 13:04:54 +00:00
|
|
|
self = esc(:self)
|
2017-02-24 15:48:52 +00:00
|
|
|
quote
|
2016-04-01 21:11:42 +00:00
|
|
|
$(build_type(T, params))
|
2017-03-09 01:10:44 +00:00
|
|
|
$(@q $(esc(:(Flux.runmodel(self::$T, $(args...)) = $(build_forward(body, args))))))
|
2017-03-09 00:13:26 +00:00
|
|
|
($self::$(esc(T)))($(args...)) = runrawbatched((xs...) -> runmodel($self, xs...), $(args...))
|
2016-12-21 13:04:54 +00:00
|
|
|
$(esc(:(Flux.update!(self::$T, η)))) = ($(map(p -> :(update!($self.$p, η)), pnames)...);)
|
2016-11-14 21:56:40 +00:00
|
|
|
$(esc(:(Flux.graph(self::$T)))) = $(DataFlow.constructor(mapconst(esc, makegraph(body, args))))
|
2016-08-22 13:49:34 +00:00
|
|
|
nothing
|
2016-11-14 21:56:40 +00:00
|
|
|
end
|
2016-04-01 21:11:42 +00:00
|
|
|
end
|
|
|
|
|
2016-08-23 15:22:11 +00:00
|
|
|
function process_anon(ex)
|
|
|
|
args, body = process_func(ex)
|
|
|
|
@assert length(args) == 1
|
2017-03-20 19:57:00 +00:00
|
|
|
:(Capacitor($(DataFlow.constructor(mapconst(esc, makegraph(body, args)[1])))))
|
2016-08-23 15:22:11 +00:00
|
|
|
end
|
|
|
|
|
2017-03-20 19:57:00 +00:00
|
|
|
macro net(ex)
|
|
|
|
ex = shortdef(ex)
|
|
|
|
isexpr(ex, :type) ? process_type(ex) :
|
|
|
|
@capture(ex, (__,) -> _) ? process_anon(ex) :
|
|
|
|
@capture(ex, _(__) = _) ? error("@net functions not implemented") :
|
|
|
|
error("Unsupported model expression $ex")
|
2016-08-22 13:49:34 +00:00
|
|
|
end
|