2020-04-28 22:24:55 -07:00
|
|
|
.. _serve-pytorch-tutorial:
|
|
|
|
|
|
|
|
PyTorch Tutorial
|
|
|
|
================
|
|
|
|
|
|
|
|
In this guide, we will load and serve a PyTorch Resnet Model.
|
|
|
|
In particular, we show:
|
|
|
|
|
|
|
|
- How to load the model from PyTorch's pre-trained modelzoo.
|
|
|
|
- How to parse the JSON request, transform the payload and evaluated in the model.
|
|
|
|
|
2020-05-27 09:03:28 -07:00
|
|
|
Please see the :doc:`../key-concepts` to learn more general information about Ray Serve.
|
2020-04-28 22:24:55 -07:00
|
|
|
|
2020-05-19 19:13:54 -07:00
|
|
|
This tutorial requires Pytorch and Torchvision installed in your system. Ray Serve
|
2020-10-16 01:00:48 +01:00
|
|
|
is framework agnostic and works with any version of PyTorch.
|
2020-04-28 22:24:55 -07:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
pip install torch torchvision
|
|
|
|
|
2020-05-19 19:13:54 -07:00
|
|
|
Let's import Ray Serve and some other helpers.
|
2020-04-28 22:24:55 -07:00
|
|
|
|
|
|
|
.. literalinclude:: ../../../../python/ray/serve/examples/doc/tutorial_pytorch.py
|
|
|
|
:start-after: __doc_import_begin__
|
|
|
|
:end-before: __doc_import_end__
|
|
|
|
|
|
|
|
|
|
|
|
Services are just defined as normal classes with ``__init__`` and ``__call__`` methods.
|
|
|
|
The ``__call__`` method will be invoked per request.
|
|
|
|
|
|
|
|
.. literalinclude:: ../../../../python/ray/serve/examples/doc/tutorial_pytorch.py
|
|
|
|
:start-after: __doc_define_servable_begin__
|
|
|
|
:end-before: __doc_define_servable_end__
|
|
|
|
|
2020-05-19 19:13:54 -07:00
|
|
|
Now that we've defined our services, let's deploy the model to Ray Serve. We will
|
2020-04-28 22:24:55 -07:00
|
|
|
define an :ref:`endpoint <serve-endpoint>` for the route representing the digit classifier task, a
|
|
|
|
:ref:`backend <serve-backend>` correspond the physical implementation, and connect them together.
|
|
|
|
|
|
|
|
.. literalinclude:: ../../../../python/ray/serve/examples/doc/tutorial_pytorch.py
|
|
|
|
:start-after: __doc_deploy_begin__
|
|
|
|
:end-before: __doc_deploy_end__
|
|
|
|
|
|
|
|
Let's query it!
|
|
|
|
|
|
|
|
.. literalinclude:: ../../../../python/ray/serve/examples/doc/tutorial_pytorch.py
|
|
|
|
:start-after: __doc_query_begin__
|
|
|
|
:end-before: __doc_query_end__
|