Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lightning_app/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Changed

-
- Add support for async predict method in PythonServer and remove torch context ([#16453](https://github.com/Lightning-AI/lightning/pull/16453))


### Deprecated
Expand Down
17 changes: 9 additions & 8 deletions src/lightning_app/components/serve/python_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
import asyncio
import base64
import os
import platform
Expand Down Expand Up @@ -252,19 +253,19 @@ def _get_sample_dict_from_datatype(datatype: Any) -> dict:
return out

def _attach_predict_fn(self, fastapi_app: FastAPI) -> None:
from torch import inference_mode, no_grad

input_type: type = self.configure_input_type()
output_type: type = self.configure_output_type()

device = _get_device()
context = no_grad if device.type == "mps" else inference_mode
def predict_fn_sync(request: input_type): # type: ignore
return self.predict(request)

def predict_fn(request: input_type): # type: ignore
with context():
return self.predict(request)
async def async_predict_fn(request: input_type): # type: ignore
return await self.predict(request)

fastapi_app.post("/predict", response_model=output_type)(predict_fn)
if asyncio.iscoroutinefunction(self.predict):
fastapi_app.post("/predict", response_model=output_type)(async_predict_fn)
else:
fastapi_app.post("/predict", response_model=output_type)(predict_fn_sync)

def get_code_sample(self, url: str) -> Optional[str]:
input_type: Any = self.configure_input_type()
Expand Down