Repository navigation
Expand file tree
/
Copy pathlibmatrixdnn.cpp
More file actions
454 lines (404 loc) · 15.6 KB
/
Copy pathlibmatrixdnn.cpp
File metadata and controls
454 lines (404 loc) · 15.6 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 "config.h"
#include "libmatrixmult.h"
#include "libmatrixdnn.h"
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#ifdef USE_INTEL_MKL
#include "mkl_dnn.h"
#else
#include "omp.h"
#endif
int computeNNZ(double* arr, int limit) {
int nnz = 0;
#ifndef USE_INTEL_MKL
#pragma omp parallel for reduction(+: nnz)
#endif
for(int i=0; i<limit; i++)
nnz += (arr[i]!=0) ? 1 : 0;
return nnz;
}
void rotate180(double* inputArray, double* outputArray, int N, int C, int H, int W,
int K, int R, int S, int stride_h, int stride_w, int pad_h,
int pad_w, int P, int Q) {
int PQ = P*Q;
int KQ = K*Q;
for (int k = 0; k < K; k++) {
for (int p = 0; p < P; p++) {
for (int q = 0; q < Q; q++) {
outputArray[p*KQ + q*K + k] = inputArray[k*PQ + p*Q + q];
}
}
}
}
void col2im(double* inputArray, double* outputArray, int N, int C, int H, int W,
int K, int R, int S, int stride_h, int stride_w, int pad_h,
int pad_w, int P, int Q) {
for (int p = 0; p < P; p++) {
// h = p*stride_h + r - pad_h
// = r + hOffset
// Based on restrictions: h >= 0 and r >= 0 and h < H and r < R, we get
// max(0, - hOffset) <= r < min(R, H - hOffset)
int hOffset = p*stride_h - pad_h;
int rStart = MAX(0, - hOffset);
int rEnd = MIN(R, H - hOffset);
for (int q = 0; q < Q; q++) {
// Using the same logic as above on following:
// w = q*stride_w + s - pad_w
int wOffset = q*stride_w - pad_w;
int sStart = MAX(0, - wOffset);
int sEnd = MIN(S, W - wOffset);
int tempOffset = (p*Q + q)*C*R*S;
for (int c = 0; c < C; c++) {
int outOffset = c*H*W;
int inputOffset = tempOffset + c*R*S;
for (int r = rStart; r < rEnd; r++) {
for (int s = sStart; s < sEnd; s++) {
int inputIndex = inputOffset + r*S + s;
int outIndex = outOffset + (hOffset + r)*W + wOffset + s;
outputArray[outIndex] += inputArray[inputIndex];
}
}
}
}
}
}
void im2col(double* inputArray, double* outputArray, int N, int C, int H, int W,
int K, int R, int S, int stride_h, int stride_w, int pad_h,
int pad_w, int P, int Q) {
int CRS = C * R * S;
std::size_t size = Q * sizeof(double);
if (stride_h == 1 && stride_w == 1 && pad_h == 0 && pad_w == 0) {
for (int c = 0; c < CRS; ++c) {
int wOffset = c % S;
int hOffset = (c / S) % R;
int cInput = c / R / S;
for (int h = 0; h < P; ++h) {
int hPadded = h + hOffset;
int outOffset = (c * P + h) * Q;
int inputOffset = (cInput * H + hPadded) * W;
std::memcpy(outputArray + outOffset, inputArray + inputOffset + wOffset,
size);
int w = Q - 1;
int wPadded = w + wOffset;
if (hPadded < H && wPadded < W)
outputArray[outOffset + w] = inputArray[inputOffset + wPadded];
else
outputArray[outOffset + w] = 0;
}
}
} else {
for (int c = 0; c < CRS; ++c) {
int wOffset = c % S;
int hOffset = (c / S) % R;
int cInput = c / R / S;
for (int h = 0; h < P; ++h) {
int outOffset = (c * P + h) * Q;
int hPadded = h * stride_h - pad_h + hOffset;
int inputOffset = (cInput * H + hPadded) * W;
if (hPadded < 0 || hPadded >= H) {
std::memset(outputArray + outOffset, 0, size);
} else {
for (int w = 0; w < Q; ++w) {
int wPadded = w * stride_w - pad_w + wOffset;
if (wPadded >= 0 && wPadded < W)
outputArray[outOffset + w] = inputArray[inputOffset + wPadded];
else
outputArray[outOffset + w] = 0;
}
}
}
}
}
}
#ifdef USE_INTEL_MKL
// Returns true if error
bool MKL_DNN_ERROR(dnnError_t code) {
if(code == E_SUCCESS) return false;
else if(code == E_INCORRECT_INPUT_PARAMETER) std::cerr << "ERROR: Incorrect input parameter\n";
else if(code == E_MEMORY_ERROR) std::cerr << "ERROR: Memory error\n";
else if(code == E_UNSUPPORTED_DIMENSION) std::cerr << "ERROR: Unsupported dimensions\n";
else if(code == E_UNIMPLEMENTED) std::cerr << "ERROR: Unimplemented operation\n";
return true;
}
#endif
int conv2dBackwardFilterDense(double* inputPtr, double* doutPtr, double* retPtr, int N, int C, int H, int W, int K, int R, int S,
int stride_h, int stride_w, int pad_h, int pad_w, int P, int Q, int numThreads) {
int CRS = C*R*S;
#ifdef USE_INTEL_MKL
setNumThreadsForBLAS(numThreads);
// Step 1: Create a description of a DNN operation
dnnPrimitive_t pConvolution;
size_t dimension = 4;
size_t srcSize[4] = {W, H, C, N};
size_t dstSize[4] = {Q, P, K, N};
size_t filterSize[4] = {S, R, C, K};
size_t convolutionStrides[2] = {stride_w, stride_h};
int pads[2] = {-pad_w, -pad_h};
void* resources[dnnResourceNumber] = {0};
resources[dnnResourceDiffDst] = doutPtr;
resources[dnnResourceSrc] = inputPtr;
resources[dnnResourceDiffFilter] = retPtr;
dnnConvolutionCreateBackwardFilter_F64(&pConvolution, NULL, dnnAlgorithmConvolutionDirect, dimension,
srcSize, dstSize, filterSize, convolutionStrides, pads, dnnBorderZeros);
// Step 2: Perform the DNN operation
if(MKL_DNN_ERROR(dnnExecute_F64(pConvolution, resources))) {
return -1; // nnz == -1 indicates error.
}
// Step 3: Destroy the description of the operation
dnnDelete_F64(pConvolution);
#else
// First step: Avoids oversubscription and other openmp/internal blas threading issues
setNumThreadsForBLAS(1);
int CHW = C * H * W;
int PQ = P*Q;
int KPQ = K*PQ;
int numRotatedElem = KPQ;
int numIm2ColElem = CRS * PQ;
int numTempElem = CRS * K;
int m1 = CRS;
int n1 = K;
int k1 = PQ;
// Allocate temporary data structures used in parallel for
int numOpenMPThreads = MIN(numThreads, N);
double* temp = new double[numTempElem*numOpenMPThreads];
std::memset(temp, 0, numTempElem*numOpenMPThreads*sizeof(double));
double* rotatedDoutPtrArrays = new double[numRotatedElem*numOpenMPThreads];
double* loweredMatArrays = new double[numIm2ColElem*numOpenMPThreads];
#pragma omp parallel for num_threads(numOpenMPThreads)
for (int n = 0; n < N; n++) {
int threadID = omp_get_thread_num();
double* loweredMat = loweredMatArrays + numIm2ColElem*threadID;
// Step 1: Perform im2col
im2col(inputPtr + n * CHW, loweredMat, 1, C, H, W, K,
R, S, stride_h, stride_w, pad_h, pad_w,
P, Q);
// Step 2: Rotate dout
double* rotatedDoutPtr = rotatedDoutPtrArrays + numRotatedElem*threadID;
rotate180(doutPtr + n * KPQ, rotatedDoutPtr, 1, C, H, W, K,
R, S, stride_h, stride_w, pad_h, pad_w,
P, Q);
// Multiply to get tmp1 = CRS X K
double* temp1 = temp + numTempElem*threadID;
// Step 3: temp1 = alpha * (loweredMat (CRS X PQ) %*% rotated_dout (PQ X K)) + beta*temp1
int m1rlen = C * R * S; int m1clen = P * Q; int m2clen = K;
double* m1Ptr = loweredMat; double* m2Ptr = rotatedDoutPtr; double alpha = 1; double beta = 1;
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m1rlen, m2clen, m1clen, alpha, m1Ptr, m1clen, m2Ptr, m2clen, beta, temp1, m2clen);
} // end omp parallel for
delete [] loweredMatArrays;
delete [] rotatedDoutPtrArrays;
// Inplace transpose addition
int numRow = CRS;
for(int t = 0; t < numOpenMPThreads; t++) {
int iter = 0;
double* temp1 = temp + numTempElem*t;
for(int i = 0; i < CRS; i++) {
for(int j = 0; j < K; j++, iter++) {
int index = j*numRow+i;
retPtr[index] += temp1[iter];
}
}
}
delete [] temp;
#endif
return computeNNZ(retPtr, K*CRS);
}
int conv2dBackwardDataDense(double* filterPtr, double* doutPtr, double* retPtr, int N, int C, int H, int W, int K, int R, int S,
int stride_h, int stride_w, int pad_h, int pad_w, int P, int Q, int numThreads) {
int CHW = C * H * W;
#ifdef USE_INTEL_MKL
setNumThreadsForBLAS(numThreads);
// Step 1: Create a description of a DNN operation
dnnPrimitive_t pConvolution;
size_t dimension = 4;
size_t srcSize[4] = {W, H, C, N};
size_t dstSize[4] = {Q, P, K, N};
size_t filterSize[4] = {S, R, C, K};
size_t convolutionStrides[2] = {stride_w, stride_h};
int pads[2] = {-pad_w, -pad_h};
void* resources[dnnResourceNumber] = {0};
resources[dnnResourceDiffDst] = doutPtr;
resources[dnnResourceFilter] = filterPtr;
resources[dnnResourceDiffSrc] = retPtr;
dnnConvolutionCreateBackwardData_F64(&pConvolution, NULL, dnnAlgorithmConvolutionDirect, dimension,
srcSize, dstSize, filterSize, convolutionStrides, pads, dnnBorderZeros);
// Step 2: Perform the DNN operation
if(MKL_DNN_ERROR(dnnExecute_F64(pConvolution, resources))) {
return -1; // nnz == -1 indicates error.
}
// Step 3: Destroy the description of the operation
dnnDelete_F64(pConvolution);
#else
// First step: Avoids oversubscription and other openmp/internal blas threading issues
setNumThreadsForBLAS(1);
int CRS = C * R * S;
int PQ = P * Q;
int KPQ = K * PQ;
int numRotatedElem = PQ * K;
int numCol2ImElem = PQ * CRS;
// Allocate temporary data structures used in parallel for
int numOpenMPThreads = MIN(numThreads, N);
double* rotatedDoutPtrArrays = new double[numRotatedElem*numOpenMPThreads];
double* col2imInputArrays = new double[numCol2ImElem*numOpenMPThreads];
#pragma omp parallel for num_threads(numOpenMPThreads)
for (int n = 0; n < N; n++) {
int threadID = omp_get_thread_num();
// Step 1: Rotate dout
double* rotatedDoutPtr = rotatedDoutPtrArrays + numRotatedElem*threadID;
rotate180(doutPtr + n * KPQ, rotatedDoutPtr, 1, C, H, W, K,
R, S, stride_h, stride_w, pad_h, pad_w,
P, Q);
// Step 2: t(rotatedDout (PQ X K) %*% filter (K X CRS))
double* col2imInput = col2imInputArrays + numCol2ImElem*threadID;
matmult(rotatedDoutPtr, filterPtr, col2imInput,
PQ, K, CRS, 1);
// Step 3: Perform col2im
double* outputArr = retPtr + n * CHW;
col2im(col2imInput, outputArr, 1, C, H, W, K,
R, S, stride_h, stride_w, pad_h, pad_w,
P, Q);
} // end omp parallel for
delete [] rotatedDoutPtrArrays;
delete [] col2imInputArrays;
#endif
return computeNNZ(retPtr, N*CHW);
}
void conv2dSparse(int apos, int alen, int* aix, double* avals, double* filterPtr, double* retPtr, int N, int C, int H, int W,
int K, int R, int S, int stride_h, int stride_w, int pad_h, int pad_w, int P, int Q, int numThreads) {
setNumThreadsForBLAS(1);
double* loweredMat = new double[C * R * S * P * Q];
// Step 1: Perform im2col
double* temp = new double[C * H * W];
std::size_t size = C * H * W * sizeof(double);
std::memset(temp, 0, size);
for(int j=apos; j<apos+alen; j++)
temp[ aix[j] ] = avals[j];
im2col(temp, loweredMat, 1, C, H, W, K,
R, S, stride_h, stride_w, pad_h, pad_w,
P, Q);
delete [] temp;
// Step 2: filter (K X CRS) %*% loweredMat (CRS X PQ)
matmult(filterPtr, loweredMat, retPtr, K, C * R * S, P * Q, 1);
delete [] loweredMat;
}
void conv2dBackwardFilterSparseDense(int apos, int alen, int* aix, double* avals, double* rotatedDoutPtr, double* retPtr, int N, int C, int H, int W,
int K, int R, int S, int stride_h, int stride_w, int pad_h, int pad_w, int P, int Q, int numThreads) {
setNumThreadsForBLAS(1);
int CHW = C * H * W;
int CRS = C*R*S;
int PQ = P*Q;
int KPQ = K*PQ;
int m1 = CRS;
int n1 = K;
int k1 = PQ;
double* loweredMat = new double[CRS * PQ];
// Step 1: Perform im2col
double* temp = new double[C * H * W];
std::size_t size = C * H * W * sizeof(double);
std::memset(temp, 0, size);
for(int j=apos; j<apos+alen; j++)
temp[ aix[j] ] = avals[j];
im2col(temp, loweredMat, 1, C, H, W, K,
R, S, stride_h, stride_w, pad_h, pad_w,
P, Q);
delete [] temp;
// Multiply to get CRS X K
double* temp1 = new double[CRS * K];
// Step 3: loweredMat (CRS X PQ) %*% rotatedDoutPtr (PQ X K)
matmult(loweredMat, rotatedDoutPtr, temp1, C * R * S, P * Q, K, 1);
delete [] loweredMat;
// Inplace addition
for(int iter = 0; iter<K*CRS; iter++) {
retPtr[iter] += temp1[iter];
}
delete [] temp1;
}
int conv2dBiasAddDense(double* inputPtr, double* biasPtr, double* filterPtr, double* retPtr, int N, int C, int H, int W, int K, int R, int S,
int stride_h, int stride_w, int pad_h, int pad_w, int P, int Q, bool addBias, int numThreads) {
int KPQ = K * P * Q;
#ifdef USE_INTEL_MKL
setNumThreadsForBLAS(numThreads);
// Step 1: Create a description of a DNN operation
dnnPrimitive_t pConvolution;
size_t dimension = 4;
size_t srcSize[4] = {W, H, C, N};
size_t dstSize[4] = {Q, P, K, N};
size_t filterSize[4] = {S, R, C, K};
size_t convolutionStrides[2] = {stride_w, stride_h};
int pads[2] = {-pad_w, -pad_h};
void* resources[dnnResourceNumber] = {0};
resources[dnnResourceSrc] = inputPtr;
resources[dnnResourceFilter] = filterPtr;
resources[dnnResourceDst] = retPtr;
if(addBias) {
dnnConvolutionCreateForwardBias_F64(&pConvolution, NULL, dnnAlgorithmConvolutionDirect, dimension,
srcSize, dstSize, filterSize, convolutionStrides, pads, dnnBorderZeros);
resources[dnnResourceBias] = biasPtr;
}
else {
dnnConvolutionCreateForward_F64(&pConvolution, NULL, dnnAlgorithmConvolutionDirect, dimension,
srcSize, dstSize, filterSize, convolutionStrides, pads, dnnBorderZeros);
}
// Step 2: Perform the DNN operation
if(MKL_DNN_ERROR(dnnExecute_F64(pConvolution, resources))) {
return -1; // nnz == -1 indicates error.
}
// Step 3: Destroy the description of the operation
dnnDelete_F64(pConvolution);
#else
// ------------------------------------------------------------------------------------
// First step: Avoids oversubscription and other openmp/internal blas threading issues
setNumThreadsForBLAS(1);
int CHW = C * H * W;
int PQ = P * Q;
int numIm2ColElem = C * R * S * P * Q;
// Allocate temporary data structures used in parallel for
int numOpenMPThreads = MIN(numThreads, N);
double* loweredMatArrays = new double[numIm2ColElem*numOpenMPThreads];
#pragma omp parallel for num_threads(numOpenMPThreads)
for (int n = 0; n < N; n++) {
int threadID = omp_get_thread_num();
double* loweredMat = loweredMatArrays + numIm2ColElem*threadID;
// Step 1: Perform im2col
im2col(inputPtr + n * CHW, loweredMat, 1, C, H, W, K,
R, S, stride_h, stride_w, pad_h, pad_w,
P, Q);
// Step 2: filter (K X CRS) %*% loweredMat (CRS X PQ)
matmult(filterPtr, loweredMat, retPtr + n * KPQ, K,
C * R * S, P * Q, 1);
// Step 3: Add bias
double* outputArr = retPtr + n*KPQ;
if(addBias) {
int index = 0;
for(int k = 0; k < K; k++) {
for(int pq = 0; pq < PQ; pq++, index++) {
outputArr[index] += biasPtr[k];
}
}
}
} // end omp parallel for
delete [] loweredMatArrays;
// ------------------------------------------------------------------------------------
#endif
return computeNNZ(retPtr, N*KPQ);
}