-
-
Notifications
You must be signed in to change notification settings - Fork 381
[GSK-2378] Only persist/read cache when explicitly asked #1680
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
Hartorn
merged 9 commits into
main
from
feature/gsk-2378-add-a-clear-model-cache-button-in-the-hub
Dec 18, 2023
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e443602
Only persist/read cache when explicitly asked
kevinmessiaen e1e3ca5
Only load artifact once
kevinmessiaen 101a294
Merge branch 'main' into feature/gsk-2378-add-a-clear-model-cache-but…
kevinmessiaen a7d874b
Fixed cache logic
kevinmessiaen 856b09a
Merge branch 'main' into feature/gsk-2378-add-a-clear-model-cache-but…
kevinmessiaen 94fb6af
Typo
kevinmessiaen 8657ef6
Merge remote-tracking branch 'origin/feature/gsk-2378-add-a-clear-mod…
kevinmessiaen 3b7485a
Code improvement
kevinmessiaen 953a681
Added test to ensure same model instance is used in test suite
kevinmessiaen 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 |
|---|---|---|
|
|
@@ -2,10 +2,10 @@ | |
| import os | ||
| import shutil | ||
| import uuid | ||
| from typing import Any, Dict, List, Optional | ||
|
|
||
| import pandas as pd | ||
| from mlflow.store.artifact.artifact_repo import verify_artifact_path | ||
| from typing import Any, Dict, List, Optional, Callable | ||
|
|
||
| from giskard.client.giskard_client import GiskardClient | ||
| from giskard.core.suite import DatasetInput, ModelInput, SuiteInput | ||
|
|
@@ -158,7 +158,24 @@ def map_dataset_process_function_meta_ws(callable_type): | |
| } | ||
|
|
||
|
|
||
| def parse_function_arguments(client: Optional[GiskardClient], request_arguments: List[websocket.FuncArgument]): | ||
| def _get_or_load(loaded_artifacts: Dict[str, Dict[str, Any]], type: str, uuid: str, load_fn: Callable[[], Any]) -> Any: | ||
| if type not in loaded_artifacts: | ||
| loaded_artifacts[type] = dict() | ||
|
||
|
|
||
| if uuid not in loaded_artifacts[type]: | ||
| loaded_artifacts[type][uuid] = load_fn() | ||
|
|
||
| return loaded_artifacts[type][uuid] | ||
|
|
||
|
|
||
| def parse_function_arguments( | ||
| client: Optional[GiskardClient], | ||
| request_arguments: List[websocket.FuncArgument], | ||
| loaded_artifacts: Optional[Dict[str, Dict[str, Any]]] = None, | ||
| ): | ||
| if loaded_artifacts is None: | ||
| loaded_artifacts = dict() | ||
|
|
||
| arguments = dict() | ||
|
|
||
| # Processing empty list | ||
|
|
@@ -169,14 +186,24 @@ def parse_function_arguments(client: Optional[GiskardClient], request_arguments: | |
| if arg.is_none: | ||
| continue | ||
| if arg.dataset is not None: | ||
| arguments[arg.name] = Dataset.download( | ||
| client, | ||
| arg.dataset.project_key, | ||
| arguments[arg.name] = _get_or_load( | ||
| loaded_artifacts, | ||
| "Dataset", | ||
| arg.dataset.id, | ||
| arg.dataset.sample, | ||
| lambda: Dataset.download( | ||
| client, | ||
| arg.dataset.project_key, | ||
| arg.dataset.id, | ||
| arg.dataset.sample, | ||
| ), | ||
| ) | ||
| elif arg.model is not None: | ||
| arguments[arg.name] = BaseModel.download(client, arg.model.project_key, arg.model.id) | ||
| arguments[arg.name] = _get_or_load( | ||
| loaded_artifacts, | ||
| "BaseModel", | ||
| arg.model.id, | ||
| lambda: BaseModel.download(client, arg.model.project_key, arg.model.id), | ||
| ) | ||
| elif arg.slicingFunction is not None: | ||
| arguments[arg.name] = SlicingFunction.download( | ||
| arg.slicingFunction.id, client, arg.slicingFunction.project_key | ||
|
|
||
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 |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| import csv | ||
| from pathlib import Path | ||
| from typing import Any, Iterable, List, Optional | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| from typing import Any, Iterable, List, Optional | ||
|
|
||
| from ...client.python_utils import warning | ||
| from ...core.core import SupportedModelTypes | ||
|
|
@@ -26,14 +26,23 @@ def flatten(xs): | |
| class ModelCache: | ||
| _default_cache_dir_prefix = Path(settings.home_dir / settings.cache_dir / "global" / "prediction_cache") | ||
|
|
||
| def __init__(self, model_type: SupportedModelTypes, id: Optional[str] = None, cache_dir: Optional[Path] = None): | ||
| def __init__( | ||
| self, | ||
| model_type: SupportedModelTypes, | ||
| id: Optional[str] = None, | ||
| persist_cache: bool = False, | ||
| cache_dir: Optional[Path] = None, | ||
| ): | ||
| self.id = id | ||
| self.prediction_cache = dict() | ||
|
|
||
| if cache_dir is None and self.id: | ||
| cache_dir = self._default_cache_dir_prefix.joinpath(self.id) | ||
| if persist_cache: | ||
| if cache_dir is None and self.id: | ||
| cache_dir = self._default_cache_dir_prefix.joinpath(self.id) | ||
|
||
|
|
||
| self.cache_file = cache_dir / CACHE_CSV_FILENAME if cache_dir else None | ||
| self.cache_file = cache_dir / CACHE_CSV_FILENAME if cache_dir else None | ||
| else: | ||
| self.cache_file = None | ||
|
|
||
| self.vectorized_get_cache_or_na = np.vectorize(self.get_cache_or_na, otypes=[object]) | ||
| self.model_type = model_type | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it initialized here if it's also set in the function with the 'if none' condition ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it' done to share the cache between the global arguments and test ones