Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix import torch #15849

Merged
merged 13 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 3 additions & 1 deletion .github/workflows/ci-pkg-install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: DocTests actions
working-directory: .actions/
run: |
pip install pytest -q
pip install -q pytest
python -m pytest setup_tools.py

- run: python -c "print('NB_DIRS=' + str(2 if '${{ matrix.pkg-name }}' == 'pytorch' else 1))" >> $GITHUB_ENV
Expand All @@ -69,5 +69,7 @@ jobs:
env:
PY_IGNORE_IMPORTMISMATCH: 1
run: |
pip install -q "pytest-doctestplus>=0.9.0"
pip list
PKG_NAME=$(python -c "print({'app': 'lightning_app', 'lite': 'lightning_lite', 'pytorch': 'pytorch_lightning', 'lightning': 'lightning'}['${{matrix.pkg-name}}'])")
python -m pytest src/${PKG_NAME} --ignore-glob="**/cli/*-template/**"
1 change: 1 addition & 0 deletions requirements/app/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ codecov==2.1.12
pytest==7.2.0
pytest-timeout==2.1.0
pytest-cov==4.0.0
pytest-doctestplus>=0.9.0
playwright==1.27.1
httpx
trio<0.22.0
Expand Down
13 changes: 10 additions & 3 deletions src/lightning_app/components/serve/python_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pathlib import Path
from typing import Any, Dict, Optional

import torch
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
Expand All @@ -13,16 +12,21 @@
from lightning_app.core.queues import MultiProcessQueue
from lightning_app.core.work import LightningWork
from lightning_app.utilities.app_helpers import Logger
from lightning_app.utilities.imports import _is_torch_available, requires
from lightning_app.utilities.proxies import _proxy_setattr, unwrap, WorkRunExecutor, WorkStateObserver

logger = Logger(__name__)

# Skip doctests if requirements aren't available
if not _is_torch_available():
__doctest_skip__ = ["PythonServer", "PythonServer.*"]


class _PyTorchSpawnRunExecutor(WorkRunExecutor):

"""This Executor enables to move PyTorch tensors on GPU.

Without this executor, it woud raise the following expection:
Without this executor, it would raise the following exception:
RuntimeError: Cannot re-initialize CUDA in forked subprocess.
To use CUDA with multiprocessing, you must use the 'spawn' start method
"""
Expand Down Expand Up @@ -86,6 +90,7 @@ def _get_sample_data() -> Dict[Any, Any]:


class PythonServer(LightningWork, abc.ABC):
@requires("torch")
def __init__( # type: ignore
self,
host: str = "127.0.0.1",
Expand Down Expand Up @@ -199,11 +204,13 @@ 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

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

def predict_fn(request: input_type): # type: ignore
with torch.inference_mode():
with inference_mode():
return self.predict(request)

fastapi_app.post("/predict", response_model=output_type)(predict_fn)
Expand Down
6 changes: 4 additions & 2 deletions src/lightning_app/utilities/name_generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from random import choice, randint

__doctest_skip__ = ["get_unique_name"]

_adjectives = [
# Appearance, sound, smell...
"acrid",
Expand Down Expand Up @@ -1334,9 +1336,9 @@ def get_unique_name():

Examples
--------
>>> get_unique_name() # doctest: +SKIP
>>> get_unique_name()
'focused-turing-23'
>>> get_unique_name() # doctest: +SKIP
>>> get_unique_name()
'thirsty-allen-9200'
"""
adjective, surname, i = choice(_adjectives), choice(_surnames), randint(0, 9999)
Expand Down