-
Notifications
You must be signed in to change notification settings - Fork 0
fix: cap inference-pod CPU threads and offload blocking event-loop calls #442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c212e12
fix: cap inference-pod CPU threads and offload blocking calls from ev…
honeytung 1183b18
Automatically reformatting code with black and isort
0bf6232
Parameterize inference-pod CPU threads + request; drop CPU limit
honeytung 25f8e02
Make inference-pod CPU request optional (default BestEffort)
honeytung 9b72616
Merge branch 'main' into edge-endpoint-perf-implementation
honeytung 40cf856
Remove cpuRequest; inference pods are always BestEffort
honeytung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query, Request, status | ||
| from groundlight import Groundlight | ||
| from model import ImageQuery | ||
| from starlette.concurrency import run_in_threadpool | ||
|
|
||
| from app.core.app_state import ( | ||
| AppState, | ||
|
|
@@ -114,7 +115,9 @@ async def post_image_query( # noqa: PLR0913, PLR0915, PLR0912 | |
|
|
||
| # Ensure that detector_id has correct casing by pulling the detector ID out of detector_metadata | ||
| # get_detector_metadata returns the correctly-cased, canonical detector ID | ||
| detector_metadata = get_detector_metadata(detector_id=detector_id, gl=gl) # NOTE: API call (once, then cached) | ||
| detector_metadata = await run_in_threadpool( | ||
| get_detector_metadata, detector_id=detector_id, gl=gl | ||
| ) # NOTE: API call (once, then cached) | ||
| # Refresh against the caller-supplied key, since that is the key this cache entry is stored under. | ||
| background_tasks.add_task(refresh_detector_metadata_if_needed, detector_id, gl) | ||
| detector_id = detector_metadata.id | ||
|
|
@@ -173,11 +176,15 @@ async def post_image_query( # noqa: PLR0913, PLR0915, PLR0912 | |
| # If human review is required, we should skip edge inference completely | ||
| logger.debug("Received human_review=ALWAYS. Skipping edge inference.") | ||
| record_activity_for_metrics(detector_id, activity_type="escalations") | ||
| elif app_state.edge_inference_manager.inference_is_available(detector_id=detector_id): | ||
| elif await run_in_threadpool(app_state.edge_inference_manager.inference_is_available, detector_id=detector_id): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually I would like to get rid of this readiness cache. I have a PR that should do the trick: #419 But for now this seem fine. |
||
| # -- Edge-model Inference -- | ||
| logger.debug(f"Local inference is available for {detector_id=}. Running inference...") | ||
| results = app_state.edge_inference_manager.run_inference( | ||
| detector_id=detector_id, image_bytes=image_bytes, content_type=content_type, mode=detector_metadata.mode | ||
| results = await run_in_threadpool( | ||
| app_state.edge_inference_manager.run_inference, | ||
| detector_id=detector_id, | ||
| image_bytes=image_bytes, | ||
| content_type=content_type, | ||
| mode=detector_metadata.mode, | ||
| ) | ||
| ml_confidence = results["confidence"] | ||
| class_index = results["label"] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,6 +75,17 @@ spec: | |
| value: "1" | ||
| - name: EDGE_COMPILE_ENABLED | ||
| value: "{{ .Values.edgeCompileEnabled }}" | ||
| # Cap PyTorch/OpenMP/MKL/OpenBLAS intra-op threads so that many inference pods | ||
| # don't oversubscribe the node's cores. See values.yaml inferenceDeployment.cpuThreads. | ||
| - name: OMP_NUM_THREADS | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good work! |
||
| value: "{{ .Values.inferenceDeployment.cpuThreads }}" | ||
| - name: MKL_NUM_THREADS | ||
| value: "{{ .Values.inferenceDeployment.cpuThreads }}" | ||
| - name: OPENBLAS_NUM_THREADS | ||
| value: "{{ .Values.inferenceDeployment.cpuThreads }}" | ||
| # No CPU requests or limits: inference pods run BestEffort. Threads are bounded by | ||
| # cpuThreads and most detectors are idle at any instant, so the scheduler launches | ||
| # every detector and the OS time-slices the occasional simultaneous burst. | ||
| volumeMounts: | ||
| - name: edge-endpoint-persistent-volume | ||
| mountPath: *modelRepository | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems unnecessary, but maybe it doesn't hurt.
I ran a load test with this code and didn't see any issues.