Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/high-to-mid/float-ein.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/high-to-mid/float-ein.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3582 - (view) (download)

1 : jhr 3556 (* float-ein.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2016 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 : jhr 3555
9 : jhr 3556 structure FloatEin : sig
10 :    
11 : jhr 3582 val transform : MidIR.var * Ein.ein * MidIR.var list -> MidIR.assign list
12 : jhr 3555
13 :     end = struct
14 :    
15 :     structure IR = MidIR
16 : jhr 3556 structure V = IR.Var
17 :     structure Ty = MidTypes
18 : jhr 3555 structure E = Ein
19 :    
20 :     fun cut (name, origProbe, params, index, sx, argsOrig, avail, newvx) = let
21 :     (*clean and rewrite current body*)
22 : jhr 3563 val (tshape, sizes, body) = CleanIndex.clean (origProbe, index, sx)
23 : jhr 3555 val id = length params
24 :     val Rparams = params@[E.TEN(true, sizes)]
25 : jhr 3576 val y = V.new (concat[name, "_l_", Int.toString id], Ty.TensorTy sizes)
26 :     val IR.EINAPP(ein, args) = CleanParams.clean (body, Rparams, sizes, argsOrig@[y])
27 : jhr 3556 (* shift indices in probe body from constant to variable *)
28 :     val Ein.EIN{
29 :     body=E.Probe(E.Conv(V, [c1], h, dx), pos),
30 :     index = index0,
31 :     params = params0
32 :     } = ein
33 : jhr 3555 (* FIXME: this code is specialized to 3D *)
34 :     val index1 = index0@[3]
35 : jhr 3556 val unshiftedBody = E.Probe(E.Conv(V, [E.V newvx], h, dx), pos)
36 : jhr 3555 (* clean to get body indices in order *)
37 : jhr 3563 val (_ , _, body1) = CleanIndex.clean (unshiftedBody, index1, [])
38 : jhr 3556 val lhs1 = V.new ("L", Ty.TensorTy index1)
39 : jhr 3576 val ein1 = E.EIN{params = params0, index = index0, body = body1}
40 :     val lhs2 = AvailRHS.addAssign avail (lhs1, IR.EINAPP(ein1, args))
41 : jhr 3555 val Rargs = argsOrig @ [lhs2]
42 :     (*Probe that tensor at a constant position c1*)
43 :     val nx = List.mapi (fn (i, _) => E.V i) dx
44 :     val Re = E.Tensor(id, c1 :: tshape)
45 :     val Rparams = params @ [E.TEN(true, index1)]
46 :     in
47 :     (Re, Rparams, Rargs)
48 :     end
49 :    
50 :     (* lift:ein_app*params*index*sum_id*args-> (ein_exp* params*args*code)
51 :     *lifts expression and returns replacement tensor
52 :     * cleans the index and params of subexpression
53 :     *creates new param and replacement tensor for the original ein_exp
54 :     *)
55 :     fun lift (name, e, params, index, sx, args, avail) = let
56 : jhr 3563 val (tshape, sizes, body) = CleanIndex.clean(e, index, sx)
57 : jhr 3555 val id = length params
58 :     val Rparams = params @ [E.TEN(true, sizes)]
59 :     val Re = E.Tensor(id, tshape)
60 : jhr 3556 val M = V.new (concat[name, "_l_", Int.toString id], Ty.TensorTy sizes)
61 : jhr 3576 val einapp = CleanParams.clean (body, Rparams, sizes, args @ [M])
62 : jhr 3556 val var = AvailRHS.addAssign avail (M, einapp)
63 :     val Rargs = args @ [var]
64 : jhr 3555 in
65 :     (Re, Rparams, Rargs)
66 :     end
67 :    
68 :     fun isOp e = (case e
69 :     of E.Op1 _ => true
70 :     | E.Op2 _ => true
71 :     | E.Opn _ => true
72 :     | E.Sum _ => true
73 :     | E.Probe _ => true
74 :     | _ => false
75 :     (* end case *))
76 :    
77 :     fun transform (y, ein as Ein.EIN{body=E.Probe _, ...}, args) =
78 : jhr 3582 [(y, IR.EINAPP(ein, args))]
79 : jhr 3555 | transform (y, ein as Ein.EIN{body=E.Sum(_, E.Probe _), ...}, args) =
80 : jhr 3582 [(y, IR.EINAPP(ein, args))]
81 : jhr 3555 | transform (y, Ein.EIN{params, index, body}, args) = let
82 :     val avail = AvailRHS.new()
83 :     fun filterOps (es, params, args, index, sx) = let
84 :     fun filter ([], es', params, args) = (rev es', params, args)
85 :     | filter (e::es, es', params, args) = if isOp e
86 :     then let
87 :     val (e', params', args') = lift("op1_e3", e, params, index, sx, args, avail)
88 :     in
89 : jhr 3556 filter (es, e'::es', params', args')
90 : jhr 3555 end
91 :     else filter (es, e::es', params, args)
92 :     in
93 :     filter (es, [], params, args)
94 :     end
95 :     fun rewrite (sx, exp, params, args) = (case exp
96 :     of E.Probe(E.Conv(_, [E.C _], _, []), _) =>
97 :     cut ("cut", exp, params, index, sx, args, avail, 0)
98 :     | E.Probe(E.Conv(_, [E.C _ ], _, [E.V 0]), _) =>
99 :     cut ("cut", exp, params, index, sx, args, avail, 1)
100 :     | E.Probe(E.Conv(_, [E.C _ ], _, [E.V 0, E.V 1]), _) =>
101 :     cut ("cut", exp, params, index, sx, args, avail, 2)
102 :     | E.Probe(E.Conv(_, [E.C _ ], _, [E.V 0, E.V 1, E.V 2]), _) =>
103 :     cut ("cut", exp, params, index, sx, args, avail, 3)
104 :     | E.Probe _ => lift ("probe", exp, params, index, sx, args, avail)
105 :     | E.Sum(_, E.Probe _) => lift ("probe", exp, params, index, sx, args, avail)
106 :     | E.Op1(op1, e1) => let
107 :     val (e1', params', args') = rewrite (sx, e1, params, args)
108 :     val ([e1], params', args') = filterOps ([e1'], params', args', index, sx)
109 :     in
110 :     (E.Op1(op1, e1), params', args')
111 :     end
112 :     | E.Op2(op2, e1, e2) => let
113 :     val (e1', params', args') = rewrite (sx, e1, params, args)
114 :     val (e2', params', args') = rewrite (sx, e2, params', args')
115 :     val ([e1', e2'], params', args') =
116 :     filterOps ([e1', e2'], params', args', index, sx)
117 :     in
118 :     (E.Op2(op2, e1', e2'), params', args')
119 :     end
120 :     | E.Opn(opn, es) => let
121 :     fun iter ([], es, params, args) = (List.rev es, params, args)
122 :     | iter (e::es, es', params, args) = let
123 : jhr 3556 val (e', params', args') = rewrite (sx, e, params, args)
124 : jhr 3555 in
125 :     iter (es, e'::es', params', args')
126 :     end
127 :     val (es, params, args) = iter (es, [], params, args)
128 :     val (es, params, args) = filterOps (es, params, args, index, sx)
129 :     in
130 :     (E.Opn(opn, es), params, args)
131 :     end
132 :     | E.Sum(sx1, e) => let
133 :     val (e', params', args') = rewrite (sx1@sx, e, params, args)
134 :     in
135 :     (E.Sum(sx1, e'), params', args')
136 :     end
137 :     | _ => (exp, params, args)
138 :     (* end case *))
139 :     val (body', params', args') = rewrite ([], body, params, args)
140 : jhr 3576 val einapp = CleanParams.clean (body', params', index, args')
141 : jhr 3555 in
142 : jhr 3582 List.rev ((y, einapp) :: AvailRHS.getAssignments avail)
143 : jhr 3555 end
144 :    
145 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0