Skip to content
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

prefetch: use a separate temporary cache for prefetching #730

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

skshetry
Copy link
Member

@skshetry skshetry commented Dec 23, 2024

This PR will use a separate temporary cache for prefetching that resides in .datachain/tmp/prefetch-<random> directory when prefetch= is set but cache is not.
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.

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 instance 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 prefetch cache.

Eg:

dataset = dc.to_pytorch(...)
with closing(dataset):
    pass

Copy link

cloudflare-workers-and-pages bot commented Dec 23, 2024

Deploying datachain-documentation with  Cloudflare Pages  Cloudflare Pages

Latest commit: a516de8
Status: ✅  Deploy successful!
Preview URL: https://7d2ddfe7.datachain-documentation.pages.dev
Branch Preview URL: https://prefetch-cache.datachain-documentation.pages.dev

View logs

Copy link

Deploying datachain-documentation with  Cloudflare Pages  Cloudflare Pages

Latest commit: afae789
Status: ✅  Deploy successful!
Preview URL: https://d7bd07c5.datachain-documentation.pages.dev
Branch Preview URL: https://prefetch-cache.datachain-documentation.pages.dev

View logs

Copy link

codecov bot commented Dec 24, 2024

Codecov Report

Attention: Patch coverage is 92.64706% with 10 lines in your changes missing coverage. Please review.

Project coverage is 87.39%. Comparing base (8dfa4ff) to head (a516de8).

Files with missing lines Patch % Lines
src/datachain/lib/file.py 60.00% 2 Missing and 2 partials ⚠️
src/datachain/progress.py 78.57% 3 Missing ⚠️
src/datachain/lib/pytorch.py 93.75% 1 Missing and 1 partial ⚠️
src/datachain/cache.py 95.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #730      +/-   ##
==========================================
+ Coverage   87.33%   87.39%   +0.06%     
==========================================
  Files         116      116              
  Lines       11147    11217      +70     
  Branches     1532     1536       +4     
==========================================
+ Hits         9735     9803      +68     
  Misses       1032     1032              
- Partials      380      382       +2     
Flag Coverage Δ
datachain 87.33% <92.64%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -179,6 +180,7 @@ def iterate(self, timeout=None) -> Generator[ResultT, None, None]:
self.shutdown_producer()
if not async_run.done():
async_run.cancel()
wait([async_run])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.cancel() does not immediately cancel the task the underlying asyncio task.

We could add a .result() to wait for the future, but that does not seem to work either for the cancelled future from run_coroutine_threadsafe(). See python/cpython#105836.

So, I have added wait(...) as it seems to wait the cancelled future, and wait for underlying asyncio task.

Alternatively, we could add an asyncio.Event and wait for it.

src/datachain/asyn.py Outdated Show resolved Hide resolved
@skshetry skshetry marked this pull request as ready for review December 31, 2024 16:42
@skshetry skshetry requested a review from a team December 31, 2024 16:42
Comment on lines 285 to 280
if client.protocol == HfClient.protocol:
self._set_stream(catalog, self._caching_enabled, download_cb=download_cb)
return False
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefetch is disabled for huggingface. See #746.

Comment on lines +128 to +136
if os.getenv("DATACHAIN_SHOW_PREFETCH_PROGRESS"):
download_cb = get_download_callback(
f"{total_rank}/{total_workers}", position=total_rank
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows a prefetch download progressbar for each worker which will be useful for debugging.

We cannot enable this by default, as this will mess up user's progressbar due to multiprocessing.

pass


class TqdmCombinedDownloadCallback(CombinedDownloadCallback, TqdmCallback):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have modified the callback to also show file counts on prefetching.
This will not show up on pytorch however.

Eg:

Download: 1.03MB [00:01, 605kB/s, 50 files]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant