alibaba / MNN

MNN is a blazing fast, lightweight deep learning framework, battle-tested by business-critical use cases in Alibaba
http://www.mnn.zone/
8.72k stars 1.67k forks source link

用表达式法实现多输入不生效 #2160

Closed bigsealing closed 1 year ago

bigsealing commented 1 year ago

平台(如果交叉编译请再附上交叉编译目标平台): linux

Github版本: Merge pull request https://github.com/alibaba/MNN/pull/2155 from alibaba/feature/sync

我现在的模型需要两个输入 分别是 “onnx_input”和“input_last_state” , 产生两个输出 onnx_output 和output_last_state,其中输出的output_last_state需要再作为输入传递给模型, onnx_output 是真正的数据输出, 我会将output_last_state 赋值给input_last_state, 我也试过用output_last_state ->readmap的指针赋值给input_last_state->writemap的指针的方法得到状态更新,但结果,这个状态输入没有效果,跟初始值为0的结果是一样的。 我用libtorch这个模型是可以正常工作的,请问MNN该如何操作,我需要怎么修改我的如下的代码来实现?

static VARP onnx_input; static VARP input_last_state; static VARP onnx_output; static VARP output_last_state;

static int size = 0;
void mnn_init(const char* file)
{
    auto exe = Executor::getGlobalExecutor();
    MNN::BackendConfig config;
    config.precision = MNN::BackendConfig::Precision_Low;
    MNNForwardType forwardType = MNN_FORWARD_AUTO;//MNN_FORWARD_AUTO;// MNN_FORWARD_CPU;
    exe->setGlobalExecutorConfig(forwardType, config, 1);

 model = Variable::loadMap("./libavfilter/mnn/model/gpu.mnn"); //gpu_dccrn_lstm_c.mnn  test.mnn
 auto inputOutput = Variable::getInputAndOutput(model);

 auto inputs = inputOutput.first;
 auto outputs = inputOutput.second;

 input_last_state = inputs["input_last_state"];
 onnx_input=inputs["onnx_input"];

 onnx_output=outputs["onnx_output"];
 output_last_state = outputs["output_last_state"];

 auto inputPtr1 = input_last_state->writeMap<float>();

    for (int i = 0; i < 256; ++i)
    {
        inputPtr1[i] = 0.;
    }
}

void mnn_predict(const float* input_data, float* output_data, int length)
{
    auto inputPtr = onnx_input->writeMap<float>();

    for (int i = 0; i < length; ++i)
    {
        inputPtr[i] = input_data[i];
    }

    if (length < size)
    {
        for (int i = length; i < size; ++i) {
            inputPtr[i] = 0.;
        }
    }       
    {
        auto outputPtr = onnx_output->readMap<float>();
        if (nullptr == outputPtr) {
            MNN_ERROR("Output Not valid read error\n");
            return;
        }

        for (int i = 0; i < length; ++i) {
            output_data[i] = outputPtr[i];
        }
        input_last_state=output_last_state;
        return;
    //outputPtr = output_last_state->readMap<float>();
    //auto inputPtr1 = input_last_state->writeMap<float>();
    //for (int i = 0; i < 256; ++i) {
    //      inputPtr1[i] = outputPtr[i];
    //  }
    }

}
bigsealing commented 1 year ago

暂时先关闭