a
    þd³  ã                   @   sD   d dl mZmZmZmZ d dlZd dlmZ G dd„ dejjƒZ	dS )é    )ÚAnyÚCallableÚDictÚTupleN)ÚTensorc                   @   s|   e Zd ZdZeeef dœdd„Zeeee	f eee	f f dœdd„Z
eeeeef dœdd	„Zeeef dœd
d„ZdS )ÚServableModuleaX  The ServableModule provides a simple API to make your model servable.

    .. warning::

        This is currently an experimental feature and API changes are to be expected.

    Here is an example of how to use the ``ServableModule`` module.

    .. code-block:: python

        from typing import Dict, Any, Callable

        import torch

        from pytorch_lightning import Trainer
        from pytorch_lightning.demos.boring_classes import BoringModel
        from pytorch_lightning.serve.servable_module_validator import ServableModule, ServableModuleValidator


        class ServableBoringModel(BoringModel, ServableModule):
            def configure_payload(self) -> Dict[str, Any]:
                return {"body": {"x": list(range(32))}}

            def configure_serialization(self) -> Tuple[Dict[str, Callable], Dict[str, Callable]]:
                def deserialize(x):
                    return torch.tensor(x, dtype=torch.float)

                def serialize(x):
                    return x.tolist()

                return {"x": deserialize}, {"output": serialize}

            def serve_step(self, x: torch.Tensor) -> Dict[str, torch.Tensor]:
                return {"output": torch.tensor([0, 1])}

            def configure_response(self):
                return {"output": [0, 1]}


        serve_cb = ServableModuleValidator()
        trainer = Trainer(
            max_epochs=1,
            limit_train_batches=2,
            limit_val_batches=0,
            callbacks=[serve_cb],
        )
        trainer.fit(ServableBoringModel())
        assert serve_cb.resp.json() == {"output": [0, 1]}
    )Úreturnc                 C   s   dS )z*Returns a request payload as a dictionary.N© ©Úselfr	   r	   úp/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/serve/servable_module.pyÚconfigure_payload;   s    z ServableModule.configure_payloadc                 C   s   dS )aÂ  Returns a tuple of dictionaries.

        The first dictionary contains the name of the ``serve_step`` input variables name as its keys
        and the associated de-serialization function (e.g function to convert a payload to tensors).

        The second dictionary contains the name of the ``serve_step`` output variables name as its keys
        and the associated serialization function (e.g function to convert a tensors into payload).
        Nr	   r
   r	   r	   r   Úconfigure_serialization?   s    	z&ServableModule.configure_serialization)ÚargsÚkwargsr   c                 O   s   dS )a0  
        Returns the predictions of your model as a dictionary.

        .. code-block:: python

            def serve_step(self, x: torch.Tensor) -> Dict[str, torch.Tensor]:
                return {"predictions": self(x)}

        Args:
            args: The output from de-serializer functions provided by the ``configure_serialization`` hook.
            kwargs: The keyword output of the de-serializer functions provided by the ``configure_serialization`` hook.

        Return:
            - ``dict`` - A dictionary with their associated tensors.
        Nr	   )r   r   r   r	   r	   r   Ú
serve_stepJ   s    zServableModule.serve_stepc                 C   s   dS )z3Returns a response to validate the server response.Nr	   r
   r	   r	   r   Úconfigure_response\   s    z!ServableModule.configure_responseN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   r   r   r   r   r   r   r   r	   r	   r	   r   r      s
   2&r   )
Útypingr   r   r   r   Ztorchr   ÚnnÚModuler   r	   r	   r	   r   Ú<module>   s   