-
Notifications
You must be signed in to change notification settings - Fork 567
Initialize and reuse V1 renderer pools efficiently #1791
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
Closed
Closed
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import asyncio | ||
| from collections import Counter | ||
| from unittest.mock import AsyncMock, Mock | ||
|
|
||
| import pytest | ||
| import renderers | ||
| import renderers.client | ||
|
|
||
| import verifiers.v1.serve.server as serve_server | ||
| from verifiers.v1.clients import EvalClientConfig, TrainClientConfig | ||
| from verifiers.v1.clients.train import TrainClient | ||
| from verifiers.v1.dialects import ChatDialect | ||
| from verifiers.v1.serve.server import EnvServer | ||
| from verifiers.v1.types import SamplingConfig | ||
|
|
||
|
|
||
| def test_env_server_client_cache_keys(monkeypatch): | ||
| resolve = Mock(side_effect=lambda _: object()) | ||
| monkeypatch.setattr(serve_server, "resolve_client", resolve) | ||
| server = object.__new__(EnvServer) | ||
| server._clients = {} | ||
|
|
||
| pinned = TrainClientConfig(renderer_model_name="base-model") | ||
| pinned_clients = [server._client(pinned, f"adapter-{i}") for i in range(8)] | ||
| assert len({id(client) for client in pinned_clients}) == 1 | ||
|
|
||
| server._clients.clear() | ||
| unpinned = TrainClientConfig() | ||
| assert server._client(unpinned, "adapter-0") is not server._client( | ||
| unpinned, "adapter-1" | ||
| ) | ||
|
|
||
| server._clients.clear() | ||
| eval_config = EvalClientConfig() | ||
| assert server._client(eval_config, "model-0") is not server._client( | ||
| eval_config, "model-1" | ||
| ) | ||
|
|
||
|
|
||
| async def test_pinned_train_client_routes_512_requests_through_one_pool(monkeypatch): | ||
| server = object.__new__(EnvServer) | ||
| server._clients = {} | ||
| config = TrainClientConfig( | ||
| base_url="http://127.0.0.1:1", renderer_model_name="base-model" | ||
| ) | ||
| adapters = [f"adapter-{i}" for i in range(8)] | ||
| contexts = [ | ||
| server._context(config, adapter, SamplingConfig()) | ||
| for adapter in adapters | ||
| for _ in range(64) | ||
| ] | ||
| shared_client = contexts[0].client | ||
| assert isinstance(shared_client, TrainClient) | ||
|
|
||
| renderer = object() | ||
| create_pool = Mock(return_value=renderer) | ||
| monkeypatch.setattr(renderers, "create_renderer_pool", create_pool) | ||
|
|
||
| generate_mock = AsyncMock( | ||
| return_value={ | ||
| "request_id": "response", | ||
| "content": "ok", | ||
| "finish_reason": "stop", | ||
| "prompt_ids": [1] * 160, | ||
| "completion_ids": [2], | ||
| "completion_logprobs": [-0.1], | ||
| } | ||
| ) | ||
| monkeypatch.setattr(renderers.client, "generate", generate_mock) | ||
| responses = [] | ||
| for start in range(0, len(contexts), 128): | ||
| responses.extend( | ||
| await asyncio.gather( | ||
| *( | ||
| ctx.client.get_response( | ||
| ChatDialect(), | ||
| {"messages": [{"role": "user", "content": "hello"}]}, | ||
| ctx.model, | ||
| ctx.sampling, | ||
| ) | ||
| for ctx in contexts[start : start + 128] | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| assert create_pool.call_args.args == ("base-model", None) | ||
| assert create_pool.call_args.kwargs == {"size": 1} | ||
| assert create_pool.call_count == 1 | ||
| assert Counter(call.kwargs["model"] for call in generate_mock.call_args_list) == { | ||
| adapter: 64 for adapter in adapters | ||
| } | ||
| assert sum(response.usage.total_tokens for response in responses) == 82_432 | ||
|
|
||
| close = AsyncMock() | ||
| monkeypatch.setattr(shared_client, "close", close) | ||
| for client in server._clients.values(): | ||
| await client.close() | ||
| close.assert_awaited_once() | ||
|
|
||
|
|
||
| async def test_renderer_pool_initialization_failure_is_cached(monkeypatch): | ||
| failure = RuntimeError("renderer failed") | ||
| create_pool = Mock(side_effect=failure) | ||
| monkeypatch.setattr(renderers, "create_renderer_pool", create_pool) | ||
| client = TrainClient(AsyncMock(), renderer_model_name="base-model") | ||
|
|
||
| results = await asyncio.gather( | ||
| *(client._renderer_pool(f"adapter-{i}") for i in range(32)), | ||
| return_exceptions=True, | ||
| ) | ||
|
|
||
| assert create_pool.call_count == 1 | ||
| assert all(str(result) == "renderer failed" for result in results) | ||
| with pytest.raises(RuntimeError, match="renderer failed"): | ||
| await client._renderer_pool("another-adapter") | ||
| assert create_pool.call_count == 1 |
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
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.
This moves
create_renderer_poolinto a worker thread, but the package metadata still allowsrenderers>=0.1.8.dev40and the lockfile still resolves0.1.8.dev43; the required upstream fix (renderers#91) is still open. Fresh evidence beyond the prior comment is that this commit did not bump or lock the dependency, so installs resolving the current range can still run the old process-wide fastokens/Transformers patch concurrently with environment or harness tokenizer loads during the first train request.Useful? React with 👍 / 👎.