From cd8a3dcb4ca42f22ad8f46a95e00977c92be6bbd Mon Sep 17 00:00:00 2001
From: AlexeyAB <alexeyab84@gmail.com>
Date: Thu, 08 Feb 2018 23:22:42 +0000
Subject: [PATCH] Compile fixes
---
src/blas_kernels.cu | 104 +++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 91 insertions(+), 13 deletions(-)
diff --git a/src/blas_kernels.cu b/src/blas_kernels.cu
index 271f017..8e1cf19 100644
--- a/src/blas_kernels.cu
+++ b/src/blas_kernels.cu
@@ -23,7 +23,7 @@
dim3 dimGrid((size-1)/BLOCK + 1, n, batch);
dim3 dimBlock(BLOCK, 1, 1);
- scale_bias_kernel<<<dimGrid, dimBlock>>>(output, biases, n, size);
+ scale_bias_kernel<<<dimGrid, dimBlock, 0, get_cuda_stream()>>>(output, biases, n, size);
check_error(cudaPeekAtLastError());
}
@@ -67,7 +67,7 @@
dim3 dimGrid((size-1)/BLOCK + 1, n, batch);
dim3 dimBlock(BLOCK, 1, 1);
- add_bias_kernel<<<dimGrid, dimBlock>>>(output, biases, n, size);
+ add_bias_kernel<<<dimGrid, dimBlock, 0, get_cuda_stream()>>>(output, biases, n, size);
check_error(cudaPeekAtLastError());
}
@@ -140,6 +140,21 @@
}
+__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)
+{
+ adam_kernel<<<cuda_gridsize(n), BLOCK>>>(n, x, m, v, B1, B2, rate, eps, t);
+ check_error(cudaPeekAtLastError());
+}
+
__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;
@@ -208,6 +223,7 @@
local[id] += (i+id < spatial) ? delta[index] : 0;
}
}
+ __syncthreads();
if(id == 0){
mean_delta[filter] = 0;
@@ -236,6 +252,7 @@
local[id] += (i+id < spatial) ? delta[index]*(x[index] - mean[filter]) : 0;
}
}
+ __syncthreads();
if(id == 0){
variance_delta[filter] = 0;
@@ -365,7 +382,7 @@
__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] = min(ALPHA, max(-ALPHA, X[i*INCX]));
+ if(i < N) X[i*INCX] = fminf(ALPHA, fmaxf(-ALPHA, X[i*INCX]));
}
__global__ void supp_kernel(int N, float ALPHA, float *X, int INCX)
@@ -410,7 +427,7 @@
extern "C" void normalize_gpu(float *x, float *mean, float *variance, int batch, int filters, int spatial)
{
size_t N = batch*filters*spatial;
- normalize_kernel<<<cuda_gridsize(N), BLOCK>>>(N, x, mean, variance, batch, filters, spatial);
+ normalize_kernel<<<cuda_gridsize(N), BLOCK, 0, get_cuda_stream()>>>(N, x, mean, variance, batch, filters, spatial);
check_error(cudaPeekAtLastError());
}
@@ -431,6 +448,7 @@
local[id] += (i+id < spatial) ? x[index] : 0;
}
}
+ __syncthreads();
if(id == 0){
mean[filter] = 0;
@@ -459,6 +477,7 @@
local[id] += (i+id < spatial) ? pow((x[index] - mean[filter]), 2) : 0;
}
}
+ __syncthreads();
if(id == 0){
variance[filter] = 0;
@@ -471,13 +490,13 @@
extern "C" void fast_mean_gpu(float *x, int batch, int filters, int spatial, float *mean)
{
- fast_mean_kernel<<<filters, BLOCK>>>(x, batch, filters, spatial, mean);
+ fast_mean_kernel<<<filters, BLOCK, 0, get_cuda_stream()>>>(x, batch, filters, spatial, mean);
check_error(cudaPeekAtLastError());
}
extern "C" void fast_variance_gpu(float *x, float *mean, int batch, int filters, int spatial, float *variance)
{
- fast_variance_kernel<<<filters, BLOCK>>>(x, mean, batch, filters, spatial, variance);
+ fast_variance_kernel<<<filters, BLOCK, 0, get_cuda_stream() >>>(x, mean, batch, filters, spatial, variance);
check_error(cudaPeekAtLastError());
}
@@ -501,13 +520,13 @@
extern "C" void pow_ongpu(int N, float ALPHA, float * X, int INCX, float * Y, int INCY)
{
- pow_kernel<<<cuda_gridsize(N), BLOCK>>>(N, ALPHA, X, INCX, Y, INCY);
+ pow_kernel<<<cuda_gridsize(N), BLOCK, 0, get_cuda_stream() >>>(N, ALPHA, X, INCX, Y, INCY);
check_error(cudaPeekAtLastError());
}
extern "C" void axpy_ongpu_offset(int N, float ALPHA, float * X, int OFFX, int INCX, float * Y, int OFFY, int INCY)
{
- axpy_kernel<<<cuda_gridsize(N), BLOCK>>>(N, ALPHA, X, OFFX, INCX, Y, OFFY, INCY);
+ axpy_kernel<<<cuda_gridsize(N), BLOCK, 0, get_cuda_stream()>>>(N, ALPHA, X, OFFX, INCX, Y, OFFY, INCY);
check_error(cudaPeekAtLastError());
}
@@ -524,20 +543,44 @@
extern "C" void copy_ongpu_offset(int N, float * X, int OFFX, int INCX, float * Y, int OFFY, int INCY)
{
- copy_kernel<<<cuda_gridsize(N), BLOCK>>>(N, X, OFFX, INCX, Y, OFFY, INCY);
+ copy_kernel<<<cuda_gridsize(N), BLOCK, 0, get_cuda_stream()>>>(N, X, OFFX, INCX, Y, OFFY, INCY);
+ check_error(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)
+{
+ int size = spatial*batch*layers;
+ flatten_kernel<<<cuda_gridsize(size), BLOCK, 0, get_cuda_stream()>>>(size, x, spatial, layers, batch, forward, out);
check_error(cudaPeekAtLastError());
}
extern "C" void reorg_ongpu(float *x, int w, int h, int c, int batch, int stride, int forward, float *out)
{
int size = w*h*c*batch;
- reorg_kernel<<<cuda_gridsize(size), BLOCK>>>(size, x, w, h, c, batch, stride, forward, out);
+ reorg_kernel<<<cuda_gridsize(size), BLOCK, 0, get_cuda_stream()>>>(size, x, w, h, c, batch, stride, forward, out);
check_error(cudaPeekAtLastError());
}
extern "C" void mask_ongpu(int N, float * X, float mask_num, float * mask)
{
- mask_kernel<<<cuda_gridsize(N), BLOCK>>>(N, X, mask_num, mask);
+ mask_kernel<<<cuda_gridsize(N), BLOCK, 0, get_cuda_stream() >>>(N, X, mask_num, mask);
check_error(cudaPeekAtLastError());
}
@@ -556,7 +599,7 @@
extern "C" void scal_ongpu(int N, float ALPHA, float * X, int INCX)
{
- scal_kernel<<<cuda_gridsize(N), BLOCK>>>(N, ALPHA, X, INCX);
+ scal_kernel<<<cuda_gridsize(N), BLOCK, 0, get_cuda_stream()>>>(N, ALPHA, X, INCX);
check_error(cudaPeekAtLastError());
}
@@ -568,7 +611,7 @@
extern "C" void fill_ongpu(int N, float ALPHA, float * X, int INCX)
{
- fill_kernel<<<cuda_gridsize(N), BLOCK>>>(N, ALPHA, X, INCX);
+ fill_kernel<<<cuda_gridsize(N), BLOCK, 0, get_cuda_stream()>>>(N, ALPHA, X, INCX);
check_error(cudaPeekAtLastError());
}
@@ -691,3 +734,38 @@
mult_add_into_kernel<<<cuda_gridsize(num), BLOCK>>>(num, a, b, c);
check_error(cudaPeekAtLastError());
}
+
+
+__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)
+{
+ int inputs = n;
+ int batch = groups;
+ softmax_kernel<<<cuda_gridsize(batch), BLOCK, 0, get_cuda_stream()>>>(inputs, offset, batch, input, temp, output);
+ check_error(cudaPeekAtLastError());
+}
--
Gitblit v1.10.0