You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <malloc.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include "cpu.h"
  5. #define LOG( X ) m_pLog->Log( "HCPU", this, X )
  6. #define LOG_ERROR( X ) m_pLog->Log( "HCPU", this, _LOG_ERROR, X )
  7. float *cpuHOST::makeFloatArray(uint32_t n, float *hostMem){
  8. float *RetVal;
  9. RetVal = (float*)malloc(n * sizeof(float));
  10. if (NULL != RetVal){
  11. if (NULL != hostMem){
  12. memcpy(RetVal, hostMem, n);
  13. } else {
  14. memset(RetVal, 0, n);
  15. };
  16. };
  17. return RetVal;
  18. };
  19. uint32_t *cpuHOST::makeIntArray(uint32_t n, uint32_t *hostMem){
  20. uint32_t *RetVal;
  21. RetVal = (uint32_t*)malloc(n*sizeof(uint32_t));
  22. if (NULL!=RetVal){
  23. if (NULL != hostMem){
  24. memcpy(RetVal, hostMem, n);
  25. } else {
  26. memset(RetVal, 0, n);
  27. };
  28. };
  29. return RetVal;
  30. };
  31. bool cpuHOST::makeRandom(float *cpuMem, uint32_t n){
  32. if( cpuMem == NULL ) return false;
  33. return true;
  34. };
  35. /* Math Functions */
  36. bool cpuHOST::makeReorg(float *x, uint32_t w, uint32_t h, uint32_t c, uint32_t batch, uint32_t stride, uint32_t forward, float *out){
  37. if( stride == 0 ) return false;
  38. if( x == NULL ) return false;
  39. if( out == NULL ) return false;
  40. size_t out_c = c / (stride*stride);
  41. for (uint32_t b = 0; b < batch; ++b){
  42. for (uint32_t k = 0; k < c; ++k){
  43. for (uint32_t j = 0; j < h; ++j){
  44. for (uint32_t i = 0; i < w; ++i){
  45. uint32_t in_index = i + w*(j + h*(k + c*b));
  46. uint32_t c2 = k % out_c;
  47. uint32_t offset = k / out_c;
  48. uint32_t w2 = i*stride + offset % stride;
  49. uint32_t h2 = j*stride + offset / stride;
  50. uint32_t out_index = w2 + w*stride*(h2 + h*stride*(c2 + out_c*b));
  51. if (forward) out[out_index] = x[in_index];
  52. else out[in_index] = x[out_index];
  53. };
  54. };
  55. };
  56. };
  57. return true;
  58. };
  59. bool cpuHOST::makeFlatten(float *x, uint32_t size, uint32_t layers, uint32_t batch, bool forward, float *out){
  60. float *swap = (float*)calloc(size*layers*batch, sizeof(float));
  61. if (NULL == swap) return false;
  62. for (uint32_t b = 0; b < batch; ++b){
  63. for (uint32_t c = 0; c < layers; ++c){
  64. for (uint32_t i = 0; i < size; ++i){
  65. uint32_t i1 = b*layers*size + c*size + i;
  66. uint32_t i2 = b*layers*size + i*layers + c;
  67. if (forward) swap[i2] = x[i1];
  68. else swap[i1] = x[i2];
  69. };
  70. };
  71. };
  72. memcpy(x, swap, size*layers*batch*sizeof(float));
  73. free(swap);
  74. return true;
  75. };
  76. bool cpuHOST::makeShortcut(uint32_t batch, uint32_t w1, uint32_t h1, uint32_t c1, float *add, uint32_t w2, uint32_t h2, uint32_t c2, float *out){
  77. if( w2 == 0 ) return false;
  78. if( w1 == 0 ) return false;
  79. uint32_t stride = w1 / w2;
  80. uint32_t sample = w2 / w1;
  81. assert(stride == h1 / h2);
  82. assert(sample == h2 / h1);
  83. if (stride < 1) stride = 1;
  84. if (sample < 1) sample = 1;
  85. uint32_t minw = (w1 < w2) ? w1 : w2;
  86. uint32_t minh = (h1 < h2) ? h1 : h2;
  87. uint32_t minc = (c1 < c2) ? c1 : c2;
  88. for (uint32_t b = 0; b < batch; ++b){
  89. for (uint32_t k = 0; k < minc; ++k){
  90. for (uint32_t j = 0; j < minh; ++j){
  91. for (uint32_t i = 0; i < minw; ++i){
  92. uint32_t out_index = i*sample + w2*(j*sample + h2*(k + c2*b));
  93. uint32_t add_index = i*stride + w1*(j*stride + h1*(k + c1*b));
  94. out[out_index] += add[add_index];
  95. };
  96. };
  97. };
  98. };
  99. return true;
  100. };
  101. bool cpuHOST::makeNormalized(float *x, float *mean, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial){
  102. if( x == NULL ) return false;
  103. if( mean == NULL ) return false;
  104. if( variance == NULL ) return false;
  105. for (uint32_t b = 0; b < batch; ++b){
  106. for (uint32_t f = 0; f < filters; ++f){
  107. for (uint32_t i = 0; i < spatial; ++i){
  108. uint32_t index = b*filters*spatial + f*spatial + i;
  109. x[index] = (float)((x[index] - mean[f]) / (sqrt(variance[f]) + .000001f));
  110. };
  111. };
  112. };
  113. return true;
  114. };
  115. bool cpuHOST::makeNormalizedDelta(float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, uint32_t batch, uint32_t filters, uint32_t spatial, float *delta){
  116. for (uint32_t j = 0; j < batch; ++j){
  117. for (uint32_t f = 0; f < filters; ++f){
  118. for (uint32_t k = 0; k < spatial; ++k){
  119. uint32_t index = j*filters*spatial + f*spatial + k;
  120. delta[index] = (float)(delta[index] * 1.f / (sqrt(variance[f]) + .00001f) + variance_delta[f] * 2.f * (x[index] - mean[f]) / (spatial * batch) + mean_delta[f] / (spatial*batch));
  121. };
  122. };
  123. };
  124. return true;
  125. };
  126. bool cpuHOST::makeCopy(uint32_t N, float *X, uint32_t INCX, float *Y, uint32_t INCY){
  127. if( X == NULL ) return false;
  128. if( Y == NULL ) return false;
  129. for (uint32_t i = 0; i < N; ++i) Y[i*INCY] = X[i*INCX];
  130. return true;
  131. };
  132. bool cpuHOST::makeSmoothL1(uint32_t n, float *pred, float *truth, float *delta, float *error){
  133. for (uint32_t i = 0; i < n; ++i){
  134. float diff = truth[i] - pred[i];
  135. float abs_val = (float)fabs(diff);
  136. if (abs_val < 1) {
  137. error[i] = diff * diff;
  138. delta[i] = diff;
  139. } else {
  140. error[i] = 2.f*abs_val - 1.f;
  141. delta[i] = (diff < 0) ? -1.f : 1.f;
  142. };
  143. };
  144. return true;
  145. };
  146. bool cpuHOST::makeL2(uint32_t n, float *pred, float *truth, float *delta, float *error){
  147. for (size_t i = 0; i < n; ++i){
  148. float diff = truth[i] - pred[i];
  149. error[i] = diff * diff;
  150. delta[i] = diff;
  151. };
  152. return true;
  153. };
  154. bool cpuHOST::makeSoftMax(float *input, uint32_t n, uint32_t offset, uint32_t groups, float temp, float *output){
  155. //bool cpuHOST::makeSoftMax(float *input, uint32_t n, float temp, float *output){
  156. float sum = .0f;
  157. float largest = -FLT_MAX;
  158. if( input == NULL ) return false;
  159. if( output == NULL ) return false;
  160. for (size_t i = 0; i < n; ++i){
  161. if (input[i] > largest) largest = input[i];
  162. };
  163. for (size_t i = 0; i < n; ++i){
  164. float e = (float)exp(input[i] / temp - largest / temp);
  165. sum += e;
  166. output[i] = e;
  167. };
  168. if( sum == 0.f ) return false;
  169. for (size_t i = 0; i < n; ++i){
  170. output[i] /= sum;
  171. };
  172. return true;
  173. };
  174. #undef LOG
  175. #undef LOG_ERROR