-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: support benchmark on scaffolding (#3328) #4286
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
Merged
Merged
Changes from all commits
Commits
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,101 @@ | ||
| import asyncio | ||
| import time | ||
| from typing import List, Mapping, Tuple, Type | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from tensorrt_llm.scaffolding.scaffolding_llm import (ScaffoldingLlm, | ||
| ScaffoldingResult) | ||
| from tensorrt_llm.scaffolding.task_collection import (TaskCollection, | ||
| with_task_collection) | ||
|
|
||
|
|
||
| class ScaffoldingBenchRequest(BaseModel): | ||
| prompt: str | ||
|
|
||
|
|
||
| async def enqueue_requests(input_queue, requests): | ||
| for request in requests: | ||
| await input_queue.put(request) | ||
|
|
||
| await input_queue.put(None) | ||
|
|
||
|
|
||
| async def process_request(scaffolding_llm, request, output_queue, semaphore): | ||
| async with semaphore: | ||
| request_start_time = time.time() | ||
| result = scaffolding_llm.generate_async(request.prompt) | ||
| await result.aresult() | ||
| request_execution_time = time.time() - request_start_time | ||
| await output_queue.put((result, request_execution_time)) | ||
|
|
||
|
|
||
| async def run_scaffolding_llm(scaffolding_llm, input_queue, output_queue, | ||
| concurrency): | ||
| semaphore = asyncio.Semaphore(concurrency) | ||
| tasks = set() | ||
|
|
||
| while True: | ||
| request = await input_queue.get() | ||
| if request is None: | ||
| break | ||
| task = asyncio.create_task( | ||
| process_request(scaffolding_llm, request, output_queue, semaphore)) | ||
| tasks.add(task) | ||
| task.add_done_callback(tasks.discard) | ||
|
|
||
| await asyncio.gather(*tasks) | ||
| await output_queue.put(None) | ||
|
|
||
|
|
||
| def wrapper_prototype_controller_with_task_collection(scaffolding_llm, | ||
| task_collection_types): | ||
| prototype_controller_type = type(scaffolding_llm.prototype_controller) | ||
| controller_type_with_task_collection = prototype_controller_type | ||
|
|
||
| for name, task_collection_type in task_collection_types.items(): | ||
| scaffolding_llm.prototype_controller.task_collections[ | ||
| name] = task_collection_type() | ||
| controller_type_with_task_collection = with_task_collection( | ||
| name, task_collection_type)(controller_type_with_task_collection) | ||
|
|
||
| scaffolding_llm.enable_output_task_collection() | ||
|
|
||
|
|
||
| async def async_scaffolding_benchmark( | ||
| scaffolding_llm: ScaffoldingLlm, | ||
| task_collection_types: Mapping[str, Type[TaskCollection]], | ||
| requests: List[ScaffoldingBenchRequest], | ||
| concurrency: int) -> Tuple[List[ScaffoldingResult], List[float], float]: | ||
| wrapper_prototype_controller_with_task_collection(scaffolding_llm, | ||
| task_collection_types) | ||
|
|
||
| input_queue = asyncio.Queue() | ||
| output_queue = asyncio.Queue() | ||
|
|
||
| start_time = time.time() | ||
| results = [] | ||
| requests_execution_time = [] | ||
| enqueue_task = asyncio.create_task(enqueue_requests(input_queue, requests)) | ||
|
|
||
| run_scaffolding_llm_task = asyncio.create_task( | ||
| run_scaffolding_llm(scaffolding_llm, input_queue, output_queue, | ||
| concurrency)) | ||
|
|
||
| while True: | ||
| try: | ||
| item = await asyncio.wait_for(output_queue.get(), timeout=1.0) | ||
| if item is None: | ||
| break | ||
| result, request_execution_time = item | ||
| results.append(result) | ||
| requests_execution_time.append(request_execution_time) | ||
| except asyncio.TimeoutError: | ||
| continue | ||
|
|
||
| total_time = time.time() - start_time | ||
|
|
||
| enqueue_task.result() | ||
| run_scaffolding_llm_task.result() | ||
|
|
||
| return results, requests_execution_time, total_time |
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,59 @@ | ||
| import asyncio | ||
| from typing import List | ||
|
|
||
| from tensorrt_llm.scaffolding import (GenerationTask, | ||
| NativeGenerationController, | ||
| ScaffoldingBenchRequest, ScaffoldingLlm, | ||
| Task, TaskCollection, TaskStatus, Worker, | ||
| async_scaffolding_benchmark) | ||
|
|
||
| OUTPUT_STR = "Yes." | ||
|
|
||
|
|
||
| class DummyWorker(Worker): | ||
|
|
||
| async def dummy_generation_handler(self, task: GenerationTask): | ||
| task.output_str = OUTPUT_STR | ||
| return TaskStatus.SUCCESS | ||
|
|
||
| task_handlers = {GenerationTask: dummy_generation_handler} | ||
|
|
||
|
|
||
| class DummyTaskCollection(TaskCollection): | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.output_len = 0 | ||
|
|
||
| def before_yield(self, tasks: List[Task]): | ||
| pass | ||
|
|
||
| def after_yield(self, tasks: List[Task]): | ||
| self.output_len = len(tasks[0].output_str) | ||
|
|
||
|
|
||
| def test_scaffolding_benchmark(): | ||
| task_collection_types = {"bench_dummy_collection": DummyTaskCollection} | ||
|
|
||
| prototype_controller = NativeGenerationController() | ||
| dummy_worker = DummyWorker() | ||
| workers = {NativeGenerationController.WorkerTag.GENERATION: dummy_worker} | ||
| scaffolding_llm = ScaffoldingLlm(prototype_controller, workers) | ||
|
|
||
| requests_num = 100 | ||
| requests = [ | ||
| ScaffoldingBenchRequest(prompt="Is today a nice day?") | ||
| for _ in range(requests_num) | ||
| ] | ||
|
|
||
| concurrency = 10 | ||
|
|
||
| results, requests_execution_time, total_time = asyncio.run( | ||
| async_scaffolding_benchmark(scaffolding_llm, task_collection_types, | ||
| requests, concurrency)) | ||
|
|
||
| assert len(results) == requests_num | ||
| assert len(requests_execution_time) == requests_num | ||
| assert results[0].output.output_str == OUTPUT_STR | ||
| assert results[0].task_collections[ | ||
| "bench_dummy_collection"].output_len == len(OUTPUT_STR) |
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.
Uh oh!
There was an error while loading. Please reload this page.