Skip to content

Commit aa139ba

Browse files
committed
Add section about transform_fn and fix input_fn signature
1 parent 69c45ce commit aa139ba

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

src/sagemaker/mxnet/README.rst

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -538,29 +538,39 @@ For more information on how to enable MXNet to interact with Amazon Elastic Infe
538538
Model serving
539539
^^^^^^^^^^^^^
540540

541-
After the SageMaker model server has loaded your model, by calling either the default ``model_fn`` or the implementation in your training script, SageMaker will serve your model. Model serving is the process of responding to inference requests, received by SageMaker InvokeEndpoint API calls. The SageMaker MXNet model server breaks request handling into three steps:
541+
After the SageMaker model server has loaded your model, by calling either the default ``model_fn`` or the implementation in your script, SageMaker will serve your model.
542+
Model serving is the process of responding to inference requests, received by SageMaker InvokeEndpoint API calls.
543+
Defining how to handle these requests can be done in one of two ways:
542544

545+
- using ``input_fn``, ``predict_fn``, and ``output_fn``, some of which may be your own implementations
546+
- writing your own ``transform_fn`` for handling input processing, prediction, and output processing
543547

544-
- input processing,
545-
- prediction, and
546-
- output processing.
548+
Using ``input_fn``, ``predict_fn``, and ``output_fn``
549+
'''''''''''''''''''''''''''''''''''''''''''''''''''''
547550

548-
In a similar way to previous steps, you configure these steps by defining functions in your Python source file.
551+
The SageMaker MXNet model server breaks request handling into three steps:
549552

550-
Each step involves invoking a python function, with information about the request and the return-value from the previous function in the chain. Inside the SageMaker MXNet model server, the process looks like:
553+
- input processing
554+
- prediction
555+
- output processing
556+
557+
Just like with ``model_fn``, you configure these steps by defining functions in your Python source file.
558+
559+
Each step has its own Python function, which takes in information about the request and the return value from the previous function in the chain.
560+
Inside the SageMaker MXNet model server, the process looks like:
551561

552562
.. code:: python
553563
554564
# Deserialize the Invoke request body into an object we can perform prediction on
555-
input_object = input_fn(request_body, request_content_type, model)
565+
input_object = input_fn(request_body, request_content_type)
556566
557567
# Perform prediction on the deserialized object, with the loaded model
558568
prediction = predict_fn(input_object, model)
559569
560570
# Serialize the prediction result into the desired response content type
561571
ouput = output_fn(prediction, response_content_type)
562572
563-
The above code-sample shows the three function definitions:
573+
The above code sample shows the three function definitions that correlate to the three steps mentioned above:
564574

565575
- ``input_fn``: Takes request data and deserializes the data into an
566576
object for prediction.
@@ -569,7 +579,10 @@ The above code-sample shows the three function definitions:
569579
- ``output_fn``: Takes the result of prediction and serializes this
570580
according to the response content type.
571581

572-
The SageMaker MXNet model server provides default implementations of these functions. These work with common-content types, and Gluon API and Module API model objects. You can provide your own implementations for these functions in your training script. If you omit any definition then the SageMaker MXNet model server will use its default implementation for that function.
582+
The SageMaker MXNet model server provides default implementations of these functions.
583+
These work with common content types, and Gluon API and Module API model objects.
584+
You can also provide your own implementations for these functions in your training script.
585+
If you omit any definition then the SageMaker MXNet model server will use its default implementation for that function.
573586

574587
If you rely solely on the SageMaker MXNet model server defaults, you get the following functionality:
575588

@@ -581,36 +594,36 @@ If you rely solely on the SageMaker MXNet model server defaults, you get the fol
581594
In the following sections we describe the default implementations of input_fn, predict_fn, and output_fn. We describe the input arguments and expected return types of each, so you can define your own implementations.
582595

583596
Input processing
584-
''''''''''''''''
597+
""""""""""""""""
585598

586599
When an InvokeEndpoint operation is made against an Endpoint running a SageMaker MXNet model server, the model server receives two pieces of information:
587600

588-
- The request Content-Type, for example "application/json"
589-
- The request data body, a byte array
601+
- The request's content type, for example "application/json"
602+
- The request data body as a byte array
590603

591-
The SageMaker MXNet model server will invoke an ``input_fn`` function in your training script, passing in this information. If you define an ``input_fn`` function definition, it should return an object that can be passed to ``predict_fn`` and have the following signature:
604+
The SageMaker MXNet model server will invoke ``input_fn``, passing in this information. If you define an ``input_fn`` function definition, it should return an object that can be passed to ``predict_fn`` and have the following signature:
592605

593606
.. code:: python
594607
595-
def input_fn(request_body, request_content_type, model)
608+
def input_fn(request_body, request_content_type)
596609
597-
Where ``request_body`` is a byte buffer, ``request_content_type`` is a Python string, and model is the result of invoking ``model_fn``.
610+
Where ``request_body`` is a byte buffer and ``request_content_type`` is the content type of the request.
598611

599612
The SageMaker MXNet model server provides a default implementation of ``input_fn``. This function deserializes JSON or CSV encoded data into an MXNet ``NDArrayIter`` `(external API docs) <https://mxnet.incubator.apache.org/api/python/io.html#mxnet.io.NDArrayIter>`__ multi-dimensional array iterator. This works with the default ``predict_fn`` implementation, which expects an ``NDArrayIter`` as input.
600613

601-
Default json deserialization requires ``request_body`` contain a single json list. Sending multiple json objects within the same ``request_body`` is not supported. The list must have a dimensionality compatible with the MXNet ``net`` or ``Module`` object. Specifically, after the list is loaded, it's either padded or split to fit the first dimension of the model input shape. The list's shape must be identical to the model's input shape, for all dimensions after the first.
614+
Default JSON deserialization requires ``request_body`` contain a single json list. Sending multiple json objects within the same ``request_body`` is not supported. The list must have a dimensionality compatible with the MXNet ``net`` or ``Module`` object. Specifically, after the list is loaded, it's either padded or split to fit the first dimension of the model input shape. The list's shape must be identical to the model's input shape, for all dimensions after the first.
602615

603-
Default csv deserialization requires ``request_body`` contain one or more lines of CSV numerical data. The data is loaded into a two-dimensional array, where each line break defines the boundaries of the first dimension. This two-dimensional array is then re-shaped to be compatible with the shape expected by the model object. Specifically, the first dimension is kept unchanged, but the second dimension is reshaped to be consistent with the shape of all dimensions in the model, following the first dimension.
616+
Default CSV deserialization requires ``request_body`` contain one or more lines of CSV numerical data. The data is loaded into a two-dimensional array, where each line break defines the boundaries of the first dimension. This two-dimensional array is then re-shaped to be compatible with the shape expected by the model object. Specifically, the first dimension is kept unchanged, but the second dimension is reshaped to be consistent with the shape of all dimensions in the model, following the first dimension.
604617

605618
If you provide your own implementation of input_fn, you should abide by the ``input_fn`` signature. If you want to use this with the default
606-
``predict_fn``, then you should return an NDArrayIter. The NDArrayIter should have a shape identical to the shape of the model being predicted on. The example below shows a custom ``input_fn`` for preparing pickled numpy arrays.
619+
``predict_fn``, then you should return an ``NDArrayIter``. The ``NDArrayIter`` should have a shape identical to the shape of the model being predicted on. The example below shows a custom ``input_fn`` for preparing pickled numpy arrays.
607620

608621
.. code:: python
609622
610623
import numpy as np
611624
import mxnet as mx
612625
613-
def input_fn(request_body, request_content_type, model):
626+
def input_fn(request_body, request_content_type):
614627
"""An input_fn that loads a pickled numpy array"""
615628
if request_content_type == 'application/python-pickle':
616629
array = np.load(StringIO(request_body))
@@ -622,7 +635,7 @@ If you provide your own implementation of input_fn, you should abide by the ``in
622635
pass
623636
624637
Prediction
625-
''''''''''
638+
""""""""""
626639

627640
After the inference request has been deserialized by ``input_fn``, the SageMaker MXNet model server invokes ``predict_fn``. As with ``input_fn``, you can define your own ``predict_fn`` or use the SageMaker Mxnet default.
628641

@@ -655,9 +668,9 @@ If you implement your own prediction function, you should take care to ensure th
655668
``output_fn``, this should be an ``NDArrayIter``.
656669

657670
Output processing
658-
'''''''''''''''''
671+
"""""""""""""""""
659672

660-
After invoking ``predict_fn``, the model server invokes ``output_fn``, passing in the return-value from ``predict_fn`` and the InvokeEndpoint requested response content-type.
673+
After invoking ``predict_fn``, the model server invokes ``output_fn``, passing in the return value from ``predict_fn`` and the InvokeEndpoint requested response content type.
661674

662675
The ``output_fn`` has the following signature:
663676

@@ -666,10 +679,29 @@ The ``output_fn`` has the following signature:
666679
def output_fn(prediction, content_type)
667680
668681
Where ``prediction`` is the result of invoking ``predict_fn`` and
669-
``content_type`` is the InvokeEndpoint requested response content-type. The function should return a byte array of data serialized to content_type.
682+
``content_type`` is the InvokeEndpoint requested response content type. The function should return a byte array of data serialized to the expected content type.
670683

671684
The default implementation expects ``prediction`` to be an ``NDArray`` and can serialize the result to either JSON or CSV. It accepts response content types of "application/json" and "text/csv".
672685

686+
Using ``transform_fn``
687+
''''''''''''''''''''''
688+
689+
If you would rather not structure your code around the three methods described above, you can also define your own ``transform_fn`` to handle inference requests. This function has the following signature:
690+
691+
.. code:: python
692+
693+
def transform_fn(model, request_body, content_type, accept_type)
694+
695+
Where ``model`` is the model objected loaded by ``model_fn``, ``request_body`` is the data from the inference request, ``content_type`` is the content type of the request, and ``accept_type`` is the request content type for the response.
696+
697+
This one function should handle processing the input, performing a prediction, and processing the output.
698+
The return object should be one of the following:
699+
700+
- a tuple with two items: the response data and ``accept_type`` (the content type of the response data), or
701+
- a Flask response object: http://flask.pocoo.org/docs/1.0/api/#response-objects
702+
703+
You can find examples of hosting scripts using this structure in the example notebooks, such as the `mxnet_gluon_sentiment <https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/mxnet_gluon_sentiment/sentiment.py#L344-L387>`__ notebook.
704+
673705
Working with existing model data and training jobs
674706
--------------------------------------------------
675707

0 commit comments

Comments
 (0)