forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
42 lines (39 loc) · 966 Bytes
/
engine.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
package gorgonia
import (
"github.com/pkg/errors"
"gorgonia.org/tensor"
)
// StandardEngine is the default CPU engine for gorgonia
type StandardEngine struct {
tensor.StdEng
}
// Transpose tensor a according to expStrides
func (e StandardEngine) Transpose(a tensor.Tensor, expStrides []int) error {
if !a.IsNativelyAccessible() {
return errors.Errorf("Cannot Transpose() on non-natively accessible tensor")
}
size := a.DataSize()
it := a.Iterator()
var i int
switch a.Dtype() {
case tensor.Float64:
tmp := make([]float64, size)
data := a.Data().([]float64)
for next, err := it.Next(); err == nil; next, err = it.Next() {
tmp[i] = data[next]
i++
}
copy(data, tmp)
case tensor.Float32:
tmp := make([]float32, size)
data := a.Data().([]float32)
for next, err := it.Next(); err == nil; next, err = it.Next() {
tmp[i] = data[next]
i++
}
copy(data, tmp)
default:
return e.StdEng.Transpose(a, expStrides)
}
return nil
}