Skip to content

Commit 133c882

Browse files
committed
ADDED: _2_OPTIMIZE 0B00000010 aka REDUCE_RAM_STATIC_REFERENCE_FOR_MULTIPLE_NN_OBJECTS
1 parent 9c07032 commit 133c882

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ byte Actv_Functions[] = { 0, ..., 0, 1};
318318
| ```0B00100000``` |<sup><sub>❌</sub></sup>| <details><summary>Reduces RAM for Weights, level 1</summary>*(Partially reduce)* Not yet implimented</details>| <sub><sup>`REDUCE_RAM_WEIGHTS_LVL1`</sup></sub>|
319319
| ```0B00010000``` |<sup><sub>📌</sub></sup>| <details><summary>Reduces RAM for Weights, level 2 </summary> by a factor of (number_of_layers-1)*[2](## 'Size of a pointer (two bytes in the arduino)') Bytes</details>|<sub><sup>`REDUCE_RAM_WEIGHTS_LVL2`</sup></sub>|
320320
| ```0B00001000``` |<sup><sub>🟢</sub></sup>| <details><summary>Deletes previous layer's Gamma</summary>Always enabled **<sub><sup>(not switchable yet.)</sup></sub>**</details>|<sub><sup>`REDUCE_RAM_..._LAYER_GAMMA`</sup></sub>|
321-
| ```0B00000100``` |<sup><sub>ⓘ</sub></sup> |<details><summary>Reduces RAM using static reference</summary>... to the NN-object (for layers) \| by a factor of [2](## 'Size of a pointer (two bytes in the arduino)')*(number_of_layers - 1 or 2)bytes. _(With this optimization)_ Note that, when you are using multiple NN-**objects** interchangeably in your sketch, you should always update `NN.me` before using the next one</details>|<sub><sup>`REDUCE_RAM_STATIC_REFERENCE`</sup></sub>|
321+
| ```0B00000100``` |<sup><sub>ⓘ</sub></sup> |<details><summary>Reduces RAM using static reference</summary>... to the NN-object (for layers) \| by a factor of [2](## 'Size of a pointer (two bytes in the arduino)')*(number_of_layers - 1 or 2)bytes. _(With this optimization)_ Note that, when you are using multiple NN-**objects** interchangeably in your sketch, you should either update `NN.me` manually before using the next one like `NN.me = &NN2` or just use `_2_OPTIMIZE 0B00000010` instead</details>|<sub><sup>`REDUCE_RAM_STATIC_REFERENCE`</sup></sub>|
322322
| ```0B00000010``` |<sup><sub>📌</sub></sup>|<details><summary>Disables MSE function</summary>Disables the default loss function \| Reduces ROM, RAM & CPU consumption, althought usually needed for backpropagation</details> |<sub><sup>`DISABLE_MSE`</sup></sub>|
323323
| ```0B00000001``` |<sup><sub>ⓘ</sub></sup>|<details><summary>Use 8-Byte double instead of float</summary>This will work only if your MCU supports 8byte doubles eg. Arduino UNO DOESN'T</details> |<sub><sup>`USE_64_BIT_DOUBLE`</sup></sub>|
324324
| **_2_OPTIMIZE** | | ||
@@ -328,6 +328,7 @@ byte Actv_Functions[] = { 0, ..., 0, 1};
328328
| ```0B00010000``` |<sup><sub></sub></sup>|<details><summary>Use [F() macro](https://www.arduino.cc/reference/en/language/variables/utilities/progmem/#:~:text=about%20myself.%5Cn%22-,The%20F()%20macro,-When%20an%20instruction) for print function</summary>`Serial.print(...)` strings, normally saved in RAM. This ensures strings are stored in PROGMEM *(At least for Arduino boards)*</details> |<sub><sup>`MULTIPLE_BIASES_PER_LAYER`</sup></sub>|
329329
| ```0B00001000``` |<sup><sub>📌</sub></sup>|<details><summary>Use `int16_t` quantization</summary> Weights and biases are stored as `int16_t` *(2-bytes each)*. During the proccess of feedforward each individual weight or bias: temporarily converts back to it's equivalent float [...] Reduces memmory-footprint by a factor of half the size of the "equivalent" `float` weights and biases. Slightly CPU intensive. *(**See also:** [Training > int-quantization + details](#int-quantization))*</details> |<sub><sup>`USE_INT_QUANTIZATION`</sup></sub>|
330330
| ```0B00000100``` |<sup><sub></sub></sup>|<details><summary>Use `int8_t ` quantization</summary>Weights and biases are stored as `int8_t` *(1-byte each)*. During the proccess of feedforward each individual weight or bias: temporarily converts back to it's equivalent float [...] Reduces memmory-footprint by a factor of half the size of the "equivalent" `int16_t` weights and biases. Slightly CPU intensive. *(**See also:** [Training > int-quantization + details](#int-quantization))*</details> |<sub><sup>`USE_INT_QUANTIZATION`</sup></sub>|
331+
| ```0B00000010``` |<sup><sub></sub></sup>|<details><summary><sub><sup>`REDUCE_RAM_STATIC_REFERENCE`</sup></sub> for multiple NN </summary>It does the same thing as `REDUCE_RAM_STATIC_REFERENCE` but for multiple NN objects *(instead for just one)*. If you use `FeedForward_Individual` It is recommended to fallback to the original `_1_OPTIMIZE 0B00000100` and manually change `NN.me`</details> |<sub><sup>`...FOR_MULTIPLE_NN_OBJECTS`</sup></sub>|
331332

332333

333334
<br>

src/NeuralNetwork.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
#define MSG12
100100
#define MSG13
101101
#define MSG14
102+
#define MSG15
102103
#define LOVE \n 𝖀𝖓𝖈𝖔𝖓𝖉𝖎𝖙𝖎𝖔𝖓𝖆𝖑 𝕷𝖔𝖛𝖊
103104

104105
#define F_MACRO
@@ -149,7 +150,7 @@
149150
#if ((_1_OPTIMIZE bitor 0B11111011) == 0B11111111)
150151
#define REDUCE_RAM_STATIC_REFERENCE
151152
#undef MSG5
152-
#define MSG5 \n- " [1] 0B00000100 [⚠] [𝗥𝗲𝗺𝗶𝗻𝗱𝗲𝗿] Be careful with multiple NN objects."
153+
#define MSG5 \n- " [1] 0B00000100 [⚠] [𝗥𝗲𝗺𝗶𝗻𝗱𝗲𝗿] If multiple NN objects, see: _2_OPTIMIZE 0B00000010!"
153154
#endif
154155

155156
#if ((_1_OPTIMIZE bitor 0B11111101) == 0B11111111)
@@ -283,6 +284,16 @@
283284

284285
#define CAST_TO_LLONG_IF_NOT_INT_QUANTIZATION(value) value
285286
#endif
287+
288+
#if ((_2_OPTIMIZE bitor 0B11111101) == 0B11111111)
289+
#undef MSG15
290+
#if defined(REDUCE_RAM_STATIC_REFERENCE)
291+
#error "You don't have to define both _1_ 0B00000100 (REDUCE_RAM_STATIC_REFERENCE) and _2_ 0B00000010 (REDUCE_RAM_STATIC_REFERENCE_FOR_MULTIPLE_NN_OBJECTS)."
292+
#endif
293+
#define MSG15 \n- " [2] 0B00000010 [ⓘ] [𝗥𝗲𝗺𝗶𝗻𝗱𝗲𝗿] If single NN object, use: _1_OPTIMIZE 0B00000100 instead."
294+
#define REDUCE_RAM_STATIC_REFERENCE
295+
#define REDUCE_RAM_STATIC_REFERENCE_FOR_MULTIPLE_NN_OBJECTS
296+
#endif
286297
#endif
287298

288299
// Disable SIMD parallel processing if double-precision or int-quntization is enabled
@@ -757,7 +768,7 @@
757768
#define STR_HELPER(x) #x
758769
#define STR(x) STR_HELPER(x)
759770

760-
#define INFORMATION LOVE __NN_VERSION__ MSG0 MSG1 MSG2 MSG3 MSG4 MSG5 MSG6 MSG7 MSG8 MSG9 MSG10 MSG11 MSG12 MSG13 MSG14 \n\n 𝗨𝗦𝗜𝗡𝗚 [ƒx] AL A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 CSTA CA1 CA2 CA3 CA4 CA5 |~|\n\n NB A9 A10 A11 A12 A13 A14 NB_CA1 NB_CA2 NB_CA3 NB_CA4 NB_CA5
771+
#define INFORMATION LOVE __NN_VERSION__ MSG0 MSG1 MSG2 MSG3 MSG4 MSG5 MSG6 MSG7 MSG8 MSG9 MSG10 MSG11 MSG12 MSG13 MSG14 MSG15 \n\n 𝗨𝗦𝗜𝗡𝗚 [ƒx] AL A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 CSTA CA1 CA2 CA3 CA4 CA5 |~|\n\n NB A9 A10 A11 A12 A13 A14 NB_CA1 NB_CA2 NB_CA3 NB_CA4 NB_CA5
761772
#pragma message( STR(INFORMATION) )
762773

763774
// i might change static variables to plain variables and just pass a pointer from outer class?
@@ -1295,6 +1306,9 @@ class NeuralNetwork
12951306

12961307
DFLOAT *NeuralNetwork::FeedForward_Individual(const DFLOAT &input)
12971308
{
1309+
#if defined(REDUCE_RAM_STATIC_REFERENCE_FOR_MULTIPLE_NN_OBJECTS)
1310+
me = this;
1311+
#endif
12981312
#if defined(USE_PROGMEM)
12991313
layers[0].FdF_Individual_PROGMEM(input, Individual_Input);
13001314
#elif defined(USE_INTERNAL_EEPROM)
@@ -1378,6 +1392,10 @@ class NeuralNetwork
13781392
DFLOAT *NeuralNetwork::FeedForward(const DFLOAT *inputs)
13791393
{
13801394
_inputs = inputs;
1395+
1396+
#if defined(REDUCE_RAM_STATIC_REFERENCE_FOR_MULTIPLE_NN_OBJECTS)
1397+
me = this;
1398+
#endif
13811399

13821400
#if defined(REDUCE_RAM_DELETE_OUTPUTS)
13831401
if (FIRST_TIME_FDFp == true) // is it the first time ? if not, then delete trashes
@@ -1445,6 +1463,10 @@ class NeuralNetwork
14451463
#endif
14461464
*/
14471465

1466+
#if defined(REDUCE_RAM_STATIC_REFERENCE_FOR_MULTIPLE_NN_OBJECTS)
1467+
me = this;
1468+
#endif
1469+
14481470
layers[numberOflayers - 1].BackPropOutput(expected, layers[numberOflayers - 2].outputs); // issue because backprop einai anapoda ta weights [Fixed]
14491471

14501472
for (int i = numberOflayers - 2; i > 0; i--)

0 commit comments

Comments
 (0)