1 /******************************************************************************/\r
2 /*!\r
3 * \file timlCNNMemory.c\r
4 */\r
5 /* Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/\r
6 *\r
7 * Redistribution and use in source and binary forms, with or without\r
8 * modification, are permitted provided that the following conditions\r
9 * are met:\r
10 *\r
11 * Redistributions of source code must retain the above copyright\r
12 * notice, this list of conditions and the following disclaimer.\r
13 *\r
14 * Redistributions in binary form must reproduce the above copyright\r
15 * notice, this list of conditions and the following disclaimer in the\r
16 * documentation and/or other materials provided with the\r
17 * distribution.\r
18 *\r
19 * Neither the name of Texas Instruments Incorporated nor the names of\r
20 * its contributors may be used to endorse or promote products derived\r
21 * from this software without specific prior written permission.\r
22 *\r
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
34 *\r
35 ******************************************************************************/\r
36 \r
37 \r
38 /*******************************************************************************\r
39 *\r
40 * INCLUDES\r
41 *\r
42 ******************************************************************************/\r
43 \r
44 #include "../api/timl.h"\r
45 \r
46 \r
47 /******************************************************************************/\r
48 /*!\r
49 * \ingroup cnn\r
50 * \brief Calculate the memory in bytes required by the cnn\r
51 * \param[in,out] cnn CNN\r
52 * \return Error code\r
53 */\r
54 /******************************************************************************/\r
55 \r
56 int timlCNNMemory(timlConvNeuralNetwork *cnn)\r
57 {\r
58 timlCNNLayer *layer;\r
59 int dataSize = sizeof(float);\r
60 \r
61 cnn->forwardMemory = 0;\r
62 cnn->backwardMemory = 0;\r
63 cnn->paramsMemory = 0;\r
64 cnn->fixedMemory = sizeof(cnn);\r
65 \r
66 layer = cnn->head;\r
67 while (layer != NULL) {\r
68 switch (layer->type) {\r
69 case CNN_Input:\r
70 timlCNNInputMemory(layer);\r
71 cnn->memPoolSize = layer->forwardMemory + dataSize*layer->maxBatchSize*layer->inputParams.row*layer->inputParams.col*layer->inputParams.channel;\r
72 break;\r
73 case CNN_Conv:\r
74 timlCNNConvMemory(layer);\r
75 break;\r
76 case CNN_Linear:\r
77 timlCNNLinearMemory(layer);\r
78 break;\r
79 case CNN_Nonlinear:\r
80 timlCNNNonlinearMemory(layer);\r
81 break;\r
82 case CNN_Pooling:\r
83 timlCNNPoolingMemory(layer);\r
84 break;\r
85 case CNN_Norm:\r
86 timlCNNNormMemory(layer);\r
87 break;\r
88 case CNN_Dropout:\r
89 timlCNNDropoutMemory(layer);\r
90 break;\r
91 case CNN_Softmax:\r
92 timlCNNSoftmaxMemory(layer);\r
93 break;\r
94 case CNN_SoftmaxCost:\r
95 timlCNNSoftmaxCostMemory(layer);\r
96 break;\r
97 case CNN_Accuracy:\r
98 timlCNNAccuracyMemory(layer);\r
99 break;\r
100 default:\r
101 break;\r
102 }\r
103 if (layer->prev != NULL && (layer->prev->forwardMemory + layer->forwardMemory > cnn->memPoolSize)) {\r
104 cnn->memPoolSize = layer->prev->forwardMemory + layer->forwardMemory;\r
105 }\r
106 cnn->forwardMemory += layer->forwardMemory;\r
107 cnn->backwardMemory += layer->backwardMemory;\r
108 cnn->paramsMemory += layer->paramsMemory;\r
109 cnn->fixedMemory += sizeof(timlCNNLayer);\r
110 layer = layer->next;\r
111 }\r
112 return 0;\r
113 }\r