-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
157 lines (128 loc) · 3.43 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
/*
* Copyright (C) 2023 Zhaolan Huang <[email protected]>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v3. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup apps
* @{
*
* @file
* @brief U-TOE Application
*
* @author Zhaolan Huang <[email protected]>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include "xtimer.h"
#include "random.h"
#include <tvm/runtime/crt/microtvm_rpc_server.h>
#include <tvm/runtime/crt/logging.h>
#include "stdio_base.h"
#include "mlmci.h"
#ifndef UTOE_ONLY
#include "net/nanocoap_sock.h"
#endif
#ifndef UTOE_RANDOM_SEED
#define UTOE_RANDOM_SEED 42
#endif
#ifndef UTOE_TRIAL_NUM
#define UTOE_TRIAL_NUM 10
#endif
/* UTOE_GRANULARITY 0 - Per Model, 1 - Per Operator*/
#ifndef UTOE_GRANULARITY
#define UTOE_GRANULARITY 0
#endif
#define UTOE_RPC_BUFFER_SIZE 128
#ifndef UTOE_INPUT_SIZE
#define UTOE_INPUT_SIZE 4
#endif
#ifndef UTOE_OUTPUT_SIZE
#define UTOE_OUTPUT_SIZE 4
#endif
// Called by TVM to write serial data to the UART.
ssize_t write_serial(void* unused_context, const uint8_t* data, size_t size) {
(void) unused_context;
stdio_write(data, size);
return size;
}
#if (UTOE_GRANULARITY==0)
#include <tvmgen_default.h>
extern mlmodel_t *model_ptr;
void per_model_eval(void)
{
// #include "model_io_vars.h"
(void) printf("U-TOE Per-Model Evaluation \n");
#ifdef UTOE_ONLY
(void) printf("Press any key to start >\n");
(void) getchar();
#endif
random_init(UTOE_RANDOM_SEED);
uint32_t start, end;
for(int i = 0; i < UTOE_TRIAL_NUM;i++) {
for(int j = mlmodel_get_num_input_vars(model_ptr); j > 0; j--) {
mlmodel_iovar_t *input = mlmodel_get_input_variable(model_ptr, j - 1);
random_bytes(input->values, input->num_bytes);
}
start = xtimer_now_usec();
int ret_val = mlmodel_inference(model_ptr);
end = xtimer_now_usec();
printf("trial: %d, usec: %ld, ret: %d \n", i, (long int)(end - start), ret_val);
}
(void) printf("Evaluation finished >\n");
}
#endif
#if (UTOE_GRANULARITY==1)
microtvm_rpc_server_t server;
void per_ops_eval(void)
{
uint8_t buffer[UTOE_RPC_BUFFER_SIZE];
random_init(UTOE_RANDOM_SEED);
server = MicroTVMRpcServerInit(write_serial, NULL);
TVMLogf("U-TOE Per-Operator Evaluation");
TVMLogf("microTVM RIOT runtime - running");
for(;;) {
size_t bytes_read = stdio_read(buffer, UTOE_RPC_BUFFER_SIZE);
uint8_t* arr_ptr = buffer;
while (bytes_read > 0) {
// Pass the received bytes to the RPC server.
tvm_crt_error_t err = MicroTVMRpcServerLoop(server, &arr_ptr, &bytes_read);
if (err != kTvmErrorNoError && err != kTvmErrorFramingShortPacket) {
TVMPlatformAbort(err);
}
}
}
}
#endif
#ifdef USE_SUIT
extern int suit_init(void);
#endif
#ifndef UTOE_ONLY
extern void coap_server_init();
#endif
int main(void)
{
/* comment-out - causing no icmp echo */
// xtimer_init();
#ifdef USE_SUIT
suit_init();
#endif
#if (UTOE_GRANULARITY==1)
per_ops_eval();
#elif (UTOE_GRANULARITY==0)
mlmodel_init(model_ptr);
mlmodel_set_global_model(model_ptr);
per_model_eval();
#endif
#ifndef UTOE_ONLY
printf("{\"IPv6 addresses\": [\"");
netifs_print_ipv6("\", \"");
puts("\"]}");
coap_server_init();
#endif
return 0;
}