Skip to content
This repository was archived by the owner on Jun 23, 2024. It is now read-only.

Commit 52800bf

Browse files
authored
Merge pull request #17 from iabdalkader/libtf_clean_up
Libtf: Clean up ops hash code and logging handler.
2 parents 8233af9 + 824dbee commit 52800bf

File tree

6 files changed

+7
-36
lines changed

6 files changed

+7
-36
lines changed

libtf.cc

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,14 @@
99
#include "tensorflow/lite/micro/cortex_m_generic/debug_log_callback.h"
1010
#include "tensorflow/lite/micro/examples/micro_speech/micro_features/micro_features_generator.h"
1111
#include "tensorflow/lite/micro/micro_interpreter.h"
12-
#include "tensorflow/lite/schema/schema_utils.h"
1312

1413
#include "libtf.h"
1514
#define LIBTF_MAX_OPS 80
1615

1716
extern "C" {
18-
// These are set by openmv py_tf.c code to redirect printing to an error message buffer...
19-
char *py_tf_putchar_buffer = NULL;
20-
size_t py_tf_putchar_buffer_index = 0;
21-
size_t py_tf_putchar_buffer_len = 0;
22-
23-
static void libtf_debug_log(const char *s) {
24-
for (size_t i = 0, j = strlen(s); i < j; i++) {
25-
if (py_tf_putchar_buffer_len) {
26-
py_tf_putchar_buffer[py_tf_putchar_buffer_index++] = s[i];
27-
py_tf_putchar_buffer_len--;
28-
} else {
29-
putchar(s[i]);
30-
}
31-
}
17+
// Default log handler.
18+
__attribute__((weak)) void libtf_log_handler(const char *s) {
19+
printf(s);
3220
}
3321

3422
static int libtf_align_tensor_arena(unsigned char **tensor_arena, size_t *tensor_arena_size) {
@@ -240,24 +228,11 @@ extern "C" {
240228
resolver.AddZerosLike();
241229
}
242230

243-
static uint32_t libtf_ops_hash(const tflite::Model *model) {
244-
uint32_t hash = 5381;
245-
const auto *subgraph = model->subgraphs()->Get(0);
246-
for (size_t i = 0; i < subgraph->operators()->size(); i++) {
247-
const auto *op = subgraph->operators()->Get(i);
248-
uint32_t opcode = GetBuiltinCode(model->operator_codes()->Get(op->opcode_index()));
249-
for (size_t x=0; x<4; x++) {
250-
hash = ((hash << 5) + hash) + ((uint8_t*) &opcode)[x];
251-
}
252-
}
253-
return hash;
254-
}
255-
256231
static int libtf_get_parameters(const unsigned char *model_data,
257232
unsigned char *tensor_arena, size_t tensor_arena_size,
258233
libtf_parameters_t *params,
259234
libtf_resolver_init_t libtf_resolver_init) {
260-
RegisterDebugLogCallback(libtf_debug_log);
235+
RegisterDebugLogCallback(libtf_log_handler);
261236

262237
tflite::MicroErrorReporter micro_error_reporter;
263238
tflite::ErrorReporter *error_reporter = &micro_error_reporter;
@@ -278,8 +253,6 @@ extern "C" {
278253
libtf_resolver_init(resolver);
279254

280255
tflite::MicroInterpreter interpreter(model, resolver, tensor_arena, tensor_arena_size, error_reporter);
281-
params->operators_size = interpreter.operators_size();
282-
params->operators_hash = libtf_ops_hash(model);
283256

284257
if (interpreter.AllocateTensors() != kTfLiteOk) {
285258
error_reporter->Report("AllocateTensors() failed!");
@@ -426,7 +399,7 @@ extern "C" {
426399
libtf_output_data_callback_t output_callback,
427400
void *output_callback_data,
428401
libtf_resolver_init_t libtf_resolver_init) {
429-
RegisterDebugLogCallback(libtf_debug_log);
402+
RegisterDebugLogCallback(libtf_log_handler);
430403

431404
tflite::MicroErrorReporter micro_error_reporter;
432405
tflite::ErrorReporter *error_reporter = &micro_error_reporter;
@@ -502,7 +475,7 @@ extern "C" {
502475
}
503476

504477
int libtf_initialize_micro_features() {
505-
RegisterDebugLogCallback(libtf_debug_log);
478+
RegisterDebugLogCallback(libtf_log_handler);
506479

507480
tflite::MicroErrorReporter micro_error_reporter;
508481
tflite::ErrorReporter *error_reporter = &micro_error_reporter;
@@ -516,7 +489,7 @@ extern "C" {
516489

517490
int libtf_generate_micro_features(const int16_t *input, int input_size,
518491
int output_size, int8_t *output, size_t *num_samples_read) {
519-
RegisterDebugLogCallback(libtf_debug_log);
492+
RegisterDebugLogCallback(libtf_log_handler);
520493

521494
tflite::MicroErrorReporter micro_error_reporter;
522495
tflite::ErrorReporter *error_reporter = &micro_error_reporter;

libtf.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ typedef struct libtf_parameters {
2828
libtf_datatype_t output_datatype;
2929
float output_scale;
3030
int output_zero_point;
31-
size_t operators_size;
32-
uint32_t operators_hash;
3331
} libtf_parameters_t;
3432

3533
// Call this first to get the model parameters.

libtf/cortex-m0plus/libtf.a

-638 Bytes
Binary file not shown.

libtf/cortex-m4/libtf.a

-634 Bytes
Binary file not shown.

libtf/cortex-m55/libtf.a

-638 Bytes
Binary file not shown.

libtf/cortex-m7/libtf.a

-638 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)