-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add --app_args support from the CLI #13625
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f867f15
wip
tchaton 3d85038
wip
tchaton e0c99ce
wip
tchaton 0ba50bf
update
tchaton a6a34c3
Update examples/app_argparse/app.py
tchaton 5c6927f
update
tchaton 050afaf
Merge branch 'add_app_args' of https://github.com/Lightning-AI/lightn…
tchaton 4d337f8
update
tchaton 732ed78
update
tchaton ef299f4
Merge branch 'master' into add_app_args
tchaton 6ac7802
update
tchaton a5da235
Merge branch 'add_app_args' of https://github.com/Lightning-AI/lightn…
tchaton 6d134fc
update
tchaton 2357629
Merge branch 'master' into add_app_args
tchaton 4595f66
Merge branch 'master' into add_app_args
tchaton c5210aa
Merge branch 'master' into add_app_args
tchaton 3d17cf0
update
tchaton f58d974
Merge branch 'add_app_args' of https://github.com/Lightning-AI/lightn…
tchaton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import argparse | ||
|
|
||
| import lightning as L | ||
|
|
||
|
|
||
| class Work(L.LightningWork): | ||
| def __init__(self, cloud_compute): | ||
| super().__init__(cloud_compute=cloud_compute) | ||
|
|
||
| def run(self): | ||
| pass | ||
|
|
||
|
|
||
| class Flow(L.LightningFlow): | ||
| def __init__(self, cloud_compute): | ||
| super().__init__() | ||
| self.work = Work(cloud_compute) | ||
|
|
||
| def run(self): | ||
| assert self.work.cloud_compute.name == "gpu", self.work.cloud_compute.name | ||
| self._exit() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--use_gpu", action="store_true", default=False, help="Whether to use GPU in the cloud") | ||
| hparams = parser.parse_args() | ||
| app = L.LightningApp(Flow(L.CloudCompute("gpu" if hparams.use_gpu else "cpu"))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import os | ||
| import sys | ||
|
|
||
| from lightning_app import _PACKAGE_ROOT | ||
| from lightning_app.testing.testing import application_testing | ||
| from lightning_app.utilities.load_app import _patch_sys_argv | ||
|
|
||
|
|
||
| def test_app_argparse_example(): | ||
| original_argv = sys.argv | ||
|
|
||
| command_line = [ | ||
| os.path.join(os.path.dirname(os.path.dirname(_PACKAGE_ROOT)), "examples/app_argparse/app.py"), | ||
| "--app_args", | ||
| "--use_gpu", | ||
| "--without-server", | ||
| ] | ||
| result = application_testing(command_line=command_line) | ||
| assert result.exit_code == 0, result.__dict__ | ||
| assert sys.argv == original_argv | ||
|
|
||
|
|
||
| def test_patch_sys_argv(): | ||
| original_argv = sys.argv | ||
|
|
||
| sys.argv = expected = ["lightning", "run", "app", "app.py"] | ||
| with _patch_sys_argv(): | ||
| assert sys.argv == ["app.py"] | ||
|
|
||
| assert sys.argv == expected | ||
|
|
||
| sys.argv = expected = ["lightning", "run", "app", "app.py", "--without-server", "--env", "name=something"] | ||
| with _patch_sys_argv(): | ||
| assert sys.argv == ["app.py"] | ||
|
|
||
| assert sys.argv == expected | ||
|
|
||
| sys.argv = expected = ["lightning", "run", "app", "app.py", "--app_args"] | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| with _patch_sys_argv(): | ||
| assert sys.argv == ["app.py"] | ||
|
|
||
| assert sys.argv == expected | ||
|
|
||
| sys.argv = expected = ["lightning", "run", "app", "app.py", "--app_args", "--env", "name=something"] | ||
| with _patch_sys_argv(): | ||
| assert sys.argv == ["app.py"] | ||
|
|
||
| assert sys.argv == expected | ||
|
|
||
| sys.argv = expected = [ | ||
| "lightning", | ||
| "run", | ||
| "app", | ||
| "app.py", | ||
| "--without-server", | ||
| "--app_args", | ||
| "--use_gpu", | ||
| "--name=hello", | ||
| "--env", | ||
| "name=something", | ||
| ] | ||
| with _patch_sys_argv(): | ||
| assert sys.argv == ["app.py", "--use_gpu", "--name=hello"] | ||
|
|
||
| assert sys.argv == expected | ||
|
|
||
| sys.argv = original_argv | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.