-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
dense_matop_memmove.go
134 lines (113 loc) · 3.72 KB
/
dense_matop_memmove.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
package tensor
import "github.com/pkg/errors"
// This file contains code pertaining to tensor operations that actually move memory
// Transpose() actually transposes the data.
// This is a generalized version of the inplace matrix transposition algorithm from Wikipedia:
// https://en.wikipedia.org/wiki/In-place_matrix_transposition
func (t *Dense) Transpose() error {
// if there is no oldinfo, that means the current info is the latest, and not the transpose
if t.old.IsZero() {
return nil
}
if t.IsScalar() {
return nil // cannot transpose scalars - no data movement
}
defer func() {
t.old.zero()
t.transposeWith = nil
}()
expShape := t.Shape()
// important! because the strides would have changed once the underlying data changed
var expStrides []int
if t.AP.o.IsColMajor() {
expStrides = expShape.CalcStridesColMajor()
} else {
expStrides = expShape.CalcStrides()
}
defer ReturnInts(expStrides)
defer func() {
copy(t.AP.strides, expStrides) // dimensions do not change, so it's actually safe to do this
t.sanity()
}()
if t.IsVector() {
// no data movement
return nil
}
// actually move data
var e Engine = t.e
transposer, ok := e.(Transposer)
if !ok {
return errors.Errorf("Engine does not support Transpose()")
}
return transposer.Transpose(t, expStrides)
}
// Repeat is like Numpy's repeat. It repeats the elements of an array.
// The repeats param defines how many times each element in the axis is repeated.
// Just like NumPy, the repeats param is broadcasted to fit the size of the given axis.
func (t *Dense) Repeat(axis int, repeats ...int) (retVal Tensor, err error) {
e := t.Engine()
if rp, ok := e.(Repeater); ok {
return rp.Repeat(t, axis, repeats...)
}
return nil, errors.New("Engine does not support Repeat")
}
// Concat concatenates the other tensors along the given axis. It is like Numpy's concatenate() function.
func (t *Dense) Concat(axis int, Ts ...*Dense) (retVal *Dense, err error) {
e := t.Engine()
if c, ok := e.(Concater); ok {
var ret Tensor
others := densesToTensors(Ts)
if ret, err = c.Concat(t, axis, others...); err != nil {
return nil, errors.Wrapf(err, opFail, "Concat")
}
return ret.(*Dense), nil
}
return nil, errors.New("Engine does not support Concat")
}
// Hstack stacks other tensors columnwise (horizontal stacking)
func (t *Dense) Hstack(others ...*Dense) (*Dense, error) {
// check that everything is at least 1D
if t.Dims() == 0 {
return nil, errors.Errorf(atleastDims, 1)
}
for _, d := range others {
if d.Dims() < 1 {
return nil, errors.Errorf(atleastDims, 1)
}
}
if t.Dims() == 1 {
return t.Concat(0, others...)
}
return t.Concat(1, others...)
}
// Vstack stacks other tensors rowwise (vertical stacking). Vertical stacking requires all involved Tensors to have at least 2 dimensions
func (t *Dense) Vstack(others ...*Dense) (*Dense, error) {
// check that everything is at least 2D
if t.Dims() < 2 {
return nil, errors.Errorf(atleastDims, 2)
}
for _, d := range others {
if d.Dims() < 2 {
return nil, errors.Errorf(atleastDims, 2)
}
}
return t.Concat(0, others...)
}
// Stack stacks the other tensors along the axis specified. It is like Numpy's stack function.
func (t *Dense) Stack(axis int, others ...*Dense) (retVal *Dense, err error) {
var ret DenseTensor
var ok bool
if ret, err = t.stackDense(axis, densesToDenseTensors(others)...); err != nil {
return nil, err
}
if retVal, ok = ret.(*Dense); !ok {
return nil, errors.Errorf("Return not *Dense")
}
return
}
func (t *Dense) stackDense(axis int, others ...DenseTensor) (retVal DenseTensor, err error) {
if ds, ok := t.Engine().(DenseStacker); ok {
return ds.StackDense(t, axis, others...)
}
return nil, errors.Errorf("Engine does not support DenseStacker")
}