Skip to content

Commit

Permalink
Seldon examples (kubeflow#1405)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandawsonuk authored and gaoning777 committed Aug 16, 2019
1 parent a6f3f40 commit e849f22
Show file tree
Hide file tree
Showing 5 changed files with 644 additions and 0 deletions.
10 changes: 10 additions & 0 deletions samples/contrib/seldon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Seldon Samples

[Seldon](https://github.com/SeldonIO/seldon-core) is a model serving solution that supports multiple deployment strategies and provides out of the box [observability](https://docs.seldon.io/projects/seldon-core/en/latest/analytics/analytics.html). These examples are an overview of how seldon can be used with pipelines:

- iris_storagebucket.py is the simplest case and best place to start. It shows how seldon can [serve a packaged model from a storage bucket URI](https://docs.seldon.io/projects/seldon-core/en/latest/servers/overview.html).
- mabdeploy_seldon.py shows how components can be assembled into [inference graphs](https://docs.seldon.io/projects/seldon-core/en/latest/analytics/routers.html) such as [multi-armed bandits](https://docs.seldon.io/projects/seldon-core/en/latest/examples/helm_examples.html#Serve-Multi-Armed-Bandit).
- mnist_tf.py shows how seldon can be used with [custom-built serving images](https://docs.seldon.io/projects/seldon-core/en/latest/workflow/README.html) where a model is baked into the docker image.
- mnist_tf_volume.py shows a model being stored in a volume and served using a serving image.

See the seldon docs for other uses such as [autoscaling](https://docs.seldon.io/projects/seldon-core/en/latest/examples/autoscaling_example.html), [canaries](https://docs.seldon.io/projects/seldon-core/en/latest/examples/istio_canary.html) and [tracing](https://docs.seldon.io/projects/seldon-core/en/latest/examples/tmpl_model_tracing.html)
67 changes: 67 additions & 0 deletions samples/contrib/seldon/iris_storagebucket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import json
import kfp.dsl as dsl
from string import Template

@dsl.pipeline(
name="Deploy example SKLearn Iris",
description="SKLearn Iris simple deployment example"
)
def iris_storagebucket(bucket='gs://seldon-models/sklearn/iris'):

#simple serving of an iris sklearn model based on https://docs.seldon.io/projects/seldon-core/en/latest/servers/overview.html
#requires seldon 0.3.2 or higher
sklearnjson_template = Template("""
{
"apiVersion": "machinelearning.seldon.io/v1alpha2",
"kind": "SeldonDeployment",
"metadata": {
"name": "sklearn"
},
"spec": {
"name": "iris",
"predictors": [
{
"graph": {
"children": [],
"implementation": "SKLEARN_SERVER",
"modelUri": "$bucket",
"name": "classifier"
},
"name": "default",
"replicas": 1
}
]
}
}
""")

sklearnjson = sklearnjson_template.substitute({ 'bucket': str(bucket)})

sklearndeployment = json.loads(sklearnjson)

deploy = dsl.ResourceOp(
name="deploy",
k8s_resource=sklearndeployment,
action="apply",
success_condition='status.state == Available'
)


if __name__ == "__main__":
import kfp.compiler as compiler
compiler.Compiler().compile(iris_storagebucket, __file__ + ".tar.gz")
152 changes: 152 additions & 0 deletions samples/contrib/seldon/mabdeploy_seldon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import json
import kfp.dsl as dsl
from string import Template

@dsl.pipeline(
name="Deploy example MAB",
description="Multi-armed bandit example"
)
def mabdeploy_seldon(docker_image1='seldonio/mock_classifier:1.0',
docker_image2='seldonio/mock_classifier:1.0',
mab_router_image='seldonio/mab_epsilon_greedy:1.1'):

#serve two models load balanced as bandit as per https://github.com/SeldonIO/seldon-core/blob/master/notebooks/helm_examples.ipynb
#in this example no volumes or buckets required as the models are baked into images
#seldon can also be used with volumes - see seldon tf mnist example
mabjson_template = Template("""
{
"apiVersion": "machinelearning.seldon.io/v1alpha2",
"kind": "SeldonDeployment",
"metadata": {
"labels": {
"app": "seldon"
},
"name": "mnist-classifier-mab"
},
"spec": {
"name": "mnist-classifier-mab",
"predictors": [
{
"name": "abtest",
"replicas": 1,
"componentSpecs": [{
"spec": {
"containers": [
{
"image": "$image1",
"imagePullPolicy": "IfNotPresent",
"name": "classifier-1",
"resources": {
"requests": {
"memory": "1Mi"
}
}
}],
"terminationGracePeriodSeconds": 20
}},
{
"metadata":{
"labels":{
"version":"v2"
}
},
"spec":{
"containers":[
{
"image": "$image2",
"imagePullPolicy": "IfNotPresent",
"name": "classifier-2",
"resources": {
"requests": {
"memory": "1Mi"
}
}
}
],
"terminationGracePeriodSeconds": 20
}
},
{
"spec":{
"containers": [{
"image": "$router",
"name": "eg-router"
}],
"terminationGracePeriodSeconds": 20
}}
],
"graph": {
"name": "eg-router",
"type":"ROUTER",
"parameters": [
{
"name": "n_branches",
"value": "2",
"type": "INT"
},
{
"name": "epsilon",
"value": "0.2",
"type": "FLOAT"
},
{
"name": "verbose",
"value": "1",
"type": "BOOL"
}
],
"children": [
{
"name": "classifier-1",
"endpoint":{
"type":"REST"
},
"type":"MODEL",
"children":[]
},
{
"name": "classifier-2",
"endpoint":{
"type":"REST"
},
"type":"MODEL",
"children":[]
}
]
}
}
]
}
}
""")

mabjson = mabjson_template.substitute({ 'image1': str(docker_image1),'image2': str(docker_image2),'router': str(mab_router_image)})

mabdeployment = json.loads(mabjson)

deploy = dsl.ResourceOp(
name="deploy",
k8s_resource=mabdeployment,
action="apply",
success_condition='status.state == Available'
)


if __name__ == "__main__":
import kfp.compiler as compiler
compiler.Compiler().compile(mabdeploy_seldon, __file__ + ".tar.gz")
Loading

0 comments on commit e849f22

Please sign in to comment.