forked from Yancey0623/gotorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.go
More file actions
145 lines (123 loc) · 3.82 KB
/
tensor.go
File metadata and controls
145 lines (123 loc) · 3.82 KB
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
//go:generate cgotorch/build.sh
package gotorch
// #cgo CFLAGS: -I ${SRCDIR}/cgotorch
// #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.h"
import "C"
import (
"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))
}
// Close the tensor
func (a *Tensor) Close() {
if a.T != nil {
C.Tensor_Close(C.Tensor(*a.T))
a.T = nil
}
}
// 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())
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
MustNil(unsafe.Pointer(C.Tensor_To(C.Tensor(*a.T), device.T, C.int8_t(dtype), &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)}
}
// 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 creating a Tensor with the give data memory
func FromBlob(data unsafe.Pointer, dtype int8, sizes []int64) Tensor {
var t C.Tensor
C.Tensor_FromBlob(data, C.int8_t(dtype), (*C.int64_t)(unsafe.Pointer(&sizes[0])), C.int64_t(len(sizes)), &t)
return Tensor{(*unsafe.Pointer)(&t)}
}