From 8a8522c60025e9331c6f99cef8d325476890ba73 Mon Sep 17 00:00:00 2001 From: vishal Date: Fri, 23 Aug 2019 15:54:51 -0400 Subject: [PATCH 1/4] Fix api tracker key --- docs/deployments/request-handlers.md | 5 ++--- pkg/operator/api/userconfig/apis.go | 6 ++---- pkg/workloads/cortex/lib/api_utils.py | 5 ++++- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/deployments/request-handlers.md b/docs/deployments/request-handlers.md index bcddd4e6ac..7eb6b8e99f 100644 --- a/docs/deployments/request-handlers.md +++ b/docs/deployments/request-handlers.md @@ -80,10 +80,9 @@ msgpack==0.6.1 numpy>=1.13.3,<2 requirements-parser==0.2.0 packaging==19.0.0 -pillow==6.1.0 -regex==2017.4.5 requests==2.21.0 -tensorflow==1.14.0 # In TensorFlow model formats only +tensorflow==1.14.0 # In TensorFlow serving images only +onnxruntime==0.5.0 # In ONNX serving images only ``` You can install additional PyPI packages and import your own Python packages. See [Python Packages](../piplines/python-packages.md) for more details. diff --git a/pkg/operator/api/userconfig/apis.go b/pkg/operator/api/userconfig/apis.go index 86c1e47228..8fa19c5664 100644 --- a/pkg/operator/api/userconfig/apis.go +++ b/pkg/operator/api/userconfig/apis.go @@ -40,7 +40,7 @@ type API struct { } type Tracker struct { - Key string `json:"key" yaml:"key"` + Key *string `json:"key" yaml:"key"` ModelType ModelType `json:"model_type" yaml:"model_type"` } @@ -67,9 +67,7 @@ var apiValidation = &cr.StructValidation{ StructFieldValidations: []*cr.StructFieldValidation{ { StructField: "Key", - StringValidation: &cr.StringValidation{ - Required: true, - }, + StringValidation: &cr.StringValidation{}, }, { StructField: "ModelType", diff --git a/pkg/workloads/cortex/lib/api_utils.py b/pkg/workloads/cortex/lib/api_utils.py index c25f61ff2f..9b1788cdba 100644 --- a/pkg/workloads/cortex/lib/api_utils.py +++ b/pkg/workloads/cortex/lib/api_utils.py @@ -46,7 +46,10 @@ def prediction_metrics(dimensions, api, predictions): metric_list = [] tracker = api.get("tracker") for prediction in predictions: - predicted_value = prediction.get(tracker["key"]) + predicted_value = prediction + if tracker.get("key") is None: + predicted_value = prediction + if predicted_value is None: logger.warn( "failed to track key '{}': not found in response payload".format(tracker["key"]) From 407840560eadb0a8d7b59d680fa127ef8c0264ba Mon Sep 17 00:00:00 2001 From: vishal Date: Mon, 26 Aug 2019 15:00:21 +0000 Subject: [PATCH 2/4] String pointer validation --- pkg/operator/api/userconfig/apis.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/operator/api/userconfig/apis.go b/pkg/operator/api/userconfig/apis.go index 8fa19c5664..81464a198d 100644 --- a/pkg/operator/api/userconfig/apis.go +++ b/pkg/operator/api/userconfig/apis.go @@ -67,7 +67,8 @@ var apiValidation = &cr.StructValidation{ StructFieldValidations: []*cr.StructFieldValidation{ { StructField: "Key", - StringValidation: &cr.StringValidation{}, + StringPtrValidation: &cr.StringPtrValidation{ + }, }, { StructField: "ModelType", From 78231c1b40f605b2bdd4661fb293ab46343da7a2 Mon Sep 17 00:00:00 2001 From: vishal Date: Mon, 26 Aug 2019 16:30:34 +0000 Subject: [PATCH 3/4] Improve error message --- docs/deployments/apis.md | 2 +- pkg/workloads/cortex/lib/api_utils.py | 28 +++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/docs/deployments/apis.md b/docs/deployments/apis.md index 9d6da10388..6286dcc482 100644 --- a/docs/deployments/apis.md +++ b/docs/deployments/apis.md @@ -11,7 +11,7 @@ Serve models at scale. model_format: # model format, must be "tensorflow" or "onnx" (default: "onnx" if model path ends with .onnx, "tensorflow" if model path ends with .zip) request_handler: # path to the request handler implementation file, relative to the cortex root tracker: - key: # json key to track in the response payload + key: # json key to track if the response payload is a dictionary model_type: # model type, must be "classification" or "regression" compute: min_replicas: # minimum number of replicas (default: 1) diff --git a/pkg/workloads/cortex/lib/api_utils.py b/pkg/workloads/cortex/lib/api_utils.py index 58a184217e..4d711e7a9b 100644 --- a/pkg/workloads/cortex/lib/api_utils.py +++ b/pkg/workloads/cortex/lib/api_utils.py @@ -73,26 +73,34 @@ def extract_predicted_values(api, predictions): tracker = api.get("tracker") for prediction in predictions: - predicted_value = prediction - if tracker.get("key") is None: + if tracker.get("key") is not None: + key = tracker["key"] + if type(prediction) != dict: + raise ValueError( + "failed to track key '{}': expected prediction to be of type dict but found '{}'".format( + key, type(prediction) + ) + ) + if prediction.get(key) is None: + raise ValueError( + "failed to track key '{}': not found in prediction".format(tracker["key"]) + ) + predicted_value = prediction[key] + else: predicted_value = prediction - if predicted_value is None: - raise ValueError( - "failed to track key '{}': not found in response payload".format(tracker["key"]) - ) if tracker["model_type"] == "classification": if type(predicted_value) != str and type(predicted_value) != int: raise ValueError( - "failed to track key '{}': expected type 'str' or 'int' but encountered '{}'".format( - tracker["key"], type(predicted_value) + "failed to track classification prediction: expected type 'str' or 'int' but encountered '{}'".format( + type(predicted_value) ) ) else: if type(predicted_value) != float and type(predicted_value) != int: # allow ints raise ValueError( - "failed to track key '{}': expected type 'float' or 'int' but encountered '{}'".format( - tracker["key"], type(predicted_value) + "failed to track regression prediction: expected type 'float' or 'int' but encountered '{}'".format( + type(predicted_value) ) ) predicted_values.append(predicted_value) From cda97fafd826d5f144d1f1b7c39c216d1197f32d Mon Sep 17 00:00:00 2001 From: vishal Date: Mon, 26 Aug 2019 17:18:42 +0000 Subject: [PATCH 4/4] Fix lint --- pkg/operator/api/userconfig/apis.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/operator/api/userconfig/apis.go b/pkg/operator/api/userconfig/apis.go index 81464a198d..2cc5bd1634 100644 --- a/pkg/operator/api/userconfig/apis.go +++ b/pkg/operator/api/userconfig/apis.go @@ -40,7 +40,7 @@ type API struct { } type Tracker struct { - Key *string `json:"key" yaml:"key"` + Key *string `json:"key" yaml:"key"` ModelType ModelType `json:"model_type" yaml:"model_type"` } @@ -66,9 +66,8 @@ var apiValidation = &cr.StructValidation{ DefaultNil: true, StructFieldValidations: []*cr.StructFieldValidation{ { - StructField: "Key", - StringPtrValidation: &cr.StringPtrValidation{ - }, + StructField: "Key", + StringPtrValidation: &cr.StringPtrValidation{}, }, { StructField: "ModelType",