PaddleDetection icon indicating copy to clipboard operation
PaddleDetection copied to clipboard

cpp部署代码在windows端进行连续预测产生内存泄漏

Open ximitiejiang opened this issue 5 years ago • 7 comments

现象: 采用deploy cpp代码修改后部署到windows端基于GPU进行连续图像检测,运行过程中内存占用率逐渐增加,比如初始8G,预测500张图像之后内存会达到12G左右。

软件版本:

  • paddle inference2.1.1+cuda10.2+cudnn7.6

参考了https://github.com/PaddlePaddle/PaddleDetection/issues/2417 尝试了如下两个修改

  1. 在预测完成后增加清除中间tensor的指令
predictor->Run();
...
predictor->ClearIntermediateTensor();

对内存泄漏没有效果

  1. 不采用原始predictor而是拷贝之后再使用
auto main_predictor = CreatePredictor(config);
predict = std::move(main_predictor->Clone());

对内存泄漏没有效果

还请指教,有什么可改进的地方?

ximitiejiang avatar Jul 27 '21 09:07 ximitiejiang

更新:如果设置为cpu模式,由于预测速度下来了,在连续测试时内存上涨的速度慢下来了,但持续测试200张图后内存还是从8.2G涨到10个G...也说明内存泄漏应该跟当前我用的cuda10.2+cudnn7.6无关

ximitiejiang avatar Jul 28 '21 03:07 ximitiejiang

更新:经过定位,基本确定是在predict()函数内,不跑这个函数,整个程序内存泄漏就没有了

void PPDetector::Predict(const cv::Mat& im,
	const double threshold,
	const int repeats,
	const bool run_benchmark,
	std::vector<ObjectResult>* result)
{
    memset(&inputs_, 0, sizeof(ImageBlob));
    result->clear();
    Preprocess(im);

    auto input_names = predictor_->GetInputNames();
    for (const auto& tensor_name : input_names) {
        auto in_tensor = predictor_->GetInputHandle(tensor_name);
        if (tensor_name == "image") {
			int rh = inputs_.in_net_shape_[0];
			int rw = inputs_.in_net_shape_[1];
			in_tensor->Reshape({ 1, 3, rh, rw });
			in_tensor->CopyFromCpu(inputs_.im_data_.data());
		}
		else if (tensor_name == "im_shape") {
			in_tensor->Reshape({ 1, 2 });
			in_tensor->CopyFromCpu(inputs_.im_shape_.data());
		}
		else if (tensor_name == "scale_factor") {
			in_tensor->Reshape({ 1, 2 });
			in_tensor->CopyFromCpu(inputs_.scale_factor_.data());
		}
	}
    for (int i = 0; i < repeats; i++)
	{
        try{
            predictor_->Run();
        }catch(...){
            PRINTF("[ERR] (in predict) paddle inference predictor run() one time fail.\n");
            break;
        };
        PRINTF("[LOG] (in predict) paddle inference predictor run() one time succeed.\n");
        auto output_names = predictor_->GetOutputNames();
        auto out_tensor = predictor_->GetOutputHandle(output_names[0]);
		std::vector<int> output_shape = out_tensor->shape();
	int output_size = 1;
	for (int j = 0; j < output_shape.size(); ++j) {
		output_size *= output_shape[j];
	}

	if (output_size < 6) {
            PRINTF("[WARNING] No object detected. output_size=%d", output_size);
            output_size = 6; // [suliang] 修改,为了避免run()失败时output_size=0影响后续,手工设置output_size=6
		}

	output_data_.resize(output_size);
	out_tensor->CopyToCpu(output_data_.data());
        }
	}
    if (!run_benchmark) {
		Postprocess(im, result);
	}
    predictor_->ClearIntermediateTensor();
}

尝试换了cuda10.1+cudnn7.6, cuda10.2+cudnn7.6, cuda11+cudnn8.0组合,都没有效果。

ximitiejiang avatar Jul 28 '21 07:07 ximitiejiang

@ximitiejiang 非常感谢反馈,我们正在定位修复中。cudnn8.0.4, 8.0.5明确有内存泄漏问题,暂时不要使用了。

yghstill avatar Jul 28 '21 09:07 yghstill

已解决,排除跟cuda/cudnn/paddle有关~ 谢谢!

ximitiejiang avatar Jul 30 '21 03:07 ximitiejiang

您可以说说怎么解决的么?我们也遇到了类似的问题

gsdust avatar Apr 29 '22 03:04 gsdust

已解决,排除跟cuda/cudnn/paddle有关~ 谢谢!

怎么解决的,可以分享下吗

HansenLYX0708 avatar Nov 02 '23 07:11 HansenLYX0708

已解决,排除跟cuda/cudnn/paddle有关~ 谢谢!

请问下怎么解决的吗?

bert-y avatar May 26 '25 02:05 bert-y