-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dataloading Revamp #3216
base: main
Are you sure you want to change the base?
Dataloading Revamp #3216
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice progress! sorry its not fast but i think i know why:
i think the main reason this is slower than expected is because _get_collated_batch()
gets called per raybundle and sadly _get_collated_batch()
is AFAIK needlessly slow.
- take note about how the current
CachedDataloader
avoids doing_get_collated_batch()
per raybundle. it would have been nice for the author to have left some notes about how slow_get_collated_batch()
is, but evidently that author found it's necessary to not collate images per raybundle . - in my impl, I just
_get_collated_batch()
once on a small set of images an keep that batch cached. the main problem I saw is that_get_collated_batch()
on thousands of images seemed to use 2x or 3x as much RAM as actually needed and thus cause many minutes of swapping and stuff
Even if you only call _get_collated_batch()
once tho, you might need a bigger prefetch factor and/or more workers depending on the model.
IMO it's worth trying to find a way to get the result of nerfstudio_collate
on cameras (I think the cameras do need to be collated because they can be ragged? i could be wrong and they don't need collation) but on images just have the worker read image files / buffers and never call collate on those tensors.
Just to be clear, this is the line where collate on images can go nuts and start taking forever to allocate 200GB or more of RAM for many images in code in main
:
storage = elem.storage()._new_shared(numel, device=elem.device) |
So! If a worker is just emitting raybundles then the images never need to be in shared tensor memory then eh? Thus should be able to save some RAM and CPU by skipping that line for images. Still need to think about the cost of reading the images themselves, but collate is definitely a troublemaker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just took a quick look (can't do a full review right now), so cool to see this coming along!!
Sounds like this change will target the case that uncompressed image tensors can't fit in RAM, but the raw image files (typically jpeg) do fit in RAM. In that case I guess we do want each worker to literally load the file bytes into Python RAM (as implemented) versus let the OS disk cache work, because the idea is that the uncompressed image tensors will otherwise blow out the disk cache.
I think it would be important to test in the end like a case where the user only has limited RAM (say 16GB) and e.g. a 8GB laptop graphics card, in that case I think there are moderate or larger image datasets where the whole thing would OOM when using the current cache impl. In that case, it would be helpful to have some way to disable the cache, or just communicate to the user that they simply have too weak of a machine for the dataset (e.g. just a CONSOLE.print("[bold yellow]Warning ...")
in the line where the workers start reading image files into RAM.
nice work keep going! |
…t sure why but single worker will have to do
…not a subclass of nn.Module
…ding all into one datamanager
…now uses FullImageDatamanager (which has been changed)
…mage_datamanager now has full parallelized support
Did anything ever come of this? I'm running into OOM errors as are many others due to large image sets. Would love to see this implemented. |
Problems and Background
parallel_datamanager.py
will try to cache the entire dataset into RAM, which will lead to an out-of-memory (OOM) errorparallel_datamanager.py
only uses one worker to generate ray bundles. Since various subprocesses such as unprojecting during ray generation, or pixel sampling within a custom mask can be a CPU-intensive task, it may be better suited to parallelize this. Whileparallel_datamanager.py
does support multiple workers, each worker caches the entire dataset to RAM and it does not support massive datasets, leading to duplicate copies of the dataset in computer memory. It also implements parallelism from scratch and is not friendly to build off.VanillaDataManager
andParallelDataManager
rely on CacheDataloader, which subclassestorch.utils.data.DataLoader
, which is a strange coding practice, and actually serves no particular use in the current nerfstudio implementation.full_images_datamanager.py
: As we can not fit the entire dataset in RAM, the current implementation loads in entire dataset into theFullImageDataloader
'scached_train
attribute. To do this efficiently, we need multiprocess parallelization to load in images, undistort them, and do this quickly to keep up with GPU's forward and backward passes of the model.Overview of Changes
CacheDataloader
withRayBatchStream
, which subclassestorch.utils.data.IterableDataset
. The goal of this class is to generate ray bundles directly without caching all images to RAM. This is done by collating a sampled batch of images to sample from. A newParallelDatamanager
class is written which is available side-by-side but can completely replace the originalVanillaDatamanager
ImageBatchStream
to create a parallel, OOM-resistant version ofFullImageDataManager
. This can be configured to load from the disk by settingcache_images
config variable to disk.pil_to_numpy()
function is added. This function reads a PIL.Image's data buffer and fills an empty numpy array while reading, hastening the conversion process and removing an extra memory allocation. It is the fastest way to get from a PIL Image to a Pytorch tensor averaging ~2.5ms for a 1080x1920 image (~40% faster)cache_compressed_imgs
now caches your images to RAM in their compressed form (for example, caching) and relies on parallelized CPU dataloading to efficiently decode them into pytorch tensors to be used in training.Impact