forked from dereklstinson/gocudnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudnnCTCLoss.go
208 lines (185 loc) · 5.96 KB
/
cudnnCTCLoss.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
package gocudnn
/*
#include <cudnn.h>
void MakeAlgorithmforCTCL(cudnnAlgorithm_t *input,cudnnCTCLossAlgo_t Algo ){
input->algo.CTCLossAlgo=Algo;
}
*/
import "C"
import (
"runtime"
"unsafe"
"github.com/dereklstinson/cutil"
)
//Algo returns al algo
func (c CTCLossAlgo) Algo() Algorithm {
var algo C.cudnnAlgorithm_t
C.MakeAlgorithmforCTCL(&algo, c.c())
return Algorithm(algo)
}
//CTCLossD holdes the C.cudnnCTCLossDescriptor_t
type CTCLossD struct {
descriptor C.cudnnCTCLossDescriptor_t
gogc bool
}
//CreateCTCLossDescriptor creates
func CreateCTCLossDescriptor() (*CTCLossD, error) {
x := new(CTCLossD)
err := Status(C.cudnnCreateCTCLossDescriptor(&x.descriptor)).error("CreateCTCLossDescriptor-create")
if err != nil {
return nil, err
}
x.gogc = true
if setfinalizer || x.gogc {
runtime.SetFinalizer(x, cudnnDestroyCTCLossDescriptor)
}
return x, err
}
//Set sets the CTCLossD
func (c *CTCLossD) Set(data DataType) error {
return Status(C.cudnnSetCTCLossDescriptor(c.descriptor, data.c())).error("CreateCTCLossDescriptor-set")
}
//Get returns the datatype and error
func (c *CTCLossD) Get() (DataType, error) {
var data C.cudnnDataType_t
err := Status(C.cudnnGetCTCLossDescriptor(c.descriptor, &data)).error("GetDescriptor")
return DataType(data), err
}
//Destroy destroys the descriptor inside CTCLossD if go's gc is not in use.
//if gc is being used destroy will just return nil
func (c *CTCLossD) Destroy() error {
if setfinalizer || c.gogc {
return nil
}
return cudnnDestroyCTCLossDescriptor(c)
}
func cudnnDestroyCTCLossDescriptor(c *CTCLossD) error {
return Status(C.cudnnDestroyCTCLossDescriptor(c.descriptor)).error("DestroyDescriptor")
}
//CTCLossAlgo used to hold flags
type CTCLossAlgo C.cudnnCTCLossAlgo_t
//Deterministic sets c to and returns CTCLossAlgo(C.CUDNN_CTC_LOSS_ALGO_DETERMINISTIC)
func (c *CTCLossAlgo) Deterministic() CTCLossAlgo {
*c = CTCLossAlgo(C.CUDNN_CTC_LOSS_ALGO_DETERMINISTIC)
return *c
}
//NonDeterministic sets c to and returns CTCLossAlgo(C.CUDNN_CTC_LOSS_ALGO_NON_DETERMINISTIC) Flag
func (c *CTCLossAlgo) NonDeterministic() CTCLossAlgo {
*c = CTCLossAlgo(C.CUDNN_CTC_LOSS_ALGO_NON_DETERMINISTIC)
return *c
}
func (c CTCLossAlgo) c() C.cudnnCTCLossAlgo_t {
return C.cudnnCTCLossAlgo_t(c)
}
//Need to finish this
//CTCLoss calculates loss
func (c *CTCLossD) CTCLoss(
handle *Handle,
probsD *TensorD, /* Tensor descriptor for probabilities, the dimensions are T,N,A (T is the timing steps, N is the mini batch size, A is the alphabet size) */
probs cutil.Mem, /* probabilities after softmax, in GPU memory */
labels []int32, /* labels, in CPU memory */
labelLengths []int32, /* the length of each label, in CPU memory */
inputLengths []int32, /* the lengths of timing steps in each batch, in CPU memory */
costs cutil.Mem, //output /* the returned costs of CTC, in GPU memory */
gradientsD *TensorD, /* Tensor descriptor for gradients, the dimensions are T,N,A */
gradients cutil.Mem, //output /* the returned CTC gradients, in GPU memory, to compute costs only, set it to NULL */
algo CTCLossAlgo, /* algorithm selected, supported now 0 and 1 */
wspace cutil.Mem, /* pointer to the workspace, in GPU memory */
wspacesize uint,
) error {
toclabels := int32Tocint(labels)
toclablen := int32Tocint(labelLengths)
tocinlen := int32Tocint(inputLengths)
if wspace == nil {
return Status(C.cudnnCTCLoss(
handle.x,
probsD.descriptor,
probs.Ptr(),
&toclabels[0],
&toclablen[0],
&tocinlen[0],
costs.Ptr(),
gradientsD.descriptor,
gradients.Ptr(),
algo.c(),
c.descriptor,
nil,
C.size_t(0),
)).error("CTCLoss")
}
err := Status(C.cudnnCTCLoss(
handle.x,
probsD.descriptor,
probs.Ptr(),
&toclabels[0],
&toclablen[0],
&tocinlen[0],
costs.Ptr(),
gradientsD.descriptor,
gradients.Ptr(),
algo.c(),
c.descriptor,
wspace.Ptr(),
C.size_t(wspacesize),
)).error("CTCLoss")
return err
}
//CTCLossUS is like CTCLoss but uses unsafe.Pointer instead of cutil.Mem
func (c *CTCLossD) CTCLossUS(
handle *Handle,
probsD *TensorD, probs unsafe.Pointer, /* probabilities after softmax, in GPU memory */
labels []int32, /* labels, in CPU memory */
labelLengths []int32, /* the length of each label, in CPU memory */
inputLengths []int32, /* the lengths of timing steps in each batch, in CPU memory */
costs unsafe.Pointer, //output /* the returned costs of CTC, in GPU memory */
gradientsD *TensorD, gradients unsafe.Pointer, //output /* the returned CTC gradients, in GPU memory, to compute costs only, set it to NULL */
algo CTCLossAlgo, /* algorithm selected, supported now 0 and 1 */
wspace unsafe.Pointer, wspacesize uint,
) error {
toclabels := int32Tocint(labels)
toclablen := int32Tocint(labelLengths)
tocinlen := int32Tocint(inputLengths)
err := Status(C.cudnnCTCLoss(
handle.x,
probsD.descriptor,
probs,
&toclabels[0],
&toclablen[0],
&tocinlen[0],
costs,
gradientsD.descriptor,
gradients,
algo.c(),
c.descriptor,
wspace,
C.size_t(wspacesize),
)).error("CTCLoss")
return err
}
//GetWorkspaceSize calculates workspace size
func (c *CTCLossD) GetWorkspaceSize(
handle *Handle,
probsD *TensorD, /* Tensor descriptor for probabilities, the dimensions are T,N,A (T is the timing steps, N is the mini batch size, A is the alphabet size) */
gradientsD *TensorD, /* Tensor descriptor for gradients, the dimensions are T,N,A */
labels []int32, /* labels, in CPU memory */
labelLengths []int32, /* the length of each label, in CPU memory */
inputLengths []int32, /* the lengths of timing steps in each batch, in CPU memory */
algo CTCLossAlgo, /* algorithm selected, supported now 0 and 1 */
) (uint, error) {
toclabels := int32Tocint(labels)
toclablen := int32Tocint(labelLengths)
tocinlen := int32Tocint(inputLengths)
var bsize C.size_t
err := Status(C.cudnnGetCTCLossWorkspaceSize(
handle.x,
probsD.descriptor,
gradientsD.descriptor,
&toclabels[0],
&toclablen[0],
&tocinlen[0],
algo.c(),
c.descriptor,
&bsize,
)).error("GetCTCLossWorkspaceSize")
return uint(bsize), err
}