diff options
author | Asim Shankar <ashankar@google.com> | 2016-11-18 10:15:20 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2016-11-18 10:25:04 -0800 |
commit | 86e348e59ffe6529c150bb2b3c0ce4bc1fcec5d2 (patch) | |
tree | b24be6e8491db4af8be2e910ac09d63bd05591be /tensorflow/go/tensor_test.go | |
parent | e4daca83dc28e6baf84cd01fc129d348a60284e6 (diff) |
Go: Cut memory footprint of Go type -> Tensor conversion by 1/2.
When converting a Go type to the raw bytes for a Tensor representation,
encode directly into the C memory of the Tensor instead of first filling
in the content in a buffer in Go memory and then memcpy()ing it to C.
Added a benchmark, results from:
go test -bench BenchmarkNewTensor -benchmem -benchtime=5s
BEFORE: 19042821 ns/op 3444744 B/op 150547 allocs/op
AFTER: 14478480 ns/op 1205597 B/op 150531 allocs/op
Note the total bytes/op is cut down considerably.
The total number of allocs/op is ridiculously high though
and I suspect this is because encoding/binary.Write is doing
one allocation per call (explaining 150528 of the 150531 allocations
for the benchmark vector). Perhaps that will be dealt with when
someone feels its pain.
Another step towards #10
Change: 139593834
Diffstat (limited to 'tensorflow/go/tensor_test.go')
-rw-r--r-- | tensorflow/go/tensor_test.go | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/tensorflow/go/tensor_test.go b/tensorflow/go/tensor_test.go index 29562e2048..d5f3f74bfd 100644 --- a/tensorflow/go/tensor_test.go +++ b/tensorflow/go/tensor_test.go @@ -24,17 +24,17 @@ func TestNewTensor(t *testing.T) { shape []int64 value interface{} }{ - {[]int64{}, int8(5)}, - {[]int64{}, int16(5)}, - {[]int64{}, int32(5)}, - {[]int64{}, int64(5)}, - {[]int64{}, int64(5)}, - {[]int64{}, uint8(5)}, - {[]int64{}, uint16(5)}, - {[]int64{}, float32(5)}, - {[]int64{}, float64(5)}, - {[]int64{}, complex(float32(5), float32(6))}, - {[]int64{}, complex(float64(5), float64(6))}, + {nil, int8(5)}, + {nil, int16(5)}, + {nil, int32(5)}, + {nil, int64(5)}, + {nil, int64(5)}, + {nil, uint8(5)}, + {nil, uint16(5)}, + {nil, float32(5)}, + {nil, float64(5)}, + {nil, complex(float32(5), float32(6))}, + {nil, complex(float64(5), float64(6))}, {[]int64{1}, []float64{1}}, {[]int64{1}, [1]float64{1}}, {[]int64{3, 2}, [][]float64{{1, 2}, {3, 4}, {5, 6}}}, @@ -61,7 +61,7 @@ func TestNewTensor(t *testing.T) { uint64(5), []uint64{5}, // Mismatched dimensions - [][]float32{{1,2,3},{4}}, + [][]float32{{1, 2, 3}, {4}}, } for _, test := range tests { @@ -94,3 +94,21 @@ func TestNewTensor(t *testing.T) { } } } + +func benchmarkNewTensor(b *testing.B, v interface{}) { + for i := 0; i < b.N; i++ { + if t, err := NewTensor(v); err != nil || t == nil { + b.Fatalf("(%v, %v)", t, err) + } + } +} + +func BenchmarkNewTensor(b *testing.B) { + var ( + // Some sample sizes from the Inception image labeling model. + // Where input tensors correspond to a 224x224 RGB image + // flattened into a vector. + vector [224 * 224 * 3]int32 + ) + b.Run("[150528]", func(b *testing.B) { benchmarkNewTensor(b, vector) }) +} |