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

Jupyter Code Executor in v0.4 (alternative implementation) #4885

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

Leon0402
Copy link
Contributor

@Leon0402 Leon0402 commented Jan 3, 2025

Why are these changes needed?

The new v4 version has no support for jupter notebooks yet compared to the old version.

NOTE: This is an alternative implementation to #4795 which uses the nbclient library rather than implementing the protocol ourself. While nbclient is probably not made for that use case, it works quite well with only small hacks. Additionally no explicit external jupyter gateway server is used anymore (which could be removed in the other implementation as well though).
Compared to the other implementation it is much simpler and more importantly much more robust. I extensively tested both implementations in a real life scenario and the other one had problems with getting frequent timeouts and got stuck after running for a while. I debugged it quite a bit, but I had the feeling that providing a robust implementation is quite complicated and better left to experts in that domain (i.e. nblient).

Related issue number

Closes #4792

Checks

@Leon0402 Leon0402 changed the title Jupyter Code Executor in v0.4 (alternative implementation Jupyter Code Executor in v0.4 (alternative implementation) Jan 3, 2025
Copy link
Member

@jackgerrits jackgerrits left a comment

Choose a reason for hiding this comment

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

This looks great! I agree, this feels like the better route to take. Simpler, more robust and offloads the complexity of processing the notebook to a library built for the task!

python/packages/autogen-ext/pyproject.toml Outdated Show resolved Hide resolved
@Leon0402 Leon0402 marked this pull request as draft January 4, 2025 08:34
@Leon0402
Copy link
Contributor Author

Leon0402 commented Jan 4, 2025

@jackgerrits Updated the code, I was missing some local changes that I had done in nbclient.

But there is some memory leak somewhere. When I run my program, I can see memory grow extremely. After 40min it was multiple GB. I run 10 tasks in parallel, where each task is a dialog with up to 20 turns. While I work a lot with images, which could lead to spikes in individual dialogs, the memory consumption should not be increasing with time, but be rather constant or have (unpredictable) spikes.
Unfortunately, I don't really know if it is related to this patch or some general issue in AutoGen. I already spent significant time on debugging, but i find it hard to find the exact cause. Have you experienced any similar issue without my PR here?
I could use some help :)

Some context:

async def run_task(cfg: Config, task: TaskType):
    ...

    semaphore = asyncio.Semaphore(cfg.concurrency_limit)

    async def run_single_sample(task_runner: TaskRunner, task_sample: TaskSample):
        async with semaphore:
            await task_runner.run_agent(task_sample, cfg.output_dir / task.value)

    task_runner = TaskRunner(cfg)
    samples = [run_single_sample(task_runner, task_sample) for task_sample in sliced_samples]
    await tqdm.gather(*samples, desc=f"Task: {task.value}")


async def run_tasks(cfg: Config):
    for task in cfg.tasks:
        await run_task(cfg, task)

This is my code task_runner.run_agent will then define the agents and code executor (in a async with block). So I would generally expect that each task frees up memory shortly after finishing in tqdm.gather.

Copy link

codecov bot commented Jan 4, 2025

Codecov Report

Attention: Patch coverage is 96.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 68.44%. Comparing base (e8797a2) to head (99ff064).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...t/code_executors/jupyter/_jupyter_code_executor.py 95.91% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4885      +/-   ##
==========================================
+ Coverage   68.21%   68.44%   +0.23%     
==========================================
  Files         158      160       +2     
  Lines        9960    10060     +100     
==========================================
+ Hits         6794     6886      +92     
- Misses       3166     3174       +8     
Flag Coverage Δ
unittests 68.44% <96.00%> (+0.23%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 4, 2025

@Leon0402 Can you show where your runtime is created? this might be due to the runtime is not removing references to created agents.

To mitigate you might want to create new instances of runtime for each task.

I think we should handle it in a separate PR.

@jackgerrits
Copy link
Member

Haven't seen this in the main branch myself, would be curious to know what going on. I agree with Eric, sounds plausible

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.

Port Jupyter code executor from v0.2
3 participants