:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(llma): limit sentiment onnx runtime threads (#64944)
Radu Raicea059a4b216585
diff --git a/posthog/temporal/ai_observability/sentiment/README.md b/posthog/temporal/ai_observability/sentiment/README.mdindex a286ba8b849b..d5bde16f26cf 100644--- a/posthog/temporal/ai_observability/sentiment/README.md+++ b/posthog/temporal/ai_observability/sentiment/README.md@@ -60,6 +60,18 @@ uv run --group sentiment bin/download-sentiment-modelThis is a no-op if the model already exists.+### Runtime CPU controls++The ONNX Runtime session defaults to one intra-op and one inter-op thread to+avoid CPU oversubscription when Temporal runs multiple+sentiment activities concurrently.+Override these only for a dedicated worker with measured headroom:++```bash+POSTHOG_SENTIMENT_ONNX_INTRA_OP_NUM_THREADS=2+POSTHOG_SENTIMENT_ONNX_INTER_OP_NUM_THREADS=1+```+## Running tests```bashdiff --git a/posthog/temporal/ai_observability/sentiment/constants.py b/posthog/temporal/ai_observability/sentiment/constants.pyindex 50478b916452..339a22bb740a 100644--- a/posthog/temporal/ai_observability/sentiment/constants.py+++ b/posthog/temporal/ai_observability/sentiment/constants.py@@ -3,11 +3,22 @@import osfrom pathlib import Path++def _positive_int_env(name: str, default: int) -> int:+ try:+ value = int(os.environ.get(name, default))+ except ValueError:+ return default+ return max(1, value)++# ModelMODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment-latest"MODEL_MAX_TOKENS = 512 # context window for the cardiffnlp modelLABELS = ["negative", "neutral", "positive"]ONNX_CACHE_DIR = Path(os.environ.get("POSTHOG_SENTIMENT_MODEL_CACHE", "/tmp/posthog-sentiment-onnx-cache"))+ONNX_INTRA_OP_NUM_THREADS = _positive_int_env("POSTHOG_SENTIMENT_ONNX_INTRA_OP_NUM_THREADS", 1)+ONNX_INTER_OP_NUM_THREADS = _positive_int_env("POSTHOG_SENTIMENT_ONNX_INTER_OP_NUM_THREADS", 1)# Extraction boundsMAX_USER_MESSAGES = 50diff --git a/posthog/temporal/ai_observability/sentiment/model.py b/posthog/temporal/ai_observability/sentiment/model.pyindex da5cfcaefe5e..a701d6743461 100644--- a/posthog/temporal/ai_observability/sentiment/model.py+++ b/posthog/temporal/ai_observability/sentiment/model.py@@ -20,6 +20,8 @@ from posthog.temporal.ai_observability.sentiment.constants import (MODEL_MAX_TOKENS,MODEL_NAME,ONNX_CACHE_DIR,+ ONNX_INTER_OP_NUM_THREADS,+ ONNX_INTRA_OP_NUM_THREADS,)from posthog.temporal.ai_observability.sentiment.schema import SentimentResult@@ -57,12 +59,22 @@ def _load_pipeline():f"Ensure Dockerfile.llm-analytics bakes the model into {ONNX_CACHE_DIR}.")+ from onnxruntime import SessionOptionsfrom optimum.onnxruntime import ORTModelForSequenceClassificationfrom transformers import AutoTokenizer, pipeline- logger.info("Loading sentiment model from ONNX cache", cache_dir=cache_dir)+ session_options = SessionOptions()+ session_options.intra_op_num_threads = ONNX_INTRA_OP_NUM_THREADS+ session_options.inter_op_num_threads = ONNX_INTER_OP_NUM_THREADS++ logger.info(+ "Loading sentiment model from ONNX cache",+ cache_dir=cache_dir,+ onnx_intra_op_threads=ONNX_INTRA_OP_NUM_THREADS,+ onnx_inter_op_threads=ONNX_INTER_OP_NUM_THREADS,+ )tokenizer = AutoTokenizer.from_pretrained(cache_dir)- model = ORTModelForSequenceClassification.from_pretrained(cache_dir)+ model = ORTModelForSequenceClassification.from_pretrained(cache_dir, session_options=session_options)_pipeline_cache["pipe"] = pipeline("sentiment-analysis",