Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
FullPCNetwork.h
Go to the documentation of this file.
1#pragma once
6#include <memory>
7#include <random>
8#include <vector>
9
26
27namespace Deep
28{
33{
34public:
39 explicit FullPCNetwork(int batchSize, DeviceType device = DeviceType::DEVICE_CPU) noexcept;
40 ~FullPCNetwork() = default;
41
42 FullPCNetwork(const FullPCNetwork&) = delete;
44
60 void AddLayer(size_t size, size_t nextSize, size_t terminalSize, float lr, float ir, float fl,
61 float lmbda, ActivationType aType, ActivationType dType);
62
68 void SetUseMuPCScaling(bool enabled) noexcept
69 {
70 useMuPCScaling = enabled;
71 }
72
80 void SetUseResidualConnections(bool enabled) noexcept
81 {
82 useResidualConnections = enabled;
83 }
84
87 void RandomizeWeights(std::mt19937& rng);
89 void ResetState() noexcept;
92 void Clamp(const std::vector<float>& input);
93
97 void ProjectForward() noexcept;
102 float CalculateTerminalError() noexcept;
106 void DirectFeedbackUpdate() noexcept;
110 float Step(bool computeEnergy = true) noexcept;
112 void UpdateWeights() noexcept;
113
116 {
117 for (auto& layer : layers)
118 layer->SetOptimizer(o);
119 }
120
122 {
123 for (auto& layer : layers)
124 layer->SetPsiOptimizer(o);
125 }
126
127 void SetLearningRate(float lr) noexcept
128 {
129 for (auto& layer : layers)
130 layer->SetLearningRate(lr);
131 }
132
133 void SetFeedbackRate(float fl) noexcept
134 {
135 for (auto& layer : layers)
136 layer->SetFeedbackRate(fl);
137 }
138
141 {
142 return layers.back().get();
143 }
144
145 std::vector<std::unique_ptr<FullPCLayer>>& GetLayers() noexcept
146 {
147 return layers;
148 }
149
150 const std::vector<std::unique_ptr<FullPCLayer>>& GetLayers() const noexcept
151 {
152 return layers;
153 }
154
155 int GetBatchSize() const noexcept
156 {
157 return batchSize;
158 }
159
160 DeviceType GetDevice() const noexcept
161 {
162 return device;
163 }
164
171 float TrainStep(const std::vector<float>& x, const std::vector<float>& y, int inferenceSteps = 1);
172
175 std::vector<float> Predict(const std::vector<float>& x, int inferenceSteps);
176
185 void Compile();
186
192 void SetUseIPC(bool enabled) noexcept
193 {
194 useIPC = enabled;
195 }
196
200 void SetUseMomentum(bool enabled, float beta = 0.9f) noexcept
201 {
202 for (auto& layer : layers)
203 layer->SetMomentum(enabled, beta);
204 }
205
211 void SetUseCrossEntropy(bool enabled) noexcept
212 {
214 }
215
216private:
217 std::vector<std::unique_ptr<FullPCLayer>> layers;
218
219 std::unique_ptr<IComputeBackend> backend;
220 DeviceType device;
221
222 std::unique_ptr<MemoryArena> cpuArena;
223#if defined(DEEPITY_USE_CUDA)
224 std::unique_ptr<DeviceMemoryArena> gpuArena;
225#endif
226
227 int batchSize;
228 bool useIPC = false;
229
230 bool useMuPCScaling = false;
231 bool useResidualConnections = false;
232
233 bool graphCaptured = false;
234 int capturedInferenceSteps = -1;
235};
236} // namespace Deep
A 64-byte-aligned bump-pointer allocator over a single device buffer, mirroring this codebase's host-...
New, standalone PC layer (not derived from DirectKPPCLayer) meant to eventually carry every technique...
A 64-byte-aligned bump-pointer allocator over a single heap-allocated block, used to pack every layer...
PC layer combining muPC scaling, optional residual connections, and DKP direct feedback – every extra...
Definition FullPCLayer.h:44
void SetCrossEntropy(bool enabled) noexcept
Enables softmax cross-entropy energy for THIS layer's error against layerBelow's mu (i....
Definition FullPCLayer.h:208
void SetOptimizer(OptimizerType o) noexcept
Sets the weight optimizer (W) on every layer.
Definition FullPCNetwork.h:115
FullPCNetwork(const FullPCNetwork &)=delete
void UpdateWeights() noexcept
Applies weight updates to every layer.
Definition FullPCNetwork.cpp:88
float Step(bool computeEnergy=true) noexcept
Runs one settling step on every layer.
Definition FullPCNetwork.cpp:78
int GetBatchSize() const noexcept
Returns the batch size given at construction.
Definition FullPCNetwork.h:155
void SetUseMomentum(bool enabled, float beta=0.9f) noexcept
Enables momentum (inertial) settling on every layer. Loops over layers and calls each one's own SetMo...
Definition FullPCNetwork.h:200
void Compile()
Loads all layers into one contiguous block of memory, wires layerAbove/layerBelow/terminalLayer acros...
Definition FullPCNetwork.cpp:194
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 FullPCNetwork.cpp:94
void DirectFeedbackUpdate() noexcept
Runs the DFA phase on every non-terminal layer: perturbs each layer's W using the layer above's Psi a...
Definition FullPCNetwork.cpp:67
void SetUseCrossEntropy(bool enabled) noexcept
Enables softmax cross-entropy energy on the TERMINAL layer only (unlike the other Set* toggles,...
Definition FullPCNetwork.h:211
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 FullPCNetwork.cpp:18
void ResetState() noexcept
Resets each layer's state without touching learned weights.
Definition FullPCNetwork.cpp:39
void SetPsiOptimizer(OptimizerType o) noexcept
Sets the feedback-weight optimizer (Psi) on every layer.
Definition FullPCNetwork.h:121
void SetFeedbackRate(float fl) noexcept
Sets the feedback rate (Psi) on every layer.
Definition FullPCNetwork.h:133
void SetUseMuPCScaling(bool enabled) noexcept
Enables muPC-style per-layer forward scaling (Table 1 of the muPC paper). OFF by default....
Definition FullPCNetwork.h:68
void SetUseIPC(bool enabled) noexcept
Enables iPC: UpdateWeights() runs every settling step instead of once after settling completes....
Definition FullPCNetwork.h:192
void RandomizeWeights(std::mt19937 &rng)
Randomizes the weights (W and Psi) of each layer.
Definition FullPCNetwork.cpp:33
void ProjectForward() noexcept
Seeds hidden layers from a genuine forward pass through current weights, instead of zero-init....
Definition FullPCNetwork.cpp:50
~FullPCNetwork()=default
const std::vector< std::unique_ptr< FullPCLayer > > & GetLayers() const noexcept
const overload of GetLayers() above.
Definition FullPCNetwork.h:150
FullPCLayer * GetTerminalLayer() noexcept
Returns the last layer (nextSize==0, the terminal one).
Definition FullPCNetwork.h:140
float CalculateTerminalError() noexcept
Computes the terminal layer's error/energy against its clamped target. Call AFTER clamping the target...
Definition FullPCNetwork.cpp:73
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 FullPCNetwork.cpp:167
std::vector< std::unique_ptr< FullPCLayer > > & GetLayers() noexcept
Returns every layer in the network, in the order they were added.
Definition FullPCNetwork.h:145
void Clamp(const std::vector< float > &input)
Clamps the input to the first layer.
Definition FullPCNetwork.cpp:45
DeviceType GetDevice() const noexcept
Returns which device this network's layers run on.
Definition FullPCNetwork.h:160
FullPCNetwork(int batchSize, DeviceType device=DeviceType::DEVICE_CPU) noexcept
Constructs an empty network; add layers via AddLayer(), then Compile() before use.
Definition FullPCNetwork.cpp:11
void SetLearningRate(float lr) noexcept
Sets the learning rate (W) on every layer.
Definition FullPCNetwork.h:127
void SetUseResidualConnections(bool enabled) noexcept
Enables residual/skip connections on middle hidden layers (muPC's "1-skip" ResNet formulation – see t...
Definition FullPCNetwork.h:80
FullPCNetwork & operator=(const FullPCNetwork &)=delete
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