Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
RBLayer.h
Go to the documentation of this file.
1#pragma once
2
5#include <cstddef>
6#include <memory>
7
21
22namespace Deep
23{
27 class RBLayer : public Layer
28 {
29 public:
45 RBLayer(size_t inSize, size_t outSize,
46 float var = 1.0f, float var_td = 10.0f,
47 float k_1 = 1e-3f, float k_2 = 1e-5f,
48 float lmbda = 1e-6f, float alpha = 1.0f,
49 size_t batchSize = 64, int stepSize = 30,
50 void(*act)(float *, size_t) = relu, void(*dAct)(float *, size_t, bool) = dRelu);
51
52 ~RBLayer() override;
53
55 void RunPrediction(const float *input, size_t currentBatchSize) noexcept;
56
58 void RunInferenceStep(const float *input, const float *topDown, size_t currentBatchSize) noexcept;
59
61 void CalcError(const float *bottomUp, const float *topDown, size_t currentBatchSize) noexcept;
62
63 // --------------------------------------------------------
64 // Core Execution (Predictive Coding Dynamics)
65 // --------------------------------------------------------
66
76 void UpdateBeliefs(const float *bottomUp, const float *topDown, size_t currentBatchSize) noexcept;
77
86 void UpdateWeights(size_t currentBatchSize) noexcept;
87
94 [[nodiscard]] float CalculateState() noexcept override;
95
102 void UpdateState() noexcept override;
103
110 void UpdateWeights() noexcept override;
111
112 void Flush() noexcept override;
113
114 [[nodiscard]] const float *GetErrors() const noexcept override { return e_bu; }
115
117 [[nodiscard]] size_t GetTotalSize() const noexcept;
118
120 void Attach(float *ptr) noexcept;
121
123 [[nodiscard]] float *GetWeights() const noexcept { return U; }
124
125 [[nodiscard]] size_t GetBatchSize() const noexcept override;
126 [[nodiscard]] size_t GetInputSize() const noexcept override;
127 [[nodiscard]] size_t GetOutputSize() const noexcept override;
128
129 [[nodiscard]] float *GetBeliefs() noexcept override;
133 [[nodiscard]] const float *GetInferenceError() const noexcept;
134
135#ifdef _DEBUG
137 void DebugStats(int layerIndex) const;
138#endif
139
140 private:
142 void CacheInput(const float *input, size_t currentBatchSize) noexcept;
143
144 // --- Hyperparameters ---
145 float var;
146 float var_td;
147 float k_1;
148 float k_2;
149 float lmbda;
150 float alpha;
151 int stepSize;
152
153 // Batch Tracking
154 size_t batchSize;
155 size_t cachedBatchSize = 0;
156 const float *cachedInput = nullptr;
157
158 // SIMD Activation
159 void (*act)(float *, size_t);
160 void (*dAct)(float *, size_t, bool);
161
162 // Arena Memory Pointers
163 float *r = nullptr;
164 float *U = nullptr;
165 float *e_bu = nullptr;
166 float *e_td = nullptr;
167
168 // BLAS Temporary Buffers
169 std::unique_ptr<float[]> inputBuffer;
170 size_t pendingCount = 0;
171 bool ownsMemory = false;
172
173 float *tmpBelief = nullptr;
174 float *tmpWeight1 = nullptr;
175 float *tmpWeight2 = nullptr;
176 float *tmpWeight3 = nullptr;
177 float *tmpWeight4 = nullptr;
178 };
179} // namespace Deep
Defines the activation functions of a predictive coding model.
Virtual Layer.
A deepity layer virtual class.
Definition Layer.h:40
void CalcError(const float *bottomUp, const float *topDown, size_t currentBatchSize) noexcept
Computes the precision-weighted prediction errors.
Definition RBLayer.cpp:134
float * GetBeliefs() noexcept override
Should return belief state z.
Definition RBLayer.cpp:299
const float * GetErrors() const noexcept override
Should return errors e.
Definition RBLayer.h:114
void UpdateWeights() noexcept override
Parameter-less override for generic layer weight updates.
Definition RBLayer.cpp:290
const float * GetInferenceError() const noexcept
Same underlying buffer as GetErrors() – the bottom-up prediction error, named separately here for cal...
Definition RBLayer.cpp:300
void UpdateState() noexcept override
Computes the state derivatives and applies them to current beliefs.
Definition RBLayer.cpp:285
size_t GetOutputSize() const noexcept override
Should return output size.
Definition RBLayer.cpp:298
void UpdateBeliefs(const float *bottomUp, const float *topDown, size_t currentBatchSize) noexcept
Updates internal state beliefs via gradient descent on the prediction error.
Definition RBLayer.cpp:168
void Flush() noexcept override
Flushes remaining batches.
Definition RBLayer.cpp:262
~RBLayer() override
Definition RBLayer.cpp:27
float * GetWeights() const noexcept
Returns the synaptic weight matrix U.
Definition RBLayer.h:123
size_t GetTotalSize() const noexcept
Returns the total byte size required by this layer for flat allocation.
Definition RBLayer.cpp:37
size_t GetBatchSize() const noexcept override
Should return batch size.
Definition RBLayer.cpp:296
RBLayer(size_t inSize, size_t outSize, float var=1.0f, float var_td=10.0f, float k_1=1e-3f, float k_2=1e-5f, float lmbda=1e-6f, float alpha=1.0f, size_t batchSize=64, int stepSize=30, void(*act)(float *, size_t)=relu, void(*dAct)(float *, size_t, bool)=dRelu)
Constructs an RBLayer with specified dimensions and hyperparameters.
Definition RBLayer.cpp:13
void RunPrediction(const float *input, size_t currentBatchSize) noexcept
Executes a forward pass prediction without updating internal beliefs.
Definition RBLayer.cpp:88
void Attach(float *ptr) noexcept
Attaches the layer to a pre-allocated, 64-byte aligned memory block.
Definition RBLayer.cpp:52
void RunInferenceStep(const float *input, const float *topDown, size_t currentBatchSize) noexcept
Executes a single bidirectional inference step integrating bottom-up and top-down signals.
Definition RBLayer.cpp:121
size_t GetInputSize() const noexcept override
Should return input size.
Definition RBLayer.cpp:297
float CalculateState() noexcept override
Calculates the total layer energy state.
Definition RBLayer.cpp:279
This file exists to provide both Tensor.h and IComputeBackend.h with device types without introducing...
Definition Backend.cpp:10
static void relu(float *, size_t) noexcept
See Relu.h.
static void dRelu(float *, size_t, bool) noexcept
See Relu.h.