Description
🐛 Bug
Currently the caching mechanism of AnchorGenerator is completely disabled because the Cache dictionary is wiped every time at the end of forward:
vision/torchvision/models/detection/anchor_utils.py
Lines 156 to 157 in e617474
The clear statement was introduced at #1657 to solve a memory leak reported at #1689.
Before providing a solution, I think it's worth to:
- Measure the speed benefit of caching. This should be done by measuring the speed of AnchorGenerator both individually and while being used by a detection model to assess its true impact.
- Validate that the memory leak is still present by measuring precisely the size of the cache in AnchorGenerator. The best way to do this would be to pass through it a sample of a real-world dataset like coco.
After having the above we could decide how to solve the bug. Here are a few proposed options:
- Remove the clear statement from forward, if the benchmarks show that the AnchorGenerator has no significant memory issues.
- Remove the caching completely, if the benchmarks show that is does not offer a material speed improvement (for example improving by over 5% the speed of end-to-end image prediction).
- Replace the cache with an LRU cache, if the benchmarks show that the memory leak exists and the caching is beneficial. Note that we need to ensure that the LRU cache is compatible with JIT.