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.
614 lines
22 KiB
614 lines
22 KiB
#include "cuda_runtime.h"
|
|
#include "curand.h"
|
|
#include "cublas_v2.h"
|
|
#include <assert.h>
|
|
#include "../cpu/blas.h"
|
|
#include "cuda.h"
|
|
#include "../base/basicCortex.h"
|
|
|
|
__global__ void const_kernel(int N, float ALPHA, float *X, int INCX)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(i < N) X[i*INCX] = ALPHA;
|
|
}
|
|
|
|
__global__ void constrain_kernel(int N, float ALPHA, float *X, int INCX)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(i < N) X[i*INCX] = fminf(ALPHA, fmaxf(-ALPHA, X[i*INCX]));
|
|
}
|
|
|
|
__global__ void fill_kernel(int N, float ALPHA, float *X, int INCX)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(i < N) X[i*INCX] = ALPHA;
|
|
}
|
|
|
|
__global__ void mask_kernel(int n, float *x, float mask_num, float *mask)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(i < n && mask[i] == mask_num) x[i] = mask_num;
|
|
}
|
|
|
|
__global__ void copy_kernel(int N, float *X, int OFFX, int INCX, float *Y, int OFFY, int INCY)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(i < N) Y[i*INCY + OFFY] = X[i*INCX + OFFX];
|
|
}
|
|
|
|
__global__ void backward_scale_kernel(float *x_norm, float *delta, int batch, int n, int size, float *scale_updates)
|
|
{
|
|
__shared__ float part[BLOCK];
|
|
int i,b;
|
|
int filter = blockIdx.x;
|
|
int p = threadIdx.x;
|
|
float sum = 0;
|
|
for(b = 0; b < batch; ++b){
|
|
for(i = 0; i < size; i += BLOCK){
|
|
int index = p + i + size*(filter + n*b);
|
|
sum += (p+i < size) ? delta[index]*x_norm[index] : 0;
|
|
}
|
|
}
|
|
part[p] = sum;
|
|
__syncthreads();
|
|
if (p == 0) {
|
|
for(i = 0; i < BLOCK; ++i) scale_updates[filter] += part[i];
|
|
}
|
|
}
|
|
|
|
bool gpuCUDA::makeBackwardScale(float *x_norm, float *delta, uint32_t batch, uint32_t n, uint32_t size, float *scale_updates){
|
|
backward_scale_kernel<<<n, BLOCK>>>(x_norm, delta, batch, n, size, scale_updates);
|
|
return checkError();
|
|
};
|
|
|
|
__global__ void backward_bias_kernel(float *bias_updates, float *delta, int batch, int n, int size)
|
|
{
|
|
__shared__ float part[BLOCK];
|
|
int i,b;
|
|
int filter = blockIdx.x;
|
|
int p = threadIdx.x;
|
|
float sum = 0;
|
|
for(b = 0; b < batch; ++b){
|
|
for(i = 0; i < size; i += BLOCK){
|
|
int index = p + i + size*(filter + n*b);
|
|
sum += (p+i < size) ? delta[index] : 0;
|
|
}
|
|
}
|
|
part[p] = sum;
|
|
__syncthreads();
|
|
if (p == 0) {
|
|
for(i = 0; i < BLOCK; ++i) bias_updates[filter] += part[i];
|
|
}
|
|
}
|
|
|
|
__global__ void dot_kernel(float *output, float scale, int batch, int n, int size, float *delta)
|
|
{
|
|
int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
int f1 = index / n;
|
|
int f2 = index % n;
|
|
if (f2 <= f1) return;
|
|
|
|
float sum = 0;
|
|
float norm1 = 0;
|
|
float norm2 = 0;
|
|
int b, i;
|
|
for(b = 0; b < batch; ++b){
|
|
for(i = 0; i < size; ++i){
|
|
int i1 = b * size * n + f1 * size + i;
|
|
int i2 = b * size * n + f2 * size + i;
|
|
sum += output[i1] * output[i2];
|
|
norm1 += output[i1] * output[i1];
|
|
norm2 += output[i2] * output[i2];
|
|
}
|
|
}
|
|
norm1 = sqrt(norm1);
|
|
norm2 = sqrt(norm2);
|
|
float norm = norm1 * norm2;
|
|
sum = sum / norm;
|
|
for(b = 0; b < batch; ++b){
|
|
for(i = 0; i < size; ++i){
|
|
int i1 = b * size * n + f1 * size + i;
|
|
int i2 = b * size * n + f2 * size + i;
|
|
delta[i1] += - scale * sum * output[i2] / norm;
|
|
delta[i2] += - scale * sum * output[i1] / norm;
|
|
}
|
|
}
|
|
};
|
|
|
|
bool gpuCUDA::makeDotError(cortexLayer *l){
|
|
dot_kernel <<< gridSize(l->m_nN*l->m_nN), BLOCK >>> (l->m_pGpuOutput, l->m_fDOT, l->m_nBatch, l->m_nN, l->m_nOut_Width * l->m_nOut_Height, l->m_pGpuDelta);
|
|
return checkError();
|
|
};
|
|
|
|
bool gpuCUDA::makeBackwardBias(float *bias_updates, float *delta, uint32_t batch, uint32_t n, uint32_t size){
|
|
backward_bias_kernel<<<n, BLOCK>>>(bias_updates, delta, batch, n, size);
|
|
return checkError();
|
|
};
|
|
|
|
__global__ void adam_kernel(int N, float *x, float *m, float *v, float B1, float B2, float rate, float eps, int t)
|
|
{
|
|
int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if (index >= N) return;
|
|
|
|
x[index] = x[index] - (rate * sqrt(1.-pow(B2, t)) / (1.-pow(B1, t)) * m[index] / (sqrt(v[index]) + eps));
|
|
//if(index == 0) printf("%f %f %f %f\n", m[index], v[index], (rate * sqrt(1.-pow(B2, t)) / (1.-pow(B1, t)) * m[index] / (sqrt(v[index]) + eps)));
|
|
}
|
|
|
|
//extern "C" void adam_gpu(int n, float *x, float *m, float *v, float B1, float B2, float rate, float eps, int t)
|
|
bool gpuCUDA::makeAdam(uint32_t n, float *x, float *m, float *v, float B1, float B2, float rate, float eps, uint32_t t){
|
|
adam_kernel<<<gridSize(n), BLOCK>>>(n, x, m, v, B1, B2, rate, eps, t);
|
|
return checkError();
|
|
};
|
|
|
|
__global__ void normalize_kernel(int N, float *x, float *mean, float *variance, int batch, int filters, int spatial)
|
|
{
|
|
int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if (index >= N) return;
|
|
int f = (index/spatial)%filters;
|
|
|
|
x[index] = (x[index] - mean[f])/(sqrt(variance[f]) + .000001f);
|
|
}
|
|
|
|
__global__ void normalize_delta_kernel(int N, float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, int batch, int filters, int spatial, float *delta)
|
|
{
|
|
int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if (index >= N) return;
|
|
int f = (index/spatial)%filters;
|
|
|
|
delta[index] = delta[index] * 1./(sqrt(variance[f]) + .000001f) + variance_delta[f] * 2. * (x[index] - mean[f]) / (spatial * batch) + mean_delta[f]/(spatial*batch);
|
|
}
|
|
|
|
//extern "C" void normalize_delta_gpu(float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, int batch, int filters, int spatial, float *delta)
|
|
bool gpuCUDA::makeNormalizedDelta(float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, uint32_t batch, uint32_t filters, uint32_t spatial, float *delta){
|
|
unsigned int N = batch*filters*spatial;
|
|
normalize_delta_kernel<<<gridSize(N), BLOCK>>>(N, x, mean, variance, mean_delta, variance_delta, batch, filters, spatial, delta);
|
|
return checkError();
|
|
};
|
|
|
|
__global__ void variance_delta_kernel(float *x, float *delta, float *mean, float *variance, int batch, int filters, int spatial, float *variance_delta)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if (i >= filters) return;
|
|
int j,k;
|
|
variance_delta[i] = 0;
|
|
for(j = 0; j < batch; ++j){
|
|
for(k = 0; k < spatial; ++k){
|
|
int index = j*filters*spatial + i*spatial + k;
|
|
variance_delta[i] += delta[index]*(x[index] - mean[i]);
|
|
}
|
|
}
|
|
variance_delta[i] *= -.5 * pow(variance[i] + .000001f, (float)(-3./2.));
|
|
}
|
|
|
|
__global__ void accumulate_kernel(float *x, int n, int groups, float *sum)
|
|
{
|
|
int k;
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if (i >= groups) return;
|
|
sum[i] = 0;
|
|
for(k = 0; k < n; ++k){
|
|
sum[i] += x[k*groups + i];
|
|
}
|
|
}
|
|
|
|
__global__ void fast_mean_delta_kernel(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta)
|
|
{
|
|
const int threads = BLOCK;
|
|
__shared__ float local[threads];
|
|
|
|
int id = threadIdx.x;
|
|
local[id] = 0;
|
|
|
|
int filter = blockIdx.x;
|
|
|
|
int i, j;
|
|
for(j = 0; j < batch; ++j){
|
|
for(i = 0; i < spatial; i += threads){
|
|
int index = j*spatial*filters + filter*spatial + i + id;
|
|
local[id] += (i+id < spatial) ? delta[index] : 0;
|
|
}
|
|
}
|
|
__syncthreads();
|
|
|
|
if(id == 0){
|
|
mean_delta[filter] = 0;
|
|
for(i = 0; i < threads; ++i){
|
|
mean_delta[filter] += local[i];
|
|
}
|
|
mean_delta[filter] *= (-1./sqrt(variance[filter] + .000001f));
|
|
}
|
|
}
|
|
|
|
__global__ void fast_variance_delta_kernel(float *x, float *delta, float *mean, float *variance, int batch, int filters, int spatial, float *variance_delta)
|
|
{
|
|
const int threads = BLOCK;
|
|
__shared__ float local[threads];
|
|
|
|
int id = threadIdx.x;
|
|
local[id] = 0;
|
|
|
|
int filter = blockIdx.x;
|
|
|
|
int i, j;
|
|
for(j = 0; j < batch; ++j){
|
|
for(i = 0; i < spatial; i += threads){
|
|
int index = j*spatial*filters + filter*spatial + i + id;
|
|
|
|
local[id] += (i+id < spatial) ? delta[index]*(x[index] - mean[filter]) : 0;
|
|
}
|
|
}
|
|
__syncthreads();
|
|
|
|
if(id == 0){
|
|
variance_delta[filter] = 0;
|
|
for(i = 0; i < threads; ++i){
|
|
variance_delta[filter] += local[i];
|
|
}
|
|
variance_delta[filter] *= -.5 * pow(variance[filter] + .000001f, (float)(-3./2.));
|
|
}
|
|
}
|
|
|
|
|
|
__global__ void mean_delta_kernel(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if (i >= filters) return;
|
|
int j,k;
|
|
mean_delta[i] = 0;
|
|
for (j = 0; j < batch; ++j) {
|
|
for (k = 0; k < spatial; ++k) {
|
|
int index = j*filters*spatial + i*spatial + k;
|
|
mean_delta[i] += delta[index];
|
|
}
|
|
}
|
|
mean_delta[i] *= (-1./sqrt(variance[i] + .000001f));
|
|
}
|
|
|
|
//extern "C" void mean_delta_gpu(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta)
|
|
bool gpuCUDA::makeMeanDelta(float *delta, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean_delta){
|
|
mean_delta_kernel<<<gridSize(filters), BLOCK>>>(delta, variance, batch, filters, spatial, mean_delta);
|
|
return checkError();
|
|
}
|
|
|
|
//extern "C" void fast_mean_delta_gpu(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta)
|
|
bool gpuCUDA::makeFastMeanDelta(float *delta, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean_delta){
|
|
fast_mean_delta_kernel<<<filters, BLOCK>>>(delta, variance, batch, filters, spatial, mean_delta);
|
|
return checkError();
|
|
};
|
|
|
|
//extern "C" void fast_variance_delta_gpu(float *x, float *delta, float *mean, float *variance, int batch, int filters, int spatial, float *variance_delta)
|
|
bool gpuCUDA::makeFastVarianceDelta(float *x, float *delta, float *mean, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance_delta){
|
|
fast_variance_delta_kernel<<<filters, BLOCK>>>(x, delta, mean, variance, batch, filters, spatial, variance_delta);
|
|
return checkError();
|
|
};
|
|
|
|
__global__ void mean_kernel(float *x, int batch, int filters, int spatial, float *mean)
|
|
{
|
|
float scale = 1./(batch * spatial);
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if (i >= filters) return;
|
|
int j,k;
|
|
mean[i] = 0;
|
|
for(j = 0; j < batch; ++j){
|
|
for(k = 0; k < spatial; ++k){
|
|
int index = j*filters*spatial + i*spatial + k;
|
|
mean[i] += x[index];
|
|
}
|
|
}
|
|
mean[i] *= scale;
|
|
}
|
|
|
|
__global__ void variance_kernel(float *x, float *mean, int batch, int filters, int spatial, float *variance)
|
|
{
|
|
float scale = 1./(batch * spatial - 1);
|
|
int j,k;
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if (i >= filters) return;
|
|
variance[i] = 0;
|
|
for(j = 0; j < batch; ++j){
|
|
for(k = 0; k < spatial; ++k){
|
|
int index = j*filters*spatial + i*spatial + k;
|
|
variance[i] += pow((x[index] - mean[i]), 2);
|
|
}
|
|
}
|
|
variance[i] *= scale;
|
|
}
|
|
|
|
__global__ void reorg_kernel(int N, float *x, int w, int h, int c, int batch, int stride, bool forward, float *out)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(i >= N) return;
|
|
int in_index = i;
|
|
int in_w = i%w;
|
|
i = i/w;
|
|
int in_h = i%h;
|
|
i = i/h;
|
|
int in_c = i%c;
|
|
i = i/c;
|
|
int b = i%batch;
|
|
|
|
int out_c = c/(stride*stride);
|
|
|
|
int c2 = in_c % out_c;
|
|
int offset = in_c / out_c;
|
|
int w2 = in_w*stride + offset % stride;
|
|
int h2 = in_h*stride + offset / stride;
|
|
//printf("%d\n", offset);
|
|
int out_index = w2 + w*stride*(h2 + h*stride*(c2 + out_c*b));
|
|
|
|
// printf("%d %d %d\n", w2, h2, c2);
|
|
//printf("%d %d\n", in_index, out_index);
|
|
//if(out_index >= N || out_index < 0) printf("bad bad bad \n");
|
|
|
|
if(forward) out[out_index] = x[in_index];
|
|
else out[in_index] = x[out_index];
|
|
//if(forward) out[1] = x[1];
|
|
//else out[0] = x[0];
|
|
}
|
|
|
|
//extern "C" void normalize_gpu(float *x, float *mean, float *variance, int batch, int filters, int spatial)
|
|
bool gpuCUDA::makeNormalized(float *x, float *mean, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial){
|
|
unsigned int N = batch*filters*spatial;
|
|
normalize_kernel<<<gridSize(N), BLOCK>>>(N, x, mean, variance, batch, filters, spatial);
|
|
return checkError();
|
|
};
|
|
|
|
__global__ void fast_mean_kernel(float *x, int batch, int filters, int spatial, float *mean)
|
|
{
|
|
const int threads = BLOCK;
|
|
__shared__ float local[threads];
|
|
|
|
int id = threadIdx.x;
|
|
local[id] = 0;
|
|
|
|
int filter = blockIdx.x;
|
|
|
|
int i, j;
|
|
for(j = 0; j < batch; ++j){
|
|
for(i = 0; i < spatial; i += threads){
|
|
int index = j*spatial*filters + filter*spatial + i + id;
|
|
local[id] += (i+id < spatial) ? x[index] : 0;
|
|
}
|
|
}
|
|
__syncthreads();
|
|
|
|
if(id == 0){
|
|
mean[filter] = 0;
|
|
for(i = 0; i < threads; ++i){
|
|
mean[filter] += local[i];
|
|
}
|
|
mean[filter] /= spatial * batch;
|
|
}
|
|
}
|
|
|
|
__global__ void fast_variance_kernel(float *x, float *mean, int batch, int filters, int spatial, float *variance)
|
|
{
|
|
const int threads = BLOCK;
|
|
__shared__ float local[threads];
|
|
|
|
int id = threadIdx.x;
|
|
local[id] = 0;
|
|
|
|
int filter = blockIdx.x;
|
|
|
|
int i, j;
|
|
for(j = 0; j < batch; ++j){
|
|
for(i = 0; i < spatial; i += threads){
|
|
int index = j*spatial*filters + filter*spatial + i + id;
|
|
|
|
local[id] += (i+id < spatial) ? pow((x[index] - mean[filter]), 2) : 0;
|
|
}
|
|
}
|
|
__syncthreads();
|
|
|
|
if(id == 0){
|
|
variance[filter] = 0;
|
|
for(i = 0; i < threads; ++i){
|
|
variance[filter] += local[i];
|
|
}
|
|
variance[filter] /= (spatial * batch - 1);
|
|
}
|
|
}
|
|
|
|
//extern "C" void fast_mean_gpu(float *x, int batch, int filters, int spatial, float *mean)
|
|
bool gpuCUDA::makeFastMean(float *x, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean){
|
|
fast_mean_kernel<<<filters, BLOCK>>>(x, batch, filters, spatial, mean);
|
|
return checkError();
|
|
};
|
|
|
|
//extern "C" void fast_variance_gpu(float *x, float *mean, int batch, int filters, int spatial, float *variance)
|
|
bool gpuCUDA::makeFastVariance(float *x, float *mean, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance){
|
|
fast_variance_kernel<<<filters, BLOCK>>>(x, mean, batch, filters, spatial, variance);
|
|
return checkError();
|
|
};
|
|
|
|
|
|
//extern "C" void mean_gpu(float *x, int batch, int filters, int spatial, float *mean)
|
|
bool gpuCUDA::makeMean(float *x, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean){
|
|
mean_kernel<<<gridSize(filters), BLOCK>>>(x, batch, filters, spatial, mean);
|
|
return checkError();
|
|
};
|
|
|
|
//extern "C" void variance_gpu(float *x, float *mean, int batch, int filters, int spatial, float *variance)
|
|
bool gpuCUDA::makeVariance(float *x, float *mean, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance){
|
|
variance_kernel<<<gridSize(filters), BLOCK>>>(x, mean, batch, filters, spatial, variance);
|
|
return checkError();
|
|
};
|
|
|
|
//extern "C" void copy_ongpu(int N, float * X, int INCX, float * Y, int INCY)
|
|
bool gpuCUDA::makeCopy(uint32_t N, float * X, uint32_t INCX, float * Y, uint32_t INCY){
|
|
return makeCopyOffset(N, X, 0, INCX, Y, 0, INCY);
|
|
};
|
|
|
|
//extern "C" void copy_ongpu_offset(int N, float * X, int OFFX, int INCX, float * Y, int OFFY, int INCY)
|
|
bool gpuCUDA::makeCopyOffset(uint32_t N, float * X, uint32_t OFFX, uint32_t INCX, float * Y, uint32_t OFFY, uint32_t INCY){
|
|
copy_kernel<<<gridSize(N), BLOCK>>>(N, X, OFFX, INCX, Y, OFFY, INCY);
|
|
return checkError();//cudaPeekAtLastError());
|
|
};
|
|
|
|
__global__ void flatten_kernel(int N, float *x, int spatial, int layers, int batch, int forward, float *out)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(i >= N) return;
|
|
int in_s = i%spatial;
|
|
i = i/spatial;
|
|
int in_c = i%layers;
|
|
i = i/layers;
|
|
int b = i;
|
|
|
|
int i1 = b*layers*spatial + in_c*spatial + in_s;
|
|
int i2 = b*layers*spatial + in_s*layers + in_c;
|
|
|
|
if (forward) out[i2] = x[i1];
|
|
else out[i1] = x[i2];
|
|
}
|
|
|
|
//extern "C" void flatten_ongpu(float *x, int spatial, int layers, int batch, int forward, float *out)
|
|
bool gpuCUDA::makeFlatten(float *x, uint32_t spatial, uint32_t layers, uint32_t batch, uint32_t forward, float *out){
|
|
int size = spatial*batch*layers;
|
|
flatten_kernel<<<gridSize(size), BLOCK>>>(size, x, spatial, layers, batch, forward, out);
|
|
return checkError();
|
|
};
|
|
|
|
//extern "C" void reorg_ongpu(float *x, int w, int h, int c, int batch, int stride, int forward, float *out)
|
|
bool gpuCUDA::makeReorg(float *x, uint32_t w, uint32_t h, uint32_t c, uint32_t batch, uint32_t stride, bool forward, float *out){
|
|
int size = w*h*c*batch;
|
|
reorg_kernel<<<gridSize(size), BLOCK>>>(size, x, w, h, c, batch, stride, forward, out);
|
|
return checkError();
|
|
};
|
|
|
|
//extern "C" void mask_ongpu(int N, float * X, float mask_num, float * mask)
|
|
bool gpuCUDA::makeMask(uint32_t N, float * X, float mask_num, float * mask){
|
|
mask_kernel<<<gridSize(N), BLOCK>>>(N, X, mask_num, mask);
|
|
return checkError();
|
|
};
|
|
|
|
bool gpuCUDA::makeConst(uint32_t N, float ALPHA, float * X, uint32_t INCX){
|
|
const_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, INCX);
|
|
return checkError();
|
|
};
|
|
|
|
//extern "C" void constrain_ongpu(int N, float ALPHA, float * X, int INCX)
|
|
bool gpuCUDA::makeConstrain(uint32_t N, float ALPHA, float * X, uint32_t INCX){
|
|
constrain_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, INCX);
|
|
return checkError();
|
|
};
|
|
|
|
//extern "C" void fill_ongpu(int N, float ALPHA, float * X, int INCX)
|
|
bool gpuCUDA::makeFill(uint32_t N, float ALPHA, float * X, uint32_t INCX){
|
|
fill_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, INCX);
|
|
return checkError();
|
|
};
|
|
|
|
__global__ void shortcut_kernel(int size, int minw, int minh, int minc, int stride, int sample, int batch, int w1, int h1, int c1, float *add, int w2, int h2, int c2, float *out)
|
|
{
|
|
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if (id >= size) return;
|
|
int i = id % minw;
|
|
id /= minw;
|
|
int j = id % minh;
|
|
id /= minh;
|
|
int k = id % minc;
|
|
id /= minc;
|
|
int b = id % batch;
|
|
|
|
int out_index = i*sample + w2*(j*sample + h2*(k + c2*b));
|
|
int add_index = i*stride + w1*(j*stride + h1*(k + c1*b));
|
|
out[out_index] += add[add_index];
|
|
}
|
|
|
|
//extern "C" void shortcut_gpu(int batch, int w1, int h1, int c1, float *add, int w2, int h2, int c2, float *out)
|
|
bool gpuCUDA::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){
|
|
int minw = (w1 < w2) ? w1 : w2;
|
|
int minh = (h1 < h2) ? h1 : h2;
|
|
int minc = (c1 < c2) ? c1 : c2;
|
|
|
|
int stride = w1/w2;
|
|
int sample = w2/w1;
|
|
assert(stride == h1/h2);
|
|
assert(sample == h2/h1);
|
|
if(stride < 1) stride = 1;
|
|
if(sample < 1) sample = 1;
|
|
|
|
int size = batch * minw * minh * minc;
|
|
shortcut_kernel<<<gridSize(size), BLOCK>>>(size, minw, minh, minc, stride, sample, batch, w1, h1, c1, add, w2, h2, c2, out);
|
|
return checkError();
|
|
};
|
|
|
|
__global__ void smooth_l1_kernel(int n, float *pred, float *truth, float *delta, float *error)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(i < n){
|
|
float diff = truth[i] - pred[i];
|
|
float abs_val = abs(diff);
|
|
if(abs_val < 1) {
|
|
error[i] = diff * diff;
|
|
delta[i] = diff;
|
|
}
|
|
else {
|
|
error[i] = 2*abs_val - 1;
|
|
delta[i] = (diff < 0) ? -1 : 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
//extern "C" void smooth_l1_gpu(int n, float *pred, float *truth, float *delta, float *error)
|
|
bool gpuCUDA::makeSmoothL1(uint32_t n, float *pred, float *truth, float *delta, float *error){
|
|
smooth_l1_kernel<<<gridSize(n), BLOCK>>>( n, pred, truth, delta, error );
|
|
return checkError();
|
|
}
|
|
|
|
__global__ void l2_kernel(int n, float *pred, float *truth, float *delta, float *error)
|
|
{
|
|
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(i < n){
|
|
float diff = truth[i] - pred[i];
|
|
error[i] = diff * diff; //I know this is technically wrong, deal with it.
|
|
delta[i] = diff;
|
|
}
|
|
}
|
|
|
|
//extern "C" void l2_gpu(int n, float *pred, float *truth, float *delta, float *error)
|
|
bool gpuCUDA::makeL2(uint32_t n, float *pred, float *truth, float *delta, float *error){
|
|
l2_kernel<<<gridSize(n), BLOCK>>>( n, pred, truth, delta, error );
|
|
return checkError();
|
|
};
|
|
|
|
|
|
__device__ void softmax_device(int n, float *input, float temp, float *output)
|
|
{
|
|
int i;
|
|
float sum = 0;
|
|
float largest = -INFINITY;
|
|
for(i = 0; i < n; ++i){
|
|
int val = input[i];
|
|
largest = (val>largest) ? val : largest;
|
|
}
|
|
for(i = 0; i < n; ++i){
|
|
float e = exp(input[i]/temp - largest/temp);
|
|
sum += e;
|
|
output[i] = e;
|
|
}
|
|
for(i = 0; i < n; ++i){
|
|
output[i] /= sum;
|
|
}
|
|
}
|
|
|
|
__global__ void softmax_kernel(int n, int offset, int batch, float *input, float temp, float *output)
|
|
{
|
|
int b = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
|
|
if(b >= batch) return;
|
|
softmax_device(n, input + b*offset, temp, output + b*offset);
|
|
}
|
|
|
|
//extern "C" void softmax_gpu(float *input, int n, int offset, int groups, float temp, float *output)
|
|
bool gpuCUDA::makeSoftMax(float *input, uint32_t n, uint32_t offset, uint32_t groups, float temp, float *output){
|
|
int inputs = n;
|
|
int batch = groups;
|
|
softmax_kernel<<<gridSize(batch), BLOCK>>>( inputs, offset, batch, input, temp, output );
|
|
return checkError();
|
|
};
|
|
|
|
|
|
|
|
|