forked from pytorch/glow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
364 lines (322 loc) · 12.4 KB
/
main.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
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
/**
* Copyright (c) Glow Contributors. See CONTRIBUTORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
#include <assert.h>
#include <inttypes.h>
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include "resnet50.h"
/// This is an example demonstrating how to use auto-generated bundles and
/// create standalone executables that can perform neural network computations.
/// This example loads and runs the compiled resnet50 network model.
/// This example is using the dynamic bundle API (default).
#define DEFAULT_HEIGHT 224
#define DEFAULT_WIDTH 224
//===----------------------------------------------------------------------===//
// Image processing helpers
//===----------------------------------------------------------------------===//
std::vector<std::string> inputImageFilenames;
/// \returns the index of the element at x,y,z,w.
size_t getXYZW(const size_t *dims, size_t x, size_t y, size_t z, size_t w) {
return (x * dims[1] * dims[2] * dims[3]) + (y * dims[2] * dims[3]) +
(z * dims[3]) + w;
}
/// \returns the index of the element at x,y,z.
size_t getXYZ(const size_t *dims, size_t x, size_t y, size_t z) {
return (x * dims[1] * dims[2]) + (y * dims[2]) + z;
}
/// Reads a PNG image from a file into a newly allocated memory block \p imageT
/// representing a WxHxNxC tensor and returns it. The client is responsible for
/// freeing the memory block.
bool readPngImage(const char *filename, std::pair<float, float> range,
float *&imageT, size_t *imageDims) {
unsigned char header[8];
// open file and test for it being a png.
FILE *fp = fopen(filename, "rb");
// Can't open the file.
if (!fp) {
return true;
}
// Validate signature.
size_t fread_ret = fread(header, 1, 8, fp);
if (fread_ret != 8) {
return true;
}
if (png_sig_cmp(header, 0, 8)) {
return true;
}
// Initialize stuff.
png_structp png_ptr =
png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png_ptr) {
return true;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
return true;
}
if (setjmp(png_jmpbuf(png_ptr))) {
return true;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
size_t width = png_get_image_width(png_ptr, info_ptr);
size_t height = png_get_image_height(png_ptr, info_ptr);
int color_type = png_get_color_type(png_ptr, info_ptr);
int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
const bool isGray = color_type == PNG_COLOR_TYPE_GRAY;
const size_t numChannels = isGray ? 1 : 3;
(void)bit_depth;
assert(bit_depth == 8 && "Invalid image");
assert((color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
color_type == PNG_COLOR_TYPE_RGB || isGray) &&
"Invalid image");
bool hasAlpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
int number_of_passes = png_set_interlace_handling(png_ptr);
(void)number_of_passes;
assert(number_of_passes == 1 && "Invalid image");
png_read_update_info(png_ptr, info_ptr);
// Error during image read.
if (setjmp(png_jmpbuf(png_ptr))) {
return true;
}
auto *row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * height);
for (size_t y = 0; y < height; y++) {
row_pointers[y] = (png_byte *)malloc(png_get_rowbytes(png_ptr, info_ptr));
}
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, info_ptr);
imageDims[0] = width;
imageDims[1] = height;
imageDims[2] = numChannels;
imageT = static_cast<float *>(
calloc(1, width * height * numChannels * sizeof(float)));
float scale = ((range.second - range.first) / 255.0);
float bias = range.first;
for (size_t row_n = 0; row_n < height; row_n++) {
png_byte *row = row_pointers[row_n];
for (size_t col_n = 0; col_n < width; col_n++) {
png_byte *ptr =
&(row[col_n * (hasAlpha ? (numChannels + 1) : numChannels)]);
if (isGray) {
imageT[getXYZ(imageDims, row_n, col_n, 0)] =
float(ptr[0]) * scale + bias;
} else {
imageT[getXYZ(imageDims, row_n, col_n, 0)] =
float(ptr[0]) * scale + bias;
imageT[getXYZ(imageDims, row_n, col_n, 1)] =
float(ptr[1]) * scale + bias;
imageT[getXYZ(imageDims, row_n, col_n, 2)] =
float(ptr[2]) * scale + bias;
}
}
}
for (size_t y = 0; y < height; y++) {
free(row_pointers[y]);
}
free(row_pointers);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fp);
printf("Loaded image: %s\n", filename);
return false;
}
/// Loads and normalizes all PNGs into a tensor memory block \p resultT in the
/// NCHW 3x224x224 format.
static void loadImagesAndPreprocess(const std::vector<std::string> &filenames,
float *&resultT, size_t *resultDims) {
assert(filenames.size() > 0 &&
"There must be at least one filename in filenames");
std::pair<float, float> range = std::make_pair(0., 1.0);
unsigned numImages = filenames.size();
// N x C x H x W
resultDims[0] = numImages;
resultDims[1] = 3;
resultDims[2] = DEFAULT_HEIGHT;
resultDims[3] = DEFAULT_WIDTH;
size_t resultSizeInBytes =
numImages * 3 * DEFAULT_HEIGHT * DEFAULT_WIDTH * sizeof(float);
resultT = static_cast<float *>(malloc(resultSizeInBytes));
// We iterate over all the png files, reading them all into our result tensor
// for processing
for (unsigned n = 0; n < numImages; n++) {
float *imageT{nullptr};
size_t dims[3];
bool loadSuccess = !readPngImage(filenames[n].c_str(), range, imageT, dims);
assert(loadSuccess && "Error reading input image.");
(void)loadSuccess;
assert((dims[0] == DEFAULT_HEIGHT && dims[1] == DEFAULT_WIDTH) &&
"All images must have the same Height and Width");
// Convert to BGR, as this is what NN is expecting.
for (unsigned z = 0; z < 3; z++) {
for (unsigned y = 0; y < dims[1]; y++) {
for (unsigned x = 0; x < dims[0]; x++) {
resultT[getXYZW(resultDims, n, 2 - z, x, y)] =
imageT[getXYZ(dims, x, y, z)];
}
}
}
}
printf("Loaded images size in bytes is: %lu\n", resultSizeInBytes);
}
/// Parse images file names into a vector.
void parseCommandLineOptions(int argc, char **argv) {
int arg = 1;
while (arg < argc) {
inputImageFilenames.push_back(argv[arg++]);
}
}
//===----------------------------------------------------------------------===//
// Wrapper code for executing a bundle
//===----------------------------------------------------------------------===//
/// Find in the bundle's symbol table a weight variable whose name starts with
/// \p name.
const SymbolTableEntry *getWeightVar(const BundleConfig &config,
const char *name) {
for (unsigned i = 0, e = config.numSymbols; i < e; ++i) {
if (!strncmp(config.symbolTable[i].name, name, strlen(name))) {
return &config.symbolTable[i];
}
}
return nullptr;
}
/// Find in the bundle's symbol table a mutable weight variable whose name
/// starts with \p name.
const SymbolTableEntry &getMutableWeightVar(const BundleConfig &config,
const char *name) {
const SymbolTableEntry *mutableWeightVar = getWeightVar(config, name);
if (!mutableWeightVar) {
printf("Expected to find variable '%s'\n", name);
}
assert(mutableWeightVar && "Expected to find a mutable weight variable");
assert(mutableWeightVar->kind != 0 &&
"Weight variable is expected to be mutable");
return *mutableWeightVar;
}
/// Allocate an aligned block of memory.
void *alignedAlloc(const BundleConfig &config, size_t size) {
void *ptr;
// Properly align the memory region.
int res = posix_memalign(&ptr, config.alignment, size);
assert(res == 0 && "posix_memalign failed");
assert((size_t)ptr % config.alignment == 0 && "Wrong alignment");
memset(ptr, 0, size);
(void)res;
return ptr;
}
/// Initialize the constant weights memory block by loading the weights from the
/// weights file.
static uint8_t *initConstantWeights(const char *weightsFileName,
const BundleConfig &config) {
// Load weights.
FILE *weightsFile = fopen(weightsFileName, "rb");
if (!weightsFile) {
fprintf(stderr, "Could not open the weights file: %s\n", weightsFileName);
exit(1);
}
fseek(weightsFile, 0, SEEK_END);
size_t fileSize = ftell(weightsFile);
fseek(weightsFile, 0, SEEK_SET);
uint8_t *baseConstantWeightVarsAddr =
static_cast<uint8_t *>(alignedAlloc(config, fileSize));
printf("Allocated weights of size: %lu\n", fileSize);
printf("Expected weights of size: %" PRIu64 "\n",
config.constantWeightVarsMemSize);
assert(fileSize == config.constantWeightVarsMemSize &&
"Wrong weights file size");
int result = fread(baseConstantWeightVarsAddr, fileSize, 1, weightsFile);
if (result != 1) {
perror("Could not read the weights file");
} else {
printf("Loaded weights of size: %lu from the file %s\n", fileSize,
weightsFileName);
}
fclose(weightsFile);
return baseConstantWeightVarsAddr;
}
/// The assumed layout of the area for mutable WeightVars is:
/// data | gpu_0/data | results
static uint8_t *allocateMutableWeightVars(const BundleConfig &config) {
auto *weights = static_cast<uint8_t *>(
alignedAlloc(config, config.mutableWeightVarsMemSize));
printf("Allocated mutable weight variables of size: %" PRIu64 "\n",
config.mutableWeightVarsMemSize);
return weights;
}
/// Dump the result of the inference by looking at the results vector and
/// finding the index of the max element.
static void dumpInferenceResults(const BundleConfig &config,
uint8_t *mutableWeightVars) {
const SymbolTableEntry &outputWeights =
getMutableWeightVar(config, "gpu_0_softmax");
int maxIdx = 0;
float maxValue = 0;
float *results = (float *)(mutableWeightVars + outputWeights.offset);
for (int i = 0; i < (int)outputWeights.size; ++i) {
if (results[i] > maxValue) {
maxValue = results[i];
maxIdx = i;
}
}
printf("Result: %u\n", maxIdx);
printf("Confidence: %f\n", maxValue);
}
/// The assumed layout of the area for mutable WeightVars is:
/// data | gpu_0/data | results
static uint8_t *initMutableWeightVars(const BundleConfig &config) {
uint8_t *mutableWeightVarsAddr = allocateMutableWeightVars(config);
size_t inputDims[4];
float *inputT{nullptr};
loadImagesAndPreprocess(inputImageFilenames, inputT, inputDims);
// Copy image data into the gpu_0/data input variable in the
// mutableWeightVars area.
size_t imageDataSizeInBytes =
inputDims[0] * inputDims[1] * inputDims[2] * inputDims[3] * sizeof(float);
printf("Copying image data into mutable weight vars: %lu bytes\n",
imageDataSizeInBytes);
const SymbolTableEntry &inputGPUDataVar =
getMutableWeightVar(config, "gpu_0_data");
memcpy(mutableWeightVarsAddr + inputGPUDataVar.offset, inputT,
imageDataSizeInBytes);
return mutableWeightVarsAddr;
}
static uint8_t *initActivations(const BundleConfig &config) {
return static_cast<uint8_t *>(
alignedAlloc(config, config.activationsMemSize));
}
int main(int argc, char **argv) {
parseCommandLineOptions(argc, argv);
// Allocate and initialize constant and mutable weights.
uint8_t *constantWeightVarsAddr =
initConstantWeights("resnet50.weights.bin", resnet50_config);
uint8_t *mutableWeightVarsAddr = initMutableWeightVars(resnet50_config);
uint8_t *activationsAddr = initActivations(resnet50_config);
// Perform the computation.
int errCode =
resnet50(constantWeightVarsAddr, mutableWeightVarsAddr, activationsAddr);
if (errCode != GLOW_SUCCESS) {
printf("Error running bundle: error code %d\n", errCode);
}
// Report the results.
dumpInferenceResults(resnet50_config, mutableWeightVarsAddr);
// Free all resources.
free(activationsAddr);
free(constantWeightVarsAddr);
free(mutableWeightVarsAddr);
}