1 /******************************************************************************/\r
2 /*!\r
3 * \file timlCNNSoftmaxInitialize.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 Initialize the softmax layer\r
51 * \param[in] layer Layer ptr\r
52 * \return Error code\r
53 */\r
54 /******************************************************************************/\r
55 \r
56 int timlCNNSoftmaxInitialize(timlCNNLayer *layer)\r
57 {\r
58 timlConvNeuralNetwork *cnn = layer->cnn;\r
59 char *offset;\r
60 \r
61 // level 1\r
62 if (layer->allocatorLevel == Util_AllocatorLevel1) {\r
63 // feature map\r
64 if (timlUtilMallocAcc((void**) &(layer->featureMap), sizeof(float)*layer->maxBatchSize*layer->row*layer->col*layer->channel) != 0) {\r
65 return ERROR_CNN_LAYER_ALLOCATION;\r
66 }\r
67 // feature map delta\r
68 if (timlUtilMallocAcc((void**) &(layer->delta), sizeof(float)*layer->maxBatchSize*layer->row*layer->col*layer->channel) != 0) {\r
69 return ERROR_CNN_LAYER_ALLOCATION;\r
70 }\r
71 // jacobian matrix\r
72 if (timlUtilMallocAcc((void**) &(layer->softmaxParams.jacob), sizeof(float)*layer->maxBatchSize*layer->row*layer->col*layer->channel*layer->channel) != 0) {\r
73 return ERROR_CNN_LAYER_ALLOCATION;\r
74 }\r
75 // max\r
76 if (timlUtilMallocAcc((void**) &(layer->softmaxParams.max), sizeof(float)*layer->maxBatchSize*layer->row*layer->col) != 0) {\r
77 return ERROR_CNN_LAYER_ALLOCATION;\r
78 }\r
79 // sum\r
80 if (timlUtilMallocAcc((void**) &(layer->softmaxParams.sum), sizeof(float)*layer->maxBatchSize*layer->row*layer->col) != 0) {\r
81 return ERROR_CNN_LAYER_ALLOCATION;\r
82 }\r
83 }\r
84 \r
85 // level 2\r
86 if (layer->allocatorLevel == Util_AllocatorLevel2) {\r
87 // feature map\r
88 if (timlUtilMallocAcc((void**) &(layer->featureMap), sizeof(float)*layer->maxBatchSize*layer->row*layer->col*layer->channel) != 0) {\r
89 return ERROR_CNN_LAYER_ALLOCATION;\r
90 }\r
91 // max\r
92 if (timlUtilMallocAcc((void**) &(layer->softmaxParams.max), sizeof(float)*layer->maxBatchSize*layer->row*layer->col) != 0) {\r
93 return ERROR_CNN_LAYER_ALLOCATION;\r
94 }\r
95 // sum\r
96 if (timlUtilMallocAcc((void**) &(layer->softmaxParams.sum), sizeof(float)*layer->maxBatchSize*layer->row*layer->col) != 0) {\r
97 return ERROR_CNN_LAYER_ALLOCATION;\r
98 }\r
99 }\r
100 \r
101 // allocatorLevel 3 specific\r
102 if (layer->allocatorLevel == Util_AllocatorLevel3) {\r
103 if (layer->prev->memPoolPos == Util_MemPoolTop) {\r
104 layer->memPoolPos = Util_MemPoolTop;\r
105 layer->featureMap = layer->prev->featureMap;\r
106 offset = cnn->memPool + sizeof(float)*layer->maxBatchSize*layer->row*layer->col*layer->channel;\r
107 layer->softmaxParams.max = offset;\r
108 offset += sizeof(float)*layer->maxBatchSize*layer->row*layer->col;\r
109 layer->softmaxParams.sum = offset;\r
110 }\r
111 else {\r
112 layer->memPoolPos = Util_MemPoolBottom;\r
113 offset = cnn->memPool + cnn->memPoolSize - layer->forwardMemory;\r
114 layer->softmaxParams.max = offset;\r
115 offset += sizeof(float)*layer->maxBatchSize*layer->row*layer->col;\r
116 layer->softmaxParams.sum = offset;\r
117 layer->featureMap = layer->prev->featureMap;\r
118 }\r
119 }\r
120 return 0;\r
121 }\r