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;
41 uint32_t seed)
noexcept override;
44 uint32_t seed)
noexcept override;
53 void MatMul(
bool transA,
bool transB,
int M,
int N,
int K,
float alpha,
const float* A,
int lda,
54 const float* B,
int ldb,
float beta,
float* C,
int ldc)
noexcept override;
56 void SumRows(
float* dst,
const float* src,
size_t batchSize,
size_t width)
noexcept override;
59 void Scale(
float* buf,
size_t n,
float alpha)
noexcept override;
61 void AxpyInto(
float* y,
const float* x,
size_t n,
float alpha)
noexcept override;
64 size_t width)
noexcept override;
68 const float* bias,
float* mu,
int batchSize,
int size,
69 int nextSize)
noexcept override
80 size_t n)
noexcept override;
83 bool activated)
noexcept override;
86 size_t n)
noexcept override;
90 const float* e,
size_t n,
float ir,
float beta)
noexcept override;
93 void FusedStateUpdate(
float* z,
const float* feedback,
const float* deriv,
const float* e,
94 size_t n,
float ir)
noexcept override;
97 size_t n)
noexcept override;
99 void ComputeError(
float* e,
const float* z,
const float* mu,
size_t n)
noexcept override;
103 size_t batchSize,
size_t nextSize,
104 float* rowEnergies)
noexcept override;
108 size_t nextSize)
noexcept override;
114 void Im2Col(
const float* input,
int channels,
int height,
int width,
int kernelH,
int kernelW,
115 int strideH,
int strideW,
int padH,
int padW,
float* columns)
noexcept override;
117 void Col2Im(
const float* columns,
int channels,
int height,
int width,
int kernelH,
int kernelW,
118 int strideH,
int strideW,
int padH,
int padW,
float* outputImage)
noexcept override;
121 size_t cols)
noexcept override;
127 void AdamStep(
float* param,
const float* grad,
float* m,
float* v,
size_t n,
const int* t,
128 const float* lr,
float beta1 = 0.9f,
float beta2 = 0.999f,
129 float eps = 1e-8f) noexcept override;
131 void AdamWStep(
float* param, const
float* grad,
float* m,
float* v,
size_t n, const
int* t,
132 const
float* lr,
float weightDecay,
float beta1 = 0.9f,
float beta2 = 0.999f,
133 float eps = 1e-8f) noexcept override;
142 void MultiplyInto(
float* dst,
const float* a,
const float* b,
size_t n)
noexcept override;
144 void Fill(
float* buf,
size_t n,
float value)
noexcept override;
147 size_t spatialSize)
noexcept override;
A device-aware, RAII-managed buffer. Device is fixed at construction – there is no support for moving...
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.
Definition CPUBackend.cpp:134
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.
Definition CPUBackend.cpp:61
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...
Definition CPUBackend.cpp:339
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.
Definition CPUBackend.cpp:46
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) ...
Definition CPUBackend.cpp:258
void ReplayGraph() noexcept override
Launches a graph previously captured and instantiated by BeginGraphCapture()/EndGraphCapture()....
Definition CPUBackend.h:25
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,...
Definition CPUBackend.cpp:356
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...
Definition CPUBackend.cpp:184
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...
Definition CPUBackend.cpp:171
void Activation(ActivationType type, float *buf, size_t n) noexcept override
Applies type to buf in place.
Definition CPUBackend.cpp:147
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.
Definition CPUBackend.cpp:291
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),...
Definition CPUBackend.cpp:165
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).
Definition CPUBackend.cpp:51
void Fill(float *buf, size_t n, float value) noexcept override
buf[i] = value for all i in [0, n).
Definition CPUBackend.cpp:389
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),...
Definition CPUBackend.cpp:398
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...
Definition CPUBackend.cpp:103
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...
Definition CPUBackend.cpp:194
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...
Definition CPUBackend.cpp:122
void BeginGraphCapture() noexcept override
Starts recording subsequent backend calls into a replayable graph, instead of executing them immediat...
Definition CPUBackend.h:18
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....
Definition CPUBackend.cpp:217
float * Allocate(size_t numFloats) override
Allocates numFloats floats on this backend's device.
Definition CPUBackend.cpp:23
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...
Definition CPUBackend.h:67
DeviceType GetDeviceType() const noexcept override
Which device this backend instance runs on.
Definition CPUBackend.h:136
void Zero(float *ptr, size_t numFloats) noexcept override
Zero-fills numFloats floats starting at ptr.
Definition CPUBackend.cpp:41
void Free(float *ptr) noexcept override
Frees a buffer previously returned by Allocate(). No-op on nullptr.
Definition CPUBackend.cpp:32
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).
Definition CPUBackend.cpp:82
void ActivationInto(ActivationType type, float *dst, const float *src, size_t n) noexcept override
Two-buffer variant of Activation(): reads src, writes into dst.
Definition CPUBackend.cpp:152
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 ...
Definition CPUBackend.cpp:210
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.
Definition CPUBackend.cpp:380
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,...
Definition CPUBackend.cpp:311
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).
Definition CPUBackend.cpp:56
void Scale(float *buf, size_t n, float alpha) noexcept override
buf[i] *= alpha for all i in [0, n).
Definition CPUBackend.cpp:129
bool EndGraphCapture() noexcept override
Ends capture and instantiates the captured graph.
Definition CPUBackend.h:20
void PrepareForBatchSize(size_t batchSize) noexcept override
Must be called once, before Compile()'s first BeginGraphCapture(), for any backend that needs to prep...
Definition CPUBackend.h:50
void IncrementCounter(int *counter) noexcept override
Atomically increments *counter by 1. Device-resident so it can be read/updated by the same kernel/gra...
Definition CPUBackend.cpp:286
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 ...
Definition CPUBackend.cpp:139
~CPUBackend() override=default
void ActivationDerivative(ActivationType type, float *buf, size_t n, bool activated) noexcept override
Applies type's derivative to buf in place.
Definition CPUBackend.cpp:159
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...
Definition CPUBackend.cpp:331
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_CPU
Definition DeviceType.h:9