项目场景:
环境:python3.8.10,torch2.1.1,open3d0.13.0,numpy1.24.3,cuda11.3
最小复现代码如下:
import torch
import torchvision
from memory_profiler import profile
from flask import Flask, request, jsonify
import open3d
app = Flask(__name__)
@app.route("/test", methods=["GET"])
@profile
def test():
out_info = {"code": -1, "data": None, "msg": None}
model = torchvision.models.resnet18()
model.to("cuda:0")
input = torch.randn(1, 3, 256, 256)
input = input.to("cuda:0")
out = model(input)
out_info["code"] = 0
out_info["msg"] = "success"
return out_info
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
问题描述:
1.导入open3d库(即使不使用)且利用cuda推理时,内存开销非常大。
开销如下:
解决方案:
1.更改torch和o3d的版本,参考链接出现的问题是crash,本项目场景只是内存占用过高)
参考:https://github.com/isl-org/Open3D/issues/5597
2.从源码编译torch和o3d,根据参考链接(soumith)的说法是两者之间某个参数冲突导致的。
参考:https://github.com/pytorch/pytorch/issues/19739
3.o3d没有及时释放GPU RAM。
参考:https://github.com/isl-org/Open3D/issues/4094
4.torch张量泄露。
参考:https://zhuanlan.zhihu.com/p/517279248
问题描述2:
1.基于flask和分割模型写了一个接口,因为后处理耗时较长,所以在后处理阶段引入多线程threading,每次调用后出现内存泄漏(200m-1200m)
解决方案:
1.多线程改为单线程,5700x处理器耗时增加约5秒(30%)。
2.启动方法使用gunicorn。
问题分析:
1.对象已释放、没有使用全局变量,但有使用共享变量。
问题描述3:
在问题描述2的基础上,发现net.to(“cuda:0”)竟消耗5400m内存,而模型大小也不过50m.
net = model()
model_path = os.path.join(str(Path(__file__).parent), 'model.pth')
net.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
net.to("cuda:0")
解决方案:
1.内存出问题的环境torch1.10.0+cu113 torchvision0.16.1
2.将torch更新至2.1.1,解决内存占用过高的问题。
问题分析:
https://github.com/pytorch/pytorch/issues/72791
https://github.com/pytorch/pytorch/issues/83340