forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.go
357 lines (305 loc) · 9.88 KB
/
compile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package gorgonia
import (
"encoding/csv"
"fmt"
"io"
"github.com/pkg/errors"
)
// This file deals with the compilation from a expression graph into a program
// that is executed by an interpreter
// Compile takes a graph and outputs a program suitable for *tapeMachine to run
func Compile(g *ExprGraph) (prog *program, locMap map[*Node]register, err error) {
compileLogf("Compiling")
enterLoggingContext()
defer leaveLoggingContext()
compileLogf("sorting")
var sortedNodes Nodes
if sortedNodes, err = Sort(g); err != nil {
return nil, nil, errors.Wrap(err, sortFail)
}
inputs := g.Inputs()
df := analyze(g, sortedNodes)
df.intervals = buildIntervals(sortedNodes)
ra := newRegalloc(df)
ra.alloc(sortedNodes)
compileLogf("Intervals: %+v", FmtNodeMap(df.intervals))
logCompileState(g.name, g, df)
cg := newCodeGenerator(inputs, sortedNodes, df)
prog, locMap = cg.gen()
// prog, locMap = codegen(inputs, sortedNodes, df)
prog.locs = ra.count
prog.df = df
prog.g = g
prog.sorted = sortedNodes
return
}
// CompileFunction takes a graph, subsets it based on the input and output nodes provided and outputs a program suitable for *tapeMachine to run.
// It is analogous to theano.Function().
func CompileFunction(g *ExprGraph, inputs, outputs Nodes) (prog *program, locMap map[*Node]register, err error) {
compileLogf("CompileFunctionNEW. Inputs: %d; outputs: %d", inputs, outputs)
enterLoggingContext()
defer leaveLoggingContext()
subgraph := g.SubgraphRoots(outputs...)
var unused Nodes
for _, in := range inputs {
if !subgraph.all.Contains(in) {
unused = append(unused, in)
}
}
if len(unused) > 0 {
return nil, nil, errors.Errorf("Not all the inputs are used: %v", unused)
}
var sortedNodes Nodes
if sortedNodes, err = Sort(g); err != nil {
return nil, nil, errors.Wrap(err, sortFail)
}
df := analyze(subgraph, sortedNodes)
df.intervals = buildIntervals(sortedNodes)
ra := newRegalloc(df)
ra.alloc(sortedNodes)
cg := newCodeGenerator(inputs, sortedNodes, df)
prog, locMap = cg.gen()
// prog, locMap = codegen(inputs, sortedNodes, df)
prog.locs = ra.count
prog.df = df
prog.g = g
prog.sorted = sortedNodes
return
}
// codgenerator holds the state for the code generation process
type codegenerator struct {
locMap map[*Node]register
lastWrites map[int]int
flushed map[int]struct{}
instrMap map[*Node]fragment
queue []int // queue to flus
g *ExprGraph
inputs, sorted Nodes
df *dataflow
instructions fragment
}
func newCodeGenerator(inputs, sorted Nodes, df *dataflow) *codegenerator {
return &codegenerator{
locMap: make(map[*Node]register),
lastWrites: make(map[int]int),
flushed: make(map[int]struct{}),
instrMap: make(map[*Node]fragment),
g: inputs[0].g,
inputs: inputs,
sorted: sorted,
df: df,
}
}
func (cg *codegenerator) addInstr(node *Node, instr tapeInstr) {
if instrs := cg.instrMap[node]; instrs != nil {
instrs = append(instrs, instr)
cg.instrMap[node] = instrs
} else {
cg.instrMap[node] = fragment{instr}
}
}
// every time an instruction is added to the list of instructions,
// also add the instructionID and the register the instruction writes to.
// This helps with determining if a flushInstruction needs to be issued.
func (cg *codegenerator) updateLastWrites(id int) {
cg.lastWrites[id] = len(cg.instructions) - 1
}
func (cg *codegenerator) flush() {
for _, instrID := range cg.queue {
cg.flushed[instrID] = struct{}{}
}
cg.queue = cg.queue[:0]
}
func (cg *codegenerator) addArg(node *Node, interv *interval) {
compileLogf("LoadArg: %x", node.ID())
writeTo := interv.result
cg.locMap[node] = writeTo
instr := loadArg{
// index: index,
index: node.ID(),
writeTo: writeTo,
}
cg.instructions = append(cg.instructions, instr)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo.id)
}
func (cg *codegenerator) addStmt(node *Node, interv *interval) {
compileLogf("Statement")
writeTo := interv.result
switch op := node.op.(type) {
case letOp:
// there should be only 2 chilren
if len(node.children) != 2 {
panic("Expected only two children")
}
compileLogf("node.children %d. [1]: %v; [0]: %v", node.ID(), node.children[1], node.children[0])
compileLogf("node isInput %v", node.isInput())
from := cg.df.intervals[node.children[1]].result
to := cg.df.intervals[node.children[0]].result
instr := letInstr{
readFrom: from,
writeTo: to,
}
cg.instructions = append(cg.instructions, instr)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo.id)
case readOp:
// there should be only 1 child
if len(node.children) != 1 {
panic("Expected only one child")
}
compileLogf("node.children %d. [0]: %v", node.ID(), node.children[0])
compileLogf("node isInput %v", node.isInput())
from := cg.df.intervals[node.children[0]].result
instr := readInstr{
into: op.into,
readFrom: from,
}
cg.instructions = append(cg.instructions, instr)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo.id)
}
}
func (cg *codegenerator) addNode(node, replacement *Node, interv *interval, i int) {
compileLogf("Expr")
compileLogf("Node: %x %v", node.ID(), node.op)
compileLogf("interval %v", interv)
writeTo := interv.result
var reads []register
for _, child := range node.children {
cReplacement := cg.df.replacements[child]
cInterv := cg.df.intervals[cReplacement]
reads = append(reads, cInterv.result)
}
enterLoggingContext()
defer leaveLoggingContext()
var prealloc bool
var useUnsafe bool
// if it's not mutable, there is no chance it will be overwritten
if node.isMutable() {
// if the instruction calls an extern (cBLAS or cuBlas), then we should preallocate the vector
if node.op.CallsExtern() {
compileLogf("calls extern")
var instr alloc
// if i == 0 {
// // if the instruction is the last instruction, we STILL want to allocate to a new register
// // if this clause is not here, the last instruction will allocate to an existing register, and overwrites any val
// writeTo = register{device: writeTo.device, id: writeTo.id + 1}
// }
instr = newAlloc(node, writeTo)
cg.instructions = append(cg.instructions, instr)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo.id)
prealloc = true
cg.queue = append(cg.queue, len(cg.instructions)) // no -1.
}
// check if any previously buffered cBLAS or cuBLAS calls need to be flushed
// it doesn't matter if the machine isn't using a batchedBLAS. flushInstr would just be a no-op at runtime
for _, read := range reads {
if instrID, ok := cg.lastWrites[read.id]; ok {
viaticum := cg.instructions[instrID] // ;) - it IS on the way
if instr, ok := viaticum.(execOp); ok {
if instr.op.CallsExtern() && !node.op.CallsExtern() {
// the && bit is to make sure that if we have sequential cBLAS/cuBLAS calls,
// we just add it to the batch.
// sequential in this can mean several instructions apart. For example:
// 4 A × B ; read %2 ; write to %3
// ⋮ (doesn't use %3 or %10)
// ⋮
// 10 Aᵀ × B ; read %3 ; write to %10
// ⋮ (doesn't use %3, or %10)
// ⋮
// 12 + ; read %10 ; write to %12
//
// It is before instruction 12 that the flush will be added. 5 and 10 are considered sequential
if _, ok := cg.flushed[instrID]; !ok {
cg.instructions = append(cg.instructions, flushInstr{})
cg.addInstr(node, flushInstr{})
cg.updateLastWrites(-1) // flush doesn't write to any register
cg.flush()
}
}
}
}
}
// check the overwrites - if the overwrite and the resulting register is the same,
// then use unsafe options when available
overwrites := node.op.OverwritesInput()
if overwrites >= 0 {
compileLogf("Overwrites %d", overwrites)
overwritten := reads[overwrites]
compileLogf("NodeID:%d overwritten: %v, reads: %v, interval: %v", node.ID(), overwritten, interv.reads, interv.result)
if overwritten == interv.result {
compileLogf("Use unsafe")
useUnsafe = true
}
}
}
cg.locMap[node] = writeTo
// otherwise, the replacement has already been written
if node == replacement {
compileLogf("New Exec Op")
instr := newExecOp(node)
instr.readFrom = reads
instr.writeTo = writeTo
instr.preAllocated = prealloc
instr.useUnsafe = useUnsafe
cg.instructions = append(cg.instructions, instr)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo.id)
}
}
func (cg *codegenerator) gen() (*program, map[*Node]register) {
for i := len(cg.sorted) - 1; i >= 0; i-- {
node := cg.sorted[i]
replacement := cg.df.replacements[node]
compileLogf("Working on %x. Replacement: %x", node.ID(), replacement.ID())
nInterv := cg.df.intervals[replacement]
switch {
case node.isArg():
cg.addArg(node, nInterv)
case node.isStmt:
cg.addStmt(node, nInterv)
default:
cg.addNode(node, replacement, nInterv, i)
}
}
return &program{
instructions: cg.instructions,
args: len(cg.inputs),
g: cg.g,
m: cg.instrMap,
}, cg.locMap
}
func compileState(w io.Writer, g *ExprGraph, df *dataflow) {
header := []string{
"ID", "Op", "Type", "Register", "Interval", "Used By", "Uses", "Overwrites", "BLAS?",
}
var rows [][]string
for _, n := range g.AllNodes() {
interv := df.intervals[n]
row := make([]string, len(header))
row[0] = fmt.Sprintf("%d", n.ID())
row[2] = fmt.Sprintf("%s", n.t)
row[3] = fmt.Sprintf("%s", interv.result)
row[4] = fmt.Sprintf("%d - %d", interv.start, interv.end)
row[5] = fmt.Sprintf("%v", interv.usePositions)
row[6] = fmt.Sprintf("%d", n.children)
if n.op != nil {
row[1] = fmt.Sprintf("%s", n.op)
overwrites := n.op.OverwritesInput()
if overwrites >= 0 {
row[7] = fmt.Sprintf("%d", n.children[overwrites].ID())
}
if n.op.CallsExtern() {
row[8] = "yes"
}
}
rows = append(rows, row)
}
cw := csv.NewWriter(w)
cw.Comma = ';'
// TODO: Check errors on writes here.
cw.Write(header)
cw.WriteAll(rows)
}