1 /******************************************************************************/
2 /*!
3 * \file timlCNNProfile.c
4 */
5 /* Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
17 * distribution.
18 *
19 * Neither the name of Texas Instruments Incorporated nor the names of
20 * its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 ******************************************************************************/
38 /*******************************************************************************
39 *
40 * INCLUDES
41 *
42 ******************************************************************************/
44 #include "../api/timl.h"
47 /******************************************************************************/
48 /*!
49 * \ingroup cnn
50 * \brief Profile the CNN with both timing and memory allocation
51 * \param[in] cnn CNN
52 * \param[in] data Input data batch pointer
53 * \param[in] dim Data dimension
54 * \param[in] batchSize Data batch size
55 * \param[in] label Label ptr
56 * \param[in] iter Iterations
57 * \return Error code
58 */
59 /******************************************************************************/
61 int timlCNNProfile(timlConvNeuralNetwork *cnn, float *image, int row, int col, int channel, int *label, int labelRow, int labelCol, int batchSize, int iter)
62 {
63 int j;
64 struct timespec startTime;
65 struct timespec endTime;
66 struct timespec fpStartTime;
67 struct timespec fpEndTime;
68 struct timespec bpStartTime;
69 struct timespec bpEndTime;
70 struct timespec layerStartTime;
71 struct timespec layerEndTime;
72 float time;
73 int err;
74 timlCNNLayer *bpStartLayer;
75 timlCNNLayer *layer;
76 float cost;
77 long mem;
79 err = 0;
80 layer = cnn->head;
81 timlCNNPrint(cnn);
82 printf("Batch size = %d, iteration = %d.\n", batchSize, iter);
84 // load image and label
85 timlCNNLoadImage(cnn, image, row, col, channel, cnn->params.batchSize);
86 timlCNNLoadLabel(cnn, label, labelRow, labelCol, cnn->params.batchSize);
88 clock_gettime(CLOCK_REALTIME, &startTime);
89 clock_gettime(CLOCK_REALTIME, &fpStartTime);
91 clock_gettime(CLOCK_REALTIME, &layerStartTime);
92 timlCNNInputForwardPropagation(layer);
93 clock_gettime(CLOCK_REALTIME, &layerEndTime);
94 time = timlUtilDiffTime(layerStartTime, layerEndTime);
95 printf("[Forward] layer %d(%s) = %.2f us\n", layer->id, timlCNNLayerTypeStr(layer), time);
97 while (layer->next != NULL) {
98 clock_gettime(CLOCK_REALTIME, &layerStartTime);
99 for (j = 0; j < iter; j++) {
100 switch (layer->next->type) {
101 case CNN_Softmax:
102 timlCNNSoftmaxForwardPropagation(layer);
103 break;
104 case CNN_Accuracy:
105 timlCNNAccuracyForwardPropagation(layer);
106 break;
107 case CNN_SoftmaxCost:
108 timlCNNSoftmaxCostForwardPropagation(layer);
109 break;
110 case CNN_Conv:
111 timlCNNConvForwardPropagation(layer);
112 break;
113 case CNN_Norm:
114 timlCNNNormForwardPropagation(layer);
115 break;
116 case CNN_Pooling:
117 timlCNNPoolingForwardPropagation(layer);
118 break;
119 case CNN_Linear:
120 timlCNNLinearForwardPropagation(layer);
121 break;
122 case CNN_Nonlinear:
123 timlCNNNonlinearForwardPropagation(layer);
124 break;
125 case CNN_Dropout:
126 timlCNNDropoutForwardPropagation(layer);
127 break;
128 default:
129 break;
130 }
131 }
133 clock_gettime(CLOCK_REALTIME, &layerEndTime);
134 time = timlUtilDiffTime(layerStartTime, layerEndTime);
135 printf("[Forward] layer %d(%s) = %.2f us\n", layer->next->id, timlCNNLayerTypeStr(layer->next), time);
137 layer = layer->next;
139 }
141 clock_gettime(CLOCK_REALTIME, &fpEndTime);
142 time = timlUtilDiffTime(fpStartTime, fpEndTime);
143 printf("[Forward] total time = %.2f us\n", time);
145 if (cnn->params.phase == Util_Test) {
146 return err;
147 }
149 // back propagation
150 clock_gettime(CLOCK_REALTIME, &bpStartTime);
152 while (layer->prev != NULL) {
153 clock_gettime(CLOCK_REALTIME, &layerStartTime);
154 for (j = 0; j < iter; j++) {
155 switch (layer->type) {
156 case CNN_Input:
157 break;
158 case CNN_Conv:
159 timlCNNConvBackPropagation(layer);
160 break;
161 case CNN_Pooling:
162 timlCNNPoolingBackPropagation(layer);
163 break;
164 case CNN_Norm:
165 timlCNNNormBackPropagation(layer);
166 break;
167 case CNN_Nonlinear:
168 timlCNNNonlinearBackPropagation(layer);
169 break;
170 case CNN_Linear:
171 timlCNNLinearBackPropagation(layer);
172 break;
173 case CNN_Dropout:
174 timlCNNDropoutBackPropagation(layer);
175 break;
176 case CNN_SoftmaxCost:
177 timlCNNSoftmaxCostBackPropagation(layer);
178 break;
179 case CNN_Softmax:
180 timlCNNSoftmaxBackPropagation(layer);
181 break;
182 default:
183 break;
184 }
185 }
186 clock_gettime(CLOCK_REALTIME, &layerEndTime);
187 time = timlUtilDiffTime(layerStartTime, layerEndTime);
188 printf("[Backward] layer %d(%s) = %.2f us\n", layer->id, timlCNNLayerTypeStr(layer), time);
189 layer = layer->prev;
190 }
191 clock_gettime(CLOCK_REALTIME, &bpEndTime);
192 clock_gettime(CLOCK_REALTIME, &endTime);
193 time = timlUtilDiffTime(bpStartTime, bpEndTime);
194 printf("[Backward] total time = %.2f us\n", time);
196 time = timlUtilDiffTime(startTime, endTime);
197 printf("total time = %.2f us\n", time);
199 return err;
200 }