HarmonyOS鸿蒙Next中onnx模型可以转成om但是没法运行也没什么详细的报错信息

HarmonyOS鸿蒙Next中onnx模型可以转成om但是没法运行也没什么详细的报错信息

OH_NN_ReturnCode LoadModelFromBuffer(uint8_t *modelData, size_t modelSize, const std::string &path) {
    // check model compility
    HiAI_Compatibility compibility = HMS_HiAICompatibility_CheckFromBuffer(modelData, modelSize);
    OH_LOG_INFO(LOG_APP, "model compibility is %{public}d", compibility);

    // Create a compilation based on the model buffer.
    OH_NNCompilation *compilation = OH_NNCompilation_ConstructWithOfflineModelBuffer(modelData, modelSize);
    if (compilation == nullptr) {
        OH_LOG_ERROR(LOG_APP, "OH_NNCompilation_ConstructWithOfflineModelBuffer failed");
        return OH_NN_FAILED;
    }

    OH_LOG_INFO(LOG_APP, "base_path %{public}s", path.c_str());

//    HiAI_OmType omType = HIAI_OM_TYPE_PROFILING;
//    OH_LOG_INFO(LOG_APP, "HMS_HiAIOptions_SetOmOptions begin");
    //    OH_NN_ReturnCode ret = HMS_HiAIOptions_SetOmOptions(compilation, omType, path.c_str());     
//    if (ret != OH_NN_SUCCESS) {
//        OH_LOG_ERROR(LOG_APP, "HMS_HiAIOptions_SetOmType failed");
//    }

    size_t deviceID = GetDeviceID();
    if (deviceID == 0) {
        OH_LOG_ERROR(LOG_APP, "GetDeviceID failed");
        OH_NNCompilation_Destroy(&compilation);
        return OH_NN_FAILED;
    }
    OH_LOG_INFO(LOG_APP, "device id %{public}zu", deviceID);

    OH_NN_ReturnCode ret = OH_NNCompilation_SetDevice(compilation, deviceID);
    if (ret != OH_NN_SUCCESS) {
        OH_LOG_ERROR(LOG_APP, "OH_NNCompilation_SetDevice failed");
        OH_NNCompilation_Destroy(&compilation);
        return OH_NN_FAILED;
    }


    // set bandmode
    ret = HMS_HiAIOptions_SetBandMode(compilation, HiAI_BandMode::HIAI_BANDMODE_NORMAL);
    if (ret != OH_NN_SUCCESS) {
        OH_LOG_ERROR(LOG_APP, "HMS_HiAIOptions_SetBandMode failed");
        return ret;
    }
    HiAI_BandMode bandMode = HMS_HiAIOptions_GetBandMode(compilation);

    // set model execute device
    std::vector<HiAI_ExecuteDevice> executeDevices{HiAI_ExecuteDevice::HIAI_EXECUTE_DEVICE_NPU};
    ret = HMS_HiAIOptions_SetModelDeviceOrder(compilation, executeDevices.data(), executeDevices.size());
    if (ret != OH_NN_SUCCESS) {
        OH_LOG_ERROR(LOG_APP, "HMS_HiAIOptions_SetModelDeviceOrder failed");
        return ret;
    }
    
    // Compiling a Model
    ret = OH_NNCompilation_Build(compilation);
    if (ret != OH_NN_SUCCESS) {
        OH_LOG_ERROR(LOG_APP, "OH_NNCompilation_Build failed %{public}d", ret);
        OH_NNCompilation_Destroy(&compilation);
        return OH_NN_FAILED;
    }
    
        // Create an executor and load a model.
    OH_NNExecutor *executor_ = OH_NNExecutor_Construct(compilation);
    if (executor_ == nullptr) {
        OH_LOG_ERROR(LOG_APP, "OH_NNExecutor_Construct failed");
        OH_NNCompilation_Destroy(&compilation);
        return OH_NN_FAILED;
    }
    OH_NNCompilation_Destroy(&compilation);

    OH_LOG_INFO(LOG_APP, "LoadModelFromBuffer success");
    return OH_NN_SUCCESS;
}
OH_NNCompilation_Build 报错,返回
OH_NN_FAILED

更多关于HarmonyOS鸿蒙Next中onnx模型可以转成om但是没法运行也没什么详细的报错信息的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

ONNX模型转OM成功但运行失败,OH_NNCompilation_Build 返回 OH_NN_FAILED。这通常是模型兼容性或配置问题。

  • 模型算子不支持 - NPU可能不支持某些ONNX算子
  • 输入输出格式不匹配 - 模型输入输出与预期不符
  • 内存不足 - 模型太大或设备资源不足
  • 权限问题 - NPU访问权限未配置

检查模型兼容性:确认 HMS_HiAICompatibility_CheckFromBuffer 返回的具体兼容性级别

添加详细错误信息:使用 HMS_HiAIOptions_GetLastErrorCode 获取具体错误码

// 在Build失败后添加错误查询

int errorCode = HMS_HiAIOptions_GetLastErrorCode(compilation);

OH_LOG_ERROR(LOG_APP, "Build failed with error code: %{public}d", errorCode);

验证模型输入输出:确保模型输入输出tensor的shape和数据类型正确

尝试简化配置:先去掉一些高级选项测试

// 暂时注释掉这些设置,先测试基本功能

// ret = HMS_HiAIOptions_SetBandMode(...);

// ret = HMS_HiAIOptions_SetModelDeviceOrder(...);

更多关于HarmonyOS鸿蒙Next中onnx模型可以转成om但是没法运行也没什么详细的报错信息的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,ONNX模型转换为OM模型后无法运行且无详细报错,可能涉及模型算子不支持、转换参数配置错误或运行时环境不兼容。建议检查ONNX模型结构是否包含鸿蒙不支持的算子层,验证转换工具版本与鸿蒙SDK的匹配性,并确认OM模型是否针对目标芯片架构正确生成。可尝试简化模型结构或使用鸿蒙提供的模型验证工具进行排查。

从代码来看,OH_NNCompilation_Build 返回 OH_NN_FAILED 但缺乏详细错误信息,这通常与模型兼容性或配置问题有关。建议按以下步骤排查:

  1. 检查模型兼容性:确保 HMS_HiAICompatibility_CheckFromBuffer 返回 HIAI_COMPATIBLE。若返回不兼容,需重新转换模型或调整结构。

  2. 验证模型数据:确认 modelDatamodelSize 来自有效的 OM 模型文件,避免文件损坏或格式错误。

  3. 设备支持GetDeviceID 返回非零值仅表示设备存在,但需确保 NPU 驱动正常且支持模型算子。可尝试在 CPU 上执行(将 HIAI_EXECUTE_DEVICE_NPU 改为 HIAI_EXECUTE_DEVICE_CPU)以排除 NPU 兼容问题。

  4. 日志补充:在 OH_NNCompilation_Build 失败后,调用 OH_NNCompilation_GetErrorMsg 获取详细错误描述,补充到日志中。

  5. 资源释放:当前代码在 HMS_HiAIOptions_SetBandMode 失败时未销毁 compilation,需补充 OH_NNCompilation_Destroy 避免内存泄漏。

若上述步骤未解决问题,需结合具体模型结构和设备日志进一步分析。

回到顶部