-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
171 lines (153 loc) · 5.46 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (C) 2017 GreenWaves Technologies
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD license. See the LICENSE file for details.
*
*/
/* Autotiler includes. */
#include "cifar10_model.h"
#include "cifar10_modelKernels.h"
#include "cifar10_modelInfo.h"
#include "gaplib/ImgIO.h"
#define __XSTR(__s) __STR(__s)
#define __STR(__s) #__s
#ifndef STACK_SIZE
#define STACK_SIZE 1024
#endif
AT_HYPERFLASH_FS_EXT_ADDR_TYPE cifar10_model_L3_Flash = 0;
static char * ClassDict[10] = {"plane", "car", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"};
/* Inputs */
char *ImageName;
#ifdef MODEL_NE16
L2_MEM unsigned char Input_1[3072];
#else
L2_MEM signed char Input_1[3072];
#endif
/* Outputs */
#ifdef OUTPUT_SHORT
L2_MEM short int Output_1[10];
#else
#ifdef MODEL_NE16
L2_MEM unsigned char Output_1[10];
#else
L2_MEM signed char Output_1[10];
#endif
#endif
int MaxPred, PredClass;
static void cluster()
{
#ifdef PERF
printf("Start timer\n");
gap_cl_starttimer();
gap_cl_resethwtimer();
#endif
cifar10_modelCNN(Input_1, Output_1);
printf("Runner completed\n");
MaxPred=Output_1[0], PredClass=0;
for (int i=1; i<10; i++){
printf("Class: %10s --> %5.2f\n", ClassDict[i], (((float) Output_1[i]) - cifar10_model_Output_1_OUT_ZERO_POINT) * cifar10_model_Output_1_OUT_SCALE);
if (Output_1[i] > MaxPred) {
MaxPred = Output_1[i];
PredClass = i;
}
}
printf("Predicted Class: %10s with confidence: %.2f\n", ClassDict[PredClass], (((float) MaxPred) - cifar10_model_Output_1_OUT_ZERO_POINT) * cifar10_model_Output_1_OUT_SCALE);
}
int test_cifar10_model(void)
{
printf("Entering main controller\n");
/* ---------------->
* Put here Your input settings
* <---------------
*/
ImageName = __XSTR(AT_IMAGE);
#if defined(MODEL_HWC) || defined(MODEL_NE16)
int Traspose2CHW = 0;
#else
int Traspose2CHW = 1;
#endif
printf("Reading image in %s\n", Traspose2CHW?"CHW":"HWC");
if (ReadImageFromFile(ImageName, 32, 32, 3, Input_1, 32*32*3*sizeof(char), IMGIO_OUTPUT_CHAR, Traspose2CHW)) {
printf("Failed to load image %s\n", ImageName);
return -1;
}
// NE16 takes unsigned input with zero point implied (-128)
#if !defined(MODEL_NE16)
for (int i=0; i<32*32*3; i++) Input_1[i] -= 128;
#endif
#ifndef __EMUL__
/* Configure And open cluster. */
struct pi_device cluster_dev;
struct pi_cluster_conf cl_conf;
pi_cluster_conf_init(&cl_conf);
cl_conf.id = 0;
cl_conf.cc_stack_size = STACK_SIZE;
cl_conf.scratch_size = SLAVE_STACK_SIZE * pi_cl_cluster_nb_pe_cores();
pi_open_from_conf(&cluster_dev, (void *) &cl_conf);
if (pi_cluster_open(&cluster_dev))
{
printf("Cluster open failed !\n");
return -4;
}
pi_freq_set(PI_FREQ_DOMAIN_FC, FREQ_FC*1000*1000);
pi_freq_set(PI_FREQ_DOMAIN_CL, FREQ_CL*1000*1000);
pi_freq_set(PI_FREQ_DOMAIN_PERIPH, FREQ_FC*1000*1000);
printf("Set FC Frequency = %d MHz, CL Frequency = %d MHz, PERIIPH Frequency = %d MHz\n",
pi_freq_get(PI_FREQ_DOMAIN_FC), pi_freq_get(PI_FREQ_DOMAIN_CL), pi_freq_get(PI_FREQ_DOMAIN_PERIPH));
#ifdef VOLTAGE
pi_pmu_voltage_set(PI_PMU_VOLTAGE_DOMAIN_CHIP, VOLTAGE);
pi_pmu_voltage_set(PI_PMU_VOLTAGE_DOMAIN_CHIP, VOLTAGE);
printf("Voltage: %dmV\n", VOLTAGE);
#endif
#endif
// IMPORTANT - MUST BE CALLED AFTER THE CLUSTER IS SWITCHED ON!!!!
printf("Constructor\n");
int ConstructorErr = cifar10_modelCNN_Construct();
if (ConstructorErr)
{
printf("Graph constructor exited with error: %d\n(check the generated file cifar10_modelKernels.c to see which memory have failed to be allocated)\n", ConstructorErr);
return -6;
}
printf("Call cluster\n");
#ifndef __EMUL__
struct pi_cluster_task task;
pi_cluster_task(&task, cluster, NULL);
#ifdef __GAP8__
task.stack_size = STACK_SIZE;
task.slave_stack_size = SLAVE_STACK_SIZE;
#else
pi_cluster_task_stacks(&task, pi_cl_l1_scratch_alloc(&cluster_dev, &task, SLAVE_STACK_SIZE * pi_cl_cluster_nb_pe_cores()), SLAVE_STACK_SIZE);
#endif
pi_cluster_send_task_to_cl(&cluster_dev, &task);
#else
cluster();
#endif
cifar10_modelCNN_Destruct();
#ifdef PERF
{
unsigned int TotalCycles = 0, TotalOper = 0;
printf("\n");
for (unsigned int i=0; i<(sizeof(AT_GraphPerf)/sizeof(unsigned int)); i++) {
TotalCycles += AT_GraphPerf[i]; TotalOper += AT_GraphOperInfosNames[i];
}
for (unsigned int i=0; i<(sizeof(AT_GraphPerf)/sizeof(unsigned int)); i++) {
printf("%45s: Cycles: %12u, Cyc%%: %5.1f%%, Operations: %12u, Op%%: %5.1f%%, Operations/Cycle: %f\n", AT_GraphNodeNames[i], AT_GraphPerf[i], 100*((float) (AT_GraphPerf[i]) / TotalCycles), AT_GraphOperInfosNames[i], 100*((float) (AT_GraphOperInfosNames[i]) / TotalOper), ((float) AT_GraphOperInfosNames[i])/ AT_GraphPerf[i]);
}
printf("\n");
printf("%45s: Cycles: %12u, Cyc%%: 100.0%%, Operations: %12u, Op%%: 100.0%%, Operations/Cycle: %f\n", "Total", TotalCycles, TotalOper, ((float) TotalOper)/ TotalCycles);
printf("\n");
}
#endif
#ifdef CI
if (PredClass == CI) {printf("Correct Results\n"); return 0;}
else {printf("Wrong Results\n"); return -1;}
#endif
return 0;
}
int main(int argc, char *argv[])
{
printf("\n\n\t *** NNTOOL cifar10_model Example ***\n\n");
return test_cifar10_model();
}