-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict.cpp
294 lines (261 loc) · 8.37 KB
/
predict.cpp
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
#ifdef __linux__
//#define _GLIBCXX_USE_CXX11_ABI 0 // see https://stackoverflow.com/a/33395489
#include "predict.hpp"
#include "json.hpp"
#include "timer.h"
#include "timer.impl.hpp"
#include <algorithm>
#include <codecvt>
#include <iostream>
#include <locale>
#include <string>
#include <vector>
#include "CNTKLibrary.h"
#include "Eval.h"
using namespace CNTK;
using json = nlohmann::json;
#define CHECK(status) \
{ \
if (status != 0) { \
std::cerr << "Cuda failure on line " << __LINE__ \
<< " status = " << status << "\n"; \
return nullptr; \
} \
}
class Predictor {
public:
Predictor(FunctionPtr modelFunc, DeviceDescriptor device)
: modelFunc_(modelFunc), device_(device){};
void Predict(float *input, const char *output_layer_name,
const int batch_size);
~Predictor() {
if (prof_) {
prof_->reset();
delete prof_;
prof_ = nullptr;
}
}
FunctionPtr modelFunc_{nullptr};
DeviceDescriptor device_{DeviceDescriptor::CPUDevice()};
int pred_len_;
void *result_{nullptr};
bool profile_enabled_{false};
profile *prof_{nullptr};
};
inline std::wstring strtowstr(const std::string &str) {
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
return converter.from_bytes(str);
}
inline std::string wstrtostr(const std::wstring &wstr) {
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
return converter.to_bytes(wstr);
}
void Predictor::Predict(float *input, const char *output_layer_name,
const int batch_size) {
if (result_ != nullptr) {
free(result_);
result_ = nullptr;
}
// Get input variable. The model has only one single input.
Variable inputVar = modelFunc_->Arguments()[0];
Variable outputVar;
if (modelFunc_->Outputs().size() == 1) {
outputVar = modelFunc_->Output();
} else {
const auto outputs = modelFunc_->Outputs();
const auto output_layer_name_string = strtowstr(output_layer_name);
auto f =
std::find_if(outputs.begin(), outputs.end(), [=](const Variable &var) {
if (var.Name() == output_layer_name_string && var.IsOutput()) {
return true;
}
return false;
});
if (f == outputs.end()) {
std::cerr << "cannot find " << std::string(output_layer_name)
<< " in the model. Valid outputs are: \n";
for (const auto out : modelFunc_->Outputs()) {
std::cerr << wstrtostr(out.AsString())
<< " with name = " << wstrtostr(out.Name()) << "\n";
}
std::cerr << "make sure that the layer exists.";
return;
}
outputVar = *f;
}
// Create input value and input data map
std::vector<float> inputData(input, input + inputVar.Shape().TotalSize() *
batch_size);
ValuePtr inputVal = Value::CreateBatch(inputVar.Shape(), inputData, device_);
std::unordered_map<Variable, ValuePtr> inputDataMap = {{inputVar, inputVal}};
// Create output data map. Using null as Value to indicate using system
// allocated memory.
// Alternatively, create a Value object and add it to the data map.
std::unordered_map<Variable, ValuePtr> outputDataMap = {{outputVar, nullptr}};
// Start evaluation on the device
modelFunc_->Evaluate(inputDataMap, outputDataMap, device_);
std::vector<std::vector<float>> resultsWrapper;
CNTK::ValuePtr outputVal = outputDataMap[outputVar];
outputVal.get()->CopyVariableValueTo(outputVar, resultsWrapper);
pred_len_ = resultsWrapper[0].size();
const auto pred_size = pred_len_ * sizeof(float);
std::vector<float> ret;
result_ = (float*) malloc(batch_size * pred_size);
for (int cnt = 0; cnt < batch_size; cnt++) {
memcpy((float *)result_ + cnt * pred_size, resultsWrapper[cnt].data(), pred_size);
}
}
PredictorContext NewCNTK(const char *modelFile, const char *deviceType,
const int deviceID) {
try {
auto device = DeviceDescriptor::CPUDevice();
if (deviceType != nullptr && std::string(deviceType) == "GPU") {
// std::cerr << "cntk is using the gpu!!\n";
device = DeviceDescriptor::GPUDevice(deviceID);
}
auto modelFunc =
Function::Load(strtowstr(modelFile), device, ModelFormat::CNTKv2);
Predictor *pred = new Predictor(modelFunc, device);
return (PredictorContext)pred;
} catch (const std::invalid_argument &ex) {
RuntimeError("exception: %s\n", ex.what());
errno = EINVAL;
return nullptr;
} catch (std::exception &ex) {
RuntimeError("exception: catch all [ %s ]\n", ex.what());
return nullptr;
}
}
void InitCNTK() {}
error_t PredictCNTK(PredictorContext pred, float *input,
const char *output_layer_name, const int batch_size) {
try {
auto predictor = (Predictor *)pred;
if (predictor == nullptr) {
std ::cout << __func__ << " " << __LINE__ << " ... got a null pointer\n";
return error_invalid_memory;
}
predictor->Predict(input, output_layer_name, batch_size);
return success;
} catch (std::exception &ex) {
RuntimeError("exception: catch all [ %s ]\n", ex.what());
return error_exception;
}
}
float *GetPredictionsCNTK(PredictorContext pred) {
try {
auto predictor = (Predictor *)pred;
if (predictor == nullptr) {
return nullptr;
}
if (predictor->result_ == nullptr) {
throw std::runtime_error("expected a non-nil result");
}
return (float *)predictor->result_;
} catch (std::exception &ex) {
RuntimeError("exception: catch all [ %s ]\n", ex.what());
return nullptr;
}
}
void DeleteCNTK(PredictorContext pred) {
try {
auto predictor = (Predictor *)pred;
if (predictor == nullptr) {
return;
}
if (predictor->result_) {
free(predictor->result_);
}
if (predictor->prof_) {
predictor->prof_->reset();
delete predictor->prof_;
predictor->prof_ = nullptr;
}
delete predictor;
} catch (std::exception &ex) {
RuntimeError("exception: catch all [ %s ]\n", ex.what());
return;
}
}
void StartProfilingCNTK(PredictorContext pred, const char *name,
const char *metadata) {
try {
if (name == nullptr) {
name = "";
}
if (metadata == nullptr) {
metadata = "";
}
if (pred == nullptr) {
return;
}
auto predictor = (Predictor *)pred;
predictor->profile_enabled_ = true;
if (predictor->prof_ == nullptr) {
predictor->prof_ = new profile(name, metadata);
} else {
predictor->prof_->reset();
}
} catch (std::exception &ex) {
RuntimeError("exception: catch all [ %s ]\n", ex.what());
return;
}
}
void EndProfilingCNTK(PredictorContext pred) {
try {
auto predictor = (Predictor *)pred;
if (predictor == nullptr) {
return;
}
if (predictor->prof_) {
predictor->prof_->end();
}
} catch (std::exception &ex) {
RuntimeError("exception: catch all [ %s ]\n", ex.what());
return;
}
}
void DisableProfilingCNTK(PredictorContext pred) {
try {
auto predictor = (Predictor *)pred;
if (predictor == nullptr) {
return;
}
if (predictor->prof_) {
predictor->prof_->reset();
}
} catch (std::exception &ex) {
RuntimeError("exception: catch all [ %s ]\n", ex.what());
return;
}
}
char *ReadProfileCNTK(PredictorContext pred) {
try {
auto predictor = (Predictor *)pred;
if (predictor == nullptr) {
return strdup("");
}
if (predictor->prof_ == nullptr) {
return strdup("");
}
const auto s = predictor->prof_->read();
const auto cstr = s.c_str();
return strdup(cstr);
} catch (std::exception &ex) {
RuntimeError("exception: catch all [ %s ]\n", ex.what());
return nullptr;
}
}
int GetPredLenCNTK(PredictorContext pred) {
try {
auto predictor = (Predictor *)pred;
if (predictor == nullptr) {
return 0;
}
return predictor->pred_len_;
} catch (std::exception &ex) {
RuntimeError("exception: catch all [ %s ]\n", ex.what());
return 0;
}
}
#endif // __linux__