Skip to content

feat(serving,client): pool upstream HTTP/1.1 keep-alive with TCP_NODELAY and preserve lossless slot logprobs - #81

Merged
ghchinoy merged 1 commit into
GoogleCloudPlatform:mainfrom
jigyasujuneja:feat/serving-keepalive-logprobs-and-platt
Nov 21, 2026
Merged

ghchinoy merged 1 commit into
GoogleCloudPlatform:mainfrom
jigyasujuneja:feat/serving-keepalive-logprobs-and-platt

Conversation

@jigyasujuneja

Copy link
Copy Markdown
Collaborator

Summary

Follow-up from fork addressing review feedback on #69 (since GoogleCloudPlatform/dgem switched to fork-based contributor pushes), scoped strictly to the serving HTTP/1.1 keep-alive path and lossless diagnostics.questions[qid].logprobs wire-through into StructuredDecisionResponse.ApplyTemperature ("prior_bias" and "combine": "poe" are split out for a separate pre-cascade gating + 4-surface Studio design):

  1. Persistent per-thread HTTP/1.1 keep-alive + TCP_NODELAY (deploy/cloudrun/server/structured_server.py, dgem.patch):
    • Replaces per-read urllib.request.urlopen socket creation in upstream_chat and upstream_completions with a thread-local http.client keep-alive connection pool (_upstream_post_json), reconnecting once on stale socket resets and preserving exact urllib.error.HTTPError 422/400/502 behavior.
    • Sets TCP_NODELAY on both accepted HTTP/1.1 client connections (_Server.get_request) and upstream vLLM connections.
  2. Lossless pre-softmax slot log-probabilities (structured_server.py, pkg/client/types.go, pkg/client/temperature.go):
    • Exposes centered label log-probabilities in diagnostics.questions[qid].logprobs (computed via logsumexp across draws when samples > 1 so exp(logprobs) remains strictly argmax-consistent with the arithmetic-mean answers[qid].probabilities while preserving exact log-odds when p == 1.0 in float64).
    • Consumes Diagnostics.Questions[id].Logprobs inside StructuredDecisionResponse.ApplyTemperature (pkg/client/temperature.go) so temperature scaling avoids 1e-12 clamp distortion on saturated float probabilities.

Verification

  • ./scripts/regen_serving_patch.sh (clean GNU diff -u regeneration and re-application check against structured_server.upstream.py)
  • unset DGEM_VERTEX_URL && go vet ./... && go test -race ./... && go -C examples vet ./... && go -C examples test -race ./...
  • python3 -m unittest -v deploy/cloudrun/server/test_upstream_keepalive_and_logprobs.py deploy/cloudrun/server/test_server_key.py deploy/cloudrun/server/test_media_fields.py deploy/cloudrun/server/test_upstream_errors.py deploy/cloudrun/server/test_predict_route.py deploy/cloudrun/server/test_ready_route.py
  • make check-public

…LAY and preserve lossless slot logprobs

- Replace per-read urllib.request.urlopen socket creation in deploy/cloudrun/server/structured_server.py with thread-local http.client HTTP/1.1 keep-alive pooling (_upstream_post_json) and TCP_NODELAY on both accepted client sockets and upstream vLLM connections.
- Expose exact centered log-probabilities (via single-draw normalization and multi-sample logsumexp over per-draw probabilities) in diagnostics.questions[qid].logprobs so extreme float64 probabilities (p == 1.0) retain lossless log-odds gaps while remaining strictly argmax-consistent with answers[qid].probabilities across multi-sample reads.
- Wire QuestionDiagnostic.Logprobs into pkg/client/types.go and consume lossless diagnostic logprobs in StructuredDecisionResponse.ApplyTemperature (pkg/client/temperature.go) without 1e-12 clamping loss.
@jigyasujuneja
jigyasujuneja force-pushed the feat/serving-keepalive-logprobs-and-platt branch from 5bafa38 to b6d7399 Compare November 21, 2026 01:31
@ghchinoy

Copy link
Copy Markdown
Collaborator

Thanks for scoping this cleanly to keep-alive + lossless diagnostics.questions[qid].logprobs + ApplyTemperature, @jigyasujuneja!

Approving the pending CI workflows so we can review and merge this PR as-is, and we'll open an immediate follow-up PR on main to address two things we spotted in review:

  1. Shared bounded LIFO upstream pool instead of _UPSTREAM_LOCAL = threading.local(): decide_group calls read_many, which spawns fresh short-lived threading.Threads per read even for samples: 1 (and ThreadingHTTPServer / ThreadPoolExecutor also spawn per-connection/per-group threads), so a thread-local cache won't reuse connections across requests and leaves sockets open until GC. Switching _upstream_post_json to a shared bounded LIFO pool (plus skipping thread spawn in read_many when n == 1) and testing reuse across read_many/threads will make the keep-alive pooling take effect end-to-end.
  2. CI wiring: Adding deploy/cloudrun/server/test_upstream_keepalive_and_logprobs.py to .github/workflows/build.yml (test-matrix) and AGENTS.md so CI executes it on every PR.

@ghchinoy
ghchinoy merged commit a635a81 into GoogleCloudPlatform:main Nov 21, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants