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

libtf: Add model ops hash. #15

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading