You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux/x64平台TfLiteMicro张量空指针引发段错误问题求助

TfLiteMicro在Linux/x64环境下张量访问段错误问题解决

问题现象

  • 调用AllocateTensors()后,输入/输出张量的data指针解引用触发段错误
  • 张量指针非空,但dims、data.f、data.data均为nullptr,张量类型显示为NOTYPE
  • 更换模型、调整张量池大小无效,Invoke()能正常执行但未触发预期的访问错误

编译环境与步骤

编译TfLiteMicro静态库

make -f tensorflow/lite/micro/tools/make/Makefile

构建可执行文件

g++ -o test.out test.cpp ../tflite-micro/gen/linux_x86_64_default/lib/libtensorflow-microlite.a -I../tflite-micro/ -I/home/gstukelj/projects/plume/tflite-micro/tensorflow/lite/micro/tools/make/downloads/flatbuffers/include -I../tflite-micro/tensorflow/lite/micro/tools/make/downloads/gemmlowp/

测试代码

#include <stdio.h>

#include "hw-float.h"
#include "tensorflow/lite/core/c/common.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"

int main(void) {

  tflite::InitializeTarget();

  const tflite::Model* model =
      ::tflite::GetModel(g_hello_world);

  TFLITE_CHECK_EQ(model->version(), TFLITE_SCHEMA_VERSION);

  static ::tflite::MicroMutableOpResolver<4> op_resolver;

  TF_LITE_ENSURE_STATUS(op_resolver.AddFullyConnected());
  TF_LITE_ENSURE_STATUS(op_resolver.AddSoftmax());
  TF_LITE_ENSURE_STATUS(op_resolver.AddReadVariable());
  TF_LITE_ENSURE_STATUS(op_resolver.AddRelu());

  constexpr int kTensorArenaSize = 60 * 1024;
  uint8_t tensor_arena[kTensorArenaSize];

  tflite::MicroInterpreter interpreter(model, op_resolver, tensor_arena,
                                       kTensorArenaSize);

  if (interpreter.AllocateTensors() != kTfLiteOk) {
    printf("ERROR: AllocateTensors() failed\r\n");
  }
  printf("Tensors allocated\n");

  // If AllocateTensors() is skipped this will segfault
  TfLiteTensor* input = interpreter.input(0);

  printf("bytes == %ld\n", input->bytes);

  if (input == nullptr) {
    printf("input == nullptr\n");
  }

  if (input->dims == nullptr) {
    printf("input->dims == nullptr\n");
  }

  if (input->data.f == nullptr) {
    printf("input->data.f == nullptr\n");
  }

  if (input->data.data == nullptr) {
    printf("input->data.data == nullptr \n");
  }

  printf("input tensor type = %s\n", TfLiteTypeGetName(input->type));

  TF_LITE_ENSURE_STATUS(interpreter.Invoke());

  // // Either one of these two will segfault
  // float y_pred = interpreter.output(0)->data.f[0];
  // auto y_pred = interpreter.typed_output_tensor<float>(0)[0];

  return 0;
}

程序运行输出

Tensors allocated
bytes == 4
input->dims == nullptr
input->data.f == nullptr
input->data.data == nullptr 
input tensor type = NOTYPE

问题根源与解决步骤

1. 模型数据有效性检查

从输出的NOTYPE类型来看,模型元数据未被正确解析。需确认hw-float.h中的g_hello_world是官方hello_world示例的正确模型数据,或替换为TfLiteMicro官方示例中的hello_world_model_data.h验证。若为自定义模型,需确保FlatBuffer格式合法,且通过tflite::GetModel()正确加载。

2. 修正OpResolver算子列表

官方hello_world模型仅依赖FullyConnected和Softmax算子,多余的算子添加可能导致解析冲突。修改OpResolver代码:

static ::tflite::MicroMutableOpResolver<2> op_resolver;
TF_LITE_ENSURE_STATUS(op_resolver.AddFullyConnected());
TF_LITE_ENSURE_STATUS(op_resolver.AddSoftmax());

3. 完善编译链接参数

添加系统依赖库避免符号缺失,修改编译命令:

g++ -o test.out test.cpp ../tflite-micro/gen/linux_x86_64_default/lib/libtensorflow-microlite.a -lpthread -lm -I../tflite-micro/ -I/home/gstukelj/projects/plume/tflite-micro/tensorflow/lite/micro/tools/make/downloads/flatbuffers/include -I../tflite-micro/tensorflow/lite/micro/tools/make/downloads/gemmlowp/

4. 强化错误检查

在AllocateTensors()后添加张量有效性校验,提前发现问题:

TfLiteTensor* input = interpreter.input(0);
TFLITE_CHECK(input != nullptr);
TFLITE_CHECK(input->type != kTfLiteNoType);
TFLITE_CHECK(input->data.data != nullptr);

5. 验证模型加载状态

添加代码检查输入输出张量数量,确认模型是否正确加载:

printf("Number of inputs: %zu\n", interpreter.inputs_size());
printf("Number of outputs: %zu\n", interpreter.outputs_size());

内容的提问来源于stack exchange,提问作者gstukelj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 16:45:14