Manoj Ravi
07/31/2024, 12:12 PMbentoml.Service(). Due to the latency requirements and the size of our data, we've opted to load some datasets as global variables. However, one of these files is approximately 900MB. When the service is deployed, it spins up multiple workers, and each worker loads this file into memory independently. This has led to a significant increase in the container size, which is impacting our infrastructure costs.
We are looking for advice on how to address this issue more efficiently. Specifically, we would like to explore solutions for sharing the file between multiple workers or any other optimizations that could reduce memory consumption and container size.
We would greatly appreciate any insights or suggestions from the community on how to tackle this problem effectively.
Looking forward to learning from your experiences.
Best regards,
Manoj RaviJian Shen Yap
07/31/2024, 4:37 PMmmap before? Theoretically the OS is smart enough to know that multiple process is reading the same file so it only load once, but we have not tested this before. Let me know if you would like to try!Manoj Ravi
07/31/2024, 5:01 PMJian Shen Yap
08/01/2024, 4:07 AMf.open you use mmap which doesn't load the file into the actual memory but virtual memory which take less space.
with open("large_file.bin", "r+b") as f:
mm = mmap.mmap(f.fileno(), 0)Jian Shen Yap
08/01/2024, 4:08 AMManoj Ravi
08/01/2024, 10:15 AMManoj Ravi
08/01/2024, 11:08 AMManoj Ravi
08/03/2024, 9:11 PMJian Shen Yap
08/07/2024, 4:43 AMnumpy has a tofile API to allow this. or you can use py.arrow
Secondly, you would need to make sure the operations that you used work nicely with memory mapped data. For this I would suggest you to use numpy's operation rather than pandas, although pandas 2.0 said they have mmap file support but I am not completely sure on the internals.
> file using memory map, and convert it to dataframe. This loading process is going to happen for all the workers right,
Using memory mapped file, you are loading the data into Virtual Memory, not physical memory. It consumes significantly less memory. You can easily try this out by yourself.
Lastly, if the operations requires you to load everything into the memory, i.e. barebone sorting, i'm afraid that you may not be able to get much improvement from mmap.
You may want to consider rearchitecture your solution, such as making use of batching, or a database, etc..Manoj Ravi
08/19/2024, 12:12 PM