serve icon indicating copy to clipboard operation
serve copied to clipboard

Model's memoryUsage double its size after a inference request

Open codinnvrends opened this issue 6 years ago • 3 comments

Model's memoryUsage double its size after a request. model archive size: 106MB memoryUsage after load: 175-220 MB after an inference request (or a couple of) memoryUsage spike to ~400MB

in a pure python I can control the process size to be less than 200 MB always.

image image

Originally posted by @sermakarevich in https://github.com/pytorch/serve/issues/481#issuecomment-649718275

codinnvrends avatar Jul 06 '20 08:07 codinnvrends

Please comment in #481 - This requires fix to default handlers where with no_grade: context manager is not used.

codinnvrends avatar Jul 06 '20 08:07 codinnvrends

Updates - The increase in size [without no_grad] is model dependent per our observations .e.g in case of vgg11 or resnet-18 based cnn models memory increases by 20-30% however for densenet-161, it doubles! Tried running gc explicitly however no memory is released.

NOTE even after deactivating autograd engine, we saw a 10-15% increase in memory footprint based on model type as explained above. Example models provided with torchserve have been taken from torch hub.

Raised ticket with pytorch forum - #89035

codinnvrends avatar Jul 14 '20 19:07 codinnvrends

Hey @dhaniram-kshirsagar. I have made the fix with no-grad on my end and did additional profiling, this is what I got:

MB
- jit traced model size on disk 150
- torch-model-archive on disk  140
- inside python process (import torch; torch.jit.load) 163 + (50 torch lib), smem PSS
- single with torch.no_grad(): model(torch.rand((1, 3, 400, 400))) >> 451 MB, smem PSS
- inside torchserve process after some requests 465, smem PSS
 PID User     Command                         Swap      USS      PSS      RSS                                                 
>> python
16346 lilyai   python                             0     3536     3658     8844
>> import torch
16346 lilyai   python                             0    45440    47373    99584
>> model = torch.jit.load
>> model.eval()
16346 lilyai   python                             0   205188   207152   260088
>> with torch.no_grad(): model(torch.rand((1, 3, 400, 400)))
16346 lilyai   python                             0   433532   435863   495292

sermakarevich avatar Jul 17 '20 12:07 sermakarevich