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] Add automatic conversion to structures #15961

Merged
merged 9 commits into from Dec 8, 2022
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.
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 @@ -28,6 +28,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Added a `configure_layout` method to the `LightningWork` which can be used to control how the work is handled in the layout of a parent flow ([#15926](https://github.com/Lightning-AI/lightning/pull/15926))

- Added automatic conversion of list and dict of works and flows to structures ([#15961](https://github.com/Lightning-AI/lightning/pull/15961))


### Changed

Expand Down
12 changes: 10 additions & 2 deletions src/lightning_app/core/flow.py
Expand Up @@ -142,6 +142,14 @@ def __setattr__(self, name: str, value: Any) -> None:
if name in self._works and value != getattr(self, name):
raise AttributeError(f"Cannot set attributes as the work can't be changed once defined: {name}")

if isinstance(value, (list, dict)) and value:
tchaton marked this conversation as resolved.
Show resolved Hide resolved
_type = (LightningFlow, LightningWork, List, Dict)
if isinstance(value, list) and all(isinstance(va, _type) for va in value):
value = List(*value)

if isinstance(value, dict) and all(isinstance(va, _type) for va in value.values()):
value = Dict(**value)

if isinstance(value, LightningFlow):
self._flows.add(name)
_set_child_name(self, value, name)
Expand All @@ -163,10 +171,10 @@ def __setattr__(self, name: str, value: Any) -> None:
value._register_cloud_compute()

elif isinstance(value, (Dict, List)):
value._backend = self._backend
self._structures.add(name)
_set_child_name(self, value, name)
if self._backend:
if getattr(self, "_backend", None) is not None:
value._backend = self._backend
for flow in value.flows:
LightningFlow._attach_backend(flow, self._backend)
for work in value.works:
Expand Down
26 changes: 26 additions & 0 deletions tests/tests_app/structures/test_structures.py
Expand Up @@ -518,3 +518,29 @@ def __init__(self):
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"


class FlowWiStructures(LightningFlow):
def __init__(self):
super().__init__()

self.ws = [EmptyFlow(), EmptyFlow()]

self.ws1 = {"a": EmptyFlow(), "b": EmptyFlow()}

self.ws2 = {
"a": EmptyFlow(),
"b": EmptyFlow(),
"c": List(EmptyFlow(), EmptyFlow()),
"d": Dict(**{"a": EmptyFlow()}),
}

def run(self):
pass


def test_flow_without_structures():

flow = FlowWiStructures()
assert isinstance(flow.ws, List)
assert isinstance(flow.ws1, Dict)