Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
IComputeBackend.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
5
6namespace Deep
7{
8
14{
15public:
16 virtual ~IComputeBackend() = default;
17
18 // Graphs
19
23 virtual void BeginGraphCapture() noexcept = 0;
31 virtual bool EndGraphCapture() noexcept = 0;
35 virtual void ReplayGraph() noexcept = 0;
36
37 // Memory
38
41 virtual float* Allocate(size_t numFloats) = 0;
43 virtual void Free(float* ptr) noexcept = 0;
45 virtual void Zero(float* ptr, size_t numFloats) noexcept = 0;
48 virtual void Copy(float* dst, const float* src, size_t numFloats) noexcept = 0;
51 virtual void CopyFromHost(float* deviceDst, const float* hostSrc, size_t numFloats) noexcept = 0;
54 virtual void CopyToHost(float* hostDst, const float* deviceSrc, size_t numFloats) noexcept = 0;
61 virtual void RandomizeNormal(float* buf, size_t n, float mean, float stddev,
62 uint32_t seed) noexcept = 0;
70 virtual void RandomizeUniform(float* buf, size_t n, float min, float max,
71 uint32_t seed) noexcept = 0;
72
77 virtual void PrepareForBatchSize(size_t batchSize) noexcept = 0;
78
79 // GEMM
80
94 virtual void MatMul(bool transA, bool transB, int M, int N, int K, float alpha, const float* A,
95 int lda, const float* B, int ldb, float beta, float* C, int ldc) noexcept = 0;
96
104 virtual void SumRows(float* dst, const float* src, size_t batchSize, size_t width) noexcept = 0;
105
106 // Elementwise scalar ops
107
109 virtual void Scale(float* buf, size_t n, float alpha) noexcept = 0;
111 virtual void AxpyInto(float* y, const float* x, size_t n, float alpha) noexcept = 0;
115 virtual void AddBiasBroadcast(float* buf, const float* bias, size_t batchSize,
116 size_t width) noexcept = 0;
117
118 // Activation
119
121 virtual void Activation(ActivationType type, float* buf, size_t n) noexcept = 0;
123 virtual void ActivationInto(ActivationType type, float* dst, const float* src,
124 size_t n) noexcept = 0;
131 virtual void ActivationDerivative(ActivationType type, float* buf, size_t n,
132 bool activated) noexcept = 0;
135 virtual void ActivationDerivativeInto(ActivationType type, float* dst, const float* src,
136 size_t n) noexcept = 0;
137
138 // Fused PC-specific ops
139
148 virtual void FusedStateUpdateMomentum(float* z, float* v, const float* feedback,
149 const float* deriv, const float* e, size_t n, float ir,
150 float beta) noexcept = 0;
151
155 virtual void FusedStateUpdate(float* z, const float* feedback, const float* deriv, const float* e,
156 size_t n, float ir) noexcept = 0;
160 virtual float ComputeErrorAndEnergy(float* e, const float* z, const float* mu,
161 size_t n) noexcept = 0;
164 virtual void ComputeError(float* e, const float* z, const float* mu, size_t n) noexcept = 0;
165
184 virtual float ComputeSoftmaxCrossEntropyErrorAndEnergy(float* e, const float* z, const float* mu,
185 size_t batchSize, size_t nextSize,
186 float* rowEnergies) noexcept = 0;
187
191 virtual void ComputeSoftmaxCrossEntropyError(float* e, const float* z, const float* mu,
192 size_t batchSize, size_t nextSize) noexcept = 0;
193
212 virtual bool TryFusedForwardPass(ActivationType actType, const float* zF, const float* W,
213 const float* bias, float* mu, int batchSize, int size,
214 int nextSize) noexcept = 0;
215
216 // Convolution (im2col-based, ConvPCLayer family)
217
226 virtual void Im2Col(const float* input, int channels, int height, int width, int kernelH,
227 int kernelW, int strideH, int strideW, int padH, int padW,
228 float* columns) noexcept = 0;
229
237 virtual void Col2Im(const float* columns, int channels, int height, int width, int kernelH,
238 int kernelW, int strideH, int strideW, int padH, int padW,
239 float* outputImage) noexcept = 0;
240
253 virtual void RepackForBatchedGemm(float* dst, const float* src, size_t batchSize, size_t rows,
254 size_t cols) noexcept = 0;
255
256 // Optimizer
257
261 virtual void IncrementCounter(int* counter) noexcept = 0;
262
274 virtual void AdamStep(float* param, const float* grad, float* m, float* v, size_t n, const int* t,
275 const float* lr, float beta1 = 0.9f, float beta2 = 0.999f,
276 float eps = 1e-8f) noexcept = 0;
279 virtual void AdamWStep(float* param, const float* grad, float* m, float* v, size_t n,
280 const int* t, const float* lr, float weightDecay, float beta1 = 0.9f,
281 float beta2 = 0.999f, float eps = 1e-8f) noexcept = 0;
282
284 virtual DeviceType GetDeviceType() const noexcept = 0;
285
287 virtual void MultiplyInto(float* dst, const float* a, const float* b, size_t n) noexcept = 0;
289 virtual void Fill(float* buf, size_t n, float value) noexcept = 0;
297 virtual void AddBiasPerChannel(float* buf, const float* bias, size_t channels,
298 size_t spatialSize) noexcept = 0;
299};
300} // namespace Deep
Defines the activation functions of a predictive coding model.
Device-agnostic compute backend interface: every numerical primitive a layer/network needs,...
Definition IComputeBackend.h:14
virtual void RepackForBatchedGemm(float *dst, const float *src, size_t batchSize, size_t rows, size_t cols) noexcept=0
Repacks a [batchSize, rows, cols] tensor (batch-major) into [rows, batchSize, cols] (row-major,...
virtual void Copy(float *dst, const float *src, size_t numFloats) noexcept=0
Copies numFloats floats from src to dst, both on this backend's own device.
virtual void CopyFromHost(float *deviceDst, const float *hostSrc, size_t numFloats) noexcept=0
Copies numFloats floats from a host buffer into a device buffer (a plain memcpy on CPUBackend).
virtual void SumRows(float *dst, const float *src, size_t batchSize, size_t width) noexcept=0
dst[j] = sum over b in [0,batchSize) of src[b*width + j], for all j in [0,width). Replaces a batchSiz...
virtual float ComputeSoftmaxCrossEntropyErrorAndEnergy(float *e, const float *z, const float *mu, size_t batchSize, size_t nextSize, float *rowEnergies) noexcept=0
Softmax cross-entropy variant of ComputeErrorAndEnergy, for the terminal layer only....
virtual 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=0
Adam update for n parameters in place.
virtual 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=0
Same as AdamStep(), plus decoupled weight decay (AdamW): param -= lr * weightDecay * param,...
virtual void PrepareForBatchSize(size_t batchSize) noexcept=0
Must be called once, before Compile()'s first BeginGraphCapture(), for any backend that needs to prep...
virtual void Activation(ActivationType type, float *buf, size_t n) noexcept=0
Applies type to buf in place.
virtual float ComputeErrorAndEnergy(float *e, const float *z, const float *mu, size_t n) noexcept=0
Computes e = z - mu, then returns 0.5 * sum(e^2) – the Gaussian error/energy used by every layer exce...
virtual void RandomizeNormal(float *buf, size_t n, float mean, float stddev, uint32_t seed) noexcept=0
Fills n floats with samples from a normal distribution.
virtual ~IComputeBackend()=default
virtual void ReplayGraph() noexcept=0
Launches a graph previously captured and instantiated by BeginGraphCapture()/EndGraphCapture()....
virtual void ActivationDerivative(ActivationType type, float *buf, size_t n, bool activated) noexcept=0
Applies type's derivative to buf in place.
virtual void AddBiasPerChannel(float *buf, const float *bias, size_t channels, size_t spatialSize) noexcept=0
Convolutional bias-add: buf[c*spatialSize + s] += bias[c] for all c in [0,channels),...
virtual void CopyToHost(float *hostDst, const float *deviceSrc, size_t numFloats) noexcept=0
Copies numFloats floats from a device buffer into a host buffer (a plain memcpy on CPUBackend).
virtual void MultiplyInto(float *dst, const float *a, const float *b, size_t n) noexcept=0
dst[i] = a[i] * b[i] for all i in [0, n) – elementwise product.
virtual void ActivationInto(ActivationType type, float *dst, const float *src, size_t n) noexcept=0
Two-buffer variant of Activation(): reads src, writes into dst.
virtual bool TryFusedForwardPass(ActivationType actType, const float *zF, const float *W, const float *bias, float *mu, int batchSize, int size, int nextSize) noexcept=0
Attempts a fused forward pass (GEMM + bias + activation in one kernel, via CUTLASS on GPU) for RELU o...
virtual void FusedStateUpdateMomentum(float *z, float *v, const float *feedback, const float *deriv, const float *e, size_t n, float ir, float beta) noexcept=0
Same as FusedStateUpdate, but with momentum: the update direction (feedback*deriv - e) is EMA-smoothe...
virtual void IncrementCounter(int *counter) noexcept=0
Atomically increments *counter by 1. Device-resident so it can be read/updated by the same kernel/gra...
virtual void ComputeError(float *e, const float *z, const float *mu, size_t n) noexcept=0
Same as ComputeErrorAndEnergy(), without computing (or returning) the energy – for callers that only ...
virtual void AxpyInto(float *y, const float *x, size_t n, float alpha) noexcept=0
y[i] += alpha * x[i] for all i in [0, n) – the standard AXPY.
virtual void FusedStateUpdate(float *z, const float *feedback, const float *deriv, const float *e, size_t n, float ir) noexcept=0
One settling step's state update, fused into a single call: z += ir * ((feedback * deriv) - e) Non-mo...
virtual void BeginGraphCapture() noexcept=0
Starts recording subsequent backend calls into a replayable graph, instead of executing them immediat...
virtual void ComputeSoftmaxCrossEntropyError(float *e, const float *z, const float *mu, size_t batchSize, size_t nextSize) noexcept=0
Same as ComputeSoftmaxCrossEntropyErrorAndEnergy(), without computing (or requiring rowEnergies for) ...
virtual void Scale(float *buf, size_t n, float alpha) noexcept=0
buf[i] *= alpha for all i in [0, n).
virtual void Zero(float *ptr, size_t numFloats) noexcept=0
Zero-fills numFloats floats starting at ptr.
virtual void Fill(float *buf, size_t n, float value) noexcept=0
buf[i] = value for all i in [0, n).
virtual 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=0
Rearranges a single (channels, height, width) input image into a (channels*kH*kW, outH*outW) column m...
virtual DeviceType GetDeviceType() const noexcept=0
Which device this backend instance runs on.
virtual void Free(float *ptr) noexcept=0
Frees a buffer previously returned by Allocate(). No-op on nullptr.
virtual void RandomizeUniform(float *buf, size_t n, float min, float max, uint32_t seed) noexcept=0
Fills n floats with samples from a uniform distribution over [min, max).
virtual 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=0
C = alpha * op(A) * op(B) + beta * C, the standard BLAS SGEMM convention (op(X) = X^T if the matching...
virtual void AddBiasBroadcast(float *buf, const float *bias, size_t batchSize, size_t width) noexcept=0
buf[row*width + col] += bias[col] for every row in [0,batchSize) and col in [0,width) – broadcasts a ...
virtual 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=0
The adjoint of Im2Col(): scatters a (channels*kH*kW, outH*outW) column-gradient buffer back into a (c...
virtual void ActivationDerivativeInto(ActivationType type, float *dst, const float *src, size_t n) noexcept=0
Two-buffer variant of ActivationDerivative(): reads src (the pre-activation value),...
virtual bool EndGraphCapture() noexcept=0
Ends capture and instantiates the captured graph.
virtual float * Allocate(size_t numFloats)=0
Allocates numFloats floats on this backend's device.
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