Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
CUDABackend.h
Go to the documentation of this file.
1#pragma once
3#ifdef DEEPITY_USE_CUDA
4#include <cublas_v2.h>
5#endif
7
8namespace Deep
9{
16{
17public:
19 ~CUDABackend() override;
21 void BeginGraphCapture() noexcept override;
23 bool EndGraphCapture() noexcept override;
25 void ReplayGraph() noexcept override;
26
28 float* Allocate(size_t numFloats) override;
30 void Free(float* ptr) noexcept override;
32 void Zero(float* ptr, size_t numFloats) noexcept override;
34 void Copy(float* dst, const float* src, size_t numFloats) noexcept override;
36 void CopyFromHost(float* deviceDst, const float* hostSrc, size_t numFloats) noexcept override;
38 void CopyToHost(float* hostDst, const float* deviceSrc, size_t numFloats) noexcept override;
40 void RandomizeNormal(float* buf, size_t n, float mean, float stddev,
41 uint32_t seed) noexcept override;
43 void RandomizeUniform(float* buf, size_t n, float min, float max,
44 uint32_t seed) noexcept override;
45
47 void SumRows(float* dst, const float* src, size_t batchSize, size_t width) noexcept override;
48
53 void PrepareForBatchSize(size_t batchSize) noexcept override;
54
56 void MatMul(bool transA, bool transB, int M, int N, int K, float alpha, const float* A, int lda,
57 const float* B, int ldb, float beta, float* C, int ldc) noexcept override;
58
60 void Scale(float* buf, size_t n, float alpha) noexcept override;
62 void AxpyInto(float* y, const float* x, size_t n, float alpha) noexcept override;
64 void AddBiasBroadcast(float* buf, const float* bias, size_t batchSize,
65 size_t width) noexcept override;
66
68 void FusedStateUpdateMomentum(float* z, float* v, const float* feedback, const float* deriv,
69 const float* e, size_t n, float ir, float beta) noexcept override;
70
72 bool TryFusedForwardPass(ActivationType actType, const float* zF, const float* W,
73 const float* bias, float* mu, int batchSize, int size,
74 int nextSize) noexcept override;
75
77 void Activation(ActivationType type, float* buf, size_t n) noexcept override;
79 void ActivationInto(ActivationType type, float* dst, const float* src,
80 size_t n) noexcept override;
82 void ActivationDerivative(ActivationType type, float* buf, size_t n,
83 bool activated) noexcept override;
85 void ActivationDerivativeInto(ActivationType type, float* dst, const float* src,
86 size_t n) noexcept override;
87
89 void FusedStateUpdate(float* z, const float* feedback, const float* deriv, const float* e,
90 size_t n, float ir) noexcept override;
92 float ComputeErrorAndEnergy(float* e, const float* z, const float* mu,
93 size_t n) noexcept override;
95 void ComputeError(float* e, const float* z, const float* mu, size_t n) noexcept override;
96
98 float ComputeSoftmaxCrossEntropyErrorAndEnergy(float* e, const float* z, const float* mu,
99 size_t batchSize, size_t nextSize,
100 float* rowEnergies) noexcept override;
101
103 void ComputeSoftmaxCrossEntropyError(float* e, const float* z, const float* mu, size_t batchSize,
104 size_t nextSize) noexcept override;
105
107 void IncrementCounter(int* ptr) noexcept override;
108
110 void AdamStep(float* param, const float* grad, float* m, float* v, size_t n, const int* t,
111 const float* lr, float beta1 = 0.9f, float beta2 = 0.999f,
112 float eps = 1e-8f) noexcept override;
114 void AdamWStep(float* param, const float* grad, float* m, float* v, size_t n, const int* t,
115 const float* lr, float weightDecay, float beta1 = 0.9f, float beta2 = 0.999f,
116 float eps = 1e-8f) noexcept override;
117
119 DeviceType GetDeviceType() const noexcept override
120 {
122 }
123
125 void MultiplyInto(float* dst, const float* a, const float* b, size_t n) noexcept override;
127 void Fill(float* buf, size_t n, float value) noexcept override;
128
130 void Im2Col(const float* input, int channels, int height, int width, int kernelH, int kernelW,
131 int strideH, int strideW, int padH, int padW, float* columns) noexcept override;
133 void Col2Im(const float* columns, int channels, int height, int width, int kernelH, int kernelW,
134 int strideH, int strideW, int padH, int padW, float* outputImage) noexcept override;
136 void RepackForBatchedGemm(float* dst, const float* src, size_t batchSize, size_t rows,
137 size_t cols) noexcept override;
139 void AddBiasPerChannel(float* buf, const float* bias, size_t channels,
140 size_t spatialSize) noexcept override;
141
142private:
143#ifdef DEEPITY_USE_CUDA
144 cublasHandle_t handle;
145 cudaStream_t stream;
146 cudaGraph_t graph = nullptr;
147 cudaGraphExec_t graphExec = nullptr;
148#endif
149 bool hasGraph = false;
150 float* onesVector = nullptr;
151 size_t onesCapacity = 0; // <--- Add this line
152 float* workspace = nullptr;
153};
154} // namespace Deep
A device-aware, RAII-managed buffer. Device is fixed at construction – there is no support for moving...
~CUDABackend() override
void ComputeError(float *e, const float *z, const float *mu, size_t n) noexcept override
Same as ComputeErrorAndEnergy(), without computing (or returning) the energy – for callers that only ...
void AddBiasPerChannel(float *buf, const float *bias, size_t channels, size_t spatialSize) noexcept override
Convolutional bias-add: buf[c*spatialSize + s] += bias[c] for all c in [0,channels),...
void Fill(float *buf, size_t n, float value) noexcept override
buf[i] = value for all i in [0, n).
void ActivationDerivativeInto(ActivationType type, float *dst, const float *src, size_t n) noexcept override
Two-buffer variant of ActivationDerivative(): reads src (the pre-activation value),...
void AdamStep(float *param, const float *grad, float *m, float *v, size_t n, const int *t, const float *lr, float beta1=0.9f, float beta2=0.999f, float eps=1e-8f) noexcept override
Adam update for n parameters in place.
void MultiplyInto(float *dst, const float *a, const float *b, size_t n) noexcept override
dst[i] = a[i] * b[i] for all i in [0, n) – elementwise product.
float ComputeSoftmaxCrossEntropyErrorAndEnergy(float *e, const float *z, const float *mu, size_t batchSize, size_t nextSize, float *rowEnergies) noexcept override
Softmax cross-entropy variant of ComputeErrorAndEnergy, for the terminal layer only....
void AddBiasBroadcast(float *buf, const float *bias, size_t batchSize, size_t width) noexcept override
buf[row*width + col] += bias[col] for every row in [0,batchSize) and col in [0,width) – broadcasts a ...
void Activation(ActivationType type, float *buf, size_t n) noexcept override
Applies type to buf in place.
void Im2Col(const float *input, int channels, int height, int width, int kernelH, int kernelW, int strideH, int strideW, int padH, int padW, float *columns) noexcept override
Rearranges a single (channels, height, width) input image into a (channels*kH*kW, outH*outW) column m...
void Zero(float *ptr, size_t numFloats) noexcept override
Zero-fills numFloats floats starting at ptr.
void Copy(float *dst, const float *src, size_t numFloats) noexcept override
Copies numFloats floats from src to dst, both on this backend's own device.
void IncrementCounter(int *ptr) noexcept override
Atomically increments *counter by 1. Device-resident so it can be read/updated by the same kernel/gra...
void ActivationDerivative(ActivationType type, float *buf, size_t n, bool activated) noexcept override
Applies type's derivative to buf in place.
void BeginGraphCapture() noexcept override
Starts recording subsequent backend calls into a replayable graph, instead of executing them immediat...
void ComputeSoftmaxCrossEntropyError(float *e, const float *z, const float *mu, size_t batchSize, size_t nextSize) noexcept override
Same as ComputeSoftmaxCrossEntropyErrorAndEnergy(), without computing (or requiring rowEnergies for) ...
void CopyToHost(float *hostDst, const float *deviceSrc, size_t numFloats) noexcept override
Copies numFloats floats from a device buffer into a host buffer (a plain memcpy on CPUBackend).
void PrepareForBatchSize(size_t batchSize) noexcept override
Must be called once, before Compile()'s first BeginGraphCapture(), for any backend that needs to prep...
void RepackForBatchedGemm(float *dst, const float *src, size_t batchSize, size_t rows, size_t cols) noexcept override
Repacks a [batchSize, rows, cols] tensor (batch-major) into [rows, batchSize, cols] (row-major,...
float ComputeErrorAndEnergy(float *e, const float *z, const float *mu, size_t n) noexcept override
Computes e = z - mu, then returns 0.5 * sum(e^2) – the Gaussian error/energy used by every layer exce...
void ReplayGraph() noexcept override
Launches a graph previously captured and instantiated by BeginGraphCapture()/EndGraphCapture()....
void FusedStateUpdateMomentum(float *z, float *v, const float *feedback, const float *deriv, const float *e, size_t n, float ir, float beta) noexcept override
Same as FusedStateUpdate, but with momentum: the update direction (feedback*deriv - e) is EMA-smoothe...
bool TryFusedForwardPass(ActivationType actType, const float *zF, const float *W, const float *bias, float *mu, int batchSize, int size, int nextSize) noexcept override
Attempts a fused forward pass (GEMM + bias + activation in one kernel, via CUTLASS on GPU) for RELU o...
void RandomizeUniform(float *buf, size_t n, float min, float max, uint32_t seed) noexcept override
Fills n floats with samples from a uniform distribution over [min, max).
void FusedStateUpdate(float *z, const float *feedback, const float *deriv, const float *e, size_t n, float ir) noexcept override
One settling step's state update, fused into a single call: z += ir * ((feedback * deriv) - e) Non-mo...
DeviceType GetDeviceType() const noexcept override
Which device this backend instance runs on.
Definition CUDABackend.h:119
void Scale(float *buf, size_t n, float alpha) noexcept override
buf[i] *= alpha for all i in [0, n).
void AxpyInto(float *y, const float *x, size_t n, float alpha) noexcept override
y[i] += alpha * x[i] for all i in [0, n) – the standard AXPY.
void SumRows(float *dst, const float *src, size_t batchSize, size_t width) noexcept override
dst[j] = sum over b in [0,batchSize) of src[b*width + j], for all j in [0,width). Replaces a batchSiz...
void Free(float *ptr) noexcept override
Frees a buffer previously returned by Allocate(). No-op on nullptr.
void CopyFromHost(float *deviceDst, const float *hostSrc, size_t numFloats) noexcept override
Copies numFloats floats from a host buffer into a device buffer (a plain memcpy on CPUBackend).
float * Allocate(size_t numFloats) override
Allocates numFloats floats on this backend's device.
void MatMul(bool transA, bool transB, int M, int N, int K, float alpha, const float *A, int lda, const float *B, int ldb, float beta, float *C, int ldc) noexcept override
C = alpha * op(A) * op(B) + beta * C, the standard BLAS SGEMM convention (op(X) = X^T if the matching...
void Col2Im(const float *columns, int channels, int height, int width, int kernelH, int kernelW, int strideH, int strideW, int padH, int padW, float *outputImage) noexcept override
The adjoint of Im2Col(): scatters a (channels*kH*kW, outH*outW) column-gradient buffer back into a (c...
void RandomizeNormal(float *buf, size_t n, float mean, float stddev, uint32_t seed) noexcept override
Fills n floats with samples from a normal distribution.
void AdamWStep(float *param, const float *grad, float *m, float *v, size_t n, const int *t, const float *lr, float weightDecay, float beta1=0.9f, float beta2=0.999f, float eps=1e-8f) noexcept override
Same as AdamStep(), plus decoupled weight decay (AdamW): param -= lr * weightDecay * param,...
void ActivationInto(ActivationType type, float *dst, const float *src, size_t n) noexcept override
Two-buffer variant of Activation(): reads src, writes into dst.
bool EndGraphCapture() noexcept override
Ends capture and instantiates the captured graph.
Device-agnostic compute backend interface: every numerical primitive a layer/network needs,...
Definition IComputeBackend.h:14
This file exists to provide both Tensor.h and IComputeBackend.h with device types without introducing...
Definition Backend.cpp:10
ActivationType
Identifies an activation function or its derivative, for dispatch tables and layer/backend APIs that ...
Definition ActivationType.h:25
DeviceType
Which physical device a Tensor/IComputeBackend pair runs on.
Definition DeviceType.h:8
@ DEVICE_GPU
Definition DeviceType.h:10