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

Commit

Permalink
libtf: Add model ops hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
iabdalkader committed Feb 25, 2024
1 parent 3178052 commit 738f73e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions libtf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "tensorflow/lite/micro/cortex_m_generic/debug_log_callback.h"
#include "tensorflow/lite/micro/examples/micro_speech/micro_features/micro_features_generator.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_utils.h"

#include "libtf.h"
#define LIBTF_MAX_OPS 80
Expand Down Expand Up @@ -239,6 +240,19 @@ extern "C" {
resolver.AddZerosLike();
}

static uint32_t libtf_ops_hash(const tflite::Model *model) {
uint32_t hash = 5381;
const auto *subgraph = model->subgraphs()->Get(0);
for (size_t i = 0; i < subgraph->operators()->size(); i++) {
const auto *op = subgraph->operators()->Get(i);
uint32_t opcode = GetBuiltinCode(model->operator_codes()->Get(op->opcode_index()));
for (size_t x=0; x<4; x++) {
hash = ((hash << 5) + hash) + ((uint8_t*) &opcode)[x];
}
}
return hash;
}

static int libtf_get_parameters(const unsigned char *model_data,
unsigned char *tensor_arena, size_t tensor_arena_size,
libtf_parameters_t *params,
Expand All @@ -265,6 +279,7 @@ extern "C" {

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

if (interpreter.AllocateTensors() != kTfLiteOk) {
error_reporter->Report("AllocateTensors() failed!");
Expand Down
1 change: 1 addition & 0 deletions libtf.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef struct libtf_parameters {
float output_scale;
int output_zero_point;
size_t operators_size;
uint32_t operators_hash;
} libtf_parameters_t;

// Call this first to get the model parameters.
Expand Down

0 comments on commit 738f73e

Please sign in to comment.