-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathcompile.go
676 lines (590 loc) · 17.6 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
package gorgonia
import (
"encoding/csv"
"fmt"
"io"
"github.com/pkg/errors"
"gorgonia.org/tensor"
)
// 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")
enterLogScope()
defer leaveLogScope()
switch {
case len(g.AllNodes()) == 0:
err = errors.Errorf("Cannot compile an empty graph")
return
case g.Inputs().Len() == 0:
err = errors.Errorf("Cannot compile a graph that has no input nodes")
return
}
compileLogf("sorting")
var sortedNodes Nodes
if sortedNodes, err = Sort(g); err != nil {
return nil, nil, errors.Wrap(err, sortFail)
}
reverseNodes(sortedNodes)
df := analyze(g, sortedNodes)
sortedNodes = df.insertDeviceInstr(sortedNodes)
df.buildIntervals(sortedNodes)
ra := newRegalloc(df)
ra.alloc(sortedNodes)
// debug related stuff
df.debugIntervals(sortedNodes)
logCompileState(g.name, g, df)
inputs := g.Inputs()
cg := newCodeGenerator(inputs, sortedNodes, df)
prog, locMap = cg.gen()
prog.cpulocs = ra.cpucount
prog.gpulocs = ra.gpucount
prog.cpumem = cg.cpumem
prog.gpumem = cg.gpumem
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().
// If some input nodes are not used or is not reachable, this function will return an error
func CompileFunction(g *ExprGraph, inputs, outputs Nodes) (prog *program, locMap map[*Node]register, err error) {
compileLogf("CompileFunctionNEW. Inputs: %d; outputs: %d", inputs, outputs)
enterLogScope()
defer leaveLogScope()
subgraph := g.ExactSubgraphRoots(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(subgraph); err != nil {
return nil, nil, errors.Wrap(err, sortFail)
}
reverseNodes(sortedNodes)
df := analyze(subgraph, sortedNodes)
sortedNodes = df.insertDeviceInstr(sortedNodes)
df.buildIntervals(sortedNodes)
ra := newRegalloc(df)
ra.alloc(sortedNodes)
cg := newCodeGenerator(inputs, sortedNodes, df)
prog, locMap = cg.gen()
prog.cpulocs = ra.cpucount
prog.gpulocs = ra.gpucount
prog.df = df
prog.g = subgraph
prog.sorted = sortedNodes
return
}
// codgenerator holds the state for the code generation process
type codegenerator struct {
locMap map[*Node]register
lastWrites map[register]*Node
flushed map[int]struct{}
allocated map[register]struct{}
freed map[register]struct{}
deferFree map[register]struct{}
instrMap map[*Node]fragment
queue []int // queue to flush
lastReads map[register]int
cpumem int64
gpumem []int64
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[register]*Node),
flushed: make(map[int]struct{}),
allocated: make(map[register]struct{}),
freed: make(map[register]struct{}),
deferFree: make(map[register]struct{}),
instrMap: make(map[*Node]fragment),
lastReads: make(map[register]int),
g: inputs[0].g,
inputs: inputs,
sorted: sorted,
df: df,
}
}
// addInstr adds the instruction to the associated node in the instrMap.
// when we add instructions to the node map, we also try to determine the size of the allocations required
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}
}
var dt tensor.Dtype
var err error
switch inst := instr.(type) {
case loadArg:
if dt, err = dtypeOf(node.t); err != nil {
panic(err)
}
d := instr.writes().device
if d != CPU {
if len(cg.gpumem) < int(d)+1 {
diff := int(d) + 1 - len(cg.gpumem)
cg.gpumem = append(cg.gpumem, make([]int64, diff)...)
}
}
switch d {
case CPU:
cg.cpumem += calcMemSize(dt, node.Shape())
default:
cg.gpumem[int(d)] += calcMemSize(dt, node.Shape())
}
case alloc:
if dt, err = dtypeOf(inst.t); err != nil {
panic(err)
}
d := instr.writes().device
if d != CPU {
if len(cg.gpumem) < int(d)+1 {
diff := int(d) + 1 - len(cg.gpumem)
cg.gpumem = append(cg.gpumem, make([]int64, diff)...)
}
}
switch d {
case CPU:
cg.cpumem += calcMemSize(dt, inst.s)
default:
cg.gpumem[int(d)] += calcMemSize(dt, inst.s)
}
case *execOp:
if !inst.op.ReturnsPtr() {
d := instr.writes().device
if d != CPU {
if len(cg.gpumem) < int(d)+1 {
diff := int(d) + 1 - len(cg.gpumem)
cg.gpumem = append(cg.gpumem, make([]int64, diff)...)
}
}
switch d {
case CPU:
cg.cpumem += inst.size
default:
cg.gpumem[int(d)] += inst.size
}
}
default:
// panic("EHLP")
}
}
// 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(reg register, n *Node) {
cg.lastWrites[reg] = n
}
func (cg *codegenerator) flush() {
compileLogf("Flushing")
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,
name: node.Name(),
}
// cg.instructions = append(cg.instructions, instr)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo, node)
}
func (cg *codegenerator) addStmt(node *Node, interv *interval, i int) {
compileLogf("Add Statement")
enterLogScope()
defer leaveLogScope()
writeTo := interv.result
var children Nodes
var ok bool
if children, ok = cg.df.devTransChildren[node]; !ok {
children = node.children
}
switch op := node.op.(type) {
case letOp:
// there should be only 2 chilren
if len(children) != 2 {
panic("Expected only two children")
}
compileLogf("node.children %d. [1]: %v; [0]: %v", node.ID(), children[1], children[0])
compileLogf("node isInput %v", node.isInput())
from := cg.df.intervals[children[1]].result
to := cg.df.intervals[children[0]].result
instr := letInstr{
readFrom: from,
writeTo: to,
}
// cg.instructions = append(cg.instructions, instr)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo, node)
case readOp:
// there should be only 1 child
if len(children) != 1 {
panic("Expected only one child")
}
compileLogf("node.children %d. [0]: %v", node.ID(), children[0])
compileLogf("node isInput %v", node.isInput())
compileLogf("node.children[0] Type %v, shape %v", children[0].t, children[0].shape)
if _, ok := cg.flushed[i]; !ok {
cg.addInstr(node, flushInstr{})
cg.flush()
}
from := cg.df.intervals[children[0]].result
instr := &readInstr{
into: op.into,
readFrom: from,
t: children[0].t,
s: children[0].shape,
}
// cg.instructions = append(cg.instructions, instr)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo, node)
case devTrans:
if _, ok := cg.allocated[writeTo]; !ok {
// insert new alloc
var instr alloc
instr = newAlloc(node, writeTo)
// cg.instructions = append(cg.instructions, instr)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo, node)
cg.queue = append(cg.queue, i)
cg.allocated[writeTo] = struct{}{}
}
compileLogf("devTrans")
if len(children) != 1 {
panic("Expected only one child")
}
from := cg.df.intervals[children[0]].result
to := cg.df.intervals[node].result
instr := deviceTransport{
from: from, to: to,
}
cg.addInstr(node, instr)
if op.from != CPU && op.to == CPU {
instrID := cg.sorted.index(op.toNode)
if _, ok := cg.flushed[instrID]; !ok {
// cg.instructions = append(cg.instructions, flushInstr{})
cg.addInstr(node, flushInstr{})
cg.flush()
}
}
cg.updateLastWrites(writeTo, node)
}
}
func (cg *codegenerator) addNode(node, replacement *Node, interv *interval, i int) {
compileLogf("AddNode: %x %v", node.ID(), node.op)
compileLogf("interval %v", interv)
enterLogScope()
defer leaveLogScope()
writeTo := interv.result
var reads []register
var children Nodes
var ok bool
if children, ok = cg.df.devTransChildren[node]; !ok {
children = node.children
}
for _, child := range children {
cReplacement := cg.df.replacements[child]
cInterv := cg.df.intervals[cReplacement]
reads = append(reads, cInterv.result)
}
enterLogScope()
defer leaveLogScope()
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")
if _, ok := cg.allocated[writeTo]; !ok {
compileLogf("Inserting new alloc")
var instr alloc
instr = newAlloc(node, writeTo)
cg.addInstr(node, instr)
cg.updateLastWrites(writeTo, node)
prealloc = true
cg.queue = append(cg.queue, i)
cg.allocated[writeTo] = struct{}{}
}
}
}
compileLogf("Node Reads %v", reads)
// 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 lastWriteNode, ok := cg.lastWrites[read]; ok {
instrID := cg.sorted.index(lastWriteNode)
var op Op
var onDev, nodeOnDev Device
_, isDevTrans := lastWriteNode.Op().(devTrans)
switch {
case lastWriteNode.isArg(), lastWriteNode.isStmt && !isDevTrans:
continue
default:
op = lastWriteNode.op
}
switch op.(type) {
case CUDADoer:
onDev = Device(0)
case CLDoer:
onDev = Device(0)
default:
onDev = CPU
}
switch node.op.(type) {
case CUDADoer:
nodeOnDev = Device(0)
case CLDoer:
nodeOnDev = Device(0)
default:
nodeOnDev = CPU
}
// if we have sequential Extern 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. 4 and 10 are considered sequential
//
// It is not sequential when both are not the same devices
switch {
case !op.CallsExtern():
compileLogf("ToFlush: Node doesn't call extern. NO FLUSH")
// op doesn't call extern... don't bother flushing
case op.CallsExtern() && node.op.CallsExtern() && onDev == nodeOnDev && !isDevTrans:
compileLogf("ToFlush: Both calls extern, both same device. NO FLUSH")
// same device, both calls extern
// no flush needed
case op.CallsExtern() && node.op.CallsExtern() && onDev != nodeOnDev:
compileLogf("ToFlush: Differing devices")
// different devices, both calls extern
// flush needed
fallthrough
case op.CallsExtern() && !node.op.CallsExtern():
compileLogf("ToFlush: Node requires value immediately")
// node is gonna use the value immediately
// flush needed
fallthrough
default:
compileLogf("ToFlush: FLUSH")
if _, ok := cg.flushed[instrID]; !ok {
// cg.instructions = append(cg.instructions, flushInstr{})
cg.addInstr(node, flushInstr{})
cg.flush()
}
}
// viaticum := cg.instructions[instrID] // ;) - it IS on the way
// if instr, ok := viaticum.(*execOp); ok {
// if op.CallsExtern() && !node.op.CallsExtern() {
// }
// }
}
// 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: %v", node.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, node)
}
}
func (cg *codegenerator) insertFree(instrID int, node *Node) {
compileLogf("Inserting Free for instrID %d | instr: %v | op: %v", instrID, node, node.op)
enterLogScope()
defer leaveLogScope()
var reads []register
var children Nodes
var ok bool
if children, ok = cg.df.devTransChildren[node]; !ok {
children = node.children
}
for _, child := range children {
cReplacement := cg.df.replacements[child]
cInterv := cg.df.intervals[cReplacement]
reads = append(reads, cInterv.result)
}
compileLogf("reads %v", reads)
// check if anything needs to be freed
for _, read := range reads {
var readNode *Node
for n, reg := range cg.locMap {
if reg == read {
if readNode == nil {
readNode = n
continue
}
if readNode.id < n.id {
readNode = n
}
}
}
// interv := cg.df.intervals[readNode]
readRepl := cg.df.replacements[readNode]
if readRepl == nil {
readRepl = readNode
}
if readRepl == nil {
continue
}
interv := cg.df.intervals[readRepl]
compileLogf("interv for readRepl %v: %v", readRepl, interv)
lastUse := interv.lastUse()
compileLogf("Interval: %v; read: %v; Read Node %v; Op %v; LastUse %v; Instrid: %v", interv, read, readNode, readNode.op, lastUse, instrID)
if lastUse >= 0 && lastUse <= instrID && read.device != CPU {
if _, ok := cg.freed[read]; !ok {
compileLogf("Adding Free %v. LastUse %d", read, interv.lastUse())
// cg.instructions = append(cg.instructions, free{read})
cg.addInstr(node, free{read})
cg.freed[read] = struct{}{}
}
}
}
write := cg.locMap[node]
repl := cg.df.replacements[node]
interv := cg.df.intervals[repl]
compileLogf("Node %v | write %v | Last Use %v | %v", node, write, interv.lastUse(), node.isRoot())
if interv.lastUse() == -1 || interv.lastUse() >= len(cg.sorted) {
// if node.isRoot() {
cg.deferFree[write] = struct{}{}
// return
// }
// otherwise, it's essentially a NOOP, so we free the memory immediately after the Op is executed
// TODO: do NO-OP optimizations
// if _, ok := cg.freed[write]; !ok {
// compileLogf("Adding Free %v. Last Use %d", write, interv.lastUse())
// cg.addInstr(node, free{write})
// cg.freed[write] = struct{}{}
// }
}
}
func (cg *codegenerator) insertLastFrees() int {
node := cg.sorted[len(cg.sorted)-1]
var instructionsAdded int
for reg := range cg.deferFree {
if _, ok := cg.freed[reg]; !ok {
compileLogf("Adding Free %v to the final instruction", reg)
cg.addInstr(node, free{reg})
instructionsAdded++
}
}
return instructionsAdded
}
func (cg *codegenerator) gen() (*program, map[*Node]register) {
compileLogf("Generating from SORTED: %v", cg.sorted)
enterLogScope()
defer leaveLogScope()
for i, node := range cg.sorted {
// 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, i)
default:
cg.addNode(node, replacement, nInterv, i)
}
}
var instructionCount int
for i := len(cg.sorted) - 1; i >= 0; i-- {
node := cg.sorted[i]
cg.insertFree(i, node)
instructionCount += len(cg.instrMap[node])
}
instructionCount += cg.insertLastFrees()
r := make(map[int64]*Node)
cg.instructions = make(fragment, 0, instructionCount)
for _, node := range cg.sorted {
instrs := cg.instrMap[node]
cg.instructions = append(cg.instructions, instrs...)
for _, in := range instrs {
r[in.ID()] = node
}
}
return &program{
instructions: cg.instructions,
args: len(cg.inputs),
g: cg.g,
m: cg.instrMap,
r: r,
}, 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)
}