def get_tempo_artifacts( artifacts_folder: str) -> Tuple[Pipeline, Model, Model]: sklearn_model = Model( name="test-iris-sklearn", platform=ModelFramework.SKLearn, local_folder=f"{artifacts_folder}/{SKLearnFolder}", uri="s3://tempo/basic/sklearn", description="SKLearn Iris classification model", ) xgboost_model = Model( name="test-iris-xgboost", platform=ModelFramework.XGBoost, local_folder=f"{artifacts_folder}/{XGBFolder}", uri="s3://tempo/basic/xgboost", description="XGBoost Iris classification model", ) @pipeline( name="classifier", uri="s3://tempo/basic/pipeline", local_folder=f"{artifacts_folder}/classifier", models=PipelineModels(sklearn=sklearn_model, xgboost=xgboost_model), description= "A pipeline to use either an sklearn or xgboost model for Iris classification", ) def classifier(payload: np.ndarray) -> Tuple[np.ndarray, str]: res1 = classifier.models.sklearn(input=payload) print(res1) if res1[0] == 1: return res1, SKLearnTag else: return classifier.models.xgboost(input=payload), XGBoostTag return classifier, sklearn_model, xgboost_model
def test_deploy_docker(sklearn_model: Model, docker_runtime: SeldonDockerRuntime): sklearn_model.deploy() time.sleep(2) container = docker_runtime._get_container(sklearn_model.details) assert container.status == "running" sklearn_model.undeploy()
def test_undeploy_docker(sklearn_model: Model, docker_runtime: SeldonDockerRuntime): sklearn_model.deploy() time.sleep(2) sklearn_model.undeploy() with pytest.raises(docker.errors.NotFound): docker_runtime._get_container(sklearn_model.details)
def test_seldon_sklearn_model_yaml(expected): m = Model( name="test-iris-sklearn", runtime=SeldonKubernetesRuntime(), platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn/iris", local_folder="", ) assert m.to_k8s_yaml() == expected
def test_sklearn_docker(sklearn_model: Model, x_input): sklearn_model.deploy() time.sleep(2) y_pred = sklearn_model(x_input) np.testing.assert_allclose(y_pred, [[0, 0, 0.99]], atol=1e-2) sklearn_model.undeploy()
def test_seldon_xgboost_model_yaml(expected): m = Model( name="test-iris-xgboost", runtime=SeldonKubernetesRuntime(), platform=ModelFramework.XGBoost, uri="gs://seldon-models/xgboost/iris", local_folder="", ) assert m.to_k8s_yaml() == expected
def k8s_sklearn_model( sklearn_model: Model, k8s_runtime: SeldonKubernetesRuntime) -> Generator[Model, None, None]: sklearn_model._runtime = k8s_runtime sklearn_model.deploy() time.sleep(40) yield sklearn_model sklearn_model.undeploy()
def test_pytorch_model_save(): artifacts_folder = os.path.join(TESTS_PATH, "serve", "data", "mlflow_pytorch", "model") pytorch_mnist_model = Model( name="test-pytorch-mnist", platform=ModelFramework.MLFlow, local_folder=artifacts_folder, description="A pytorch MNIST model - python 3.7", ) pytorch_mnist_model.save(save_env=True) remote_model = deploy_local(pytorch_mnist_model) data = np.random.randn(1, 28 * 28).astype(np.float32) remote_model.predict(data) remote_model.undeploy()
def test_deploy(): rt = SeldonDeployRuntime() config = SeldonDeployConfig( host="https://34.78.44.92/seldon-deploy/api/v1alpha1", user="******", password="******", oidc_server="https://34.78.44.92/auth/realms/deploy-realm", oidc_client_id="sd-api", verify_ssl=False, auth_type=SeldonDeployAuthType.oidc, ) rt.authenticate(settings=config) options = RuntimeOptions( runtime="tempo.seldon.SeldonKubernetesRuntime", k8s_options=KubernetesOptions(namespace="seldon"), ingress_options=IngressOptions(ssl=True, verify_ssl=False), ) sklearn_model = Model( name="test-iris-sklearn", platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn/iris", protocol=SeldonProtocol(), runtime_options=options, ) rt.deploy(sklearn_model) rt.wait_ready(sklearn_model) print(sklearn_model(np.array([[4.9, 3.1, 1.5, 0.2]]))) rt.undeploy(sklearn_model)
def test_deploy_yaml(): rt = SeldonDeployRuntime() config = EnterpriseRuntimeOptions( host="https://34.78.44.92/seldon-deploy/api/v1alpha1", user="******", password="******", oidc_server="https://34.78.44.92/auth/realms/deploy-realm", oidc_client_id="sd-api", verify_ssl=False, auth_type=EnterpriseRuntimeAuthType.oidc, ) rt.authenticate(settings=config) options = KubernetesRuntimeOptions( namespace="seldon", ingress_options=IngressOptions(ssl=True, verify_ssl=False), ) sklearn_model = Model( name="test-iris-sklearn", platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn/iris", protocol=SeldonProtocol(), runtime_options=options, ) spec = rt.manifest(sklearn_model) rtk = SeldonKubernetesRuntime() expected = rtk.manifest(sklearn_model) assert spec == expected
def test_deploy_yaml(): rt = SeldonDeployRuntime( host="http://34.78.44.92/seldon-deploy/api/v1alpha1", user="******", oidc_server="https://34.78.44.92/auth/realms/deploy-realm", password="******", oidc_client_id="sd-api", verify_ssl=False, ) options = RuntimeOptions( runtime="tempo.seldon.SeldonKubernetesRuntime", k8s_options=KubernetesOptions(namespace="seldon"), ingress_options=IngressOptions(ssl=True, verify_ssl=False), ) sklearn_model = Model( name="test-iris-sklearn", platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn/iris", protocol=SeldonProtocol(), runtime_options=options, ) spec = rt.to_k8s_yaml(sklearn_model) rtk = SeldonKubernetesRuntime() expected = rtk.to_k8s_yaml(sklearn_model) assert spec == expected
def xgboost_model(xgboost_iris_path: str, docker_runtime: SeldonDockerRuntime) -> Model: return Model( name="test-iris-xgboost", runtime=docker_runtime, platform=ModelFramework.XGBoost, uri="gs://seldon-models/xgboost/iris", local_folder=xgboost_iris_path, )
def sklearn_model(sklearn_iris_path: str, docker_runtime: SeldonDockerRuntime) -> Model: return Model( name="test-iris-sklearn", runtime=docker_runtime, platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn/iris", local_folder=sklearn_iris_path, )
def test_lambda(input, expected): model = Model( name="test-iris-sklearn", platform=ModelFramework.Custom, model_func=lambda x: np.array([[0, 0, 1]]), ) response = model(input) np.testing.assert_allclose(response, expected, atol=1e-2)
def create_adult_model() -> Model: sklearn_model = Model( name="income-sklearn", platform=ModelFramework.SKLearn, local_folder=os.path.join(ARTIFACTS_FOLDER, MODEL_FOLDER), uri="gs://seldon-models/test/income/model", ) return sklearn_model
def test_seldon_sklearn_model_yaml(expected): m = Model( name="test-iris-sklearn", platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn/iris", local_folder="/tmp/model", protocol=SeldonProtocol(), ) runtime = SeldonKubernetesRuntime() assert runtime.to_k8s_yaml(m) == expected
def get_tempo_artifacts( sklearn_local_path: str, xgboost_local_path: str, classifier_local_path: str, sklearn_url: str = "", xgboost_url: str = "", classifier_url: str = "", ) -> Tuple[Pipeline, Model, Model]: sklearn_model = Model( name="test-iris-sklearn", platform=ModelFramework.SKLearn, local_folder=sklearn_local_path, uri=sklearn_url, description="An SKLearn Iris classification model", ) xgboost_model = Model( name="test-iris-xgboost", platform=ModelFramework.XGBoost, local_folder=xgboost_local_path, uri=xgboost_url, description="An XGBoost Iris classification model", ) @pipeline( name="classifier", uri=classifier_url, local_folder=classifier_local_path, models=PipelineModels(sklearn=sklearn_model, xgboost=xgboost_model), description= "A pipeline to use either an sklearn or xgboost model for Iris classification", ) def classifier(payload: np.ndarray) -> Tuple[np.ndarray, str]: res1 = classifier.models.sklearn(input=payload) if res1[0] == 1: return res1, SKLearnTag else: return classifier.models.xgboost(input=payload), XGBoostTag return classifier, sklearn_model, xgboost_model
def k8s_sklearn_model(sklearn_model: Model, k8s_runtime: SeldonKubernetesRuntime) -> Generator[Model, None, None]: sklearn_model.runtime = k8s_runtime sklearn_model.deploy() sklearn_model.wait_ready(timeout_secs=60) yield sklearn_model sklearn_model.undeploy()
def test_seldon_xgboost_model_yaml(expected): m = Model( name="test-iris-xgboost", protocol=SeldonProtocol(), platform=ModelFramework.XGBoost, uri="gs://seldon-models/xgboost/iris", local_folder="/tmp/model", ) runtime = SeldonKubernetesRuntime() print(runtime.to_k8s_yaml(m)) assert runtime.to_k8s_yaml(m) == expected
def create_model(): cifar10_model = Model( name="resnet32", protocol=KFServingV1Protocol(), platform=ModelFramework.Tensorflow, uri="gs://seldon-models/tfserving/cifar10/resnet32", local_folder=os.path.join(ARTIFACTS_FOLDER, MODEL_FOLDER), ) return cifar10_model
def test_kfserving(): model = Model( name="sklearn-iris2", runtime=KFServingKubernetesRuntime(), platform=ModelFramework.SKLearn, uri="gs://kfserving-samples/models/sklearn/iris", local_folder="sklearn/model", ) # model.deploy() res = model(np.array([[1, 2, 3, 4]])) print(res)
def _model(f): return Model( name, runtime=runtime, local_folder=local_folder, uri=uri, platform=platform, inputs=inputs, outputs=outputs, model_func=f, )
def test_seldon_model_yaml_auth(): m = Model( name="test-iris-xgboost", protocol=SeldonProtocol(), platform=ModelFramework.XGBoost, uri="gs://seldon-models/xgboost/iris", local_folder="/tmp/model", ) runtime = SeldonKubernetesRuntime( runtime_options=RuntimeOptions(k8s_options=KubernetesOptions(authSecretName="auth")) ) print(runtime.to_k8s_yaml(m))
def test_lambda(input, expected): model = Model( name="test-iris-sklearn", runtime=SeldonDockerRuntime(protocol=KFServingV2Protocol()), platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn", local_folder="sklearn/model", model_func=lambda x: np.array([[0, 0, 1]]), ) response = model(input) np.testing.assert_allclose(response, expected, atol=1e-2)
def remote_copy(self): output = {} for name, model in self.items(): output[name] = Model( name=model.get_tempo().details.name, local_folder=model.get_tempo().details.local_folder, uri=model.get_tempo().details.uri, platform=model.get_tempo().details.platform, inputs=model.get_tempo().details.inputs, outputs=model.get_tempo().details.outputs, protocol=model.get_tempo().model_spec.protocol, runtime_options=model.get_tempo().model_spec.runtime_options, ) return PipelineModels(**output)
def test_seldon_xgboost_model_yaml(expected): m = Model( name="test-iris-xgboost", protocol=SeldonProtocol(), platform=ModelFramework.XGBoost, uri="gs://seldon-models/xgboost/iris", local_folder="/tmp/model", ) runtime = SeldonKubernetesRuntime() yaml_str = runtime.manifest(m) yaml_obj = yaml.safe_load(yaml_str) yaml_obj_expected = yaml.safe_load(expected) del yaml_obj["metadata"]["annotations"]["seldon.io/tempo-model"] assert yaml_obj == yaml_obj_expected
def test_seldon_sklearn_model_yaml(expected): m = Model( name="test-iris-sklearn", platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn/iris", local_folder="/tmp/model", protocol=SeldonProtocol(), ) runtime = SeldonKubernetesRuntime() yaml_str = runtime.manifest(m) yaml_obj = yaml.safe_load(yaml_str) yaml_obj_expected = yaml.safe_load(expected) del yaml_obj["metadata"]["annotations"]["seldon.io/tempo-model"] print("\n\n" + str(yaml_obj)) print("\n\n" + str(yaml_obj_expected)) assert yaml_obj == yaml_obj_expected
def test_seldon_model_yaml_auth(expected): m = Model( name="test-iris-xgboost", protocol=SeldonProtocol(), platform=ModelFramework.XGBoost, uri="gs://seldon-models/xgboost/iris", local_folder="/tmp/model", ) runtime = SeldonKubernetesRuntime(runtime_options=KubernetesRuntimeOptions( authSecretName="auth")) yaml_str = runtime.manifest(m) yaml_obj = yaml.safe_load(yaml_str) yaml_obj_expected = yaml.safe_load(expected) del yaml_obj["metadata"]["annotations"]["seldon.io/tempo-model"] print("\n\n" + str(yaml_obj)) print("\n\n" + str(yaml_obj_expected)) assert yaml_obj == yaml_obj_expected
def test_deploy(): rt = SeldonDeployRuntime( host="http://34.105.136.157/seldon-deploy/api/v1alpha1", user="******", password="******", k8s_options=KubernetesOptions(namespace="seldon"), ) sklearn_model = Model( name="test-iris-sklearn", runtime=rt, platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn/iris", local_folder=os.getcwd() + "/sklearn", ) # sklearn_model.deploy() sklearn_model(np.array([[4.9, 3.1, 1.5, 0.2]]))
def test_deploy_yaml(): rt = SeldonDeployRuntime( host="http://34.105.136.157/seldon-deploy/api/v1alpha1", user="******", password="******", k8s_options=KubernetesOptions(namespace="seldon"), ) sklearn_model = Model( name="test-iris-sklearn", runtime=rt, platform=ModelFramework.SKLearn, uri="gs://seldon-models/sklearn/iris", local_folder=os.getcwd() + "/sklearn", ) srt = SeldonKubernetesRuntime(k8s_options=KubernetesOptions(namespace="seldon")) sklearn_model.set_runtime(srt) expected = sklearn_model.to_k8s_yaml() sklearn_model.set_runtime(rt) assert sklearn_model.to_k8s_yaml() == expected