MindSpore Lite调用macBert模型报错

1 系统环境

硬件环境(Ascend/GPU/CPU): Ascend/GPU/CPU
MindSpore版本: mindspore=2.2.10
执行模式(PyNative/ Graph):不限
Python版本: Python=3.8
操作系统平台: linux

2 报错信息

2.1问题描述

使用机型:华为META60

场景:导出MacBert模型,用鸿蒙的MindSpore Lite 调用模型执行推理,代码如下:

2.2脚本信息

auto inputs = OH_AI_ModelGetInputs(model);  
    
int n1 = 1;  
int n2 = 8;  
OH_AI_ShapeInfo *shape_infos = new OH_AI_ShapeInfo[3];  
shape_infos[0].shape_num = 2;  
shape_infos[0].shape[0] = n1;  
shape_infos[0].shape[1] = n2;  
shape_infos[1].shape_num = 2;  
shape_infos[1].shape[0] = n1;  
shape_infos[1].shape[1] = n2;  
shape_infos[2].shape_num = 2;  
shape_infos[2].shape[0] = n1;  
shape_infos[2].shape[1] = n2;  
auto modelResizeInputs = OH_AI_ModelResize(model, inputs, shape_infos, 3);  
auto ret = FillInputTensor(inputs, input_data);  
auto predict_ret = OH_AI_ModelPredict(model, inputs, &outputs, nullptr, nullptr);  
if (predict_ret != OH_AI_STATUS_SUCCESS) {  
    OH_AI_ModelDestroy(&model);  
    LOGE("MS_LITE_ERR: MSLite Predict error.\n");  
    return OH_AI_STATUS_LITE_ERROR;  
}

2.3日志信息

E     signal_chain_handler call 2 rd sigchain action for signal: 11 sca_sigaction=5b31a4d874 noreturn=0 FREEZE_signo_11 thread_list_lock_status:-1 tl_lock_count=0 tl_lock_waiters=0 tl_lock_tid_fail=-1 tl_lock_count_tid=-1   
E     [mindspore/lite/src/litert/kernel/cpu/fp16/cast_fp16.cc:102] DoCast# Unsupported output data type 30   
E     [mindspore/lite/src/litert/kernel/cpu/fp16/cast_fp16.cc:102] DoCast# Unsupported output data type 30   
E     [mindspore/lite/src/litert/lite_kernel.cc:133] Execute# run kernel failed, name: /bert/Cast_1   
E     [mindspore/lite/src/executor/sub_graph_kernel.cc:473] Execute# run kernel failed, name: /bert/Cast_1   
E     [mindspore/lite/src/litert/lite_mindrt.cc:48] RunOpData# run kernel failed, name: CpuFP16SubGraph3_7   
E     signal_chain_handler call usr sigaction for signal: 11 sig_action.sa_sigaction=00000000B5217330   
E     [mindspore/lite/src/litert/mindrt_executor.cc:334] Run# MindrtRun failed   
E     [mindspore/lite/src/litert/lite_session.cc:887] RunGraph# RunGraph failed : -1   
E     [mindspore/lite/src/litert/cxx_api/model/model_impl.cc:525] Predict# Run graph failed.   
E     signal_chain_handler call 2 rd sigchain action for signal: 11 sca_sigaction=5b31a4d874 noreturn=0 FREEZE_signo_11 thread_list_lock_status:-1 tl_lock_count=0 tl_lock_waiters=0 tl_lock_tid_fail=-1 tl_lock_count_tid=-1 

3 根因分析

从报错来提示分析是cast算子不支持从fp16直接转bool类型。

4 解决方案

看代码只支持转int64,int32,loat32, float16。修改网络先转换成float32 然后再转bool类型https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/src/litert/kernel/cpu/fp16/cast_fp16.cc

switch (output_data_type) {  
    case kNumberTypeInt64:  
    Float16ToInt64(reinterpret_cast<float16_t *>(input_data) + offset,  
                    reinterpret_cast<int64_t *>(output_data) + offset, data_num);  
    break;  
    case kNumberTypeInt32:  
    Float16ToInt32(reinterpret_cast<float16_t *>(input_data) + offset,  
                    reinterpret_cast<int32_t *>(output_data) + offset, data_num);  
    break;  
    case kNumberTypeFloat32:  
    Float16ToFloat32(reinterpret_cast<float16_t *>(input_data) + offset,  
                        reinterpret_cast<float *>(output_data) + offset, data_num);  
    break;  
    case kNumberTypeFloat16:  
    memcpy(reinterpret_cast<float16_t *>(output_data) + offset, reinterpret_cast<float16_t *>(input_data) + offset,  
            data_num * sizeof(float16_t));  
    break;  
    default:  
    MS_LOG(ERROR) << "Unsupported output data type " << output_data_type;  
    return RET_ERROR;  
}