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
190 lines
5.6 KiB
#include <malloc.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include "cpu.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "HCPU", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "HCPU", this, _LOG_ERROR, X )
|
|
|
|
float *cpuHOST::makeFloatArray(uint32_t n, float *hostMem){
|
|
float *RetVal;
|
|
RetVal = (float*)malloc(n * sizeof(float));
|
|
if (NULL != RetVal){
|
|
if (NULL != hostMem){
|
|
memcpy(RetVal, hostMem, n);
|
|
} else {
|
|
memset(RetVal, 0, n);
|
|
};
|
|
};
|
|
return RetVal;
|
|
};
|
|
|
|
uint32_t *cpuHOST::makeIntArray(uint32_t n, uint32_t *hostMem){
|
|
uint32_t *RetVal;
|
|
RetVal = (uint32_t*)malloc(n*sizeof(uint32_t));
|
|
if (NULL!=RetVal){
|
|
if (NULL != hostMem){
|
|
memcpy(RetVal, hostMem, n);
|
|
} else {
|
|
memset(RetVal, 0, n);
|
|
};
|
|
};
|
|
return RetVal;
|
|
};
|
|
|
|
bool cpuHOST::makeRandom(float *cpuMem, uint32_t n){
|
|
if( cpuMem == NULL ) return false;
|
|
return true;
|
|
};
|
|
|
|
/* Math Functions */
|
|
|
|
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){
|
|
if( stride == 0 ) return false;
|
|
if( x == NULL ) return false;
|
|
if( out == NULL ) return false;
|
|
size_t out_c = c / (stride*stride);
|
|
for (uint32_t b = 0; b < batch; ++b){
|
|
for (uint32_t k = 0; k < c; ++k){
|
|
for (uint32_t j = 0; j < h; ++j){
|
|
for (uint32_t i = 0; i < w; ++i){
|
|
uint32_t in_index = i + w*(j + h*(k + c*b));
|
|
uint32_t c2 = k % out_c;
|
|
uint32_t offset = k / out_c;
|
|
uint32_t w2 = i*stride + offset % stride;
|
|
uint32_t h2 = j*stride + offset / stride;
|
|
uint32_t out_index = w2 + w*stride*(h2 + h*stride*(c2 + out_c*b));
|
|
if (forward) out[out_index] = x[in_index];
|
|
else out[in_index] = x[out_index];
|
|
};
|
|
};
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool cpuHOST::makeFlatten(float *x, uint32_t size, uint32_t layers, uint32_t batch, bool forward, float *out){
|
|
float *swap = (float*)calloc(size*layers*batch, sizeof(float));
|
|
if (NULL == swap) return false;
|
|
for (uint32_t b = 0; b < batch; ++b){
|
|
for (uint32_t c = 0; c < layers; ++c){
|
|
for (uint32_t i = 0; i < size; ++i){
|
|
uint32_t i1 = b*layers*size + c*size + i;
|
|
uint32_t i2 = b*layers*size + i*layers + c;
|
|
if (forward) swap[i2] = x[i1];
|
|
else swap[i1] = x[i2];
|
|
};
|
|
};
|
|
};
|
|
memcpy(x, swap, size*layers*batch*sizeof(float));
|
|
free(swap);
|
|
return true;
|
|
};
|
|
|
|
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){
|
|
if( w2 == 0 ) return false;
|
|
if( w1 == 0 ) return false;
|
|
uint32_t stride = w1 / w2;
|
|
uint32_t sample = w2 / w1;
|
|
assert(stride == h1 / h2);
|
|
assert(sample == h2 / h1);
|
|
if (stride < 1) stride = 1;
|
|
if (sample < 1) sample = 1;
|
|
uint32_t minw = (w1 < w2) ? w1 : w2;
|
|
uint32_t minh = (h1 < h2) ? h1 : h2;
|
|
uint32_t minc = (c1 < c2) ? c1 : c2;
|
|
for (uint32_t b = 0; b < batch; ++b){
|
|
for (uint32_t k = 0; k < minc; ++k){
|
|
for (uint32_t j = 0; j < minh; ++j){
|
|
for (uint32_t i = 0; i < minw; ++i){
|
|
uint32_t out_index = i*sample + w2*(j*sample + h2*(k + c2*b));
|
|
uint32_t add_index = i*stride + w1*(j*stride + h1*(k + c1*b));
|
|
out[out_index] += add[add_index];
|
|
};
|
|
};
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool cpuHOST::makeNormalized(float *x, float *mean, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial){
|
|
if( x == NULL ) return false;
|
|
if( mean == NULL ) return false;
|
|
if( variance == NULL ) return false;
|
|
for (uint32_t b = 0; b < batch; ++b){
|
|
for (uint32_t f = 0; f < filters; ++f){
|
|
for (uint32_t i = 0; i < spatial; ++i){
|
|
uint32_t index = b*filters*spatial + f*spatial + i;
|
|
x[index] = (float)((x[index] - mean[f]) / (sqrt(variance[f]) + .000001f));
|
|
};
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
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){
|
|
for (uint32_t j = 0; j < batch; ++j){
|
|
for (uint32_t f = 0; f < filters; ++f){
|
|
for (uint32_t k = 0; k < spatial; ++k){
|
|
uint32_t index = j*filters*spatial + f*spatial + k;
|
|
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));
|
|
};
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool cpuHOST::makeCopy(uint32_t N, float *X, uint32_t INCX, float *Y, uint32_t INCY){
|
|
if( X == NULL ) return false;
|
|
if( Y == NULL ) return false;
|
|
for (uint32_t i = 0; i < N; ++i) Y[i*INCY] = X[i*INCX];
|
|
return true;
|
|
};
|
|
|
|
bool cpuHOST::makeSmoothL1(uint32_t n, float *pred, float *truth, float *delta, float *error){
|
|
for (uint32_t i = 0; i < n; ++i){
|
|
float diff = truth[i] - pred[i];
|
|
float abs_val = (float)fabs(diff);
|
|
if (abs_val < 1) {
|
|
error[i] = diff * diff;
|
|
delta[i] = diff;
|
|
} else {
|
|
error[i] = 2.f*abs_val - 1.f;
|
|
delta[i] = (diff < 0) ? -1.f : 1.f;
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool cpuHOST::makeL2(uint32_t n, float *pred, float *truth, float *delta, float *error){
|
|
for (size_t i = 0; i < n; ++i){
|
|
float diff = truth[i] - pred[i];
|
|
error[i] = diff * diff;
|
|
delta[i] = diff;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool cpuHOST::makeSoftMax(float *input, uint32_t n, uint32_t offset, uint32_t groups, float temp, float *output){
|
|
//bool cpuHOST::makeSoftMax(float *input, uint32_t n, float temp, float *output){
|
|
float sum = .0f;
|
|
float largest = -FLT_MAX;
|
|
if( input == NULL ) return false;
|
|
if( output == NULL ) return false;
|
|
for (size_t i = 0; i < n; ++i){
|
|
if (input[i] > largest) largest = input[i];
|
|
};
|
|
for (size_t i = 0; i < n; ++i){
|
|
float e = (float)exp(input[i] / temp - largest / temp);
|
|
sum += e;
|
|
output[i] = e;
|
|
};
|
|
if( sum == 0.f ) return false;
|
|
for (size_t i = 0; i < n; ++i){
|
|
output[i] /= sum;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|