Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
SimpleConvPCLayer.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <stdexcept>
5#include <random>
6#include <memory>
7#include <cstdlib>
15
30
31namespace Deep
32{
33 class SimpleConvPCNDiagnostics;
34
38 class SimpleConvPCLayer : public Layer
39 {
40 public:
54 SimpleConvPCLayer(int inChannels, int outChannels,
55 int inHeight, int inWidth,
56 int kernelH, int kernelW,
57 int strideH = 1, int strideW = 1,
58 int padH = 0, int padW = 0,
59 int batchSize = 1,
60 float learningRate = 1e-6f, float inferenceRate = 0.1f,
61 float lmbda = 1e-2f,
64 IComputeBackend *backend = nullptr);
65
66 float CalculateState() noexcept override;
67 void UpdateState() noexcept override;
68 void UpdateWeights() noexcept override;
69
70 void Flush() noexcept override {}
71
74 void ClampState(const std::vector<float> &inputData) noexcept;
76 void UnclampState() noexcept;
77
78 float *GetBeliefs() noexcept override { return z; }
79 const float *GetErrors() const noexcept override { return e; }
80 size_t GetInputSize() const noexcept override { return (size_t)inChannels * inHeight * inWidth; }
81 size_t GetOutputSize() const noexcept override
82 {
83 return outChannels > 0 ? (size_t)outChannels * outHeight * outWidth : 0;
84 }
85 size_t GetBatchSize() const noexcept override { return batchSize; }
86
88 const float *GetWeights() const noexcept { return W; }
90 float *GetWeights() noexcept { return W; }
92 const float *GetBiases() const noexcept { return b; }
94 float *GetBiases() noexcept { return b; }
95
97 float GetLearningRate() const noexcept { return lr; }
99 float GetInferenceRate() const noexcept { return ir; }
101 float GetLambda() const noexcept { return lmbda; }
102
104 void SetLearningRate(float lr) noexcept;
106 void SetInferenceRate(float ir) noexcept { this->ir = ir; }
108 void SetLambda(float l) noexcept { this->lmbda = l; }
110 void SetOptimizer(const OptimizerType o) noexcept { opt = o; }
112 bool IsClamped() const noexcept { return isClamped; }
113
115 void SetLayerAbove(SimpleConvPCLayer *above) noexcept { layerAbove = above; }
117 void SetLayerBelow(SimpleConvPCLayer *below) noexcept { layerBelow = below; }
118
120 void ComputeMuOnly() noexcept;
121
123 void ResetState() noexcept;
125 void RandomizeWeights(std::mt19937 &twister) noexcept;
126
128 ActivationType GetActivationType() const noexcept { return activationType; }
130 ActivationType GetDerivativeType() const noexcept { return derivativeType; }
131
133 const float *GetMu() const noexcept { return mu; }
135 int GetInChannels() const noexcept { return inChannels; }
137 int GetOutChannels() const noexcept { return outChannels; }
139 int GetInHeight() const noexcept { return inHeight; }
141 int GetInWidth() const noexcept { return inWidth; }
143 int GetOutHeight() const noexcept { return outHeight; }
145 int GetOutWidth() const noexcept { return outWidth; }
147 int GetKernelH() const noexcept { return kernelH; }
149 int GetKernelW() const noexcept { return kernelW; }
150
152 size_t GetRequiredFloats() const noexcept;
153
157 template <typename ArenaT>
158 void BindMemory(ArenaT &arena);
159
160 private:
161 std::unique_ptr<MemoryArena> localArena;
162
163 int inChannels, outChannels;
164 int inHeight, inWidth;
165 int outHeight, outWidth;
166 int kernelH, kernelW;
167 int strideH, strideW;
168 int padH, padW;
169 int batchSize;
170
171 float *W = nullptr;
172 float *b = nullptr;
173
174 float *z = nullptr;
175 float *e = nullptr;
176 float *dz_dt = nullptr;
177
178 float *mu = nullptr;
179 float *colBuffer = nullptr;
180 float *feedbackScratch = nullptr;
181 float *bottom_up_cols = nullptr;
182 float *colsRepacked = nullptr;
183 float *lgRepacked = nullptr;
184 float *muRepacked = nullptr;
185
186 // All-ones vector for the bias-gradient GEMM trick (grad_b =
187 // lgRepacked @ ones) -- see UpdateWeights()'s implementation
188 // comment for why this replaces the original per-row scalar
189 // sum loop.
190 float *onesVector = nullptr;
191
192 float *cachedMu = nullptr;
193 bool muCacheValid = false;
194
195 float *grad_W = nullptr;
196 float *grad_b = nullptr;
197 float *m_W = nullptr;
198 float *v_W = nullptr;
199 float *m_b = nullptr;
200 float *v_b = nullptr;
201
202 // Device-resident t/lr -- same reasoning as SimplePCLayer's own
203 // port: graph capture (once this reaches GPU) can't re-record
204 // for every changed learning rate or Adam step count, so both
205 // must live in device memory the graph reads from directly.
206 int *t_device = nullptr;
207 float *lr_device = nullptr;
208
209 float lr, ir, lmbda;
210 bool isClamped = false;
211
212 SimpleConvPCLayer *layerAbove = nullptr;
213 SimpleConvPCLayer *layerBelow = nullptr;
214 ActivationType activationType;
215 ActivationType derivativeType;
217
218 using BackendDeleter = void (*)(IComputeBackend *);
219 std::unique_ptr<IComputeBackend, BackendDeleter> backend;
220
222 };
223
224} // namespace Deep
Defines the activation functions of a predictive coding model.
Vectorized AdamW update, operating per-element over an array of weights (or biases) – NOT the single-...
A 64-byte-aligned bump-pointer allocator over a single device buffer, mirroring this codebase's host-...
Standalone im2col / col2im utilities: convolution expressed as matrix multiplication,...
Virtual Layer.
A 64-byte-aligned bump-pointer allocator over a single heap-allocated block, used to pack every layer...
Device-agnostic compute backend interface: every numerical primitive a layer/network needs,...
Definition IComputeBackend.h:14
A deepity layer virtual class.
Definition Layer.h:40
A 64-byte-aligned bump-pointer allocator over a single contiguous block of host memory....
Definition MemoryArena.h:141
Convolutional PC layer, precision-free, AdamW-capable, routed through IComputeBackend for GPU portabi...
Definition SimpleConvPCLayer.h:39
void SetOptimizer(const OptimizerType o) noexcept
Sets the weight optimizer. Call BEFORE Compile().
Definition SimpleConvPCLayer.h:110
void Flush() noexcept override
Flushes remaining batches.
Definition SimpleConvPCLayer.h:70
void BindMemory(ArenaT &arena)
Binds this layer's buffers into a pre-allocated arena (MemoryArena for CPU, DeviceMemoryArena for GPU...
Definition SimpleConvPCLayer.cpp:93
int GetOutHeight() const noexcept
Returns the output height.
Definition SimpleConvPCLayer.h:143
float * GetBiases() noexcept
Mutable overload of GetBiases() above.
Definition SimpleConvPCLayer.h:94
void SetInferenceRate(float ir) noexcept
Sets the inference rate.
Definition SimpleConvPCLayer.h:106
void SetLambda(float l) noexcept
Sets the weight decay coefficient.
Definition SimpleConvPCLayer.h:108
int GetInWidth() const noexcept
Returns the input width.
Definition SimpleConvPCLayer.h:141
size_t GetBatchSize() const noexcept override
Should return batch size.
Definition SimpleConvPCLayer.h:85
int GetKernelH() const noexcept
Returns the kernel height.
Definition SimpleConvPCLayer.h:147
size_t GetInputSize() const noexcept override
Should return input size.
Definition SimpleConvPCLayer.h:80
int GetOutWidth() const noexcept
Returns the output width.
Definition SimpleConvPCLayer.h:145
ActivationType GetActivationType() const noexcept
Returns this layer's activation type.
Definition SimpleConvPCLayer.h:128
int GetInHeight() const noexcept
Returns the input height.
Definition SimpleConvPCLayer.h:139
const float * GetErrors() const noexcept override
Should return errors e.
Definition SimpleConvPCLayer.h:79
void SetLearningRate(float lr) noexcept
Sets the learning rate, propagating it to the optimizer state.
Definition SimpleConvPCLayer.cpp:43
float GetLambda() const noexcept
Returns the current weight decay coefficient.
Definition SimpleConvPCLayer.h:101
int GetKernelW() const noexcept
Returns the kernel width.
Definition SimpleConvPCLayer.h:149
void ClampState(const std::vector< float > &inputData) noexcept
Clamps this layer's beliefs to inputData, fixing them against UpdateState() until UnclampState() is c...
Definition SimpleConvPCLayer.cpp:405
size_t GetRequiredFloats() const noexcept
Total floats this layer needs from its MemoryArena.
Definition SimpleConvPCLayer.cpp:50
size_t GetOutputSize() const noexcept override
Should return output size.
Definition SimpleConvPCLayer.h:81
void SetLayerAbove(SimpleConvPCLayer *above) noexcept
Sets the layer immediately above this one in the network.
Definition SimpleConvPCLayer.h:115
float GetLearningRate() const noexcept
Returns the current learning rate.
Definition SimpleConvPCLayer.h:97
int GetInChannels() const noexcept
Returns the input channel count.
Definition SimpleConvPCLayer.h:135
void UpdateState() noexcept override
Updates the states after calculation.
Definition SimpleConvPCLayer.cpp:268
void SetLayerBelow(SimpleConvPCLayer *below) noexcept
Sets the layer immediately below this one in the network.
Definition SimpleConvPCLayer.h:117
friend class SimpleConvPCNDiagnostics
Definition SimpleConvPCLayer.h:221
float * GetBeliefs() noexcept override
Should return belief state z.
Definition SimpleConvPCLayer.h:78
float GetInferenceRate() const noexcept
Returns the current inference rate.
Definition SimpleConvPCLayer.h:99
void ResetState() noexcept
Resets this layer's beliefs (z) without touching learned weights.
Definition SimpleConvPCLayer.cpp:399
SimpleConvPCLayer(int inChannels, int outChannels, int inHeight, int inWidth, int kernelH, int kernelW, int strideH=1, int strideW=1, int padH=0, int padW=0, int batchSize=1, float learningRate=1e-6f, float inferenceRate=0.1f, float lmbda=1e-2f, ActivationType aType=ActivationType::RELU, ActivationType dType=ActivationType::dRELU, IComputeBackend *backend=nullptr)
Definition SimpleConvPCLayer.cpp:14
void UpdateWeights() noexcept override
Updates the weights after state updates.
Definition SimpleConvPCLayer.cpp:316
ActivationType GetDerivativeType() const noexcept
Returns this layer's activation derivative type.
Definition SimpleConvPCLayer.h:130
const float * GetBiases() const noexcept
Returns the bias vector, length outChannels.
Definition SimpleConvPCLayer.h:92
const float * GetWeights() const noexcept
Returns the weight tensor, shape [outChannels, inChannels*kernelH*kernelW].
Definition SimpleConvPCLayer.h:88
float CalculateState() noexcept override
Calculates the internal state of the layer.
Definition SimpleConvPCLayer.cpp:202
const float * GetMu() const noexcept
Returns this layer's forward prediction buffer.
Definition SimpleConvPCLayer.h:133
void UnclampState() noexcept
Releases a previous ClampState() call.
Definition SimpleConvPCLayer.cpp:414
void RandomizeWeights(std::mt19937 &twister) noexcept
Randomizes W/b.
Definition SimpleConvPCLayer.cpp:187
bool IsClamped() const noexcept
Whether ClampState() is currently active on this layer.
Definition SimpleConvPCLayer.h:112
float * GetWeights() noexcept
Mutable overload of GetWeights() above.
Definition SimpleConvPCLayer.h:90
void ComputeMuOnly() noexcept
Computes only mu (forward prediction), skipping error/energy.
Definition SimpleConvPCLayer.cpp:223
int GetOutChannels() const noexcept
Returns the output channel count (0 for a terminal layer).
Definition SimpleConvPCLayer.h:137
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
@ RELU
Definition ActivationType.h:26
@ dRELU
Definition ActivationType.h:27
OptimizerType
Selects which weight-update rule a layer applies during UpdateWeights().
Definition AdamOptimizer.h:52
@ SGD
Plain stochastic gradient descent (this codebase's existing cblas-based update).
Definition AdamOptimizer.h:53