Skip to content

Commit 4092b67

Browse files
Update EI section in regards to model loading within MXNet README (#618)
1 parent 19e7aad commit 4092b67

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

doc/using_mxnet.rst

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
Using MXNet Estimators and Models
22
---------------------------------
33

4+
-----------------
5+
Table of Contents
6+
-----------------
47
.. contents::
5-
8+
:local:
69

710

811
With MXNet estimators, you can train and host MXNet models on Amazon SageMaker.
@@ -530,11 +533,31 @@ The following code-snippet shows an example custom ``model_fn`` implementation.
530533
"""
531534
Load the gluon model. Called once when hosting service starts.
532535
:param: model_dir The directory where model files are stored.
533-
:return: a model (in this case a Gluon network)
534-
"""
535-
net = models.get_model('resnet34_v2', ctx=mx.cpu(), pretrained=False, classes=10)
536-
net.load_params('%s/model.params' % model_dir, ctx=mx.cpu())
537-
return net
536+
:return: a model (in this case a Gluon network)
537+
"""
538+
net = models.get_model('resnet34_v2', ctx=mx.cpu(), pretrained=False, classes=10)
539+
net.load_params('%s/model.params' % model_dir, ctx=mx.cpu())
540+
return net
541+
542+
MXNet on SageMaker has support for `Elastic Inference <https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html>`__, which allows for inference acceleration to a hosted endpoint for a fraction of the cost of using a full GPU instance. In order to load and serve your MXNet model through Amazon Elastic Inference, the MXNet context passed to your MXNet Symbol or Module object within your ``model_fn`` needs to be set to ``eia``, as shown `here <https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-mxnet-elastic-inference.html#ei-mxnet>`__.
543+
544+
Based on the example above, the following code-snippet shows an example custom ``model_fn`` implementation, which enables loading and serving our MXNet model through Amazon Elastic Inference.
545+
546+
.. code:: python
547+
548+
def model_fn(model_dir):
549+
"""
550+
Load the gluon model in an Elastic Inference context. Called once when hosting service starts.
551+
:param: model_dir The directory where model files are stored.
552+
:return: a model (in this case a Gluon network)
553+
"""
554+
net = models.get_model('resnet34_v2', ctx=mx.eia(), pretrained=False, classes=10)
555+
net.load_params('%s/model.params' % model_dir, ctx=mx.eia())
556+
return net
557+
558+
The `default_model_fn <https://github.com/aws/sagemaker-mxnet-container/pull/55/files#diff-aabf018d906ed282a3c738377d19a8deR71>`__ will load and serve your model through Elastic Inference, if applicable, within the SageMaker MXNet containers.
559+
560+
For more information on how to enable MXNet to interact with Amazon Elastic Inference, see `Use Elastic Inference with MXNet <https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-mxnet-elastic-inference.html>`__.
538561

539562
Model serving
540563
^^^^^^^^^^^^^

src/sagemaker/mxnet/README.rst

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ After calling ``fit``, you can call ``deploy`` on an ``MXNet`` Estimator to crea
487487
488488
You use the SageMaker MXNet model server to host your MXNet model when you call ``deploy`` on an ``MXNet`` Estimator. The model server runs inside a SageMaker Endpoint, which your call to ``deploy`` creates. You can access the name of the Endpoint by the ``name`` property on the returned ``Predictor``.
489489

490-
MXNet on SageMaker has support for `Elastic Inference <https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html>`_, which allows for inference acceleration to a hosted endpoint for a fraction of the cost of using a full GPU instance. In order to attach an Elastic Inference accelerator to your endpoint provide the accelerator type to ``accelerator_type`` to your ``deploy`` call.
490+
MXNet on SageMaker has support for `Elastic Inference <https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html>`__, which allows for inference acceleration to a hosted endpoint for a fraction of the cost of using a full GPU instance. In order to attach an Elastic Inference accelerator to your endpoint provide the accelerator type to ``accelerator_type`` to your ``deploy`` call.
491491

492492
.. code:: python
493493
@@ -525,11 +525,31 @@ The following code-snippet shows an example custom ``model_fn`` implementation.
525525
"""
526526
Load the gluon model. Called once when hosting service starts.
527527
:param: model_dir The directory where model files are stored.
528-
:return: a model (in this case a Gluon network)
529-
"""
530-
net = models.get_model('resnet34_v2', ctx=mx.cpu(), pretrained=False, classes=10)
531-
net.load_params('%s/model.params' % model_dir, ctx=mx.cpu())
532-
return net
528+
:return: a model (in this case a Gluon network)
529+
"""
530+
net = models.get_model('resnet34_v2', ctx=mx.cpu(), pretrained=False, classes=10)
531+
net.load_params('%s/model.params' % model_dir, ctx=mx.cpu())
532+
return net
533+
534+
MXNet on SageMaker has support for `Elastic Inference <https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html>`__, which allows for inference acceleration to a hosted endpoint for a fraction of the cost of using a full GPU instance. In order to load and serve your MXNet model through Amazon Elastic Inference, the MXNet context passed to your MXNet Symbol or Module object within your ``model_fn`` needs to be set to ``eia``, as shown `here <https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-mxnet-elastic-inference.html#ei-mxnet>`__.
535+
536+
Based on the example above, the following code-snippet shows an example custom ``model_fn`` implementation, which enables loading and serving our MXNet model through Amazon Elastic Inference.
537+
538+
.. code:: python
539+
540+
def model_fn(model_dir):
541+
"""
542+
Load the gluon model in an Elastic Inference context. Called once when hosting service starts.
543+
:param: model_dir The directory where model files are stored.
544+
:return: a model (in this case a Gluon network)
545+
"""
546+
net = models.get_model('resnet34_v2', ctx=mx.eia(), pretrained=False, classes=10)
547+
net.load_params('%s/model.params' % model_dir, ctx=mx.eia())
548+
return net
549+
550+
The `default_model_fn <https://github.com/aws/sagemaker-mxnet-container/pull/55/files#diff-aabf018d906ed282a3c738377d19a8deR71>`__ will load and serve your model through Elastic Inference, if applicable, within the SageMaker MXNet containers.
551+
552+
For more information on how to enable MXNet to interact with Amazon Elastic Inference, see `Use Elastic Inference with MXNet <https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-mxnet-elastic-inference.html>`__.
533553

534554
Model serving
535555
^^^^^^^^^^^^^

0 commit comments

Comments
 (0)