Skip to content

feat(serving,calibration): pool upstream HTTP/1.1 keep-alive, preserve slot logprobs, and add Platt prior-bias calibration - #69

Closed
jigyasujuneja wants to merge 2 commits into
mainfrom
feat/serving-keepalive-logprobs-and-platt
Closed

jigyasujuneja wants to merge 2 commits into
mainfrom
feat/serving-keepalive-logprobs-and-platt

Conversation

@jigyasujuneja

Copy link
Copy Markdown
Collaborator

Summary

Addresses three serving and calibration bottlenecks observed on high-concurrency and base-rate-shifted structured decision workloads:

  1. Persistent per-thread HTTP/1.1 keep-alive to upstream vLLM (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.
    • Eliminates N * samples loopback TCP handshakes and TIME_WAIT churn under samples=4..8 concurrency.
  2. Pre-softmax slot log-odds diagnostics & opt-in Product-of-Experts ("combine": "poe"):
    • Exposes centered pre-exp label log-probabilities in diagnostics.questions[qid].logprobs so extreme SD-RL logits (p == 1.0 in float64) keep lossless rank order and log-odds gaps for post-hoc calibration and AUROC evaluation.
    • Adds optional "combine": "poe" (softmax of mean per-draw log-odds) alongside the default "linear" probability mean.
  3. Policy-level Platt / vector prior-bias calibration (pkg/client/temperature.go, cmd/policy_temperature.go, scripts/platt_prior_calibration.py):
    • Upgrades StructuredDecisionResponse.ApplyTemperature / ApplyCalibration to read Diagnostics.Questions[id].Logprobs when present (avoiding 1e-12 clamp loss) and apply optional "prior_bias" (z'_k = z_k / T + b_{q,k}) across CLI, HTTP gateway, MCP, and Studio while leaving Stage-2 cascade-resolved slots untouched.
    • Adds scripts/platt_prior_calibration.py for 5-fold out-of-fold temperature vs. Platt prior-bias fitting and asymmetric zero-false-allow margin evaluation on JSONL evaluation receipts.

Verification

  • bash 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 ./... && go -C examples vet ./... && go -C examples test ./...
  • python3 -W default -m unittest deploy/cloudrun/server/test_upstream_keepalive_and_logprobs.py scripts/test_platt_prior_calibration.py
  • Full deploy/cloudrun/server/test_*.py and scripts/matrix/test_matrix.py suite (73 tests OK)

…e slot logprobs, and add Platt prior-bias calibration
@ghchinoy

Copy link
Copy Markdown
Collaborator

Thanks for putting this together, @jigyasujuneja! The thread-local HTTP/1.1 keep-alive pool (_upstream_post_json) + TCP_NODELAY in structured_server.py and the lossless diagnostics.questions[qid].logprobs wire-through into StructuredDecisionResponse.ApplyTemperature are great improvements.

Could we split this PR so the serving keep-alive + lossless logprobs path can be canary-verified and merged cleanly right away, while separating "prior_bias" and "combine": "poe" for a design check? Specifically:

  1. prior_bias vs. Stage-2 cascade gating order (cmd/policy_temperature.go):
    • applyPolicyTemperature runs after Stage 1 and after the Stage 2 Gemini cascade check (cascade_mode: "entropy"). That works for pure temperature scaling because temperature is argmax-preserving and cascade_threshold gates on raw Stage 1 entropy.
    • By contrast, prior_bias shifts the decision boundary ($z'k = z_k / T + b{q,k}$) and can flip the winning answer. If Stage 1 is confident before prior_bias (for example, p(yes) = 0.985, $H / \ln 2 = 0.11 < 0.16$, so Stage 2 cascade does not fire) and a negative prior_bias shifts the slot to an ambiguous 50/50 split (p(yes) = 0.50, $H / \ln 2 = 1.00$), applyPolicyTemperature will flip the answer after the cascade stage has already been skipped—returning a 50/50 unescalated answer. It also isn't wired into Decision Studio's client-side calibration state (4-surface parity).
  2. Keep #69 focused on HTTP/1.1 keep-alive + lossless diagnostics.questions[qid].logprobs:
    • If you scope #69 to:
      1. _upstream_post_json HTTP/1.1 keep-alive pooling + TCP_NODELAY in deploy/cloudrun/server/structured_server.py & dgem.patch (+ test_upstream_keepalive_and_logprobs.py), and
      2. diagnostics.questions[qid].logprobs in structured_server.py + QuestionDiagnostic.Logprobs in pkg/client/types.go + lossless diagLP consumption in StructuredDecisionResponse.ApplyTemperature (pkg/client/temperature.go),
        we can run the side-by-side serving canary check and merge #69 promptly, then evaluate "prior_bias" (with pre-cascade vs. post-cascade gating semantics) and "combine": "poe" on real hosted receipts in a dedicated follow-up.

@jigyasujuneja

Copy link
Copy Markdown
Collaborator Author

Thanks for catching the post-cascade prior_bias boundary-shift interaction, @ghchinoy! Agreed—because prior_bias can move an unescalated confident Stage-1 slot into an ambiguous 50/50 split if applied after cascade_mode: "entropy", any boundary-shifting offset needs pre-cascade gating semantics plus 4-surface Studio parity.

Since GoogleCloudPlatform/dgem switched to fork-based contributor permissions (push: false on in-org branches), I rebased onto main (259fa68), scoped strictly to _upstream_post_json HTTP/1.1 keep-alive + TCP_NODELAY and lossless diagnostics.questions[qid].logprobs -> StructuredDecisionResponse.ApplyTemperature (removing "prior_bias", cmd/policy_temperature*, scripts/platt_prior_calibration*, and "combine": "poe", and tightening multi-sample decide_group to logsumexp over draws), and opened #81 from my fork (maintainer_can_modify: true). Feel free to canary/merge #81 and close #69!

@ghchinoy

Copy link
Copy Markdown
Collaborator

Closing as superseded: the HTTP/1.1 upstream keep-alive pool and lossless diagnostics.questions[qid].logprobs -> ApplyTemperature path merged via #81 and follow-up #86. The remaining "prior_bias" (Platt/vector scaling + pre-cascade gating semantics) and "combine": "poe" proposals will be evaluated separately on real hosted receipts before opening a dedicated follow-up PR.

@ghchinoy ghchinoy closed this Nov 21, 2026
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