-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
153 additions
and
14 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
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,40 @@ | ||
import os | ||
|
||
from datachain.cache import DataChainCache, get_temp_cache | ||
from datachain.lib.file import File | ||
from datachain.lib.prefetcher import rows_prefetcher | ||
|
||
|
||
def test_prefetcher(mocker, tmp_dir, catalog): | ||
rows = [] | ||
for path in "abcd": | ||
(tmp_dir / path).write_text(path) | ||
row = (File(path=str(tmp_dir / path)),) | ||
rows.append(row) | ||
|
||
for (file,) in rows_prefetcher(catalog, rows, prefetch=5): | ||
assert file._catalog | ||
head, tail = os.path.split(file._catalog.cache.cache_dir) | ||
assert head == catalog.cache.tmp_dir | ||
assert tail.startswith("prefetch-") | ||
assert file._catalog.cache.contains(file) | ||
|
||
cache = get_temp_cache(tmp_dir) | ||
for (file,) in rows_prefetcher(catalog, rows, prefetch=5, cache=cache): | ||
assert file._catalog | ||
assert file._catalog.cache == cache | ||
assert cache.contains(file) | ||
|
||
|
||
def test_prefetcher_closes_temp_cache(mocker, tmp_dir, catalog): | ||
rows = [] | ||
for path in "abcd": | ||
(tmp_dir / path).write_text(path) | ||
row = (File(path=str(tmp_dir / path)),) | ||
rows.append(row) | ||
spy = mocker.spy(DataChainCache, "destroy") | ||
|
||
rows_gen = rows_prefetcher(catalog, rows, prefetch=5) | ||
next(rows_gen) | ||
rows_gen.close() | ||
assert spy.called |
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,35 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from datachain.cache import DataChainCache | ||
from datachain.lib.pytorch import PytorchDataset | ||
from datachain.lib.settings import Settings | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"cache,prefetch", [(True, 0), (True, 10), (False, 10), (False, 0)] | ||
) | ||
def test_cache(catalog, cache, prefetch): | ||
settings = Settings(cache=cache, prefetch=prefetch) | ||
ds = PytorchDataset("fake", 1, catalog, dc_settings=settings) | ||
assert ds.cache == cache | ||
assert ds.prefetch == prefetch | ||
|
||
if cache or not prefetch: | ||
assert catalog.cache is ds._cache | ||
return | ||
|
||
assert catalog.cache is not ds._cache | ||
head, tail = os.path.split(ds._cache.cache_dir) | ||
assert head == catalog.cache.tmp_dir | ||
assert tail.startswith("prefetch-") | ||
|
||
|
||
@pytest.mark.parametrize("cache", [True, False]) | ||
def test_close(mocker, catalog, cache): | ||
ds = PytorchDataset("fake", 1, catalog, dc_settings=Settings(cache=cache)) | ||
spy = mocker.spy(DataChainCache, "destroy") | ||
|
||
ds.close() | ||
assert spy.called == (not cache) |