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

[App] Fix bug when using structures with works #15911

Merged
merged 16 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lightning_app/CHANGELOG.md
Expand Up @@ -50,6 +50,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Fixed Sigterm Handler causing thread lock which caused KeyboardInterrupt to hang ([#15881](https://github.com/Lightning-AI/lightning/pull/15881))

- Fixed a bug where using `L.app.structures` would cause multiple apps to be opened and fail with an error in the cloud ([#15911](https://github.com/Lightning-AI/lightning/pull/15911))


## [1.8.3] - 2022-11-22

Expand Down
2 changes: 1 addition & 1 deletion src/lightning_app/structures/dict.py
Expand Up @@ -64,10 +64,10 @@ def __setitem__(self, k, v):
if isinstance(k, str) and "." in k:
raise Exception(f"The provided name {k} contains . which is forbidden.")

_set_child_name(self, v, k)
ethanwharris marked this conversation as resolved.
Show resolved Hide resolved
if self._backend:
if isinstance(v, LightningFlow):
LightningFlow._attach_backend(v, self._backend)
_set_child_name(self, v, k)
elif isinstance(v, LightningWork):
self._backend._wrap_run_method(_LightningAppRef().get_current(), v)
v._name = f"{self.name}.{k}"
Expand Down
8 changes: 3 additions & 5 deletions src/lightning_app/structures/list.py
Expand Up @@ -55,20 +55,18 @@ def __init__(self, *items: T):
self._backend: Optional[Backend] = None
for item in items:
self.append(item)
_set_child_name(self, item, str(self._last_index))
self._last_index += 1

def append(self, v):
from lightning_app import LightningFlow, LightningWork

_set_child_name(self, v, str(self._last_index))
if self._backend:
if isinstance(v, LightningFlow):
LightningFlow._attach_backend(v, self._backend)
_set_child_name(self, v, str(self._last_index))
elif isinstance(v, LightningWork):
self._backend._wrap_run_method(_LightningAppRef().get_current(), v)
v._name = f"{self.name}.{self._last_index}"
self._last_index += 1
v._name = f"{self.name}.{self._last_index}"
self._last_index += 1
super().append(v)

@property
Expand Down
22 changes: 22 additions & 0 deletions tests/tests_app/structures/test_structures.py
Expand Up @@ -497,3 +497,25 @@ def test_structures_with_payload():
app = LightningApp(FlowPayload(), log_level="debug")
MultiProcessRuntime(app, start_server=False).dispatch()
os.remove("payload")


def test_structures_have_name_on_init():
"""Test that the children in structures have the correct name assigned upon initialization."""

class ChildWork(LightningWork):
def run(self):
pass

class Collection(EmptyFlow):
def __init__(self):
super().__init__()
self.list_structure = List()
self.list_structure.append(ChildWork())

self.dict_structure = Dict()
self.dict_structure["dict_child"] = ChildWork()

flow = Collection()
LightningApp(flow) # wrap in app to init all component names
assert flow.list_structure[0].name == "root.list_structure.0"
assert flow.dict_structure["dict_child"].name == "root.dict_structure.dict_child"