-
Notifications
You must be signed in to change notification settings - Fork 982
Description
[STM32F411][TFLM] Model parsed incorrectly by GetModel(): tensor dims already invalid when accessed via interpreter
Environment
MCU: STM32F411 (Cortex-M4F)
FPU: FPv4-SP-D16
Toolchain: arm-none-eabi-gcc 13.2.1
Float ABI: hard
OS: Bare-metal (HAL)
Build system: Custom Makefile
Language: C++
TFLM commit: f5c36ef2efa9886d58b91106e28ab268cc725398
Model type: Float TFLite model
Problem Summary
I am loading a float TFLite model using TensorFlow Lite Micro on STM32F411.
Although tflite::GetModel() returns a non-null pointer and basic metadata
(schema version, subgraph count, input/output count) appears correct,
the model is already parsed incorrectly at the FlatBuffer level.
This becomes evident when the same model is later accessed through
MicroInterpreter, where tensor dimension metadata (dims->size)
is clearly invalid.
This indicates a model parsing or FlatBuffer interpretation issue, not a
tensor allocation or inference problem.
Evidence: Model Metadata Immediately After GetModel()
The following output is printed directly after calling GetModel():
Model schema version: 3
Model description: MLIR Converted.
Subgraph count: 1
Subgraph name: main
Inputs count: 1
Outputs count: 1
Input 0: name=serving_default_keras_tensor:0 type=0 dims=2
At first glance, the model structure looks reasonable.
Evidence: Tensor Metadata Is Already Invalid When Accessed via Interpreter
Without running inference, and only constructing a MicroInterpreter
and allocating tensors, the tensor metadata becomes:
Input dims: 537001984
Output dims: 537001984
Input type: 0
Output type: 0
The dimension values are clearly invalid and cannot represent a real tensor
shape.
This strongly suggests that the underlying FlatBuffer model data is being
misinterpreted, likely due to alignment, ABI, or memory access issues.
Minimal Reproduction Code
TfLiteStatus LoadModelAndTest() {
const tflite::Model *model =
::tflite::GetModel(g_basic_float_model_data);
MicroPrintf("Model schema version: %d", model->version());
if (model->description()) {
MicroPrintf("Model description: %s",
model->description()->c_str());
}
MicroPrintf("Subgraph count: %d",
model->subgraphs()->size());
auto subgraph = model->subgraphs()->Get(0);
MicroPrintf("Subgraph name: %s",
subgraph->name()->c_str());
MicroPrintf("Inputs count: %d",
subgraph->inputs()->size());
MicroPrintf("Outputs count: %d",
subgraph->outputs()->size());
for (int i = 0; i < subgraph->inputs()->size(); ++i) {
int idx = subgraph->inputs()->Get(i);
auto tensor = subgraph->tensors()->Get(idx);
MicroPrintf("Input %d: name=%s type=%d dims=%d",
i,
tensor->name()->c_str(),
tensor->type(),
tensor->shape()->size());
}
OpResolver op_resolver;
TF_LITE_ENSURE_STATUS(RegisterOps(op_resolver));
constexpr int kTensorArenaSize = 3000;
static uint8_t tensor_arena[kTensorArenaSize];
tflite::MicroInterpreter interpreter(
model,
op_resolver,
tensor_arena,
kTensorArenaSize);
TF_LITE_ENSURE_STATUS(interpreter.AllocateTensors());
TfLiteTensor *input = interpreter.input(0);
TfLiteTensor *output = interpreter.output(0);
MicroPrintf("Input dims: %d", input->dims->size);
MicroPrintf("Output dims: %d", output->dims->size);
MicroPrintf("Input type: %d", input->type);
MicroPrintf("Output type: %d", output->type);
return kTfLiteOk;
}Expected Behavior
-
Tensor dimension metadata should be consistent across:
- FlatBuffer model (
tensor->shape()->size()) - MicroInterpreter tensors (
input->dims->size)
- FlatBuffer model (
-
Dimension values should remain small and valid (e.g. 2–4)
Actual Behavior
- Tensor dimensions reported by
MicroInterpreterare extremely large - Indicates corrupted or misinterpreted model metadata
- Behavior is deterministic and reproducible
Questions for Maintainers
- Does
tflite::GetModel()on Cortex-M targets require stricter alignment
guarantees for the model buffer? - Are there known issues with FlatBuffer parsing on Cortex-M4F with hard-float ABI?
- Is it expected that a model can appear valid at a high level, but still be
internally misparsed due to memory layout assumptions?
Any insight would be greatly appreciated.