This repository was archived by the owner on Aug 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 727
Expand file tree
/
Copy pathNNNetworkFunctions.h
More file actions
384 lines (359 loc) · 19.8 KB
/
NNNetworkFunctions.h
File metadata and controls
384 lines (359 loc) · 19.8 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
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
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
#ifndef __NNNETWORKFUNCTIONS_H__
#define __NNNETWORKFUNCTIONS_H__
class NNNetworkFunctions {
public:
static PyObject* ClearDataSets(PyObject* self, PyObject* args);
static PyObject* LoadDataSets(PyObject* self, PyObject* args);
static PyObject* Randomize(PyObject* self, PyObject* args);
static PyObject* Validate(PyObject* self, PyObject* args);
static PyObject* Train(PyObject* self, PyObject* args);
static PyObject* PredictBatch(PyObject* self, PyObject* args);
static PyObject* CalculateTopK(PyObject* self, PyObject* args);
static PyObject* PredictTopK(PyObject* self, PyObject* args);
static PyObject* CalculateMRR(PyObject* self, PyObject* args);
static PyObject* SaveBatch(PyObject* self, PyObject* args);
static PyObject* DumpBatch(PyObject* self, PyObject* args);
static PyObject* SaveLayer(PyObject* self, PyObject* args);
static PyObject* DumpLayer(PyObject* self, PyObject* args);
static PyObject* SaveWeights(PyObject* self, PyObject* args);
static PyObject* LockWeights(PyObject* self, PyObject* args);
static PyObject* UnlockWeights(PyObject* self, PyObject* args);
static PyObject* SaveNetCDF(PyObject* self, PyObject* args);
static PyObject* P2P_Bcast(PyObject* self, PyObject* args);
static PyObject* P2P_Allreduce(PyObject* self, PyObject* args);
};
/**
* Clear the data sets from the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @throws exception upon failure to parse arguments
*/
PyObject* NNNetworkFunctions::ClearDataSets(PyObject* self, PyObject* args) {
NNNetwork* pNetwork = parsePtr<NNNetwork*>(args, "neural network");
if (pNetwork == NULL) return NULL;
pNetwork->ClearDataSets();
Py_RETURN_NONE;
}
/**
* Load a Python list of data sets into the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param pDataSetBaseList - a list of encapsulated NNDataSetBase*
* @throws exception upon failure to parse arguments or to construct a vector from the list
*/
PyObject* NNNetworkFunctions::LoadDataSets(PyObject* self, PyObject* args) {
PyObject *pDataSetBaseList = NULL;
NNNetwork* pNetwork = parsePtrAndOneValue<NNNetwork*, PyObject*>(args, pDataSetBaseList, "neural network", "OO");
if (pNetwork == NULL) return NULL;
vector<NNDataSetBase*> vDataSetBase = PythonListToDataSetBaseVector(pDataSetBaseList);
if (vDataSetBase.empty()) {
PyErr_SetString(PyExc_RuntimeError, "NNNetworkFunctions::LoadDataSets received empty vDataSetBase vector");
return NULL;
}
pNetwork->LoadDataSets(vDataSetBase);
Py_RETURN_NONE;
}
/**
* Randomize the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @throws exception upon failure to parse arguments
*/
PyObject* NNNetworkFunctions::Randomize(PyObject* self, PyObject* args) {
NNNetwork* pNetwork = parsePtr<NNNetwork*>(args, "neural network");
if (pNetwork == NULL) return NULL;
pNetwork->Randomize();
Py_RETURN_NONE;
}
/**
* Validate the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @return a PyObject* that references a boolean to indicate success
* @throws exception upon failure to parse arguments or to build the return value
*/
PyObject* NNNetworkFunctions::Validate(PyObject* self, PyObject* args) {
NNNetwork* pNetwork = parsePtr<NNNetwork*>(args, "neural network");
if (pNetwork == NULL) return NULL;
return Py_BuildValue("i", pNetwork->Validate());
}
/**
* Train the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param epochs - the epochs unsigned integer
* @param alpha - the alpha float
* @param lambda - the lambda float
* @param lambda1 - the lambda1 float
* @param mu - the mu float
* @param mu1 - the mu1 float
* @return a PyObject* that references a float that reports the average error
* @throws exception upon failure to parse arguments or to build the return value
*/
PyObject* NNNetworkFunctions::Train(PyObject* self, PyObject* args) {
uint32_t epochs = 0;
NNFloat alpha = 0.0, lambda = 0.0, lambda1 = 0.0, mu = 0.0, mu1 = 0.0;
NNNetwork* pNetwork = parsePtrAndSixValues<NNNetwork*, uint32_t, NNFloat, NNFloat, NNFloat, NNFloat, NNFloat>(args,
epochs,
alpha,
lambda,
lambda1,
mu,
mu1,
"neural network",
"OIfffff");
if (pNetwork == NULL) return NULL;
return Py_BuildValue("f", pNetwork->Train(epochs, alpha, lambda, lambda1, mu, mu1));
}
/**
* Predict batch for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param layers - an unsigned integer that specifies the number of layers
* @throws exception upon failure to parse arguments
*/
PyObject* NNNetworkFunctions::PredictBatch(PyObject* self, PyObject* args) {
uint32_t layers = 0;
NNNetwork* pNetwork = parsePtrAndOneValue<NNNetwork*, uint32_t>(args, layers, "neural network", "OI");
if (pNetwork == NULL) return NULL;
pNetwork->PredictBatch(layers);
Py_RETURN_NONE;
}
/**
* Calculate the top K results for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param layer - a string that specifies the layer name
* @param k - an unsigned integer that specifies K
* @param pbKey - an encapsulated float GpuBuffer
* @param pbValue - an encapsulates unsigned integer GpuBuffer
* @throws exception upon failure to parse arguments or to open the GpuBuffer capsules
*/
PyObject* NNNetworkFunctions::CalculateTopK(PyObject* self, PyObject* args) {
char const* layer = NULL;
uint32_t k = 0;
PyObject *pbKeyCapsule = NULL, *pbValueCapsule = NULL;
NNNetwork* pNetwork = parsePtrAndFourValues<NNNetwork*, char const*, uint32_t, PyObject*, PyObject*>(args,
layer,
k,
pbKeyCapsule,
pbValueCapsule,
"neural network",
"OsIOO");
if (pNetwork == NULL) return NULL;
GpuBuffer<NNFloat>* pbKey = reinterpret_cast<GpuBuffer<NNFloat>*>(PyCapsule_GetPointer(pbKeyCapsule, "float gpu buffer"));
GpuBuffer<uint32_t>* pbValue = reinterpret_cast<GpuBuffer<uint32_t>*>(PyCapsule_GetPointer(pbValueCapsule, "unsigned gpu buffer"));
if (pbKey == NULL || pbValue == NULL) return NULL;
pNetwork->CalculateTopK(string(layer), k, pbKey, pbValue);
Py_RETURN_NONE;
}
/**
* Do a prediction calculation and return the top K results for a neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param pDataSetBaseList - a list of encapsulated NNDataSetBase*
* @param pCDL - an encapsulated CDL*
* @param k - an unsigned integer that specifies the number K of results
* @return a PyObject* that references a list of lists wherein the inner lists contains three floats
* @throws exception upon failure to parse arguments, to get the CDL* from its capsule, or to construct a vector<NNDataSetBase*> from the list
*/
PyObject* NNNetworkFunctions::PredictTopK(PyObject* self, PyObject* args) {
PyObject *pDataSetBaseList = NULL, *pCdlCapsule = NULL;
uint32_t k = 0;
NNNetwork* pNetwork = parsePtrAndThreeValues<NNNetwork*, PyObject*, PyObject*, uint32_t>(args, pDataSetBaseList, pCdlCapsule, k, "neural network", "OOOI");
if (pNetwork == NULL) return NULL;
CDL* pCDL = reinterpret_cast<CDL*>(PyCapsule_GetPointer(pCdlCapsule, "cdl"));
if (pCDL == NULL) return NULL;
vector<NNDataSetBase*> vDataSetBase = PythonListToDataSetBaseVector(pDataSetBaseList);
if (vDataSetBase.empty()) {
PyErr_SetString(PyExc_RuntimeError, "NNNetworkFunctions::PredictTopK received empty vDataSetBase vector");
return NULL;
}
return dsstnecalculate_PredictTopK(pNetwork, vDataSetBase, *pCDL, k);
}
/**
* Calculate the MRR for a neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param pDataSetBaseList - a list of encapsulated NNDataSetBase*
* @param pOutputLayer - an encapsulated NNLayer* that references the output layer
* @param outputIndex - an unsigned integer that represents the index to the output data set
* @param NxOutput - an unsigned integer that represents the leading dimension of the output layer
* @return a PyObject* that references the MRR float
* @throws exception upon failure to parse arguments, to get the NNLayer* from its capsule, or to construct a vector<NNDataSetBase*> from the list
*/
PyObject* NNNetworkFunctions::CalculateMRR(PyObject* self, PyObject* args) {
PyObject *pDataSetBaseList = NULL, *pOutputLayerCapsule=NULL;
uint32_t outputIndex = 0, NxOutput = 0;
NNNetwork* pNetwork = parsePtrAndFourValues<NNNetwork*, PyObject*, PyObject*, uint32_t, uint32_t>(args,
pDataSetBaseList,
pOutputLayerCapsule,
outputIndex,
NxOutput,
"neural network",
"OOOII");
if (pNetwork == NULL) return NULL;
NNLayer* pOutputLayer = reinterpret_cast<NNLayer*>(PyCapsule_GetPointer(pOutputLayerCapsule, "layer"));
if (pOutputLayer == NULL) return NULL;
vector<NNDataSetBase*> vDataSetBase = PythonListToDataSetBaseVector(pDataSetBaseList);
if (vDataSetBase.empty()) {
PyErr_SetString(PyExc_RuntimeError, "NNNetworkFunctions::CalculateMRR received empty vDataSetBase vector");
return NULL;
}
return dsstnecalculate_CalculateMRR(pNetwork, vDataSetBase, pOutputLayer, outputIndex, NxOutput);
}
/**
* Save the batch to a file for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param fname - a string that specifies the filename
* @throws exception upon failure to parse arguments
*/
PyObject* NNNetworkFunctions::SaveBatch(PyObject* self, PyObject* args) {
char const* fname = NULL;
NNNetwork* pNetwork = parsePtrAndOneValue<NNNetwork*, char const*>(args, fname, "neural network", "Os");
if (pNetwork == NULL) return NULL;
pNetwork->SaveBatch(string(fname));
Py_RETURN_NONE;
}
/**
* Dump the batch to a FILE for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param fp - an encapsulated FILE*
* @throws exception upon failure to parse arguments or to open the FILE pointer capsule
*/
PyObject* NNNetworkFunctions::DumpBatch(PyObject* self, PyObject* args) {
PyObject* fpCapsule = NULL;
NNNetwork* pNetwork = parsePtrAndOneValue<NNNetwork*, PyObject*>(args, fpCapsule, "neural network", "OO");
if (pNetwork == NULL) return NULL;
FILE* fp = reinterpret_cast<FILE*>(PyCapsule_GetPointer(fpCapsule, "file pointer"));
if (fp == NULL) return NULL;
pNetwork->DumpBatch(fp);
Py_RETURN_NONE;
}
/**
* Save the layer to a file for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param fname - a string that specifies the filename
* @param layer - a string that specifies the layer name
* @throws exception upon failure to parse arguments
*/
PyObject* NNNetworkFunctions::SaveLayer(PyObject* self, PyObject* args) {
char const *fname = NULL, *layer = NULL;
NNNetwork* pNetwork = parsePtrAndTwoValues<NNNetwork*, char const*, char const*>(args, fname, layer, "neural network", "Oss");
if (pNetwork == NULL) return NULL;
pNetwork->SaveLayer(string(fname), string(layer));
Py_RETURN_NONE;
}
/**
* Dump the layer to a FILE for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param fp - an encapsulated FILE*
* @param layer - a string that specifies the layer name
* @throws exception upon failure to parse arguments or to open the FILE pointer capsule
*/
PyObject* NNNetworkFunctions::DumpLayer(PyObject* self, PyObject* args) {
PyObject* fpCapsule = NULL;
char const* layer = NULL;
NNNetwork* pNetwork = parsePtrAndTwoValues<NNNetwork*, PyObject*, char const*>(args, fpCapsule, layer, "neural network", "OOs");
if (pNetwork == NULL) return NULL;
FILE* fp = reinterpret_cast<FILE*>(PyCapsule_GetPointer(fpCapsule, "file pointer")); // Need a function that opens a FILE and returns a Python capsule
if (fp == NULL) return NULL;
pNetwork->DumpLayer(fp, string(layer));
Py_RETURN_NONE;
}
/**
* Save the weights connecting two layers to a file for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param fname - a string that specifies the filename
* @param inputLayer - a string that specifies the input layer name
* @param outputLayer - a string that specifies the output layer name
* @throws exception upon failure to parse arguments
*/
PyObject* NNNetworkFunctions::SaveWeights(PyObject* self, PyObject* args) {
char const *fname = NULL, *inputLayer = NULL, *outputLayer = NULL;
NNNetwork* pNetwork =
parsePtrAndThreeValues<NNNetwork*, char const*, char const*, char const*>(args, fname, inputLayer, outputLayer, "neural network", "Osss");
if (pNetwork == NULL) return NULL;
pNetwork->SaveWeights(string(fname), string(inputLayer), string(outputLayer));
Py_RETURN_NONE;
}
/**
* Lock the weights connecting two layers for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param inputLayer - a string that specifies the input layer name
* @param outputLayer - a string that specifies the output layer name
* @return a PyObject* that references a boolean to indicate success
* @throws exception upon failure to parse arguments or to build the return value
*/
PyObject* NNNetworkFunctions::LockWeights(PyObject* self, PyObject* args) {
char const *inputLayer = NULL, *outputLayer = NULL;
NNNetwork* pNetwork = parsePtrAndTwoValues<NNNetwork*, char const*, char const*>(args, inputLayer, outputLayer, "neural network", "Oss");
if (pNetwork == NULL) return NULL;
return Py_BuildValue("i", pNetwork->LockWeights(string(inputLayer), string(outputLayer)));
}
/**
*Unlock the weights connecting two layers for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param inputLayer - a string that specifies the input layer name
* @param outputLayer - a string that specifies the output layer name
* @return a PyObject* that references a boolean to indicate success
* @throws exception upon failure to parse arguments or to build the return value
*/
PyObject* NNNetworkFunctions::UnlockWeights(PyObject* self, PyObject* args) {
char const *inputLayer = NULL, *outputLayer = NULL;
NNNetwork* pNetwork = parsePtrAndTwoValues<NNNetwork*, char const*, char const*>(args, inputLayer, outputLayer, "neural network", "Oss");
if (pNetwork == NULL) return NULL;
return Py_BuildValue("i", pNetwork->UnlockWeights(string(inputLayer), string(outputLayer)));
}
/**
* Save the training results to a CDF file for the neural network.
* @param pNetwork - an encapsulated NNNetwork*
* @param fname - a string that specifies the CDF filename
* @return a PyObject* that references a boolean to indicate success
* @throws exception upon failure to parse arguments or to build the return value
*/
PyObject* NNNetworkFunctions::SaveNetCDF(PyObject* self, PyObject* args) {
char const* fname = NULL;
NNNetwork* pNetwork = parsePtrAndOneValue<NNNetwork*, char const*>(args, fname, "neural network", "Os");
if (pNetwork == NULL) return NULL;
return Py_BuildValue("i", pNetwork->SaveNetCDF(string(fname)));
}
/**
* Broadcast data from process 0 to all other processes for the neural network.
* @param pNetwork - the encapsulated NNNetwork*
* @param pBuffer - an encapsulated pointer that references the broadcast buffer
* @param size - an unsigned integer that specifies the size of the broadcast buffer
* @return a PyObject* that references a boolean to indicate success
* @throws exception upon failure to parse arguments, or to open or rename the buffer capsule, or to build the return value
*/
PyObject* NNNetworkFunctions::P2P_Bcast(PyObject* self, PyObject* args) {
PyObject* capsule = NULL;
size_t size = 0;
NNNetwork* pNetwork = parsePtrAndTwoValues<NNNetwork*, PyObject*, size_t>(args, capsule, size, "neural network", "OOI");
if (pNetwork == NULL) return NULL;
// Save the capsule name, override it with NULL, get the pointer using NULL for the name, and restore the capsule name.
// Ugly, but a way to ignore the capsule name and hence treat the encapsulated pointer as typeless, i.e., void*
char const* name = PyCapsule_GetName(capsule);
// Use PyErr_Occurred() to check for an error instead of checking name == NULL, because the name could in principle be NULL.
if (PyErr_Occurred != NULL) return NULL;
if (PyCapsule_SetName(capsule, NULL) != 0) return NULL;
void* pBuffer = PyCapsule_GetPointer(capsule, NULL);
if (PyCapsule_SetName(capsule, name) != 0) return NULL;
if (pBuffer == NULL) return NULL;
return Py_BuildValue("i", pNetwork->P2P_Bcast(pBuffer, size));
}
/**
* Reduce a buffer across all processes for the neural network.
* @param pNetwork - the encapsulated NNNetwork*
* @param pBuffer - an encapsulated pointer that references the buffer
* @param size - an unsigned integer that specifies the size of the buffer
* @return a PyObject* that references a boolean to indicate success
* @throws exception upon failure to parse arguments, or to open the buffer capsule, or to build the return value
*/
PyObject* NNNetworkFunctions::P2P_Allreduce(PyObject* self, PyObject* args) {
PyObject* capsule = NULL;
size_t size = 0;
NNNetwork* pNetwork = parsePtrAndTwoValues<NNNetwork*, PyObject*, size_t>(args, capsule, size, "neural network", "OOI");
if (pNetwork == NULL) return NULL;
NNFloat* pBuffer = reinterpret_cast<NNFloat*>(PyCapsule_GetPointer(capsule, "float"));
if (pBuffer == NULL) return NULL;
return Py_BuildValue("i", pNetwork->P2P_Allreduce(pBuffer, size));
}
#endif