Skip to content

Commit 5f5d99e

Browse files
Relax app dependencies for lightning | breaking imports from L (#18386)
* make App conditional import * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ci: min req. * data + store * typo * module_available * extra * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * typo * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * envs * doctest * more * docs * ** * rm * -f * Apply suggestions from code review * docs * Revert "docs" This reverts commit 20e8e6b. * Apply suggestions from code review * warnings * breaking * ... * fixing source * fixing examples * fixing tests * fixing docs * fixing source * fix mock * fix trainer --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d3ee410 commit 5f5d99e

File tree

135 files changed

+606
-582
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+606
-582
lines changed

.actions/assistant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
"requirements/pytorch/examples.txt",
3737
),
3838
"app": (
39-
"requirements/app/base.txt",
40-
"requirements/app/ui.txt",
39+
"requirements/app/app.txt",
4140
"requirements/app/cloud.txt",
41+
"requirements/app/ui.txt",
4242
),
4343
"fabric": (
4444
"requirements/fabric/base.txt",

.github/actions/pkg-install/action.yml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ inputs:
88
pkg-name:
99
description: Package name to import
1010
required: true
11+
pkg-extra:
12+
description: argument for install extra
13+
required: false
14+
default: ""
1115
pip-flags:
1216
description: Additional pip install flags
1317
required: false
@@ -19,24 +23,32 @@ runs:
1923
- name: Choose package import
2024
working-directory: ${{ inputs.pkg-folder }}
2125
run: |
22-
ls -l
23-
python -c "print('PKG_IMPORT=' + {'app': 'lightning_app', 'fabric': 'lightning_fabric', 'pytorch': 'pytorch_lightning'}.get('${{matrix.pkg-name}}', 'lightning'))" >> $GITHUB_ENV
24-
python -c "import glob ; ls = glob.glob('*.tar.gz') ; print('PKG_SOURCE=' + ls[0])" >> $GITHUB_ENV
25-
python -c "import glob ; ls = glob.glob('*.whl') ; print('PKG_WHEEL=' + ls[0])" >> $GITHUB_ENV
26-
shell: bash
26+
import os, glob
27+
28+
lut = {'app': 'lightning_app', 'fabric': 'lightning_fabric', 'pytorch': 'pytorch_lightning'}
29+
act_pkg = lut.get('${{inputs.pkg-name}}', 'lightning')
30+
pkg_sdist = glob.glob('*.tar.gz')[0]
31+
pkg_wheel = glob.glob('*.whl')[0]
32+
extra = '${{inputs.pkg-extra}}'
33+
extra = f'[{extra}]' if extra else ''
34+
35+
envs = [f'PKG_IMPORT={act_pkg}', f'PKG_SOURCE={pkg_sdist}', f'PKG_WHEEL={pkg_wheel}', f'PKG_EXTRA={extra}']
36+
with open(os.getenv('GITHUB_ENV'), "a") as gh_env:
37+
gh_env.write(os.linesep.join(envs))
38+
shell: python
2739

2840
- name: Install package - wheel
2941
working-directory: ${{ inputs.pkg-folder }}
3042
run: |
31-
pip install ${PKG_WHEEL} ${{ inputs.pip-flags }}
43+
pip install "${PKG_WHEEL}${PKG_EXTRA}" ${{ inputs.pip-flags }}
3244
pip list | grep lightning
3345
python -c "import ${{ env.PKG_IMPORT }}; print(${{ env.PKG_IMPORT }}.__version__)"
3446
shell: bash
3547

3648
- name: Install package - archive
3749
working-directory: ${{ inputs.pkg-folder }}
3850
run: |
39-
pip install ${PKG_SOURCE} ${{ inputs.pip-flags }}
51+
pip install "${PKG_SOURCE}${PKG_EXTRA}" ${{ inputs.pip-flags }}
4052
pip list | grep lightning
4153
python -c "import ${{ env.PKG_IMPORT }}; print(${{ env.PKG_IMPORT }}.__version__)"
4254
shell: bash

.github/workflows/ci-pkg-install.yml

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ jobs:
4242
strategy:
4343
fail-fast: false
4444
matrix:
45-
os: [ubuntu-22.04, macOS-12, windows-2022]
45+
os: ["ubuntu-22.04", "macOS-12", "windows-2022"]
4646
pkg-name: ["app", "fabric", "pytorch", "lightning", "notset"]
4747
python-version: ["3.8" , "3.11"]
4848
# TODO: add also install from source
49+
include:
50+
- {os: "macOS-12", pkg-name: "lightning", python-version: "3.9", pkg-extra: "app"}
51+
- {os: "macOS-12", pkg-name: "notset", python-version: "3.9", pkg-extra: "app"}
52+
- {os: "ubuntu-22.04", pkg-name: "lightning", python-version: "3.9", pkg-extra: "app"}
53+
- {os: "ubuntu-22.04", pkg-name: "notset", python-version: "3.9", pkg-extra: "app"}
4954
steps:
5055
- uses: actions/checkout@v3
5156
- uses: actions/setup-python@v4
@@ -56,19 +61,23 @@ jobs:
5661
name: dist-packages-${{ github.sha }}
5762
path: dist
5863

59-
- run: |
64+
- name: Set package dir
65+
run: |
6066
python -c "print('PKG_DIR=' + {'notset': 'lightning'}.get('${{matrix.pkg-name}}', '${{matrix.pkg-name}}'))" >> $GITHUB_ENV
6167
- name: Install package - wheel & archive
6268
uses: ./.github/actions/pkg-install
6369
with:
6470
pkg-folder: dist/${{ env.PKG_DIR }}
6571
pkg-name: ${{ matrix.pkg-name }}
72+
pkg-extra: ${{ matrix.pkg-extra }}
6673

6774
- name: Run CLI (via python)
68-
if: ${{ matrix.pkg-name == 'lightning' || matrix.pkg-name == 'notset' }}
75+
if: ${{ (matrix.pkg-name == 'lightning' || matrix.pkg-name == 'notset') && matrix.pkg-extra == 'app' }}
6976
run: python -m lightning --version
7077
- name: Run CLI (direct bash)
71-
if: ${{ matrix.pkg-name == 'lightning' || matrix.pkg-name == 'notset' || matrix.pkg-name == 'app' }}
78+
if: |
79+
((matrix.pkg-name == 'lightning' || matrix.pkg-name == 'notset') && matrix.pkg-extra == 'app') ||
80+
matrix.pkg-name == 'app'
7281
run: lightning --version
7382

7483
- name: DocTests actions
@@ -77,25 +86,33 @@ jobs:
7786
pip install -q pytest -r requirements.txt
7887
python -m pytest assistant.py
7988
80-
- name: build source
81-
run: python setup.py build
8289
- name: Adjust code for standalone
8390
if: contains(fromJSON('["app", "fabric", "pytorch"]'), matrix.pkg-name)
8491
run: |
8592
python .actions/assistant.py copy_replace_imports --source_dir="./src" \
8693
--source_import="lightning.pytorch,lightning.fabric,lightning.app" \
8794
--target_import="pytorch_lightning,lightning_fabric,lightning_app"
88-
rm src/lightning/__*.py
8995
- name: Rename src folders
9096
working-directory: src/
91-
run: mv lightning lit
97+
run: |
98+
python -c "n = '${{matrix.pkg-name}}' ; n = n if n in ('app', 'fabric', 'pytorch') else '' ; print('PKG_NAME=' + n)" >> $GITHUB_ENV
99+
rm -f ./*/__*.py
100+
rm -f ./**/__*.py
101+
mv lightning lit # rename lightning folder to prevent accidental local imports
102+
- name: drop App doctest
103+
if: ${{ (matrix.pkg-name == 'lightning' || matrix.pkg-name == 'notset') && matrix.pkg-extra != 'app' }}
104+
working-directory: src/lit
105+
run: |
106+
rm -rf app
107+
rm -rf data
108+
rm -rf store
109+
- name: Install pytest doctest extension
110+
run: |
111+
pip install -q "pytest-doctestplus>=0.9.0"
112+
pip list
113+
92114
- name: DocTest package
93115
env:
94116
LIGHTING_TESTING: 1 # path for require wrapper
95117
PY_IGNORE_IMPORTMISMATCH: 1
96-
run: |
97-
pip install -q "pytest-doctestplus>=0.9.0"
98-
pip list
99-
PKG_NAME=$(python -c "n = '${{matrix.pkg-name}}'; print(n if n in ('app', 'fabric', 'pytorch') else '')")
100-
echo $PKG_NAME
101-
python -m pytest src/lit/${PKG_NAME} --ignore-glob="**/cli/*-template/**" --doctest-plus
118+
run: python -m pytest src/lit/${PKG_NAME} --ignore-glob="**/cli/*-template/**" --doctest-plus

.github/workflows/ci-tests-store.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
- name: Install package & dependencies
6969
run: |
7070
python -m pip install -q pip -U
71-
pip install -e ".[store-test]" "pytest-timeout" -U -f ${TORCH_URL} --prefer-binary
71+
pip install -e ".[store,store-test]" "pytest-timeout" -U -f ${TORCH_URL} --prefer-binary
7272
pip list
7373
7474
- name: Testing Store

.github/workflows/docs-build.yml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ env:
3333
FREEZE_REQUIREMENTS: "1"
3434
TORCH_URL: "https://download.pytorch.org/whl/cpu/torch_stable.html"
3535
PYPI_CACHE_DIR: "_pip-wheels"
36+
PYPI_LOCAL_DIR: "pypi_pkgs/"
3637

3738
jobs:
3839
docs-checks:
@@ -63,12 +64,12 @@ jobs:
6364
unzip -qq awscli.zip
6465
bash ./aws/install
6566
aws --version
66-
- run: aws s3 sync s3://sphinx-packages/ pypi_pkgs/
67+
- run: aws s3 sync s3://sphinx-packages/ ${PYPI_LOCAL_DIR}
68+
if: ${{ matrix.pkg-name != 'pytorch' }}
6769
env:
6870
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
6971
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }}
7072
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}
71-
if: ${{ matrix.pkg-name != 'pytorch' }}
7273

7374
- name: pip wheels cache
7475
uses: actions/cache/restore@v3
@@ -83,11 +84,11 @@ jobs:
8384
sudo apt-get install -y pandoc texlive-latex-extra dvipng texlive-pictures
8485
- name: Install package & dependencies
8586
run: |
86-
mkdir -p $PYPI_CACHE_DIR # in case cache was not hit
87-
ls -lh $PYPI_CACHE_DIR
88-
mkdir -p pypi_pkgs # in case template is not pulled
89-
pip install .[extra,cloud,ui] -U -r requirements/${{ matrix.pkg-name }}/docs.txt \
90-
-f pypi_pkgs/ -f $PYPI_CACHE_DIR -f ${TORCH_URL}
87+
mkdir -p ${PYPI_CACHE_DIR} # in case cache was not hit
88+
ls -lh ${PYPI_CACHE_DIR}
89+
mkdir -p ${PYPI_LOCAL_DIR} # in case template is not pulled
90+
pip install .[app,extra,cloud,ui] -U -r requirements/${{ matrix.pkg-name }}/docs.txt \
91+
-f ${PYPI_LOCAL_DIR} -f ${PYPI_CACHE_DIR} -f ${TORCH_URL}
9192
pip list
9293
9394
- name: Test Documentation
@@ -128,12 +129,12 @@ jobs:
128129
unzip -qq awscli.zip
129130
bash ./aws/install
130131
aws --version
131-
- run: aws s3 sync s3://sphinx-packages/ pypi_pkgs/
132+
- run: aws s3 sync s3://sphinx-packages/ ${PYPI_LOCAL_DIR}
133+
if: ${{ matrix.pkg-name != 'pytorch' }}
132134
env:
133135
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
134136
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }}
135137
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}
136-
if: ${{ matrix.pkg-name != 'pytorch' }}
137138

138139
- name: pip wheels cache
139140
uses: actions/cache/restore@v3
@@ -148,12 +149,12 @@ jobs:
148149
sudo apt-get install -y pandoc texlive-latex-extra dvipng texlive-pictures
149150
- name: Install package & dependencies
150151
run: |
151-
mkdir -p $PYPI_CACHE_DIR # in case cache was not hit
152-
ls -lh $PYPI_CACHE_DIR
153-
mkdir -p pypi_pkgs # in case template is not pulled
152+
mkdir -p ${PYPI_CACHE_DIR} # in case cache was not hit
153+
ls -lh ${PYPI_CACHE_DIR}
154+
mkdir -p ${PYPI_LOCAL_DIR} # in case template is not pulled
154155
pip --version
155-
pip install . -U -r requirements/${{ matrix.pkg-name }}/docs.txt \
156-
-f pypi_pkgs/ -f $PYPI_CACHE_DIR -f ${TORCH_URL}
156+
pip install .[app] -U -r requirements/${{ matrix.pkg-name }}/docs.txt \
157+
-f ${PYPI_LOCAL_DIR} -f ${PYPI_CACHE_DIR} -f ${TORCH_URL}
157158
pip list
158159
shell: bash
159160

docs/source-app/code_samples/basics/0.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import lightning as L
1+
from lightning.app import LightningWork
22

33

4-
class ExampleWork(L.LightningWork):
4+
class ExampleWork(LightningWork):
55
def run(self, *args, **kwargs):
66
print(f"I received the following props: args: {args} kwargs: {kwargs}")
77

docs/source-app/code_samples/basics/1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import lightning as L
1+
from lightning.app import LightningWork
22

33

4-
class ExampleWork(L.LightningWork):
4+
class ExampleWork(LightningWork):
55
def __init__(self):
66
super().__init__(cache_calls=False)
77

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import lightning as L
1+
from lightning.app import LightningFlow, LightningApp, CloudCompute
22
from lightning.app.components import TracerPythonScript
33

44

5-
class RootFlow(L.LightningFlow):
5+
class RootFlow(LightningFlow):
66
def __init__(self):
77
super().__init__()
88
self.runner = TracerPythonScript(
99
"train.py",
10-
cloud_compute=L.CloudCompute("gpu"),
10+
cloud_compute=CloudCompute("gpu"),
1111
)
1212

1313
def run(self):
1414
self.runner.run()
1515

1616

17-
app = L.LightningApp(RootFlow())
17+
app = LightningApp(RootFlow())

docs/source-app/code_samples/quickstart/app/app_0.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import lightning as L
1+
from lightning.app import LightningWork, LightningFlow, LightningApp
22
from docs.quickstart.app_02 import HourLongWork
33

44

5-
class RootFlow(L.LightningFlow):
6-
def __init__(self, child_work_1: L.LightningWork, child_work_2: L.LightningWork):
5+
class RootFlow(LightningFlow):
6+
def __init__(self, child_work_1: LightningWork, child_work_2: LightningWork):
77
super().__init__()
88
self.child_work_1 = child_work_1
99
self.child_work_2 = child_work_2
@@ -18,4 +18,4 @@ def run(self):
1818
print("1 hour later `child_work_2` started!")
1919

2020

21-
app = L.LightningApp(RootFlow(HourLongWork(parallel=True), HourLongWork(parallel=True)))
21+
app = LightningApp(RootFlow(HourLongWork(parallel=True), HourLongWork(parallel=True)))

docs/source-app/code_samples/quickstart/app/app_1.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
from flash.core.data.utils import download_data
33
from flash.image import ImageClassificationData, ImageClassifier
44

5-
import lightning as L
6-
from pytorch_lightning.callbacks import ModelCheckpoint
5+
from lightning.app import LightningWork, LightningFlow, LightningApp, CloudCompute
6+
from lightning.pytorch.callbacks import ModelCheckpoint
77

88

99
# Step 1: Create a training LightningWork component that gets a backbone as input
1010
# and saves the best model and its score
11-
class ImageClassifierTrainWork(L.LightningWork):
12-
def __init__(self, max_epochs: int, backbone: str, cloud_compute: L.CloudCompute):
11+
class ImageClassifierTrainWork(LightningWork):
12+
def __init__(self, max_epochs: int, backbone: str, cloud_compute: CloudCompute):
1313
# parallel is set to True to run asynchronously
1414
super().__init__(parallel=True, cloud_compute=cloud_compute)
1515
# Number of epochs to run
@@ -44,7 +44,7 @@ def run(self, train_folder):
4444

4545

4646
# Step 2: Create a serving LightningWork component that gets a model input and serves it
47-
class ImageClassifierServeWork(L.LightningWork):
47+
class ImageClassifierServeWork(LightningWork):
4848
def run(self, best_model_path: str):
4949
# Load the model from the model path
5050
model = ImageClassifier.load_from_checkpoint(best_model_path)
@@ -53,7 +53,7 @@ def run(self, best_model_path: str):
5353

5454
# Step 3: Create a root LightningFlow component that gets number of epochs and a path to
5555
# a dataset as inputs, initialize 2 training components and serves the best model
56-
class RootFlow(L.LightningFlow):
56+
class RootFlow(LightningFlow):
5757
def __init__(self, max_epochs: int, data_dir: str):
5858
super().__init__()
5959
self.data_dir = data_dir
@@ -89,4 +89,4 @@ def run(self):
8989
download_data("https://pl-flash-data.s3.amazonaws.com/hymenoptera_data.zip", "./data")
9090

9191
# Initialize your Lightning app with 5 epochs
92-
app = L.LightningApp(RootFlow(5, "./data/hymenoptera_data"))
92+
app = LightningApp(RootFlow(5, "./data/hymenoptera_data"))

docs/source-app/code_samples/quickstart/app_01.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import lightning as L
1+
from lightning.app import LightningWork, LightningFlow, LightningApp
22
from lightning.app.utilities.app_helpers import pretty_state
33

44

5-
class Work(L.LightningWork):
5+
class Work(LightningWork):
66
def __init__(self):
77
super().__init__(cache_calls=False)
88
# Attributes are registered automatically in the state.
@@ -13,7 +13,7 @@ def run(self):
1313
self.counter += 1
1414

1515

16-
class Flow(L.LightningFlow):
16+
class Flow(LightningFlow):
1717
def __init__(self):
1818
super().__init__()
1919
self.w = Work()
@@ -24,4 +24,4 @@ def run(self):
2424
self.w.run()
2525

2626

27-
app = L.LightningApp(Flow())
27+
app = LightningApp(Flow())

docs/source-app/code_samples/quickstart/app_02.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from time import sleep
22

3-
import lightning as L
3+
from lightning.app import LightningWork, LightningFlow, LightningApp
44

55

66
# This work takes an hour to run
7-
class HourLongWork(L.LightningWork):
7+
class HourLongWork(LightningWork):
88
def __init__(self, parallel: bool = False):
99
super().__init__(parallel=parallel)
1010
self.progress = 0.0
@@ -16,8 +16,8 @@ def run(self):
1616
sleep(1)
1717

1818

19-
class RootFlow(L.LightningFlow):
20-
def __init__(self, child_work: L.LightningWork):
19+
class RootFlow(LightningFlow):
20+
def __init__(self, child_work: LightningWork):
2121
super().__init__()
2222
self.child_work = child_work
2323

@@ -29,4 +29,4 @@ def run(self):
2929
print("1 hour later!")
3030

3131

32-
app = L.LightningApp(RootFlow(HourLongWork()))
32+
app = LightningApp(RootFlow(HourLongWork()))

0 commit comments

Comments
 (0)