-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into switch-dir-copy
- Loading branch information
Showing
19 changed files
with
451 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
############ | ||
Sklearn Type | ||
############ | ||
.. automodule:: flytekit.extras.sklearn | ||
:no-members: | ||
:no-inherited-members: | ||
:no-special-members: |
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,26 @@ | ||
""" | ||
Flytekit Sklearn | ||
========================================= | ||
.. currentmodule:: flytekit.extras.sklearn | ||
.. autosummary:: | ||
:template: custom.rst | ||
:toctree: generated/ | ||
""" | ||
from flytekit.loggers import logger | ||
|
||
# TODO: abstract this out so that there's an established pattern for registering plugins | ||
# that have soft dependencies | ||
try: | ||
# isolate the exception to the sklearn import | ||
import sklearn | ||
|
||
_sklearn_installed = True | ||
except (ImportError, OSError): | ||
_sklearn_installed = False | ||
|
||
|
||
if _sklearn_installed: | ||
from .native import SklearnEstimatorTransformer | ||
else: | ||
logger.info("We won't register SklearnEstimatorTransformer because scikit-learn is not installed.") |
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,79 @@ | ||
import pathlib | ||
from typing import Generic, Type, TypeVar | ||
|
||
import joblib | ||
import sklearn | ||
|
||
from flytekit.core.context_manager import FlyteContext | ||
from flytekit.core.type_engine import TypeEngine, TypeTransformer, TypeTransformerFailedError | ||
from flytekit.models.core import types as _core_types | ||
from flytekit.models.literals import Blob, BlobMetadata, Literal, Scalar | ||
from flytekit.models.types import LiteralType | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class SklearnTypeTransformer(TypeTransformer, Generic[T]): | ||
def get_literal_type(self, t: Type[T]) -> LiteralType: | ||
return LiteralType( | ||
blob=_core_types.BlobType( | ||
format=self.SKLEARN_FORMAT, | ||
dimensionality=_core_types.BlobType.BlobDimensionality.SINGLE, | ||
) | ||
) | ||
|
||
def to_literal( | ||
self, | ||
ctx: FlyteContext, | ||
python_val: T, | ||
python_type: Type[T], | ||
expected: LiteralType, | ||
) -> Literal: | ||
meta = BlobMetadata( | ||
type=_core_types.BlobType( | ||
format=self.SKLEARN_FORMAT, | ||
dimensionality=_core_types.BlobType.BlobDimensionality.SINGLE, | ||
) | ||
) | ||
|
||
local_path = ctx.file_access.get_random_local_path() + ".joblib" | ||
pathlib.Path(local_path).parent.mkdir(parents=True, exist_ok=True) | ||
|
||
# save sklearn estimator to a file | ||
joblib.dump(python_val, local_path) | ||
|
||
remote_path = ctx.file_access.get_random_remote_path(local_path) | ||
ctx.file_access.put_data(local_path, remote_path, is_multipart=False) | ||
return Literal(scalar=Scalar(blob=Blob(metadata=meta, uri=remote_path))) | ||
|
||
def to_python_value(self, ctx: FlyteContext, lv: Literal, expected_python_type: Type[T]) -> T: | ||
try: | ||
uri = lv.scalar.blob.uri | ||
except AttributeError: | ||
TypeTransformerFailedError(f"Cannot convert from {lv} to {expected_python_type}") | ||
|
||
local_path = ctx.file_access.get_random_local_path() | ||
ctx.file_access.get_data(uri, local_path, is_multipart=False) | ||
|
||
# load sklearn estimator from a file | ||
return joblib.load(local_path) | ||
|
||
def guess_python_type(self, literal_type: LiteralType) -> Type[T]: | ||
if ( | ||
literal_type.blob is not None | ||
and literal_type.blob.dimensionality == _core_types.BlobType.BlobDimensionality.SINGLE | ||
and literal_type.blob.format == self.SKLEARN_FORMAT | ||
): | ||
return T | ||
|
||
raise ValueError(f"Transformer {self} cannot reverse {literal_type}") | ||
|
||
|
||
class SklearnEstimatorTransformer(SklearnTypeTransformer[sklearn.base.BaseEstimator]): | ||
SKLEARN_FORMAT = "SklearnEstimator" | ||
|
||
def __init__(self): | ||
super().__init__(name="Sklearn Estimator", t=sklearn.base.BaseEstimator) | ||
|
||
|
||
TypeEngine.register(SklearnEstimatorTransformer()) |
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
Oops, something went wrong.