-
Notifications
You must be signed in to change notification settings - Fork 35
/
tensor_ops.go
462 lines (405 loc) · 12.2 KB
/
tensor_ops.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package gotorch
// #cgo CFLAGS: -I ${SRCDIR}
// #cgo LDFLAGS: -L ${SRCDIR}/cgotorch -Wl,-rpath ${SRCDIR}/cgotorch -lcgotorch
// #cgo LDFLAGS: -L ${SRCDIR}/cgotorch/libtorch/lib -Wl,-rpath ${SRCDIR}/cgotorch/libtorch/lib -lc10 -ltorch -ltorch_cpu
// #include "cgotorch/cgotorch.h"
import "C"
import (
"log"
"reflect"
"strings"
"unsafe"
"github.com/wangkuiyi/gotorch/variadic"
)
// Add torch.add
func Add(a, other Tensor, alpha float32) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Add(C.Tensor(*a.T), C.Tensor(*other.T),
C.float(alpha), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Add torch.add
func (a *Tensor) Add(other Tensor, alpha float32) Tensor {
return Add(*a, other, alpha)
}
// AddI adds in-place
func (a *Tensor) AddI(other Tensor, alpha float32) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Add_(
C.Tensor(*a.T),
C.Tensor(*other.T),
C.float(alpha),
&t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Sub torch.sub
func Sub(a, other Tensor, alpha float32) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Sub(C.Tensor(*a.T), C.Tensor(*other.T),
C.float(alpha), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Sub torch.sub
func (a *Tensor) Sub(other Tensor, alpha float32) Tensor {
return Sub(*a, other, alpha)
}
// SubI subs in-place
func (a *Tensor) SubI(other Tensor, alpha float32) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Sub_(
C.Tensor(*a.T),
C.Tensor(*other.T),
C.float(alpha),
&t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Mul torch.mul
func Mul(a, other Tensor) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Mul(C.Tensor(*a.T), C.Tensor(*other.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Mul torch.Mul
func (a *Tensor) Mul(other Tensor) Tensor {
return Mul(*a, other)
}
// MulI multiplies in-place
func (a *Tensor) MulI(other Tensor) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Mul_(
C.Tensor(*a.T),
C.Tensor(*other.T),
&t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Div torch.div
func Div(a, other Tensor) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Div(C.Tensor(*a.T), C.Tensor(*other.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Div torch.Div
func (a *Tensor) Div(other Tensor) Tensor {
return Div(*a, other)
}
// DivI run divides in-place
func (a *Tensor) DivI(other Tensor) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Div_(
C.Tensor(*a.T),
C.Tensor(*other.T),
&t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Permute transpose the tensor dims.
func (a *Tensor) Permute(dims []int64) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Permute(C.Tensor(*a.T), (*C.int64_t)(&dims[0]), C.int64_t(len(dims)), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Eq wraps torch.eq, which does element-wise comparison between two tensors and returns
// a tensor of the same size as the operands.
func Eq(a, other Tensor) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Eq(C.Tensor(*a.T), C.Tensor(*other.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Eq torch.eq
func (a Tensor) Eq(other Tensor) Tensor {
return Eq(a, other)
}
// Equal compares two tensors by their content.
func Equal(a, b Tensor) bool {
var r int64
MustNil(unsafe.Pointer(C.Equal(C.Tensor(*a.T), C.Tensor(*b.T), (*C.int64_t)(&r))))
return r != 0
}
// AllClose returns true if the float tensor are all close.
func AllClose(a, b Tensor) bool {
var r int64
MustNil(unsafe.Pointer(C.AllClose(C.Tensor(*a.T), C.Tensor(*b.T), (*C.int64_t)(&r))))
return r != 0
}
// ExpandAs torch.expand_as
func ExpandAs(a, other Tensor) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.ExpandAs(C.Tensor(*a.T), C.Tensor(*other.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// ExpandAs torch.expand_as
func (a Tensor) ExpandAs(other Tensor) Tensor {
return ExpandAs(a, other)
}
// Flatten torch.flatten
func Flatten(a Tensor, startDim, endDim int64) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Flatten(C.Tensor(*a.T), C.int64_t(startDim), C.int64_t(endDim), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// IndexSelect torch.index_select
func IndexSelect(a Tensor, dim int64, index Tensor) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.IndexSelect(C.Tensor(*a.T), C.int64_t(dim), C.Tensor(*index.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// IndexSelect torch.index_select
func (a Tensor) IndexSelect(dim int64, index Tensor) Tensor {
return IndexSelect(a, dim, index)
}
// Item returns 0-dim tensor's value as an interface
// users should do type assertion and get the value like:
// v, ok := a.Item().(float64)
// Currently not support unsigned Tensor.
func (a Tensor) Item() interface{} {
dtype := a.Dtype()
switch dtype {
case Byte, Bool, Char, Short, Int, Long:
var v int64
MustNil(unsafe.Pointer(C.ItemInt64(C.Tensor(*a.T), (*C.int64_t)(&v))))
switch dtype {
case Byte:
return byte(v)
case Bool:
return bool(v != 0)
case Char:
return int8(v)
case Short:
return int16(v)
case Int:
return int32(v)
case Long:
return v
}
case Half, Float, Double:
var v float64
MustNil(unsafe.Pointer(C.ItemFloat64(C.Tensor(*a.T), (*C.double)(&v))))
switch dtype {
case Half, Float:
return float32(v)
case Double:
return v
}
}
log.Panicf("DType %d not supported now.", a.Dtype())
return nil
}
// LeakyRelu returns leaky relu of the tensor according to negativeSlope
func LeakyRelu(t Tensor, negativeSlope float64) Tensor {
return t.LeakyRelu(negativeSlope)
}
// LeakyRelu returns leaky relu of the tensor according to negativeSlope
func (a *Tensor) LeakyRelu(negativeSlope float64) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.LeakyRelu(C.Tensor(*a.T), C.double(negativeSlope), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// LogSoftmax returns log softmax of the input tensor
func LogSoftmax(t Tensor, dim int64) Tensor {
return t.LogSoftmax(dim)
}
// LogSoftmax returns log softmax of the current tensor
func (a Tensor) LogSoftmax(dim int64) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.LogSoftmax(C.Tensor(*a.T), C.int64_t(dim), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Mean returns mean of the current tensor
func Mean(t Tensor) Tensor {
return t.Mean()
}
// Mean torch.mean
func (a Tensor) Mean() Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Mean(C.Tensor(*a.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// MM multiplies each element of the input two tensors
func MM(a, b Tensor) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.MM(C.Tensor(*a.T), C.Tensor(*b.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Relu returns relu of the tensor
func (a *Tensor) Relu() Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Relu(C.Tensor(*a.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Relu returns relu of the tensor
func Relu(t Tensor) Tensor {
return t.Relu()
}
// Sigmoid returns sigmoid of the current tensor
func Sigmoid(t Tensor) Tensor {
return t.Sigmoid()
}
// Sigmoid returns sigmoid of the current tensor
func (a Tensor) Sigmoid() Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Sigmoid(C.Tensor(*a.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Stack concatenates sequence of tensors along a new dimension
func Stack(tensors []Tensor, dim int64) Tensor {
CT := []C.Tensor{}
for _, t := range tensors {
CT = append(CT, C.Tensor(*t.T))
}
p := (*C.Tensor)(unsafe.Pointer(&CT[0]))
var t C.Tensor
MustNil(unsafe.Pointer(C.Stack(p, C.int64_t(len(CT)), C.int64_t(dim), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Squeeze torch.squeeze
func Squeeze(t Tensor, dim ...int64) Tensor {
return t.Squeeze(dim...)
}
// Squeeze tensor.squeeze
func (a Tensor) Squeeze(dim ...int64) Tensor {
var t C.Tensor
switch len(dim) {
case 0:
MustNil(unsafe.Pointer(C.Squeeze(C.Tensor(*a.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
case 1:
MustNil(unsafe.Pointer(C.SqueezeWithDim(C.Tensor(*a.T), C.int64_t(dim[0]), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
default:
panic("Squeeze only accepts 0-1 dim as input")
}
}
// Sum is torch.sum
func Sum(a Tensor, opt ...map[string]interface{}) Tensor {
if variadic.Has(opt, "dim") {
dim := variadic.Get(opt, "dim").(int)
keepDim := variadic.Get(opt, "keepDim", false).(bool)
k := 0
if keepDim {
k = 1
}
var t C.Tensor
MustNil(unsafe.Pointer(C.SumByDim(C.Tensor(*a.T), C.int64_t(dim), C.int8_t(k), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
var t C.Tensor
MustNil(unsafe.Pointer(C.Sum(C.Tensor(*a.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Sum is Tensor.sum
func (a Tensor) Sum(opt ...map[string]interface{}) Tensor {
return Sum(a, opt...)
}
// Tanh returns tanh of the current tensor
func Tanh(t Tensor) Tensor {
return t.Tanh()
}
// Tanh returns tanh of the current tensor
func (a Tensor) Tanh() Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Tanh(C.Tensor(*a.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// TopK torch.topk
func TopK(a Tensor, k, dim int64, largest, sorted bool) (Tensor, Tensor) {
var values, indices C.Tensor
l := 0
if largest {
l = 1
}
s := 0
if sorted {
s = 1
}
MustNil(unsafe.Pointer(C.TopK(C.Tensor(*a.T), C.int64_t(k), C.int64_t(dim),
C.int8_t(l), C.int8_t(s), &values, &indices)))
SetTensorFinalizer((*unsafe.Pointer)(&values))
SetTensorFinalizer((*unsafe.Pointer)(&indices))
return Tensor{(*unsafe.Pointer)(&values)}, Tensor{(*unsafe.Pointer)(&indices)}
}
// Transpose torch.transpose
func Transpose(a Tensor, dim0, dim1 int64) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Transpose(C.Tensor(*a.T), C.int64_t(dim0), C.int64_t(dim1), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Transpose torch.transpose
func (a Tensor) Transpose(dim0, dim1 int64) Tensor {
return Transpose(a, dim0, dim1)
}
// View returns a new Tensor with the same data but of a different shape
func View(a Tensor, shape ...int64) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.View(C.Tensor(*a.T), &t, (*C.int64_t)(unsafe.Pointer(&shape[0])), C.int64_t(len(shape)))))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// View returns a new Tensor with the same data but of a different shape
func (a Tensor) View(shape ...int64) Tensor {
return View(a, shape...)
}
// Argmin mimics torch.argmin
func (a Tensor) Argmin(opts ...interface{}) Tensor {
return a.argMinMax(true, opts...)
}
// Argmax mimics torch.argmax
func (a Tensor) Argmax(opts ...interface{}) Tensor {
return a.argMinMax(false, opts...)
}
func (a Tensor) argMinMax(argmin bool, opts ...interface{}) Tensor {
var (
dimOpt int64
dim *int64
keepdim int8
)
if len(opts) > 0 {
// The first optional parameter must be dim integer.
if !strings.HasPrefix(reflect.TypeOf(opts[0]).Kind().String(), "int") {
log.Panicf("Tensor.Argmin(dim) requires dim in int{64|32|16|}")
}
dimOpt = reflect.ValueOf(opts[0]).Int()
dim = &dimOpt
}
if len(opts) > 1 {
// The second optional parametr must be keepdim bool.
if reflect.TypeOf(opts[1]).Kind() != reflect.Bool {
log.Panicf("Tensor.Argmin(dim) requires dim in int64")
}
if opts[1].(bool) {
keepdim = 1
}
}
var t C.Tensor
if argmin {
MustNil(unsafe.Pointer(C.Argmin(C.Tensor(*a.T), (*C.int64_t)(dim), C.int8_t(keepdim), &t)))
} else {
MustNil(unsafe.Pointer(C.Argmax(C.Tensor(*a.T), (*C.int64_t)(dim), C.int8_t(keepdim), &t)))
}
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}