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.
238 lines
15 KiB
238 lines
15 KiB
#ifndef __CUDA_PROCESSING_UNIT_HEADERS__
|
|
#define __CUDA_PROCESSING_UNIT_HEADERS__
|
|
|
|
#include "../base/basicProcessingUnit.h"
|
|
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1900
|
|
#define inline __inline
|
|
#endif
|
|
|
|
#ifdef GPU
|
|
#define BLOCK 512
|
|
|
|
#include <cuda_runtime.h>
|
|
#include <curand.h>
|
|
#include <cublas_v2.h>
|
|
|
|
#ifdef CUDNN
|
|
#include <cudnn.h>
|
|
#endif
|
|
|
|
#define CUDA_KERNEL_NODES 16
|
|
|
|
class gpuCUDA : public cortexProcessingUnit{
|
|
private:
|
|
static bool m_aCublasHandleBusy[CUDA_KERNEL_NODES];
|
|
static cublasHandle_t m_aCublasHandle[ CUDA_KERNEL_NODES];
|
|
static bool m_aCuRandHandleBusy[CUDA_KERNEL_NODES];
|
|
static curandGenerator_t m_aCuRandHandle[ CUDA_KERNEL_NODES];
|
|
cudaError_t m_sStatus;
|
|
|
|
bool checkError( void );
|
|
cublasHandle_t getBlasHandle( void);
|
|
dim3 gridSize( uint32_t n );
|
|
bool gemm_ongpu( uint32_t TA, uint32_t TB, uint32_t M, uint32_t N, uint32_t K, float ALPHA,
|
|
float *A_gpu, uint32_t lda,
|
|
float *B_gpu, uint32_t ldb,
|
|
float BETA,
|
|
float *C_gpu, uint32_t ldc);
|
|
protected:
|
|
bool setDevice( uint32_t n );
|
|
int getDevice( void );
|
|
public:
|
|
static gpuCUDA * getInstance(void);
|
|
virtual float compare(float *gpuMem, float *x, uint32_t n, char *s);
|
|
virtual bool pullFloatArray(float *gpuMem, float *hostMem, uint32_t n);
|
|
virtual bool pullIntArray(uint32_t *gpuMem, uint32_t *hostMem, uint32_t n);
|
|
virtual bool pushFloatArray(float *gpuMem, float *hostMem, uint32_t n);
|
|
virtual bool pushIntArray(uint32_t *gpuMem, uint32_t *hostMem, uint32_t n);
|
|
virtual bool freeMem(void *gpuMem);
|
|
|
|
/* Processing Maths */
|
|
virtual void gemm( uint32_t TA, uint32_t TB, uint32_t M, uint32_t N, uint32_t K, float ALPHA,
|
|
float *A, uint32_t lda,
|
|
float *B, uint32_t ldb,
|
|
float BETA,
|
|
float *C, uint32_t ldc);
|
|
|
|
virtual float trainNetworkDatum( basicCortex *net, float *X, float *Y);
|
|
virtual void forwardNetwork( basicCortex *net, cortexSTATE tState);
|
|
virtual void backwardNetwork( basicCortex *net, cortexSTATE tState);
|
|
virtual void updateNetwork( basicCortex *net);
|
|
|
|
virtual bool binarize( float *input, uint32_t n, float *binary);
|
|
virtual bool binarizeInput( float *input, uint32_t n, uint32_t size, float *binary);
|
|
virtual bool binarizeWeights(float *weights, uint32_t n, uint32_t size, float *binary);
|
|
|
|
void propogateForwardBackwardNetwork( basicCortex *net, float *x, float *y );
|
|
|
|
//THREAD_HANDLE trainNetworkMT( basicCortex *net, cortexDATA d, float *err);
|
|
//THREAD_HANDLE syncLayersMT( basicCortex **nets, uint32_t n, uint32_t j);
|
|
THREAD_HANDLE trainNetworkMT( basicCortex *net, cortexDATA d, float *err);
|
|
THREAD_HANDLE syncLayersMT( basicCortex **nets, uint32_t n, uint32_t j);
|
|
|
|
void updateLayer( cortexLayer * layer, basicCortex *net);
|
|
void mergeWeights( cortexLayer * layer, cortexLayer * base);
|
|
void scaleWeights( cortexLayer * layer, float s);
|
|
void pullWeights( cortexLayer * layer);
|
|
void pushWeights( cortexLayer * layer);
|
|
void distributeWeights( cortexLayer * layer, cortexLayer *base);
|
|
void mergeUpdates( cortexLayer * layer, cortexLayer *base);
|
|
void distributeUpdates( cortexLayer * layer, cortexLayer *base);
|
|
float * networkPredict( basicCortex * net, float *input);
|
|
float trainNetworks( basicCortex ** nets, uint32_t n, cortexDATA d, uint32_t interval);
|
|
void syncLayers( basicCortex ** net, uint32_t n, uint32_t j);
|
|
void syncNets( basicCortex ** nets, uint32_t n, uint32_t interval);
|
|
void pullUpdates( cortexLayer * layer);
|
|
void pushUpdates( cortexLayer * layer);
|
|
float * getLayerOutput( basicCortex * net, uint32_t i);
|
|
float * getNetworkOutput( basicCortex * net);
|
|
|
|
virtual bool calcAxpy(uint32_t N, float ALPHA, float * X, uint32_t INCX, float * Y, uint32_t INCY);
|
|
virtual bool calcAxpyOffset(uint32_t N, float ALPHA, float * X, uint32_t OFFX, uint32_t INCX, float * Y, uint32_t OFFY, uint32_t INCY);
|
|
virtual bool calcMul(uint32_t N, float * X, uint32_t INCX, float * Y, uint32_t INCY);
|
|
virtual bool calcMultAddInto(uint32_t num, float *a, float *b, float *c);
|
|
virtual bool calcPow(uint32_t N, float ALPHA, float * X, uint32_t INCX, float * Y, uint32_t INCY);
|
|
virtual bool calcScal(uint32_t N, float ALPHA, float * X, uint32_t INCX);
|
|
virtual bool calcScaleBias(float *output, float *biases, uint32_t batch, uint32_t n, uint32_t size);
|
|
virtual bool calcSupp(uint32_t N, float ALPHA, float * X, uint32_t INCX);
|
|
virtual bool calcTimeRandomMatrix(uint32_t TA, uint32_t TB, uint32_t m, uint32_t k, uint32_t n);
|
|
virtual bool calcTime(uint32_t TA, uint32_t TB, uint32_t m, uint32_t k, uint32_t n);
|
|
virtual bool calcWeightedDelta(float *a, float *b, float *s, float *da, float *db, float *ds, uint32_t num, float *dc);
|
|
virtual bool calcWeightedSum(float *a, float *b, float *s, uint32_t num, float *c);
|
|
|
|
virtual bool makeConstrain(uint32_t N, float ALPHA, float * X, uint32_t INCX);
|
|
virtual bool makeConst(uint32_t N, float ALPHA, float * X, uint32_t INCX);
|
|
virtual bool makeActivateArray(float *x, uint32_t n, ACTIVATION a);
|
|
virtual bool makeAdam(uint32_t n, float *x, float *m, float *v, float B1, float B2, float rate, float eps, uint32_t t);
|
|
virtual bool makeBackwardBias(float *bias_updates, float *delta, uint32_t batch, uint32_t n, uint32_t size);
|
|
virtual bool makeBackwardScale(float *x_norm, float *delta, uint32_t batch, uint32_t n, uint32_t size, float *scale_updates);
|
|
virtual bool makeCopy(uint32_t N, float *X, uint32_t INCX, float *Y, uint32_t INCY);
|
|
virtual bool makeCopyOffset(uint32_t N, float * X, uint32_t OFFX, uint32_t INCX, float * Y, uint32_t OFFY, uint32_t INCY);
|
|
virtual bool makeDotError(cortexLayer *l);
|
|
virtual bool makeGradientArray(float *x, uint32_t n, ACTIVATION a, float *delta);
|
|
virtual bool makeFastMean(float *x, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean);
|
|
virtual bool makeFastMeanDelta(float *delta, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean_delta);
|
|
virtual bool makeFastVariance(float *x, float *mean, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance);
|
|
virtual bool makeFastVarianceDelta(float *x, float *delta, float *mean, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance_delta);
|
|
virtual bool makeFill(uint32_t N, float ALPHA, float * X, uint32_t INCX);
|
|
virtual bool makeFlatten(float *x, uint32_t spatial, uint32_t layers, uint32_t batch, uint32_t forward, float *out);
|
|
virtual float * makeFloatArray(uint32_t n, float *hostMem = NULL);
|
|
virtual uint32_t * makeIntArray(uint32_t n, uint32_t *hostMem = NULL);
|
|
virtual bool makeL2(uint32_t n, float *pred, float *truth, float *delta, float *error);
|
|
virtual bool makeMask(uint32_t N, float * X, float mask_num, float * mask);
|
|
virtual bool makeMean(float *x, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean);
|
|
virtual bool makeMeanDelta(float *delta, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean_delta);
|
|
virtual bool makeNormalized(float *x, float *mean, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial);
|
|
virtual bool makeNormalizedDelta(float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, uint32_t batch, uint32_t filters, uint32_t spatial, float *delta);
|
|
virtual bool makeRandom(float *gpuMem, uint32_t n);
|
|
virtual bool makeReorg(float *x, uint32_t w, uint32_t h, uint32_t c, uint32_t batch, uint32_t stride, bool forward, float *out);
|
|
virtual bool 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);
|
|
virtual bool makeSmoothL1(uint32_t n, float *pred, float *truth, float *delta, float *error);
|
|
virtual bool makeSoftMax(float *input, uint32_t n, float temp, float *output){ return false; };
|
|
virtual bool makeSoftMax(float *input, uint32_t n, uint32_t offset, uint32_t groups, float temp, float *output);
|
|
virtual bool makeTestAccuracy(uint32_t TA, uint32_t TB, uint32_t m, uint32_t k, uint32_t n);
|
|
virtual bool makeTestBlas(void);
|
|
virtual bool makeVariance(float *x, float *mean, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance);
|
|
virtual bool makeVarianceDelta(float *x, float *delta, float *mean, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance_delta){ return false; };
|
|
|
|
virtual bool addBias(float *output, float *biases, uint32_t batch, uint32_t n, uint32_t size);
|
|
virtual bool scaleBias(float *output, float *biases, uint32_t batch, uint32_t n, uint32_t size);
|
|
virtual void col2im(float *data_col, uint32_t channels, uint32_t height, uint32_t width, uint32_t ksize, uint32_t stride, uint32_t pad, float *data_im );
|
|
virtual void im2col(float *im, uint32_t channels, uint32_t height, uint32_t width, uint32_t ksize, uint32_t stride, uint32_t pad, float *data_col );
|
|
|
|
// CPU + GPU
|
|
virtual void forward_layer(activationLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(activationLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(avgpoolLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(avgpoolLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(batchnormLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(batchnormLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(connectedLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(connectedLayer *layer, cortexSTATE state);
|
|
virtual void update_layer(connectedLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay);
|
|
virtual void forward_layer(convolutionalLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(convolutionalLayer *layer, cortexSTATE state);
|
|
virtual void update_layer(convolutionalLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay);
|
|
virtual void forward_layer(costLayer *layer, cortexSTATE state){};
|
|
virtual void backward_layer(costLayer *layer, cortexSTATE state){};
|
|
virtual void forward_layer(crnnLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(crnnLayer *layer, cortexSTATE state);
|
|
virtual void update_layer(crnnLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay);
|
|
virtual void forward_layer(cropLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(cropLayer *layer, cortexSTATE state){};
|
|
virtual void forward_layer(deconvolutionalLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(deconvolutionalLayer *layer, cortexSTATE state);
|
|
virtual void update_layer(deconvolutionalLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay);
|
|
virtual void forward_layer(detectionLayer *layer, cortexSTATE state){};
|
|
virtual void backward_layer(detectionLayer *layer, cortexSTATE state){};
|
|
virtual void forward_layer(dropoutLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(dropoutLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(localLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(localLayer *layer, cortexSTATE state);
|
|
virtual void update_layer(localLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay);
|
|
virtual void forward_layer(maxpoolLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(maxpoolLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(normalizationLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(normalizationLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(regionLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(regionLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(reorgLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(reorgLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(routeLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(routeLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(shortcutLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(shortcutLayer *layer, cortexSTATE state);
|
|
virtual void forward_layer(softmaxLayer *layer, cortexSTATE state);
|
|
virtual void backward_layer(softmaxLayer *layer, cortexSTATE state);
|
|
|
|
//Just on GPU
|
|
virtual void pull_layer(activationLayer *layer);
|
|
virtual void push_layer(activationLayer *layer);
|
|
virtual void pull_layer(avgpoolLayer *layer);
|
|
virtual void push_layer(avgpoolLayer *layer);
|
|
virtual void pull_layer(batchnormLayer *layer);
|
|
virtual void push_layer(batchnormLayer *layer);
|
|
virtual void pull_layer(connectedLayer *layer);
|
|
virtual void push_layer(connectedLayer *layer);
|
|
virtual void pull_layer(convolutionalLayer *layer);
|
|
virtual void push_layer(convolutionalLayer *layer);
|
|
virtual void pull_layer(costLayer *layer){};
|
|
virtual void push_layer(costLayer *layer){};
|
|
virtual void pull_layer(crnnLayer *layer);
|
|
virtual void push_layer(crnnLayer *layer);
|
|
virtual void pull_layer(cropLayer *layer){};
|
|
virtual void push_layer(cropLayer *layer){};
|
|
virtual void pull_layer(deconvolutionalLayer *layer);
|
|
virtual void push_layer(deconvolutionalLayer *layer);
|
|
virtual void pull_layer(detectionLayer *layer){};
|
|
virtual void push_layer(detectionLayer *layer){};
|
|
virtual void pull_layer(dropoutLayer *layer){};
|
|
virtual void push_layer(dropoutLayer *layer){};
|
|
virtual void pull_layer(localLayer *layer);
|
|
virtual void push_layer(localLayer *layer);
|
|
virtual void pull_layer(maxpoolLayer *layer){};
|
|
virtual void push_layer(maxpoolLayer *layer){};
|
|
virtual void pull_layer(normalizationLayer *layer){};
|
|
virtual void push_layer(normalizationLayer *layer){};
|
|
virtual void pull_layer(regionLayer *layer){};
|
|
virtual void push_layer(regionLayer *layer){};
|
|
virtual void pull_layer(reorgLayer *layer){};
|
|
virtual void push_layer(reorgLayer *layer){};
|
|
virtual void pull_layer(routeLayer *layer){};
|
|
virtual void push_layer(routeLayer *layer){};
|
|
virtual void pull_layer(softmaxLayer *layer);
|
|
private:
|
|
static gpuCUDA *_instance;
|
|
static void *train_thread(void *ptr);
|
|
static void *syncLayer_thread(void *ptr);
|
|
gpuCUDA();
|
|
virtual ~gpuCUDA();
|
|
};
|
|
|
|
#endif
|
|
#endif
|