damccorm commented on code in PR #41720:
URL: https://github.com/apache/beam/pull/41720#discussion_r4744100069
##########
sdks/python/apache_beam/ml/inference/anthropic_inference.py:
##########
@@ -85,6 +87,16 @@
LOGGER = logging.getLogger("AnthropicModelHandler")
+# Upper bound on the number of in-flight HTTP requests issued concurrently for
+# a single batch. The Messages API takes one conversation per request, so the
+# request functions send one request per element. Bounds thread usage when
+# adaptive batching produces large batches; use max_batch_size to further
+# limit per-batch concurrency.
+_MAX_CONCURRENT_REQUESTS_PER_BATCH = 32
Review Comment:
Since this PR was opened, #41361 landed on `master` adding a default
`max_batch_size` of `max(min_batch_size or 0,
_MAX_CONCURRENT_REQUESTS_PER_BATCH)` (32) to both `OpenAIModelHandler` and
`GeminiModelHandler` (skipping the default when `element_size_fn` is set
without `max_batch_weight`, or for a custom `request_fn`).
Could we mirror that in `AnthropicModelHandler.__init__` here as well? With
32-way concurrency, `BatchElements` adaptive batching will grow batches much
faster toward its default cap of 10,000, which adds no extra concurrency beyond
32 while causing `RemoteModelHandler` to retry (and re-bill) the entire batch
on a single 429/5xx — and also means `executor.map` won't cancel queued items
`32..N` on an early failure until slow earlier items finish.
(Keeping `max_tokens` untouched so this stays independent of #41196 makes
sense — just noting we should land #41196 alongside this since concurrent
fan-out makes 429 retries more likely.)
##########
sdks/python/apache_beam/ml/inference/anthropic_inference_test.py:
##########
@@ -111,20 +123,104 @@ def test_no_retry_on_non_api_error(self):
class MessageFromStringTest(unittest.TestCase):
def test_sends_each_prompt(self):
client = mock.MagicMock()
- client.messages.create.side_effect = [
- _make_fake_response("answer 1"),
- _make_fake_response("answer 2"),
- ]
+ client.messages.create.side_effect = _echo_last_message
results = message_from_string(_TEST_MODEL, ['hello', 'world'], client, {})
- self.assertEqual(len(results), 2)
+ self.assertEqual([r.content[0].text for r in results],
+ ["answer for: hello", "answer for: world"])
self.assertEqual(client.messages.create.call_count, 2)
- call_args = client.messages.create.call_args_list[0]
- self.assertEqual(call_args.kwargs['model'], _TEST_MODEL)
+ calls = client.messages.create.call_args_list
+ sent_messages = sorted((c.kwargs['messages'] for c in calls),
+ key=lambda m: m[0]['content'])
self.assertEqual(
- call_args.kwargs['messages'], [{
+ sent_messages,
+ [[{
"role": "user", "content": "hello"
- }])
+ }], [{
+ "role": "user", "content": "world"
+ }]])
+ for c in calls:
+ self.assertEqual(c.kwargs['model'], _TEST_MODEL)
+
+ def test_requests_run_concurrently_and_preserve_order(self):
+ lock = threading.Lock()
+ state = {'in_flight': 0, 'max_in_flight': 0}
+ completion_order = []
+ prompts = [f'p{i}' for i in range(4)]
+
+ def slow_create(**kwargs):
+ prompt = kwargs['messages'][-1]['content']
+ with lock:
+ state['in_flight'] += 1
+ state['max_in_flight'] = max(state['max_in_flight'],
state['in_flight'])
+ # Earlier prompts sleep longer so they finish last.
+ time.sleep(0.05 * (len(prompts) - prompts.index(prompt)))
Review Comment:
Optional nit: using a `threading.Barrier` / `Event` here (like you did in
`test_sends_conversations_concurrently_and_preserves_order`) would avoid any
sleep-timing flakiness on loaded CI runners, though I know this matches the
existing OpenAI/Gemini tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]