-
Notifications
You must be signed in to change notification settings - Fork 3.5k
store: mock/fixture home #16536
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
Merged
Merged
store: mock/fixture home #16536
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c0c6861
conftest
Borda b96eafa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fc5913d
monkeypatch & pkg reload
Borda 022472b
Merge branch 'store/fixture' of https://github.com/PyTorchLightning/p…
Borda 17f46ad
apply everywhere
Borda f2107ce
Merge branch 'master' into store/fixture
Borda 904ebe5
lit home
Borda 78b5717
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1e67713
fix home
Borda e98dfd2
Merge branch 'store/fixture' of https://github.com/PyTorchLightning/p…
Borda 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,42 @@ | ||
import importlib | ||
import os | ||
import tempfile | ||
import types | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
import lightning | ||
import lightning.store | ||
|
||
|
||
def reload_package(package): | ||
# credit: https://stackoverflow.com/a/28516918/4521646 | ||
assert hasattr(package, "__package__") | ||
fn = package.__file__ | ||
fn_dir = os.path.dirname(fn) + os.sep | ||
module_visit = {fn} | ||
del fn | ||
|
||
def reload_recursive_ex(module): | ||
importlib.reload(module) | ||
|
||
for module_child in vars(module).values(): | ||
if not isinstance(module_child, types.ModuleType): | ||
continue | ||
fn_child = getattr(module_child, "__file__", None) | ||
if (fn_child is not None) and fn_child.startswith(fn_dir) and fn_child not in module_visit: | ||
# print("reloading:", fn_child, "from", module) | ||
module_visit.add(fn_child) | ||
reload_recursive_ex(module_child) | ||
|
||
return reload_recursive_ex(package) | ||
|
||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
Borda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def lit_home(monkeypatch): | ||
with tempfile.TemporaryDirectory() as tmp_dirname: | ||
monkeypatch.setattr(Path, "home", lambda: tmp_dirname) | ||
# we need to reload whole subpackage to apply the mock/fixture | ||
reload_package(lightning.store) | ||
yield os.path.join(tmp_dirname, ".lightning") | ||
Borda marked this conversation as resolved.
Show resolved
Hide resolved
|
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,65 +1,48 @@ | ||
import os | ||
|
||
import pytest | ||
from tests_cloud import _API_KEY, _PROJECT_ID, _USERNAME | ||
from tests_cloud.helpers import cleanup | ||
|
||
import pytorch_lightning as pl | ||
from lightning.store import download_from_cloud, load_model, upload_to_cloud | ||
from lightning.store.save import _LIGHTNING_STORAGE_DIR | ||
from lightning.store.save import __STORAGE_DIR_NAME | ||
from pytorch_lightning.demos.boring_classes import BoringModel | ||
|
||
|
||
def test_model(model_name: str = "boring_model", version: str = "latest"): | ||
cleanup() | ||
|
||
@pytest.mark.parametrize("pbar", [True, False]) | ||
def test_model(lit_home, pbar, model_name: str = "boring_model", version: str = "latest"): | ||
upload_to_cloud(model_name, model=BoringModel(), api_key=_API_KEY, project_id=_PROJECT_ID) | ||
|
||
download_from_cloud(f"{_USERNAME}/{model_name}") | ||
assert os.path.isdir(os.path.join(_LIGHTNING_STORAGE_DIR, _USERNAME, model_name, version)) | ||
download_from_cloud(f"{_USERNAME}/{model_name}", progress_bar=pbar) | ||
assert os.path.isdir(os.path.join(lit_home, __STORAGE_DIR_NAME, _USERNAME, model_name, version)) | ||
|
||
model = load_model(f"{_USERNAME}/{model_name}") | ||
assert model is not None | ||
|
||
|
||
def test_model_without_progress_bar(model_name: str = "boring_model", version: str = "latest"): | ||
cleanup() | ||
|
||
upload_to_cloud(model_name, model=BoringModel(), api_key=_API_KEY, project_id=_PROJECT_ID, progress_bar=False) | ||
|
||
download_from_cloud(f"{_USERNAME}/{model_name}", progress_bar=False) | ||
assert os.path.isdir(os.path.join(_LIGHTNING_STORAGE_DIR, _USERNAME, model_name, version)) | ||
|
||
model = load_model(f"{_USERNAME}/{model_name}") | ||
assert model is not None | ||
|
||
|
||
def test_only_weights(model_name: str = "boring_model_only_weights", version: str = "latest"): | ||
cleanup() | ||
|
||
def test_only_weights(lit_home, model_name: str = "boring_model_only_weights", version: str = "latest"): | ||
model = BoringModel() | ||
trainer = pl.Trainer(fast_dev_run=True) | ||
trainer.fit(model) | ||
upload_to_cloud(model_name, model=model, weights_only=True, api_key=_API_KEY, project_id=_PROJECT_ID) | ||
|
||
download_from_cloud(f"{_USERNAME}/{model_name}") | ||
assert os.path.isdir(os.path.join(_LIGHTNING_STORAGE_DIR, _USERNAME, model_name, version)) | ||
assert os.path.isdir(os.path.join(lit_home, __STORAGE_DIR_NAME, _USERNAME, model_name, version)) | ||
|
||
model_with_weights = load_model(f"{_USERNAME}/{model_name}", load_weights=True, model=model) | ||
assert model_with_weights is not None | ||
assert model_with_weights.state_dict() is not None | ||
|
||
|
||
def test_checkpoint_path(model_name: str = "boring_model_only_checkpoint_path", version: str = "latest"): | ||
cleanup() | ||
|
||
def test_checkpoint_path(lit_home, model_name: str = "boring_model_only_checkpoint_path", version: str = "latest"): | ||
model = BoringModel() | ||
trainer = pl.Trainer(fast_dev_run=True) | ||
trainer.fit(model) | ||
trainer.save_checkpoint("tmp.ckpt") | ||
upload_to_cloud(model_name, checkpoint_path="tmp.ckpt", api_key=_API_KEY, project_id=_PROJECT_ID) | ||
|
||
download_from_cloud(f"{_USERNAME}/{model_name}") | ||
assert os.path.isdir(os.path.join(_LIGHTNING_STORAGE_DIR, _USERNAME, model_name, version)) | ||
assert os.path.isdir(os.path.join(lit_home, __STORAGE_DIR_NAME, _USERNAME, model_name, version)) | ||
|
||
ckpt = load_model(f"{_USERNAME}/{model_name}", load_checkpoint=True, model=model) | ||
assert ckpt is not None |
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
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
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.