-
Notifications
You must be signed in to change notification settings - Fork 2
cache remote models locally #172
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
Open
nkundiushuti
wants to merge
2
commits into
main
Choose a base branch
from
marius/use-hfcache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+342
−10
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,130 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from avex.io.paths import PureGSPath | ||
| from avex.utils.utils import _get_local_path_for_cloud_file | ||
|
|
||
|
|
||
| class FakeFS: | ||
| """Minimal fsspec-like FS for cache validation tests.""" | ||
|
|
||
| def __init__(self, *, token: str) -> None: | ||
| self._token = token | ||
| self.get_calls: list[tuple[str, str]] = [] | ||
|
|
||
| def info(self, _path: str) -> dict[str, Any]: | ||
| return {"etag": self._token, "size": 123} | ||
|
|
||
| def get(self, src: str, dst: str) -> None: | ||
| self.get_calls.append((src, dst)) | ||
| Path(dst).write_bytes(b"dummy") | ||
|
|
||
| def set_token(self, token: str) -> None: | ||
| self._token = token | ||
|
|
||
|
|
||
| def test_cache_mode_none_returns_none(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setenv("ESP_CACHE_HOME", str(tmp_path)) | ||
| fs = FakeFS(token="t1") | ||
| path = PureGSPath("gs://bucket/file.pt") | ||
|
|
||
| out = _get_local_path_for_cloud_file(path, fs, "none") | ||
|
|
||
| assert out is None | ||
| assert fs.get_calls == [] | ||
|
|
||
|
|
||
| def test_cache_use_downloads_then_reuses_when_token_same(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setenv("ESP_CACHE_HOME", str(tmp_path)) | ||
| fs = FakeFS(token="t1") | ||
| path = PureGSPath("gs://bucket/file.pt") | ||
|
|
||
| p1 = _get_local_path_for_cloud_file(path, fs, "use") | ||
| assert p1 is not None and p1.exists() | ||
| assert len(fs.get_calls) == 1 | ||
|
|
||
| p2 = _get_local_path_for_cloud_file(path, fs, "use") | ||
| assert p2 == p1 | ||
| assert len(fs.get_calls) == 1 # no re-download | ||
|
|
||
|
|
||
| def test_cache_use_redownloads_when_remote_token_changes(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setenv("ESP_CACHE_HOME", str(tmp_path)) | ||
| fs = FakeFS(token="t1") | ||
| path = PureGSPath("gs://bucket/file.pt") | ||
|
|
||
| p1 = _get_local_path_for_cloud_file(path, fs, "use") | ||
| assert p1 is not None and p1.exists() | ||
| assert len(fs.get_calls) == 1 | ||
|
|
||
| fs.set_token("t2") | ||
| p2 = _get_local_path_for_cloud_file(path, fs, "use") | ||
| assert p2 == p1 | ||
| assert len(fs.get_calls) == 2 # refreshed due to token mismatch | ||
|
|
||
|
|
||
| def test_cache_force_always_redownloads(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setenv("ESP_CACHE_HOME", str(tmp_path)) | ||
| fs = FakeFS(token="t1") | ||
| path = PureGSPath("gs://bucket/file.pt") | ||
|
|
||
| p1 = _get_local_path_for_cloud_file(path, fs, "force") | ||
| assert p1 is not None and p1.exists() | ||
| assert len(fs.get_calls) == 1 | ||
|
|
||
| p2 = _get_local_path_for_cloud_file(path, fs, "force") | ||
| assert p2 == p1 | ||
| assert len(fs.get_calls) == 2 | ||
|
|
||
|
|
||
| def test_failed_download_does_not_leave_corrupt_cache(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setenv("ESP_CACHE_HOME", str(tmp_path)) | ||
|
|
||
| class FailingFS(FakeFS): | ||
| def get(self, src: str, dst: str) -> None: # type: ignore[override] | ||
| self.get_calls.append((src, dst)) | ||
| # Simulate a partial write then failure. | ||
| Path(dst).write_bytes(b"partial") | ||
| raise RuntimeError("network error") | ||
|
|
||
| fs = FailingFS(token="t1") | ||
| path = PureGSPath("gs://bucket/file.pt") | ||
|
|
||
| with pytest.raises(RuntimeError, match="network error"): | ||
| _ = _get_local_path_for_cloud_file(path, fs, "use") | ||
|
|
||
| # Final cache file should not exist (atomic rename prevents corrupt cache). | ||
| # (Directory name is hashed; just ensure no completed cache artifact exists.) | ||
| assert not any(p.is_file() and p.suffix != ".tmp" for p in tmp_path.rglob("*")) | ||
|
|
||
|
|
||
| def test_bucket_is_hashed_in_cache_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setenv("ESP_CACHE_HOME", str(tmp_path)) | ||
| fs = FakeFS(token="t1") | ||
| # Crafted "bucket" that could be problematic if used directly. | ||
| path = PureGSPath("gs://../file.pt") | ||
|
|
||
| out = _get_local_path_for_cloud_file(path, fs, "use") | ||
|
|
||
| assert out is not None | ||
| # Should be cached under a hash directory, not "..". | ||
| assert ".." not in out.parts | ||
| assert out.resolve().is_relative_to(tmp_path.resolve()) | ||
|
|
||
|
|
||
| def test_cache_unwritable_falls_back_to_direct_read(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| # Make cache root non-writable. | ||
| cache_root = tmp_path / "cache" | ||
| cache_root.mkdir() | ||
| cache_root.chmod(0o500) # read/execute only | ||
| monkeypatch.setenv("ESP_CACHE_HOME", str(cache_root)) | ||
|
|
||
| fs = FakeFS(token="t1") | ||
| path = PureGSPath("gs://bucket/file.pt") | ||
|
|
||
| out = _get_local_path_for_cloud_file(path, fs, "use") | ||
| assert out is None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.