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 3657 - (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 : cchiw 3657 (*val einapp = CleanParams.clean (body, Rparams, sizes, args @ [M])*)
62 :     val einapp = CleanParams.clean(body, params, sizes, args)
63 : jhr 3556 val var = AvailRHS.addAssign avail (M, einapp)
64 :     val Rargs = args @ [var]
65 : jhr 3555 in
66 :     (Re, Rparams, Rargs)
67 :     end
68 :    
69 :     fun isOp e = (case e
70 :     of E.Op1 _ => true
71 :     | E.Op2 _ => true
72 :     | E.Opn _ => true
73 :     | E.Sum _ => true
74 :     | E.Probe _ => true
75 :     | _ => false
76 :     (* end case *))
77 :    
78 :     fun transform (y, ein as Ein.EIN{body=E.Probe _, ...}, args) =
79 : jhr 3582 [(y, IR.EINAPP(ein, args))]
80 : jhr 3555 | transform (y, ein as Ein.EIN{body=E.Sum(_, E.Probe _), ...}, args) =
81 : jhr 3582 [(y, IR.EINAPP(ein, args))]
82 : jhr 3555 | transform (y, Ein.EIN{params, index, body}, args) = let
83 :     val avail = AvailRHS.new()
84 :     fun filterOps (es, params, args, index, sx) = let
85 :     fun filter ([], es', params, args) = (rev es', params, args)
86 :     | filter (e::es, es', params, args) = if isOp e
87 :     then let
88 :     val (e', params', args') = lift("op1_e3", e, params, index, sx, args, avail)
89 :     in
90 : jhr 3556 filter (es, e'::es', params', args')
91 : jhr 3555 end
92 :     else filter (es, e::es', params, args)
93 :     in
94 :     filter (es, [], params, args)
95 :     end
96 :     fun rewrite (sx, exp, params, args) = (case exp
97 :     of E.Probe(E.Conv(_, [E.C _], _, []), _) =>
98 :     cut ("cut", exp, params, index, sx, args, avail, 0)
99 :     | E.Probe(E.Conv(_, [E.C _ ], _, [E.V 0]), _) =>
100 :     cut ("cut", exp, params, index, sx, args, avail, 1)
101 :     | E.Probe(E.Conv(_, [E.C _ ], _, [E.V 0, E.V 1]), _) =>
102 :     cut ("cut", exp, params, index, sx, args, avail, 2)
103 :     | E.Probe(E.Conv(_, [E.C _ ], _, [E.V 0, E.V 1, E.V 2]), _) =>
104 :     cut ("cut", exp, params, index, sx, args, avail, 3)
105 :     | E.Probe _ => lift ("probe", exp, params, index, sx, args, avail)
106 :     | E.Sum(_, E.Probe _) => lift ("probe", exp, params, index, sx, args, avail)
107 :     | E.Op1(op1, e1) => let
108 :     val (e1', params', args') = rewrite (sx, e1, params, args)
109 :     val ([e1], params', args') = filterOps ([e1'], params', args', index, sx)
110 :     in
111 :     (E.Op1(op1, e1), params', args')
112 :     end
113 :     | E.Op2(op2, e1, e2) => let
114 :     val (e1', params', args') = rewrite (sx, e1, params, args)
115 :     val (e2', params', args') = rewrite (sx, e2, params', args')
116 :     val ([e1', e2'], params', args') =
117 :     filterOps ([e1', e2'], params', args', index, sx)
118 :     in
119 :     (E.Op2(op2, e1', e2'), params', args')
120 :     end
121 :     | E.Opn(opn, es) => let
122 :     fun iter ([], es, params, args) = (List.rev es, params, args)
123 :     | iter (e::es, es', params, args) = let
124 : jhr 3556 val (e', params', args') = rewrite (sx, e, params, args)
125 : jhr 3555 in
126 :     iter (es, e'::es', params', args')
127 :     end
128 :     val (es, params, args) = iter (es, [], params, args)
129 :     val (es, params, args) = filterOps (es, params, args, index, sx)
130 :     in
131 :     (E.Opn(opn, es), params, args)
132 :     end
133 :     | E.Sum(sx1, e) => let
134 :     val (e', params', args') = rewrite (sx1@sx, e, params, args)
135 :     in
136 :     (E.Sum(sx1, e'), params', args')
137 :     end
138 :     | _ => (exp, params, args)
139 :     (* end case *))
140 :     val (body', params', args') = rewrite ([], body, params, args)
141 : jhr 3576 val einapp = CleanParams.clean (body', params', index, args')
142 : jhr 3555 in
143 : jhr 3582 List.rev ((y, einapp) :: AvailRHS.getAssignments avail)
144 : jhr 3555 end
145 :    
146 :     end

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