-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: parallelize LLM-as-judge evaluation using asyncio.gather() #3960
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
Open
aryanpatel2121
wants to merge
14
commits into
google:main
Choose a base branch
from
aryanpatel2121:feat/parallelize-llm-judge-evaluation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+226
−101
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
be36faa
feat: parallelize LLM-as-judge evaluation using asyncio.gather()
aryanpatel2121 935f85d
Merge branch 'main' into feat/parallelize-llm-judge-evaluation
aryanpatel2121 c655474
Fix lint errors by running autoformat.sh
aryanpatel2121 fcf51de
Merge branch 'main' into feat/parallelize-llm-judge-evaluation
aryanpatel2121 59be1d9
Merge branch 'main' into feat/parallelize-llm-judge-evaluation
aryanpatel2121 75380fc
Merge branch 'main' into feat/parallelize-llm-judge-evaluation
aryanpatel2121 f3b5228
Merge branch 'main' into feat/parallelize-llm-judge-evaluation
aryanpatel2121 e6e5d14
fix: resolve failing unit tests in test_litellm.py
aryanpatel2121 c566a0a
Merge branch 'main' into feat/parallelize-llm-judge-evaluation
aryanpatel2121 120e7ea
Merge branch 'main' into feat/parallelize-llm-judge-evaluation
aryanpatel2121 f2e9a48
fix: resolve CI check failures
aryanpatel2121 93effce
Merge branch 'main' into feat/parallelize-llm-judge-evaluation
aryanpatel2121 b6facdc
Merge branch 'main' into feat/parallelize-llm-judge-evaluation
aryanpatel2121 6b1a0d0
refactor: improve code quality and robustness
aryanpatel2121 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| """ | ||
| Performance demonstration for parallel LLM-as-judge evaluation. | ||
|
|
||
| This script demonstrates the performance improvement from parallelizing | ||
| LLM evaluation calls using asyncio.gather(). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from collections import defaultdict | ||
| import time | ||
| from typing import Optional | ||
|
|
||
| from google.genai import types as genai_types | ||
|
|
||
|
|
||
| # Simulated LLM call with artificial delay | ||
| async def mock_llm_call(delay: float = 0.5): | ||
| """Simulates an LLM API call with specified delay.""" | ||
| await asyncio.sleep(delay) | ||
| return genai_types.Content( | ||
| parts=[genai_types.Part(text="Mock LLM response")], | ||
| role="model", | ||
| ) | ||
|
|
||
|
|
||
| async def serial_evaluation( | ||
| num_invocations: int, num_samples: int, delay: float | ||
| ): | ||
| """Simulates the OLD serial evaluation approach.""" | ||
| results = [] | ||
| for i in range(num_invocations): | ||
| invocation_samples = [] | ||
| for j in range(num_samples): | ||
| response = await mock_llm_call(delay) | ||
| invocation_samples.append(response) | ||
| results.append(invocation_samples) | ||
| return results | ||
|
|
||
|
|
||
| async def parallel_evaluation( | ||
| num_invocations: int, num_samples: int, delay: float | ||
| ): | ||
| """Simulates the NEW parallel evaluation approach.""" | ||
| tasks = [] | ||
| invocation_indices = [] | ||
|
|
||
| # Create all N×M tasks | ||
| for i in range(num_invocations): | ||
| for j in range(num_samples): | ||
| tasks.append(mock_llm_call(delay)) | ||
| invocation_indices.append(i) | ||
|
|
||
| # Execute in parallel | ||
| all_results = await asyncio.gather(*tasks) | ||
|
|
||
| # Group by invocation | ||
| results_by_invocation = defaultdict(list) | ||
| for idx, result in zip(invocation_indices, all_results): | ||
| results_by_invocation[idx].append(result) | ||
|
|
||
| return [ | ||
| results_by_invocation[i] for i in sorted(results_by_invocation.keys()) | ||
| ] | ||
|
|
||
|
|
||
| async def main(): | ||
| """Run performance comparison.""" | ||
| num_invocations = 5 | ||
| num_samples = 2 | ||
| delay = 0.5 # 500ms per call | ||
|
|
||
| print("=" * 60) | ||
| print("LLM-as-Judge Parallel Evaluation Performance Test") | ||
| print("=" * 60) | ||
| print(f"Configuration:") | ||
| print(f" - Invocations: {num_invocations}") | ||
| print(f" - Samples per invocation: {num_samples}") | ||
| print(f" - Total LLM calls: {num_invocations * num_samples}") | ||
| print(f" - Simulated delay per call: {delay}s") | ||
| print() | ||
|
|
||
| # Test serial approach | ||
| print("Testing SERIAL approach (old)...") | ||
| start_time = time.perf_counter() | ||
| serial_results = await serial_evaluation(num_invocations, num_samples, delay) | ||
| serial_time = time.perf_counter() - start_time | ||
| print(f"✓ Completed in {serial_time:.2f}s") | ||
| print() | ||
|
|
||
| # Test parallel approach | ||
| print("Testing PARALLEL approach (new)...") | ||
| start_time = time.perf_counter() | ||
| parallel_results = await parallel_evaluation( | ||
| num_invocations, num_samples, delay | ||
| ) | ||
| parallel_time = time.perf_counter() - start_time | ||
| print(f"✓ Completed in {parallel_time:.2f}s") | ||
| print() | ||
|
|
||
| # Calculate speedup | ||
| speedup = serial_time / parallel_time | ||
| time_saved = serial_time - parallel_time | ||
|
|
||
| print("=" * 60) | ||
| print("RESULTS") | ||
| print("=" * 60) | ||
| print(f"Serial time: {serial_time:.2f}s") | ||
| print(f"Parallel time: {parallel_time:.2f}s") | ||
| print(f"Speedup: {speedup:.2f}x faster") | ||
| print( | ||
| f"Time saved: {time_saved:.2f}s ({time_saved/serial_time*100:.1f}%)" | ||
| ) | ||
| print("=" * 60) | ||
|
|
||
| # Verify results are the same | ||
| assert len(serial_results) == len(parallel_results) | ||
| for i in range(len(serial_results)): | ||
| assert len(serial_results[i]) == len(parallel_results[i]) | ||
| print("✓ Results verified: both approaches produce same output structure") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Optionaltype is imported fromtypingbut is not used within this file. It's a good practice to remove unused imports to maintain code cleanliness.