[Inference] Fix mem release problem #32654
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR types
Others
PR changes
Others
Describe
目前Predictor释放的时候,发现有一瞬间会占用所有显卡的显存。
定位到该问题在于 Predictor内部的scope_在析构的时候会遍历所有的卡,依次调用memory::Release(place)接口,该接口需调用cuda底层函数,所以会申请cuda handle等,占用显存。
在Predictor Clone()接口调用后,Scope_的声明周期可能比Predictor要长,所以无法获取用户指定的显卡即device_id,该pr的修改会导致未定义的问题:https://github.com/PaddlePaddle/Paddle/pull/28409/files#diff-f6feda974e038d722114830a39bea985fd814e28bd86bcd33248aece3c3181a4R178
所以在此处,我们去除全部遍历显卡,依次释放的逻辑,恢复原有代码逻辑,这样会导致,权重所占据的显存最后会归还显存池,但不会压缩显存池的大小。
旧有问题现象:
测试代码:https://github.com/PaddlePaddle/Paddle-Inference-Demo/tree/master/c%2B%2B/test/shrink_memory
1、初始化Predictor后(config设置initGpu为500M),显存为780M(handle + 权重等)
2、batch_size为100运行一次,显存为4418M
3、调用ShrinkMemory接口后,显存占用为1292M(handle + 权重 + 其它?)
4、batch_size为2运行一次,显存占用为1728M
5、Predictor析构后,0卡显存占用为792M,其它卡占用280M。
更改代码逻辑后,
5、Predictor析构后,显存占用1292M,但不影响其它卡显存占用。