• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gorgonia / tensor / c35555fa6dc585780eef107d742faf55ba7c2c21

09 Apr 2024 01:50AM UTC coverage: 21.6% (+0.04%) from 21.565%
c35555fa6dc585780eef107d742faf55ba7c2c21

push

github

web-flow
Fix #140 (#141)

* Fix #140

+ Fix SortIndex()
+ Add SortIndexStable()

* `any` is not supported in Go1.15

---------

Co-authored-by: Chewxy <chewxy@gmail.com>

20 of 25 new or added lines in 1 file covered. (80.0%)

33 existing lines in 3 files now uncovered.

13186 of 61046 relevant lines covered (21.6%)

15821.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.14
/defaultenginefloat64.go
1
package tensor
2

3
import (
4
        "github.com/pkg/errors"
5
        "gorgonia.org/tensor/internal/execution"
6
        "gorgonia.org/tensor/internal/storage"
7

8
        "gorgonia.org/vecf64"
9
)
10

11
func handleFuncOptsF64(expShape Shape, o DataOrder, opts ...FuncOpt) (reuse DenseTensor, safe, toReuse, incr bool, err error) {
826✔
12
        fo := ParseFuncOpts(opts...)
826✔
13

826✔
14
        reuseT, incr := fo.IncrReuse()
826✔
15
        safe = fo.Safe()
826✔
16
        toReuse = reuseT != nil
826✔
17

826✔
18
        if toReuse {
904✔
19
                var ok bool
78✔
20
                if reuse, ok = reuseT.(DenseTensor); !ok {
78✔
21
                        returnOpOpt(fo)
×
22
                        err = errors.Errorf("Cannot reuse a different type of Tensor in a *Dense-Scalar operation. Reuse is of %T", reuseT)
×
23
                        return
×
24
                }
×
25
                if reuse.len() != expShape.TotalSize() && !expShape.IsScalar() {
78✔
26
                        returnOpOpt(fo)
×
27
                        err = errors.Errorf(shapeMismatch, reuse.Shape(), expShape)
×
28
                        err = errors.Wrapf(err, "Cannot use reuse: shape mismatch")
×
29
                        return
×
30
                }
×
31

32
                if !incr && reuse != nil {
111✔
33
                        reuse.setDataOrder(o)
33✔
34
                        // err = reuse.reshape(expShape...)
33✔
35
                }
33✔
36

37
        }
38
        returnOpOpt(fo)
826✔
39
        return
826✔
40
}
41

42
func prepDataVSF64(a Tensor, b interface{}, reuse Tensor) (dataA *storage.Header, dataB float64, dataReuse *storage.Header, ait, iit Iterator, useIter bool, err error) {
2✔
43
        // get data
2✔
44
        dataA = a.hdr()
2✔
45
        switch bt := b.(type) {
2✔
46
        case float64:
2✔
47
                dataB = bt
2✔
48
        case *float64:
×
49
                dataB = *bt
×
50
        default:
×
51
                err = errors.Errorf("b is not a float64: %T", b)
×
52
                return
×
53
        }
54
        if reuse != nil {
4✔
55
                dataReuse = reuse.hdr()
2✔
56
        }
2✔
57

58
        if a.RequiresIterator() || (reuse != nil && reuse.RequiresIterator()) {
2✔
59
                ait = a.Iterator()
×
60
                if reuse != nil {
×
61
                        iit = reuse.Iterator()
×
62
                }
×
63
                useIter = true
×
64
        }
65
        return
2✔
66
}
67

68
func (e Float64Engine) checkThree(a, b Tensor, reuse Tensor) error {
830✔
69
        if !a.IsNativelyAccessible() {
1,230✔
70
                return errors.Errorf(inaccessibleData, a)
400✔
71
        }
400✔
72
        if !b.IsNativelyAccessible() {
430✔
73
                return errors.Errorf(inaccessibleData, b)
×
74
        }
×
75

76
        if reuse != nil && !reuse.IsNativelyAccessible() {
430✔
77
                return errors.Errorf(inaccessibleData, reuse)
×
78
        }
×
79

80
        if a.Dtype() != Float64 {
430✔
81
                return errors.Errorf("Expected a to be of Float64. Got %v instead", a.Dtype())
×
82
        }
×
83
        if a.Dtype() != b.Dtype() || (reuse != nil && b.Dtype() != reuse.Dtype()) {
430✔
84
                return errors.Errorf("Expected a, b and reuse to have the same Dtype. Got %v, %v and %v instead", a.Dtype(), b.Dtype(), reuse.Dtype())
×
85
        }
×
86
        return nil
430✔
87
}
88

89
func (e Float64Engine) checkTwo(a Tensor, reuse Tensor) error {
2✔
90
        if !a.IsNativelyAccessible() {
2✔
91
                return errors.Errorf(inaccessibleData, a)
×
92
        }
×
93
        if reuse != nil && !reuse.IsNativelyAccessible() {
2✔
94
                return errors.Errorf(inaccessibleData, reuse)
×
95
        }
×
96

97
        if a.Dtype() != Float64 {
2✔
98
                return errors.Errorf("Expected a to be of Float64. Got %v instead", a.Dtype())
×
99
        }
×
100

101
        if reuse != nil && reuse.Dtype() != a.Dtype() {
2✔
102
                return errors.Errorf("Expected reuse to be the same as a. Got %v instead", reuse.Dtype())
×
103
        }
×
104
        return nil
2✔
105
}
106

107
// Float64Engine is an execution engine that is optimized to only work with float64s. It assumes all data will are float64s.
108
//
109
// Use this engine only as form of optimization. You should probably be using the basic default engine for most cases.
110
type Float64Engine struct {
111
        StdEng
112
}
113

114
// makeArray allocates a slice for the array
115
func (e Float64Engine) makeArray(arr *array, t Dtype, size int) {
13,223✔
116
        if t != Float64 {
13,223✔
117
                panic("Float64Engine only creates float64s")
×
118
        }
119
        arr.Header.Raw = make([]byte, size*8)
13,223✔
120
        arr.t = t
13,223✔
121
}
122

123
func (e Float64Engine) FMA(a, x, y Tensor) (retVal Tensor, err error) {
4✔
124
        reuse := y
4✔
125
        if err = e.checkThree(a, x, reuse); err != nil {
6✔
UNCOV
126
                return nil, errors.Wrap(err, "Failed checks")
2✔
UNCOV
127
        }
2✔
128

129
        var dataA, dataB, dataReuse *storage.Header
2✔
130
        var ait, bit, iit Iterator
2✔
131
        var useIter bool
2✔
132
        if dataA, dataB, dataReuse, ait, bit, iit, useIter, _, err = prepDataVV(a, x, reuse); err != nil {
2✔
133
                return nil, errors.Wrap(err, "Float64Engine.FMA")
×
134
        }
×
135
        if useIter {
2✔
UNCOV
136
                err = execution.MulIterIncrF64(dataA.Float64s(), dataB.Float64s(), dataReuse.Float64s(), ait, bit, iit)
×
UNCOV
137
                retVal = reuse
×
UNCOV
138
                return
×
UNCOV
139
        }
×
140

141
        vecf64.IncrMul(dataA.Float64s(), dataB.Float64s(), dataReuse.Float64s())
2✔
142
        retVal = reuse
2✔
143
        return
2✔
144
}
145

146
func (e Float64Engine) FMAScalar(a Tensor, x interface{}, y Tensor) (retVal Tensor, err error) {
2✔
147
        reuse := y
2✔
148
        if err = e.checkTwo(a, reuse); err != nil {
2✔
149
                return nil, errors.Wrap(err, "Failed checks")
×
150
        }
×
151

152
        var ait, iit Iterator
2✔
153
        var dataTensor, dataReuse *storage.Header
2✔
154
        var scalar float64
2✔
155
        var useIter bool
2✔
156
        if dataTensor, scalar, dataReuse, ait, iit, useIter, err = prepDataVSF64(a, x, reuse); err != nil {
2✔
157
                return nil, errors.Wrapf(err, opFail, "Float64Engine.FMAScalar")
×
158
        }
×
159
        if useIter {
2✔
160
                err = execution.MulIterIncrVSF64(dataTensor.Float64s(), scalar, dataReuse.Float64s(), ait, iit)
×
161
                retVal = reuse
×
162
        }
×
163

164
        execution.MulIncrVSF64(dataTensor.Float64s(), scalar, dataReuse.Float64s())
2✔
165
        retVal = reuse
2✔
166
        return
2✔
167
}
168

169
// Add performs a + b elementwise. Both a and b must have the same shape.
170
// Acceptable FuncOpts are: UseUnsafe(), WithReuse(T), WithIncr(T)
171
func (e Float64Engine) Add(a Tensor, b Tensor, opts ...FuncOpt) (retVal Tensor, err error) {
1,293✔
172
        if a.RequiresIterator() || b.RequiresIterator() {
1,760✔
173
                return e.StdEng.Add(a, b, opts...)
467✔
174
        }
467✔
175

176
        var reuse DenseTensor
826✔
177
        var safe, toReuse, incr bool
826✔
178
        if reuse, safe, toReuse, incr, err = handleFuncOptsF64(a.Shape(), a.DataOrder(), opts...); err != nil {
826✔
179
                return nil, errors.Wrap(err, "Unable to handle funcOpts")
×
180
        }
×
181
        if err = e.checkThree(a, b, reuse); err != nil {
1,224✔
182
                return nil, errors.Wrap(err, "Failed checks")
398✔
183
        }
398✔
184

185
        var hdrA, hdrB, hdrReuse *storage.Header
428✔
186
        var dataA, dataB, dataReuse []float64
428✔
187

428✔
188
        if hdrA, hdrB, hdrReuse, _, _, _, _, _, err = prepDataVV(a, b, reuse); err != nil {
428✔
189
                return nil, errors.Wrapf(err, "Float64Engine.Add")
×
190
        }
×
191
        dataA = hdrA.Float64s()
428✔
192
        dataB = hdrB.Float64s()
428✔
193
        if hdrReuse != nil {
468✔
194
                dataReuse = hdrReuse.Float64s()
40✔
195
        }
40✔
196

197
        switch {
428✔
198
        case incr:
25✔
199
                vecf64.IncrAdd(dataA, dataB, dataReuse)
25✔
200
                retVal = reuse
25✔
201
        case toReuse:
15✔
202
                copy(dataReuse, dataA)
15✔
203
                vecf64.Add(dataReuse, dataB)
15✔
204
                retVal = reuse
15✔
205
        case !safe:
374✔
206
                vecf64.Add(dataA, dataB)
374✔
207
                retVal = a
374✔
208
        default:
14✔
209
                ret := a.Clone().(headerer)
14✔
210
                vecf64.Add(ret.hdr().Float64s(), dataB)
14✔
211
                retVal = ret.(Tensor)
14✔
212
        }
213
        return
428✔
214
}
215

216
func (e Float64Engine) Inner(a, b Tensor) (retVal float64, err error) {
×
217
        var A, B []float64
×
218
        var AD, BD *Dense
×
219
        var ok bool
×
220

×
221
        if AD, ok = a.(*Dense); !ok {
×
222
                return 0, errors.Errorf("a is not a *Dense")
×
223
        }
×
224
        if BD, ok = b.(*Dense); !ok {
×
225
                return 0, errors.Errorf("b is not a *Dense")
×
226
        }
×
227

228
        A = AD.Float64s()
×
229
        B = BD.Float64s()
×
230
        retVal = whichblas.Ddot(len(A), A, 1, B, 1)
×
231
        return
×
232
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc