Skip to main content

Overview

DeploymentSampler handles client-side tokenization via a HuggingFace tokenizer and returns structured SampledCompletion objects with token IDs, logprobs, and completion metadata. Serverless and dedicated Training API sampling clients both use this implementation after their infrastructure-specific setup. Use it in training scripts that need token-level outputs (e.g. GRPO, DPO).

Constructor

Concurrency Control

sample_with_tokens(n=K) fans out into K individual streaming requests. Without concurrency control, all requests fire simultaneously, which can overload the server. Two controllers are available: Auto-tunes the concurrency window using AIMD (Additive Increase / Multiplicative Decrease) based on the server’s prefill_queue_duration:
The controller reads prefill_queue_duration from server response metrics. When the queue is below target, the window grows proportionally. When above, it halves (multiplicative decrease). By default, it adjusts after every 32 completed requests and at step boundaries. Set adjustment_interval=0 to adjust only at step boundaries.

FixedConcurrencyController

Static semaphore — use when you know the right concurrency for your deployment:

sample_with_tokens(...)

Sample completions and return structured results with token IDs. This method is async, so call it with await or wrap it with asyncio.run(...) from synchronous code:

Retrieving inference logprobs

For GRPO importance sampling, pass logprobs=True:

Sequence length filtering

sample_with_tokens supports max_seq_len for automatic filtering:
Two levels of filtering are applied:
  1. Prompt pre-filter: If the tokenized prompt already meets or exceeds max_seq_len, the method returns an empty list immediately — no inference call is made.
  2. Completion post-filter: After sampling, any completion whose full token sequence (prompt + completion) exceeds max_seq_len is silently dropped.

sample_with_prompt_tokens(...)

Use sample_with_prompt_tokens when your renderer has already produced prompt token IDs. Both serverless and dedicated services expose the shared DeploymentSampler through the sampling client:
Keep sampling_client alive until all calls through sampler have finished, then close it to release the underlying HTTP clients.

RL rollout sampling

Use Inference for RL rollouts as the canonical reference for session affinity, session-ID lifecycle, and KV-cache behavior. The example below only shows how to pass a rollout session value through DeploymentSampler and fan out independent samples. The following example launches two independent trajectories for every pre-tokenized prompt:
Here, n=1 is intentional because sample_with_prompt_tokens(n=2, user=...) gives both child requests the same user value. With 32 entries in batch_prompt_token_ids, the example creates 64 individual sampling requests. Follow Inference for RL rollouts to decide when those requests should use distinct or shared session values.

SampledCompletion

Each completion returned by sample_with_tokens or sample_with_prompt_tokens: