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

Make the remote inference engine runnable in jupyter notebooks. #565

Merged
merged 7 commits into from
Oct 1, 2024

Conversation

taenin
Copy link
Collaborator

@taenin taenin commented Sep 30, 2024

Asyncio is an invasive dependency. If code using an asyncio loop (such as a Jupyter Notebook) runs code that creates its own asyncio loop, asyncio will throw a RuntimeError.

We get around this issue by creating any asyncio loops in new threads which we block on. Asyncio requires that only one asyncio loop can exist per thread, and this approach preserves that invariant.

Towards OPE-320

Copy link

linear bot commented Sep 30, 2024

OPE-320 FR: Update `lema.infer` to support batch inference

  1. Read prompts from files
  2. Write responses to files

@taenin taenin marked this pull request as ready for review September 30, 2024 19:41
Copy link
Contributor

@wizeng23 wizeng23 left a comment

Choose a reason for hiding this comment

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

Any potential downsides or gotchas to this approach you can think of? Is the thread creation overhead time minimal I assume?

@taenin
Copy link
Collaborator Author

taenin commented Sep 30, 2024

Any potential downsides or gotchas to this approach you can think of? Is the thread creation overhead time minimal I assume?

There are definitely tradeoffs.

  • As you mentioned, there is overhead for creating a thread. So long as the running job is somewhat lengthy (as it should be in our case), then this concern is minimal.
  • The real drawback is in the scenario where an upstream caller is calling us asynchronously. As our async code is running in a thread, ALL of our async jobs will only run when their current thread is active. This means if there is more than one thread that is not blocking, our async jobs will see significant slow down.

@nikg4 nikg4 requested a review from jgreer013 September 30, 2024 21:43


def safe_asyncio_run(main: Awaitable[T]) -> T:
"""Run an Awaitable in a new thread. Blocks until the thread is finished.
Copy link
Collaborator

@nikg4 nikg4 Sep 30, 2024

Choose a reason for hiding this comment

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

is it "in a new thread", or "in a new Python process" given that multiprocessing is used?
Looks like it's actually the former (a thread pool) https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing.dummy
Thsi page has this recommendation: Users should generally prefer to use concurrent.futures.ThreadPoolExecutor, which has a simpler interface that was designed around threads from the start, and which returns concurrent.futures.Future instances that are compatible with many other libraries, including asyncio.

Would it make sense to switch to ThreadPoolExecutor here ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, I don't see a problem with using ThreadPoolExecutor here. Updated

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To address your original question: my intent here is to run the event loop in a separate thread.

@taenin
Copy link
Collaborator Author

taenin commented Oct 1, 2024

See a plethora of examples here on github with similar approaches: https://github.com/search?q=asyncio.set_event_loop+thread&type=code

This is typically done for code that is executing in an environment with an asyncio loop already running (Jupyter Notebook, IPython, Discord bots).

@taenin taenin merged commit f3a0793 into main Oct 1, 2024
1 check passed
@taenin taenin deleted the taenin/write branch October 1, 2024 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants