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 pathnative_tensor.go
83 lines (73 loc) · 2.13 KB
/
native_tensor.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
/*
** 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 (
"errors"
"reflect"
fb "github.com/google/flatbuffers/go"
graphpipefb "github.com/oracle/graphpipe-go/graphpipefb"
)
// NativeTensor is an easier to use version of the flatbuffer Tensor.
type NativeTensor struct {
Type uint8
Shape []int64
StringVals []string
Data []byte
}
// InitSimple tries to infer your data shape from the value you
// give it.
func (nt *NativeTensor) InitSimple(val interface{}) error {
v := reflect.ValueOf(val)
shape, num, size, dt, err := ShapeType(v)
nt.Shape = shape
nt.Type = dt
if nt.Type == graphpipefb.TypeString {
strs := make([]string, num)
extractStrs(v, strs)
nt.StringVals = strs
} else {
nt.Data, err = getDataSafe(v, int(num*size))
}
return err
}
// InitWithData is a more explicit initialization and expects
// the data to already be in the correct format.
func (nt *NativeTensor) InitWithData(data []byte, shape []int64, dt uint8) error {
totalBytes := 1
meta := types[dt]
for _, v := range shape {
totalBytes *= int(v)
}
totalBytes *= int(meta.size)
if totalBytes != len(data) {
return errors.New("Data length does not match shape/dtype")
}
nt.Shape = shape
nt.Type = dt
nt.Data = data
return nil
}
// InitWithStringVals is a more explicit initialization and expects
// the data to already be in the correct format (for stringvals).
func (nt *NativeTensor) InitWithStringVals(stringVals []string, shape []int64) error {
totalItems := 1
for _, v := range shape {
totalItems *= int(v)
}
if len(stringVals) != totalItems {
return errors.New("Data length does not match shape/dtype")
}
nt.Shape = shape
nt.Type = graphpipefb.TypeString
nt.StringVals = stringVals
return nil
}
// Build creates the actual flatbuffer representation.
func (nt *NativeTensor) Build(b *fb.Builder) fb.UOffsetT {
if nt.Type == graphpipefb.TypeString {
return BuildStringTensorRaw(b, nt.StringVals, nt.Shape)
}
return BuildDataTensorRaw(b, nt.Data, nt.Shape, nt.Type)
}