Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
DirectKPPCNetwork.h
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <memory>
4#include <random>
9
70
71namespace Deep
72{
78 {
79 public:
86 explicit DirectKPPCNetwork(int batchSize, DeviceType device = DeviceType::DEVICE_CPU) noexcept;
88 ~DirectKPPCNetwork() = default;
89
95
111 void AddLayer(size_t size, size_t nextSize, size_t terminalSize,
112 float lr, float ir, float fl, float lmbda,
113 ActivationType aType, ActivationType dType);
114
117 void RandomizeWeights(std::mt19937 &rng);
119 void ResetState() noexcept;
122 void Clamp(const std::vector<float> &input);
123
127 void ProjectForward() noexcept;
128
134 float CalculateTerminalError() noexcept;
135
141 void DirectFeedbackUpdate() noexcept;
142
148 float Step(bool computeEnergy = true) noexcept;
149
152 void UpdateWeights() noexcept;
153
156 {
157 for (auto &layer : layers)
158 layer->SetOptimizer(o);
159 }
160
162 {
163 for (auto &layer : layers)
164 layer->SetPsiOptimizer(o);
165 }
166
167 void SetLearningRate(float lr) noexcept
168 {
169 for (auto &layer : layers)
170 layer->SetLearningRate(lr);
171 }
172
173 void SetFeedbackRate(float fl) noexcept
174 {
175 for (auto &layer : layers)
176 layer->SetFeedbackRate(fl);
177 }
178
180 DirectKPPCLayer *GetTerminalLayer() noexcept { return layers.back().get(); }
182 std::vector<std::unique_ptr<DirectKPPCLayer>> &GetLayers() noexcept { return layers; }
184 const std::vector<std::unique_ptr<DirectKPPCLayer>> &GetLayers() const noexcept { return layers; }
186 int GetBatchSize() const noexcept { return batchSize; }
188 DeviceType GetDevice() const noexcept { return device; }
189
202 float TrainStep(const std::vector<float> &x, const std::vector<float> &y,
203 int inferenceSteps = 1);
204
207 std::vector<float> Predict(const std::vector<float> &x, int inferenceSteps);
208
213 void Compile();
214
215 private:
217 std::vector<std::unique_ptr<DirectKPPCLayer>> layers;
218
221 std::unique_ptr<IComputeBackend> backend;
223 DeviceType device;
224
226 std::unique_ptr<MemoryArena> cpuArena;
229#if defined(DEEPITY_USE_CUDA)
230 std::unique_ptr<DeviceMemoryArena> gpuArena;
231#endif
232
234 int batchSize;
235
236 // @brief CUDA Graph:
237 bool graphCaptured = false;
238 int capturedInferenceSteps = -1;
239 };
240}
A 64-byte-aligned bump-pointer allocator over a single device buffer, mirroring this codebase's host-...
Direct Kolen-Pollack predictive coding layer, routed through IComputeBackend.
A 64-byte-aligned bump-pointer allocator over a single heap-allocated block, used to pack every layer...
Predictive Coding layer using Direct Kolen-Pollack feedback alignment (DFA): a per-layer feedback mat...
Definition DirectKPPCLayer.h:34
float Step(bool computeEnergy=true) noexcept
Phase 2: runs one ordinary PC settling step (CalculateState() + UpdateState()) across every layer.
Definition DirectKPPCNetwork.cpp:82
int GetBatchSize() const noexcept
Returns the batch size for the network's layers.
Definition DirectKPPCNetwork.h:186
void SetFeedbackRate(float fl) noexcept
Sets the learning rate used for Psi updates, on every layer.
Definition DirectKPPCNetwork.h:173
std::vector< std::unique_ptr< DirectKPPCLayer > > & GetLayers() noexcept
Returns every layer in the network, in the order they were added.
Definition DirectKPPCNetwork.h:182
DirectKPPCNetwork(int batchSize, DeviceType device=DeviceType::DEVICE_CPU) noexcept
Constructs an empty network with a predetermined batch size.
Definition DirectKPPCNetwork.cpp:8
void Clamp(const std::vector< float > &input)
Clamps the input to the first layer.
Definition DirectKPPCNetwork.cpp:42
void RandomizeWeights(std::mt19937 &rng)
Randomizes the weights (W and Psi) of each layer.
Definition DirectKPPCNetwork.cpp:30
void ProjectForward() noexcept
Seeds every hidden layer's z from a genuine forward pass through current weights. Assumes layers[0] i...
Definition DirectKPPCNetwork.cpp:47
DirectKPPCLayer * GetTerminalLayer() noexcept
Returns the network's terminal (final) layer.
Definition DirectKPPCNetwork.h:180
float CalculateTerminalError() noexcept
Phase 0's final step: computes the terminal layer's error alone, producing a real,...
Definition DirectKPPCNetwork.cpp:77
const std::vector< std::unique_ptr< DirectKPPCLayer > > & GetLayers() const noexcept
Returns every layer in the network, in the order they were added.
Definition DirectKPPCNetwork.h:184
void SetPsiOptimizer(OptimizerType o) noexcept
Sets the optimizer used for Psi updates, on every layer.
Definition DirectKPPCNetwork.h:161
void SetLearningRate(float lr) noexcept
Sets the learning rate used for W updates, on every layer.
Definition DirectKPPCNetwork.h:167
void UpdateWeights() noexcept
Phase 3: updates every non-terminal layer's W and Psi. Called once after the settling loop completes.
Definition DirectKPPCNetwork.cpp:92
std::vector< float > Predict(const std::vector< float > &x, int inferenceSteps)
Forward prediction: clamp input, settle with no DFA perturbation and no target clamped,...
Definition DirectKPPCNetwork.cpp:166
void AddLayer(size_t size, size_t nextSize, size_t terminalSize, float lr, float ir, float fl, float lmbda, ActivationType aType, ActivationType dType)
Adds a layer to the network.
Definition DirectKPPCNetwork.cpp:14
void DirectFeedbackUpdate() noexcept
Phase 1: runs DirectFeedbackUpdate() on every non-terminal layer. Order among layers does not matter ...
Definition DirectKPPCNetwork.cpp:71
float TrainStep(const std::vector< float > &x, const std::vector< float > &y, int inferenceSteps=1)
Full train step: reset, clamp input+target, run all four DKP-PC phases in order, unclamp.
Definition DirectKPPCNetwork.cpp:98
void SetOptimizer(OptimizerType o) noexcept
Sets the optimizer used for W updates, on every layer.
Definition DirectKPPCNetwork.h:155
DirectKPPCNetwork & operator=(const DirectKPPCNetwork &)=delete
Copy assignment is disabled; see the copy constructor.
~DirectKPPCNetwork()=default
Default destructor.
void ResetState() noexcept
Resets each layer's state without touching learned weights.
Definition DirectKPPCNetwork.cpp:36
DirectKPPCNetwork(const DirectKPPCNetwork &)=delete
Copy construction is disabled; a network owns its layers' memory arena and is not meant to be duplica...
DeviceType GetDevice() const noexcept
Returns which device this network's layers run on.
Definition DirectKPPCNetwork.h:188
void Compile()
Loads all layers into one contiguous block of memory (MemoryArena for DEVICE_CPU, DeviceMemoryArena f...
Definition DirectKPPCNetwork.cpp:187
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
OptimizerType
Selects which weight-update rule a layer applies during UpdateWeights().
Definition AdamOptimizer.h:52
DeviceType
Which physical device a Tensor/IComputeBackend pair runs on.
Definition DeviceType.h:8
@ DEVICE_CPU
Definition DeviceType.h:9