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

Reduce state size #13970

Merged
merged 35 commits into from
Aug 3, 2022
Merged

Reduce state size #13970

merged 35 commits into from
Aug 3, 2022

Conversation

tchaton
Copy link
Contributor

@tchaton tchaton commented Aug 1, 2022

What does this PR do?

This PR optimizes the size of the state. From benchmarking with the following example, the state seems to be 2 times smaller overall.

from lightning import LightningApp, LightningFlow, LightningWork
from lightning.app.runners import MultiProcessRuntime
from pympler import asizeof


class SizeWork(LightningWork):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.counter = 0

    def run(self, signal: int):
        self.counter += 1


class SizeFlow(LightningFlow):
    def __init__(self):
        super().__init__()
        self.work0 = SizeWork(parallel=True, cache_calls=True)
        self._state_sizes = {}

    def run(self):
        for idx in range(self.work0.counter + 2):
            self.work0.run(idx)

        self._state_sizes[self.work0.counter] = asizeof.asizeof(self.state)

        if self.work0.counter >= 20:
            self._exit()

if __name__ == "__main__":
    app = LightningApp(SizeFlow())
    MultiProcessRuntime(app, start_server=False).dispatch()
    print(app.root._state_sizes)
python app.py

On master, we reached 0.1MB at counter 46.

With this PR, we reached 0.1MB at counter 95.

Parts of #13944

Does your PR introduce any breaking changes? If yes, please list them.

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

cc @Borda @tchaton @rohitgr7

@github-actions github-actions bot added the app (removed) Generic label for Lightning App package label Aug 1, 2022
@tchaton tchaton added bug Something isn't working priority: 0 High priority task labels Aug 1, 2022
@tchaton tchaton self-assigned this Aug 1, 2022
@tchaton tchaton added this to the app:0.5.x milestone Aug 1, 2022
@tchaton tchaton marked this pull request as draft August 1, 2022 18:20
@tchaton tchaton marked this pull request as ready for review August 1, 2022 18:44
Copy link
Collaborator

@lantiga lantiga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

In general, this PR reduces the size of the state, but we could go one step further and keep the circulating state bounded.

If I'm not missing something, calls are essentially only useful for Flow, and not for Work, therefore we could avoid transmitting the whole calls down to Work (and up to FE).

If users want to display the status of calls, they may want to explicitly record those statuses in the circulating state from Flow.

Keeping call info in the flow only would make circulating state (which is what is causing the slowdown) considerably smaller and avoid it to grow. Then, when it's time to serialize, we would serialize call info together with Flow. This is just about avoiding to send call info around.

src/lightning_app/core/app.py Outdated Show resolved Hide resolved
src/lightning_app/core/work.py Show resolved Hide resolved
src/lightning_app/core/work.py Outdated Show resolved Hide resolved
src/lightning_app/core/work.py Outdated Show resolved Hide resolved
src/lightning_app/core/work.py Outdated Show resolved Hide resolved
src/lightning_app/utilities/proxies.py Show resolved Hide resolved
src/lightning_app/utilities/proxies.py Outdated Show resolved Hide resolved
src/lightning_app/utilities/proxies.py Outdated Show resolved Hide resolved
src/lightning_app/utilities/proxies.py Outdated Show resolved Hide resolved
tests/tests_app/core/test_lightning_app.py Outdated Show resolved Hide resolved
@tchaton
Copy link
Contributor Author

tchaton commented Aug 2, 2022

Hey @lantiga,

Quote: If I'm not missing something, calls are essentially only useful for Flow, and not for Work, therefore we could avoid transmitting the whole calls down to Work (and up to FE).

This is already done this way right there: https://github.com/Lightning-AI/lightning/pull/13970/files#diff-5347e507417db0b69cdee291b23e64f0105cf382f743f8f2d7e60f308cf05b5dR65. I am popping the call_hashes before sending the state through the caller_queue.
This drastically increased the responsiveness of the work when I was doing my tests compared to master.

@lantiga
Copy link
Collaborator

lantiga commented Aug 2, 2022

Hey @lantiga,

Quote: If I'm not missing something, calls are essentially only useful for Flow, and not for Work, therefore we could avoid transmitting the whole calls down to Work (and up to FE).

This is already done this way right there: https://github.com/Lightning-AI/lightning/pull/13970/files#diff-5347e507417db0b69cdee291b23e64f0105cf382f743f8f2d7e60f308cf05b5dR65. I am popping the call_hashes before sending the state through the caller_queue. This drastically increased the responsiveness of the work when I was doing my tests compared to master.

Ah, awesome. I didn’t catch that. Perfect, it would be good to add a comment and making it more prominent.

src/lightning_app/core/app.py Outdated Show resolved Hide resolved
src/lightning_app/core/work.py Outdated Show resolved Hide resolved
src/lightning_app/runners/runtime.py Outdated Show resolved Hide resolved
@tchaton tchaton requested a review from manskx August 2, 2022 21:00
tests/tests_app/core/test_lightning_work.py Outdated Show resolved Hide resolved
src/lightning_app/utilities/proxies.py Show resolved Hide resolved
Copy link
Contributor

@manskx manskx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice improvement 🚀

I wouldn't close #13944 as completed yet, since the memory could still grow if calling the works with multiple arguments.

Copy link
Collaborator

@lantiga lantiga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. I added two comments to require further clarification.

src/lightning_app/core/work.py Show resolved Hide resolved
src/lightning_app/utilities/proxies.py Show resolved Hide resolved
@mergify mergify bot added the ready PRs ready to be merged label Aug 3, 2022
@tchaton tchaton enabled auto-merge (squash) August 3, 2022 12:11
@tchaton tchaton merged commit 5479c60 into master Aug 3, 2022
@tchaton tchaton deleted the reduce_state_size branch August 3, 2022 13:47
tchaton added a commit that referenced this pull request Aug 9, 2022
Borda pushed a commit that referenced this pull request Aug 9, 2022
(cherry picked from commit 5479c60)
lexierule pushed a commit that referenced this pull request Aug 9, 2022
(cherry picked from commit 5479c60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app (removed) Generic label for Lightning App package bug Something isn't working priority: 0 High priority task ready PRs ready to be merged
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants