From d6005a85362f4d4a53d6725b69d7c010af940b94 Mon Sep 17 00:00:00 2001 From: Kim Date: Fri, 17 Jul 2020 15:10:47 -0700 Subject: [PATCH 01/10] doc: Add xgboost doc on bring your own model --- doc/frameworks/xgboost/using_xgboost.rst | 52 +++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index 88afee468e..5af0fa5920 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -390,6 +390,57 @@ The function should return a byte array of data serialized to ``content_type``. The default implementation expects ``prediction`` to be a NumPy array and can serialize the result to JSON, CSV, or NPY. It accepts response content types of "application/json", "text/csv", and "application/x-npy". +Bring your own model +-------------------- + +You can deploy an XGBoost model that you trained outside of SageMaker by using the Amazon SageMaker XGBoost container. +Typically, you save an XGBoost model by pickling the ``Booster`` object or calling ``booster.save_model``. +The XGBoost `built-in algorithm mode `_ +supports both a pickled ``Booster`` object and a model produced by ``booster.save_model``. +For a sample notebook that shows to use the XGBoost built-in algorith mode to load a pre-existing XGBoost model, +see `Amazon SageMaker XGBoost Bring Your Own Model `_. + +You can also deploy an XGBoost model by using XGBoost as a framework. +By using XGBoost as a framework, you have more flexibility. +To deploy an XGBoost model by using XGBoost as a framework, you need to: +- Write an inference script. +- Create the XGBoostModel object. + +Write an inference script +^^^^^^^^^^^^^^^^^^^^^^^^^ + +You must create an inference script that implements (at least) the ``model_fn`` function that calls the loaded model to get a prediction. + +Optionally, you can also implement ``input_fn`` and ``output_fn`` to process input and output, +and ``predict_fn`` to customize how the model server gets predictions from the loaded model. +For information about how to write an inference script, see `SageMaker XGBoost Model Server <#sagemaker-xgboost-model-server>`_. +Pass the filename of the inference script as the ``entry_point`` parameter when you create the `XGBoostModel` object. + +Create a XGBoostModel object +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Now call the ``sagemaker.xgboost.model.XGBoostModel`` constructor to create a model object, +and then call its ``deploy()`` method to deploy your model for inference. + +.. code:: python + + from sagemaker import get_execution_role + role = get_execution_role() + + xgboos_model = XGBoostModel( + model_data="s3://my-bucket/my-path/model.tar.gz", + role=role, + entry_point="inference.py", + framework_version="1.0-1" + ) + + predictor = xgboost_model.deploy( + instance_type='ml.c4.xlarge', + initial_instance_count=1 + ) + +Now you can call the ``predict()`` method to get predictions from your deployed model. + Host Multiple Models with Multi-Model Endpoints ----------------------------------------------- @@ -401,7 +452,6 @@ in the AWS documentation. For a sample notebook that uses Amazon SageMaker to deploy multiple XGBoost models to an endpoint, see the `Multi-Model Endpoint XGBoost Sample Notebook `_. - ************************* SageMaker XGBoost Classes ************************* From 088051251b25f6af64100354b6ec500f3a7add01 Mon Sep 17 00:00:00 2001 From: Kim Date: Wed, 22 Jul 2020 11:42:45 -0700 Subject: [PATCH 02/10] Move reference to BYOM notebook to AWS doc --- doc/frameworks/xgboost/using_xgboost.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index 5af0fa5920..27dbf7d02c 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -397,9 +397,6 @@ You can deploy an XGBoost model that you trained outside of SageMaker by using t Typically, you save an XGBoost model by pickling the ``Booster`` object or calling ``booster.save_model``. The XGBoost `built-in algorithm mode `_ supports both a pickled ``Booster`` object and a model produced by ``booster.save_model``. -For a sample notebook that shows to use the XGBoost built-in algorith mode to load a pre-existing XGBoost model, -see `Amazon SageMaker XGBoost Bring Your Own Model `_. - You can also deploy an XGBoost model by using XGBoost as a framework. By using XGBoost as a framework, you have more flexibility. To deploy an XGBoost model by using XGBoost as a framework, you need to: From 0f8ee0a029bdaf736856b4b152f7bd83ce49e0b1 Mon Sep 17 00:00:00 2001 From: Edward J Kim Date: Wed, 22 Jul 2020 11:43:32 -0700 Subject: [PATCH 03/10] Update doc/frameworks/xgboost/using_xgboost.rst Co-authored-by: Lauren Yu <6631887+laurenyu@users.noreply.github.com> --- doc/frameworks/xgboost/using_xgboost.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index 27dbf7d02c..01d5cac908 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -390,7 +390,7 @@ The function should return a byte array of data serialized to ``content_type``. The default implementation expects ``prediction`` to be a NumPy array and can serialize the result to JSON, CSV, or NPY. It accepts response content types of "application/json", "text/csv", and "application/x-npy". -Bring your own model +Bring Your Own Model -------------------- You can deploy an XGBoost model that you trained outside of SageMaker by using the Amazon SageMaker XGBoost container. From fd7da88da45c3dc63b52849f8f1a9730bb34604d Mon Sep 17 00:00:00 2001 From: Edward J Kim Date: Wed, 22 Jul 2020 11:43:48 -0700 Subject: [PATCH 04/10] Update doc/frameworks/xgboost/using_xgboost.rst Co-authored-by: Lauren Yu <6631887+laurenyu@users.noreply.github.com> --- doc/frameworks/xgboost/using_xgboost.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index 01d5cac908..2ee7f9a459 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -400,6 +400,7 @@ supports both a pickled ``Booster`` object and a model produced by ``booster.sav You can also deploy an XGBoost model by using XGBoost as a framework. By using XGBoost as a framework, you have more flexibility. To deploy an XGBoost model by using XGBoost as a framework, you need to: + - Write an inference script. - Create the XGBoostModel object. From 398dd6d14380467d8a9973079b8b729b488445a2 Mon Sep 17 00:00:00 2001 From: Edward J Kim Date: Wed, 22 Jul 2020 11:44:00 -0700 Subject: [PATCH 05/10] Update doc/frameworks/xgboost/using_xgboost.rst Co-authored-by: Lauren Yu <6631887+laurenyu@users.noreply.github.com> --- doc/frameworks/xgboost/using_xgboost.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index 2ee7f9a459..6da0cb143d 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -414,7 +414,7 @@ and ``predict_fn`` to customize how the model server gets predictions from the l For information about how to write an inference script, see `SageMaker XGBoost Model Server <#sagemaker-xgboost-model-server>`_. Pass the filename of the inference script as the ``entry_point`` parameter when you create the `XGBoostModel` object. -Create a XGBoostModel object +Create an XGBoostModel Object ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Now call the ``sagemaker.xgboost.model.XGBoostModel`` constructor to create a model object, From 9eff0974112e7655b413f9ec294e9a3ab2ddb3c2 Mon Sep 17 00:00:00 2001 From: Edward J Kim Date: Wed, 22 Jul 2020 11:44:18 -0700 Subject: [PATCH 06/10] Update doc/frameworks/xgboost/using_xgboost.rst Co-authored-by: Lauren Yu <6631887+laurenyu@users.noreply.github.com> --- doc/frameworks/xgboost/using_xgboost.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index 6da0cb143d..4c3cdb55ae 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -404,7 +404,7 @@ To deploy an XGBoost model by using XGBoost as a framework, you need to: - Write an inference script. - Create the XGBoostModel object. -Write an inference script +Write an Inference Script ^^^^^^^^^^^^^^^^^^^^^^^^^ You must create an inference script that implements (at least) the ``model_fn`` function that calls the loaded model to get a prediction. From a19c99cc5f21c1904034aa428b3ed0180de5ee21 Mon Sep 17 00:00:00 2001 From: Edward J Kim Date: Wed, 22 Jul 2020 11:44:30 -0700 Subject: [PATCH 07/10] Update doc/frameworks/xgboost/using_xgboost.rst Co-authored-by: Lauren Yu <6631887+laurenyu@users.noreply.github.com> --- doc/frameworks/xgboost/using_xgboost.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index 4c3cdb55ae..2a7bf93be1 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -417,7 +417,7 @@ Pass the filename of the inference script as the ``entry_point`` parameter when Create an XGBoostModel Object ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Now call the ``sagemaker.xgboost.model.XGBoostModel`` constructor to create a model object, +To create a model object, call the ``sagemaker.xgboost.model.XGBoostModel`` constructor, and then call its ``deploy()`` method to deploy your model for inference. .. code:: python From ac20b3f61bab1a485342529105751a1909ee447b Mon Sep 17 00:00:00 2001 From: Edward J Kim Date: Wed, 22 Jul 2020 11:45:41 -0700 Subject: [PATCH 08/10] Update doc/frameworks/xgboost/using_xgboost.rst Co-authored-by: Lauren Yu <6631887+laurenyu@users.noreply.github.com> --- doc/frameworks/xgboost/using_xgboost.rst | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index 2a7bf93be1..e866017125 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -422,12 +422,9 @@ and then call its ``deploy()`` method to deploy your model for inference. .. code:: python - from sagemaker import get_execution_role - role = get_execution_role() - - xgboos_model = XGBoostModel( + xgboost_model = XGBoostModel( model_data="s3://my-bucket/my-path/model.tar.gz", - role=role, + role="my-role", entry_point="inference.py", framework_version="1.0-1" ) From 973d822c3214f34dfb2bec208f9637bdf7473bb8 Mon Sep 17 00:00:00 2001 From: Edward J Kim Date: Wed, 22 Jul 2020 11:47:21 -0700 Subject: [PATCH 09/10] Update doc/frameworks/xgboost/using_xgboost.rst Co-authored-by: Lauren Yu <6631887+laurenyu@users.noreply.github.com> --- doc/frameworks/xgboost/using_xgboost.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index e866017125..4f6e1cb37c 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -434,7 +434,7 @@ and then call its ``deploy()`` method to deploy your model for inference. initial_instance_count=1 ) -Now you can call the ``predict()`` method to get predictions from your deployed model. +To get predictions from your deployed model, you can call the ``predict()`` method. Host Multiple Models with Multi-Model Endpoints ----------------------------------------------- From c32de018b0c3d2758d028849154063af969dd8c9 Mon Sep 17 00:00:00 2001 From: Kim Date: Wed, 22 Jul 2020 12:00:05 -0700 Subject: [PATCH 10/10] Fix title format error; Add predict() example --- doc/frameworks/xgboost/using_xgboost.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/frameworks/xgboost/using_xgboost.rst b/doc/frameworks/xgboost/using_xgboost.rst index 4f6e1cb37c..dde1a34e02 100644 --- a/doc/frameworks/xgboost/using_xgboost.rst +++ b/doc/frameworks/xgboost/using_xgboost.rst @@ -415,7 +415,7 @@ For information about how to write an inference script, see `SageMaker XGBoost M Pass the filename of the inference script as the ``entry_point`` parameter when you create the `XGBoostModel` object. Create an XGBoostModel Object -^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To create a model object, call the ``sagemaker.xgboost.model.XGBoostModel`` constructor, and then call its ``deploy()`` method to deploy your model for inference. @@ -434,6 +434,10 @@ and then call its ``deploy()`` method to deploy your model for inference. initial_instance_count=1 ) + # If payload is a string in LIBSVM format, we need to change serializer. + predictor.serializer = str + predictor.predict("