Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

Tune CPU Embedders

If your query pipeline runs an embedder such as SentenceTransformersTextEmbedder on CPU and handles several requests at the same time, you can limit the number of threads the model uses. This improves throughput and reduces tail latency.


About This Task​

A pipeline that runs a Sentence Transformers embedder on CPU, without a GPU or a Triton model server, loads one model in each query pod and shares it across all parallel requests. Each embedding call starts a pool of native threads. By default, the size of that pool depends on the number of CPU cores the pod can see, not on the CPU the pod is allowed to use. When many requests run at once, their thread pools compete for the same CPU, and queries become slower. In some cases, requests fail with WorkerError.

By default, embedder calls from parallel requests on query pods without a GPU run at the same time. Only pods with a GPU run one inference at a time to protect GPU memory. You can change this with TORCH_INFERENCE_LOCK.

To avoid thread competition, Haystack Enterprise Platform sets default thread limits in query pods, and you can override them. How you do that depends on the backend:

  • Torch (the default backend) reads the thread limits from environment variables.
  • ONNX ignores these environment variables. You set the thread count in the embedder's model_kwargs instead.

Check the Default Thread Limits​

In query pods, Haystack Enterprise Platform sets these environment variables:

VariableDefault value
OMP_NUM_THREADSThe pod's CPU request, rounded up to a whole core, and at least 1. For example, a request of 500m becomes 1 and a request of 1,730m becomes 2.
MKL_NUM_THREADSThe same value as OMP_NUM_THREADS.
TOKENIZERS_PARALLELISMfalse
OMP_WAIT_POLICYPASSIVE. Idle OpenMP threads give up the CPU instead of spinning against the pod's CPU quota.

Query pods also default torch to one inter-op thread, and give each ONNX Runtime session one intra-op thread unless the pipeline sets its own intra_op_num_threads. These limits don't depend on the CPU count of the node the pod runs on.

The defaults apply to query pods only, not to indexing.

Override the Defaults for Torch​

Use this section if your embedder uses the default Torch backend. A workspace secret with the same name as a variable takes precedence over the default.

  1. Add a secret named OMP_NUM_THREADS with the number of threads you want, for example 1. For instructions, see Add Secrets.
  2. Repeat for MKL_NUM_THREADS and, if needed, TOKENIZERS_PARALLELISM.
  3. Redeploy your pipeline so the query pods pick up the new values. If throughput doesn't change, check that the secret names match the variable names exactly and that you redeployed the pipeline after adding them.

If you override these variables with high values, parallel requests on CPU pods compete for cores again and can run slower. Keep them at the defaults or lower unless you have measured a benefit.

Set the Thread Count for ONNX​

ONNX Runtime ignores OMP_NUM_THREADS and MKL_NUM_THREADS. To set its thread count, use session_options in the embedder's model_kwargs:

query_embedder:
type: haystack_integrations.components.embedders.sentence_transformers.SentenceTransformersTextEmbedder
init_parameters:
model: intfloat/e5-base-v2
backend: onnx
model_kwargs:
file_name: onnx/model.onnx
session_options:
intra_op_num_threads: 1

This setting requires sentence-transformers-haystack 2.1.0 or later. For OptimumTextEmbedder, it requires optimum-haystack 4.1.0 or later.

Edit the embedder in your query pipeline YAML, then redeploy the pipeline. A value you set in the YAML overrides the query pod default of one thread. If throughput doesn't improve, check that intra_op_num_threads is set under session_options in model_kwargs and that you redeployed the pipeline.

You can set only options that take plain values, such as thread counts and booleans. Options that take an enum value, such as graph_optimization_level, can't be set in YAML.

Control the Torch Inference Lock​

Change TORCH_INFERENCE_LOCK only if you need to trade GPU memory protection for concurrency, or the reverse. The variable controls whether a query pod runs one torch inference at a time. The lock protects GPU memory, so by default it applies only to pods with a GPU. Set the variable the same way as the thread variables, as a workspace secret, and redeploy your pipeline.

ValueBehavior
auto (default)The lock is active on pods with a GPU and off on CPU-only pods.
onThe lock is always active, which makes CPU requests run one at a time.
offThe lock is never active. On a GPU pod, memory use can grow when many requests run at once.

Any other value is treated as auto. Keep the default unless you have a specific reason to change it.

Choose a Thread Count​

The best value depends on how many requests your pipeline handles at the same time. The following table shows embeddings per second from one local test with four parallel callers. It ran on linux/arm64 with Docker limited to two or four CPUs. Your results may differ.

Variant2 CPUs4 CPUs
Torch, default3.288.24
Torch, thread environment variables set to 16.5413.10
ONNX, default2.526.81
ONNX, intra_op_num_threads: 112.5425.61

Limiting the threads has a trade-off: a single caller gets slower with one thread because the model can no longer spread one request over several cores. Use one thread if your pipeline serves several requests in parallel. If it handles few concurrent requests, keep the defaults.

What To Do Next​