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.77k stars 1.67k forks source link

使用OPENCL推理结果出现段错误,CPU结果正常。 #3057

Open zhangshabao opened 1 month ago

zhangshabao commented 1 month ago
    初始化代码:
    _mnn_net->setCacheFile(".cache");
    _net_cfg.type = MNN_FORWARD_OPENCL;
    DEBUG_LOG(" setCacheFile end ");
    _mnn_session = _mnn_net->createSession(_net_cfg);
    _mnn_net->setSessionMode(MNN::Interpreter::Session_Release);
    _net_cfg.numThread = 4;

    for (int i = 0; i < 1; ++i) {
        auto outputTensor = _mnn_net->getSessionOutput(_mnn_session, NULL);
        MNN::Tensor* input    = _mnn_net->getSessionInput(_mnn_session, NULL);
        void* host = input->map(MNN::Tensor::MAP_TENSOR_WRITE,  input->getDimensionType());
        input->unmap(MNN::Tensor::MAP_TENSOR_WRITE,  input->getDimensionType(), host);
        _mnn_net->runSession(_mnn_session);
        host = outputTensor->map(MNN::Tensor::MAP_TENSOR_READ,  outputTensor->getDimensionType());
        outputTensor->unmap(MNN::Tensor::MAP_TENSOR_READ,  outputTensor->getDimensionType(), host);
    }
    推理代码:
     std::vector<float> inputTensorValues = substractMeanNormalize(inputImage, meanValues, normValues);

    auto input = _mnn_net->getSessionInput(_mnn_session, NULL);
    auto shape = input->shape();
    shape[0] = 1;
    shape[2] = inputImage.rows;
    shape[3] = inputImage.cols;

    _mnn_net->resizeTensor(input, shape);
    _mnn_net->resizeSession(_mnn_session);
    auto shapein = input->shape();
    std::vector<int> v = {1, (int)inputImage.channels(), inputImage.rows, inputImage.cols};
    auto nchwTensor = MNN::Tensor::create(v, halide_type_of<float>(), &inputTensorValues[0], MNN::Tensor::CAFFE);
    input->copyFromHostTensor(nchwTensor);
    delete nchwTensor;
    _mnn_net->runSession(_mnn_session);
    outputs = _mnn_net->getSessionOutputAll(_mnn_session);
    获取结果代码:
        out_dim.push_back(out_shape_c);
        out_dim.push_back(out_shape_h);
        out_dim.push_back(out_shape_w);
                float* data_ptr = tensor_ptr->host<float>();
            DEBUG_LOG(" *obj_angle_ptr :%f ",results[105600]);
    其中out_shape_c=67,out_shape_h=40,out_shape_w=40
jxt1234 commented 4 weeks ago

这个 tensor_ptr 是什么?如果使用 opencl ,不能直接使用 host ,需要用 map ,用完后 unmp