-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prefetch: use a separate temporary cache for prefetching
Unless `cache=True`, `prefetch` will use a separate temporary cache that resides in `.datachain/tmp/prefetch-<random>` directory. The temporary directory will be automatically deleted after the prefetching is done. For `cache=True`, the cache will be reused and won't be deleted at the end. Please note that auto-cleanup does not work for PyTorch datasets because there is no way to invoke cleanup from the Dataset side. The DataLoader may still have cached data or rows even after the Dataset has finished iterating. As a result, values associated with a catalog/cache instance can outlive the Dataset instance. One potential solution is to implement a custom dataloader or provide a user-facing API. In this PR, I have implemented the latter. The PytorchDataset now includes a close method, which can be used to clean up the temporary prefix cache. Eg: ```python with closing(dataset): pass ```
- Loading branch information
Showing
7 changed files
with
192 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from contextlib import contextmanager, nullcontext | ||
from functools import partial | ||
from typing import TYPE_CHECKING, Optional | ||
|
||
from fsspec.callbacks import DEFAULT_CALLBACK, Callback | ||
|
||
from datachain.asyn import AsyncMapper | ||
from datachain.cache import temporary_cache | ||
from datachain.catalog.catalog import Catalog | ||
from datachain.lib.file import File | ||
|
||
if TYPE_CHECKING: | ||
from contextlib import AbstractContextManager | ||
|
||
from datachain.cache import DataChainCache as Cache | ||
|
||
|
||
def noop(*args, **kwargs): | ||
pass | ||
|
||
|
||
async def _prefetch_input(row, catalog, download_cb): | ||
try: | ||
callback = download_cb.increment_file_count | ||
except AttributeError: | ||
callback = noop | ||
|
||
for obj in row: | ||
if isinstance(obj, File): | ||
obj._set_stream(catalog, True, download_cb) | ||
await obj._prefetch() | ||
callback() | ||
return row | ||
|
||
|
||
@contextmanager | ||
def catalog_with_cache(catalog: Catalog, cache): | ||
ocache = catalog.cache | ||
try: | ||
catalog.cache = cache | ||
yield catalog | ||
finally: | ||
catalog.cache = ocache | ||
|
||
|
||
def rows_prefetcher( | ||
catalog, | ||
rows, | ||
prefetch: int, | ||
cache: Optional["Cache"] = None, | ||
download_cb: Callback = DEFAULT_CALLBACK, | ||
): | ||
cache_ctx: AbstractContextManager[Cache] | ||
if cache: | ||
cache_ctx = nullcontext(cache) | ||
else: | ||
tmp_dir = catalog.cache.tmp_dir | ||
assert tmp_dir | ||
cache_ctx = temporary_cache(tmp_dir, prefix="prefetch-") | ||
|
||
with cache_ctx as prefetch_cache, catalog_with_cache(catalog, prefetch_cache): | ||
func = partial(_prefetch_input, download_cb=download_cb, catalog=catalog) | ||
mapper = AsyncMapper(func, rows, workers=prefetch) | ||
yield from mapper.iterate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters