This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcached.go
532 lines (491 loc) · 13.1 KB
/
cached.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
/*
** Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
** Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
*/
package graphpipe
import (
"bytes"
"crypto/sha512"
"encoding/binary"
"fmt"
"sort"
"sync"
"unsafe"
"github.com/Sirupsen/logrus"
bolt "github.com/coreos/bbolt"
graphpipefb "github.com/oracle/graphpipe-go/graphpipefb"
)
const (
emptyKey string = ".empty"
tsKey string = ".typeshape"
twoGigs = 2 * 1024 * 1024 * 1024
)
// Nt is a NativeTensor holding struct.
type Nt struct {
tensor *NativeTensor
name []byte
dlen int
rows int
typeShape []byte
}
func newNt(tensor *NativeTensor, name string, chunks int) *Nt {
typeShape := make([]byte, (len(tensor.Shape)+1)*8)
binary.LittleEndian.PutUint64(typeShape[0:8], uint64(tensor.Type))
var dlen int
if tensor.Type == graphpipefb.TypeString {
dlen = len(tensor.StringVals)
} else {
dlen = len(tensor.Data)
}
dlen /= chunks
// skip the first dimenion
dims := len(tensor.Shape)
rows := 0
if dims > 0 {
rows = int(tensor.Shape[0])
rows /= chunks
binary.LittleEndian.PutUint64(typeShape[(1*8):(1+1)*8], uint64(rows))
}
for i := 1; i < dims; i++ {
o := i + 1
size := int(tensor.Shape[i])
binary.LittleEndian.PutUint64(typeShape[(o*8):(o+1)*8], uint64(size))
}
return &Nt{
tensor,
[]byte(name),
dlen,
rows,
typeShape,
}
}
func (t *Nt) data(index int) []byte {
if t.tensor.Type == graphpipefb.TypeString {
// encode strings into TensorContent style
strs := make([][]byte, len(t.tensor.StringVals))
for i := 0; i < len(t.tensor.StringVals); i++ {
strs[i] = []byte(t.tensor.StringVals[i])
}
return encodeStrs(strs)
}
rval := t.tensor.Data[index*t.dlen : (index+1)*t.dlen]
return rval
}
func (t *Nt) tensorFromIndexes(indexes []int) *NativeTensor {
dt := t.tensor.Type
shape := make([]int64, len(t.tensor.Shape))
for i := 0; i < len(t.tensor.Shape); i++ {
shape[i] = t.tensor.Shape[i]
}
if len(shape) > 0 {
shape[0] = int64(len(indexes))
}
nt := &NativeTensor{}
if dt == graphpipefb.TypeString {
stringVals := make([]string, len(indexes)*t.dlen)
for i, index := range indexes {
for j := 0; j < t.dlen; j++ {
stringVals[i*t.dlen+j] = t.tensor.StringVals[index*t.dlen+j]
}
}
nt.InitWithStringVals(stringVals, shape)
} else {
data := []byte{}
for _, i := range indexes {
data = append(data, t.data(i)...)
}
nt.InitWithData(data, shape, dt)
}
return nt
}
type hashPair struct {
index int
hash []byte
}
func getKey(inputs []*Nt, index int) []byte {
numInputs := len(inputs)
if numInputs == 0 {
return []byte(emptyKey)
}
h := sha512.New()
for i := 0; i < numInputs; i++ {
h.Write(inputs[i].name)
h.Write(inputs[i].typeShape[0:8])
// skip the batch dimension
if len(inputs[i].typeShape) > 16 {
h.Write(inputs[i].typeShape[16:])
}
h.Write(inputs[i].data(index))
}
return h.Sum(nil)
}
func dataLen(typeShape []byte) int {
dims := len(typeShape) / 8
elements := int64(1)
for i := 1; i < dims; i++ {
elements *= int64(binary.LittleEndian.Uint64(typeShape[(i * 8) : (i+1)*8]))
}
var size int
dt := binary.LittleEndian.Uint64(typeShape[0:8])
switch dt {
case graphpipefb.TypeUint8, graphpipefb.TypeInt8:
size = 1
case graphpipefb.TypeUint16, graphpipefb.TypeInt16, graphpipefb.TypeFloat16:
size = 2
case graphpipefb.TypeUint32, graphpipefb.TypeInt32, graphpipefb.TypeFloat32:
size = 4
case graphpipefb.TypeUint64, graphpipefb.TypeInt64, graphpipefb.TypeFloat64:
size = 8
case graphpipefb.TypeString:
fallthrough
default:
return -1
}
return int(elements) * size
}
func getCache(c *appContext, keys [][]byte, outputs []string) ([][][]byte, [][]byte, []bool, []bool, error) {
numOutputs := len(outputs)
data := make([][][]byte, numOutputs)
typeShape := make([][]byte, numOutputs)
incompleteOutputs := make([]bool, numOutputs)
numChunks := len(keys)
incompleteChunks := make([]bool, numChunks)
if err := c.db.View(func(tx *bolt.Tx) error {
for i := range outputs {
data[i] = make([][]byte, numChunks)
// b is only valid for the length of the transaction
bucket := tx.Bucket([]byte(outputs[i]))
var b []byte
if bucket != nil {
b = bucket.Get([]byte(tsKey))
}
if bucket == nil || b == nil {
// if we haven't set the shape then there should be
// no keys so skip retrieval
incompleteOutputs[i] = true
for j := 0; j < numChunks; j++ {
incompleteChunks[j] = true
}
} else {
typeShape[i] = make([]byte, len(b))
copy(typeShape[i], b)
dlen := dataLen(typeShape[i])
if dlen != -1 {
// make data contiguous
content := make([]byte, dlen*numChunks)
for j := 0; j < numChunks; j++ {
data[i][j] = content[j*dlen : (j+1)*dlen]
b := bucket.Get(keys[j])
if b == nil {
incompleteOutputs[i] = true
incompleteChunks[j] = true
} else {
copy(data[i][j], b)
}
}
} else {
for j := 0; j < numChunks; j++ {
b := bucket.Get(keys[j])
if b == nil {
incompleteOutputs[i] = true
incompleteChunks[j] = true
} else {
data[i][j] = make([]byte, len(b))
copy(data[i][j], b)
}
}
}
}
}
return nil
}); err != nil {
logrus.Errorf("Failed to get item from cache: %v", err)
return nil, nil, nil, nil, err
}
return data, typeShape, incompleteChunks, incompleteOutputs, nil
}
func setCache(c *appContext, keys [][]byte, outputs []string, data [][][]byte, typeShape [][]byte, missing []int) error {
if err := c.db.Update(func(tx *bolt.Tx) error {
for i := range outputs {
bucket := tx.Bucket([]byte(outputs[i]))
if bucket == nil {
var err error
bucket, err = tx.CreateBucketIfNotExists([]byte(outputs[i]))
if err != nil {
return err
}
}
b := bucket.Get([]byte(tsKey))
if b == nil {
if err := bucket.Put([]byte(tsKey), typeShape[i]); err != nil {
return err
}
}
for j := range missing {
row := missing[j]
if err := bucket.Put(keys[row], data[i][row]); err != nil {
return err
}
}
}
return nil
}); err != nil {
if err == bolt.ErrDatabaseNotOpen {
logrus.Debugf("Ingoring put cache error: %v", err)
return nil
}
logrus.Errorf("Failed to put item in cache: %v", err)
return err
}
return nil
}
func rows(inputs []*NativeTensor) int64 {
rows := int64(0)
for _, input := range inputs {
if len(input.Shape) < 1 {
return 1
}
if rows == 0 {
rows = input.Shape[0]
}
if rows != input.Shape[0] {
return 1
}
}
return rows
}
func decodeStrs(buf []byte, num int64) ([]string, error) {
reader := bytes.NewReader(buf)
lens := make([]uint64, num)
strs := make([]string, num)
for i := int64(0); i < num; i++ {
val, err := binary.ReadUvarint(reader)
if err != nil {
return nil, err
}
lens[i] = val
}
for i := int64(0); i < num; i++ {
tmp := make([]byte, lens[i])
if lens[i] == 0 {
continue
}
n, err := reader.Read(tmp)
if err != nil || uint64(n) != lens[i] {
return nil, err
}
strs[i] = string(tmp)
}
return strs, nil
}
func encodeStrs(strs [][]byte) []byte {
num := len(strs)
buf := make([]byte, binary.MaxVarintLen64*num)
n := 0
for i := 0; i < num; i++ {
n += binary.PutUvarint(buf[n:], uint64(len(strs[i])))
}
buf = buf[:n]
for i := 0; i < num; i++ {
buf = append(buf, strs[i]...)
}
return buf
}
func ntFromData(typeShape []byte, data [][]byte) (*NativeTensor, error) {
tp := &NativeTensor{}
// The first 8 bytes holds the type
tp.Type = uint8(binary.LittleEndian.Uint64(typeShape[0:8]))
dims := (len(typeShape) - 1) / 8
tp.Shape = make([]int64, dims)
for i := 0; i < dims; i++ {
o := i + 1
size := int64(binary.LittleEndian.Uint64(typeShape[(o * 8) : (o+1)*8]))
tp.Shape[i] = size
}
// update the dimension with the number of chunks
elementsPerChunk := tp.Shape[0]
tp.Shape[0] *= int64(len(data))
if tp.Type == graphpipefb.TypeString {
for i := 0; i < len(data); i++ {
strs, err := decodeStrs(data[i], elementsPerChunk)
if err != nil {
return nil, err
}
tp.StringVals = append(tp.StringVals, strs...)
}
} else {
// data is contiguous so just grab the whole thing
dataSize := len(data) * len(data[0])
if dataSize > twoGigs {
return nil, fmt.Errorf("Proto is larger than 2 Gigabytes")
}
if dataSize > 0 {
ptr := unsafe.Pointer(&data[0][0])
tp.Data = (*(*[twoGigs]byte)(ptr))[:dataSize]
}
}
return tp, nil
}
func mergeResultsWithCacheData(results []*NativeTensor, applyIndexes []int, typeShape [][]byte, missing []int, numChunks int, data [][][]byte) [][][]byte {
numApply := len(applyIndexes)
numMissing := len(missing)
for i := 0; i < numApply; i++ {
ix := applyIndexes[i]
nt := newNt(results[i], "", numMissing)
if typeShape[ix] == nil {
typeShape[ix] = nt.typeShape
dlen := dataLen(typeShape[ix])
if dlen != -1 {
// make data contiguous
content := make([]byte, dlen*numChunks)
for j := 0; j < numChunks; j++ {
data[ix][j] = content[j*dlen : (j+1)*dlen]
}
}
}
for j := 0; j < numMissing; j++ {
if data[ix][missing[j]] == nil {
data[ix][missing[j]] = nt.data(j)
} else {
copy(data[ix][missing[j]], nt.data(j))
}
}
}
return data
}
func getInputTensors(req *graphpipefb.InferRequest) ([]*NativeTensor, error) {
inputTensors := make([]*NativeTensor, req.InputTensorsLength())
for i := 0; i < req.InputTensorsLength(); i++ {
tensor := &graphpipefb.Tensor{}
if !req.InputTensors(tensor, i) {
err := fmt.Errorf("Bad input tensor #%d", i)
return nil, err
}
nt := TensorToNativeTensor(tensor)
inputTensors[i] = nt
}
return inputTensors, nil
}
func getResultsCached(c *appContext, requestContext *RequestContext, req *graphpipefb.InferRequest) ([]*NativeTensor, error) {
inputTensors, err := getInputTensors(req)
if err != nil {
return nil, err
}
numChunks := 1
if len(inputTensors) > 0 {
numChunks = int(rows(inputTensors))
// no input, so just get values as a single chunk
if numChunks == 0 {
numChunks = 1
}
logrus.Debugf("Request divides into %d chunks", numChunks)
}
inputs, err := getInputs(c, req, numChunks)
if err != nil {
return nil, err
}
keys := make([][]byte, numChunks)
ch := make(chan hashPair, numChunks)
var wg sync.WaitGroup
wg.Add(numChunks)
for i := 0; i < numChunks; i++ {
go func(i int) {
ch <- hashPair{i, getKey(inputs, i)}
wg.Done()
}(i)
}
wg.Wait()
close(ch)
for elem := range ch {
keys[elem.index] = elem.hash
}
outputNames, err := getOutputNames(c, req)
if err != nil {
return nil, err
}
if len(outputNames) == 0 {
outputNames = append(outputNames, c.defaultOutputs...)
}
for i := range outputNames {
n := 0
if outputNames[i] == "" {
outputNames[i] = c.defaultOutputs[n]
n++
}
}
data, typeShape, incompleteChunks, incompleteOutputs, err := getCache(c, keys, outputNames)
if err != nil {
logrus.Errorf("Failed to get cached data: %v", err)
return nil, err
}
missing := []int{}
for i := 0; i < numChunks; i++ {
if incompleteChunks[i] {
missing = append(missing, i)
}
}
numOutputs := len(outputNames)
applyIndexes := []int{}
for i := 0; i < numOutputs; i++ {
if incompleteOutputs[i] {
applyIndexes = append(applyIndexes, i)
}
}
numMissing := len(missing)
numApply := len(applyIndexes)
logrus.Debugf("%d rows must be calculated", numMissing)
if numMissing == 0 {
logrus.Infof("Skipping apply because everything is cached")
} else if numApply == 0 {
logrus.Infof("Skipping apply because no outputs requested")
} else {
applyInputs := map[string]*NativeTensor{}
for i := 0; i < len(inputs); i++ {
applyInputs[string(inputs[i].name)] = inputs[i].tensorFromIndexes(missing)
}
results, err := c.apply(requestContext, string(req.Config()), applyInputs, outputNames)
if err != nil {
logrus.Errorf("Apply failed: %v", err)
return nil, err
}
data = mergeResultsWithCacheData(results, applyIndexes, typeShape, missing, numChunks, data)
// set cache async so we can complete the request
go func() {
if err := setCache(c, keys, outputNames, data, typeShape, missing); err != nil {
logrus.Errorf("Failed to set cache: %v", err)
}
}()
}
outputNts := make([]*NativeTensor, numOutputs)
for i := 0; i < numOutputs; i++ {
outputNts[i], err = ntFromData(typeShape[i], data[i])
if err != nil {
logrus.Errorf("Failed to create native tensor: %v", err)
}
}
return outputNts, nil
}
type byName []*Nt
func (a byName) Len() int { return len(a) }
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byName) Less(i, j int) bool { return bytes.Compare(a[i].name, a[j].name) < 0 }
func getInputs(c *appContext, req *graphpipefb.InferRequest, numChunks int) ([]*Nt, error) {
inputs := make([]*Nt, req.InputTensorsLength())
for i := 0; i < req.InputTensorsLength(); i++ {
tensor := &graphpipefb.Tensor{}
if !req.InputTensors(tensor, i) {
return nil, fmt.Errorf("Could not init tensor")
}
nt := TensorToNativeTensor(tensor)
name := ""
if i < req.InputNamesLength() {
name = string(req.InputNames(i))
}
if name == "" {
name = c.defaultInputs[i]
}
inputs[i] = newNt(nt, name, numChunks)
}
sort.Sort(byName(inputs))
return inputs, nil
}