-
Notifications
You must be signed in to change notification settings - Fork 35
/
tensor.go
191 lines (167 loc) · 5.04 KB
/
tensor.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
//go:generate cgotorch/build.sh
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"
"unsafe"
)
// Tensor wrappers a pointer to C.Tensor
type Tensor struct {
T *unsafe.Pointer
}
// MustNil asserts error to be nil
func MustNil(err unsafe.Pointer) {
if err != nil {
msg := C.GoString((*C.char)(err))
C.FreeString((*C.char)(err))
panic(msg)
}
}
// Detach tensor.detach
func (a *Tensor) Detach() Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Tensor_Detach(C.Tensor(*a.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// String returns the Tensor as a string
func (a Tensor) String() string {
s := C.Tensor_String(C.Tensor(*a.T))
r := C.GoString(s)
C.FreeString(s)
return r
}
// Print the tensor
func (a Tensor) Print() {
C.Tensor_Print(C.Tensor(*a.T))
}
// Save the tensor to a file
func (a Tensor) Save(path string) {
C.Tensor_Save(C.Tensor(*a.T), C.CString(path))
}
// Load tensor from a file
func Load(path string) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Tensor_Load(C.CString(path), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Dim returns dim
func (a Tensor) Dim() int64 {
var dim int64
MustNil(unsafe.Pointer(C.Tensor_Dim(C.Tensor(*a.T), (*C.int64_t)(&dim))))
return dim
}
// Shape returns shape
func (a Tensor) Shape() []int64 {
shape := make([]int64, a.Dim())
if len(shape) == 0 {
return shape
}
MustNil(unsafe.Pointer(C.Tensor_Shape(C.Tensor(*a.T), (*C.int64_t)(unsafe.Pointer(&shape[0])))))
return shape
}
// Dtype returns data type
func (a Tensor) Dtype() int8 {
var t int8
MustNil(unsafe.Pointer(C.Tensor_Dtype(C.Tensor(*a.T), (*C.int8_t)(unsafe.Pointer(&t)))))
return t
}
// Backward compute the gradient of current tensor
func (a Tensor) Backward() {
C.Tensor_Backward(C.Tensor(*a.T))
}
// Grad returns a reference of the gradient
func (a Tensor) Grad() Tensor {
t := C.Tensor_Grad(C.Tensor(*a.T))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// To returns a Tensor on the specified device with the same content as the a.
// If the specified device doesn't exist, To panics.
func (a Tensor) To(device Device, dtype ...int8) Tensor {
var t C.Tensor
var d int8
if len(dtype) == 0 {
d = a.Dtype()
} else {
d = dtype[0]
}
MustNil(unsafe.Pointer(C.Tensor_To(C.Tensor(*a.T), device.T, C.int8_t(d), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// CUDA returns a Tensor on CUDA device
func (a Tensor) CUDA(device Device, nonBlocking bool) Tensor {
var t C.Tensor
n := int8(0)
if nonBlocking {
n = 1
}
MustNil(unsafe.Pointer(C.Tensor_CUDA(C.Tensor(*a.T), device.T, C.int8_t(n), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// CastTo cast tensor dtype
func (a Tensor) CastTo(dtype int8) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Tensor_CastTo(C.Tensor(*a.T), C.int8_t(dtype), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// CopyTo cast tensor dtype
func (a Tensor) CopyTo(device Device) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Tensor_CopyTo(C.Tensor(*a.T), device.T, &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// PinMemory returns a tensor in pinned memory. Pinned memory requires CUDA.
func (a Tensor) PinMemory() Tensor {
if !IsCUDAAvailable() {
return a
}
var t C.Tensor
MustNil(unsafe.Pointer(C.Tensor_PinMemory(C.Tensor(*a.T), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// SetData sets the tensor data held by b to a
func (a Tensor) SetData(b Tensor) {
MustNil(unsafe.Pointer(C.Tensor_SetData(C.Tensor(*a.T), C.Tensor(*b.T))))
}
// To returns a Tensor on the specified device with the same content as the a.
// If the specified device doesn't exist, To panics.
func To(a Tensor, device Device, dtype int8) Tensor {
return a.To(device, dtype)
}
// FromBlob returns a deep copy Tensor with the given data memory
func FromBlob(data unsafe.Pointer, dtype int8, sizes []int64) Tensor {
var t C.Tensor
MustNil(unsafe.Pointer(C.Tensor_FromBlob(
data,
C.int8_t(dtype),
(*C.int64_t)(unsafe.Pointer(&sizes[0])),
C.int64_t(len(sizes)),
&t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}
// Index calls Tensor::index to return a single-element tensor of the element at
// the given index.
func (a Tensor) Index(index ...int64) Tensor {
if int64(len(index)) != a.Dim() {
log.Panicf("Index %v has length that differs from the tenosr dim %d", index, a.Dim())
}
var t C.Tensor
MustNil(unsafe.Pointer(C.Tensor_Index(
C.Tensor(*a.T),
(*C.int64_t)(unsafe.Pointer(&index[0])),
C.int64_t(len(index)), &t)))
SetTensorFinalizer((*unsafe.Pointer)(&t))
return Tensor{(*unsafe.Pointer)(&t)}
}