Optimize RF-DETR segmentation pipeline#48
Open
aseembits93 wants to merge 58 commits into
Open
Conversation
Replace the per-frame PIL-bilinear-antialias + to_tensor + normalize chain
in the RF-DETR TRT instance-segmentation model with a single Triton
kernel that resizes, swaps BGR↔RGB, scales by 1/255, and applies
ImageNet normalization — writing straight into the preallocated TRT
input buffer.
Byte-exact port of PIL's separable bilinear-antialias resize
(PRECISION_BITS=22, int32 fixed-point, uint8 quantization between the
horizontal and vertical passes). The horizontal uint8 intermediate
lives in registers.
Correctness
- Preproc max abs error vs PIL: 4.77e-7 (fp32 ULP on the final
/255+normalize step; the uint8 resize result is byte-identical).
- Full coco/val2017 detection parity (rfdetr-seg-nano, conf=0.4):
26,721 / 26,721 matched at IoU>0.5, mean box IoU 1.0000,
|Δscore| 0, 0 class-id disagreements, all matched masks
pixel-identical.
Performance (vehicles_312px.mp4, 538 frames)
- Baseline (PIL path): 76.25 fps
- Triton fast path: 99.83 fps (+31%)
- Preproc microbench (1080p → 312²): 27.0 ms → 2.8 ms per frame (~10×)
Scope
- Gated on: single-image numpy uint8 HWC input, stretch/letterbox/
center-crop/letterbox-reflect resize modes (all collapse to a single
PIL stretch when dataset_version_resize_dimensions is None, verified
via synthetic-package test), no static_crop/grayscale/contrast,
3-channel, scaling_factor in {None, 255}, normalization set.
- Falls back to the existing PIL-based pre_process_network_input
when any precondition fails.
Also adds the benchmark driver
development/stream_interface/rfdetr_nano_seg_trt_workflow.py used to
measure the above numbers.
INFERENCE_MODELS_RFDETR_TRITON_PREPROC_ENABLED (default true). Setting it to false short-circuits _try_fast_preprocess so every call falls back to the PIL reference path — useful for A/B benchmarking and as an escape hatch if the fused kernel is ever implicated in a regression. e2e on vehicles_312px.mp4 (538 frames, rfdetr-seg-nano TRT, mean of 3): ON (default): 98.57 fps OFF (env=false): 76.60 fps Δ: +28.7% / −2.90 ms/frame
…new-model-manager (roboflow#2406)
* fix batch processing * style: run black on asset_library_attributes v1
* Add volume mount support to inference server container Adds a --volume / -v CLI flag to `inference server start` and a corresponding `volumes` parameter to `start_inference_container()`, allowing users to bind-mount host directories into the container. * Apply black formatting * Remove unused import * Document --volume flag in server CLI docs
* Expose NumberInRange operator in workflow builder UI The (Number) in range operator was already implemented in the evaluation engine and BinaryStatement union but was not included in the introspection export, making it invisible to the workflow builder UI. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Implement NumberInRange operator in query language backend Adds the NumberInRange BinaryOperator class and its evaluation lambda so the operator exposed in the workflow builder UI has a working backend. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Add roboflow_core/current_time@v1 Workflow block New formatter block that outputs the current date/time for a user-selected timezone. Inputs a curated IANA timezone (literal or selector, default UTC) and returns a timezone-aware timestamp plus iso_string, date, and time strings. Uses stdlib zoneinfo (backports.zoneinfo for py<3.9) and adds tzdata so the timezone database is available on slim/Windows runtimes. Curated dropdown options expose friendly UTC-offset labels via values_metadata. Registered in loader.py. Includes unit tests and a full execution-engine integration test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Remove zoneinfo backport dependency --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* init rfdetr keypoints * post process keypoints * bump version * update inference-model version to 0.29.0-rc1 * update requirements cpu and gpu with inference_models 0.29.0rc1 * upd uv lock * add inf adapter for rfdetr keypoint preview * normalize kp scores * upd version * Bump inference-models version * Update inference dependencies --------- Co-authored-by: Paweł Pęczek <146137186+PawelPeczek-Roboflow@users.noreply.github.com> Co-authored-by: Paweł Pęczek <pawel@roboflow.com>
d01828a to
9582947
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
This is the top PR in the RF-DETR optimization stack:
main <- opt-python-postproc <- opt-preprocess <- opt-pipeline-integrationIt integrates the optimized Triton postprocess and preprocess branches into the streaming workflow path and removes the remaining CPU/GPU scheduling bubbles visible in Nsight. End-to-end numbers for this PR represent the full stack: Triton postproc, Triton preproc, CUDA graph execution, depth-2 pipeline scheduling, sparse RLE-to-polygon conversion, and workflow response-path optimizations.
Key changes:
postprocess, before CPU response finalization.cv2.findContours.Type of Change
Testing
Test details:
Reference command on
main:Candidate command on
opt-pipeline-integration:ENABLE_AUTO_CUDA_GRAPHS_FOR_TRT_BACKEND=true \ INFERENCE_MODELS_RFDETR_TRITON_PREPROC_ENABLED=true \ INFERENCE_MODELS_RFDETR_TRITON_POSTPROC_ENABLED=true \ RFDETR_PIPELINE_DEPTH=2 \ PYTHONPATH=/app/helloworld/inference:/app/helloworld/inference/inference_models \ python development/stream_interface/rfdetr_nano_seg_trt_workflow.py \ --video_reference <video> \ --backend trtvehicles_312px.mp4 (538 frames, src 312x176):
vehicles_720p.mp4 (538 frames, src 1280x720):
vehicles_1080p.mp4 (538 frames, src 1920x1080):
mainflags-offenv PARITY_MODEL_PATH=/app/helloworld/inference/rfdetr-seg-nano-orin-trt-package \ PYTHONPATH=/app/helloworld/inference:/app/helloworld/inference/inference_models \ python development/stream_interface/rfdetr_coco_same_shape_parity.py \ --base-ref main \ --candidate-ref opt-pipeline-integration \ --height 480 \ --width 640 \ --image-count 1000mainflags-offenv PARITY_MODEL_PATH=/app/helloworld/inference/rfdetr-seg-nano-orin-trt-package \ PYTHONPATH=/app/helloworld/inference:/app/helloworld/inference/inference_models \ python development/stream_interface/rfdetr_workflow_video_parity.py \ --video_reference vehicles_1080p.mp4 \ --base-ref main \ --candidate-ref opt-pipeline-integrationThis compares serialized workflow sink outputs frame by frame, ignoring generated detection IDs and matching detections by same class and box IoU > 0.5.
env PYTHONPATH=/app/helloworld/inference:/app/helloworld/inference/inference_models \ python development/stream_interface/rfdetr_rle_to_poly_microbenchmark.py \ --mode replay \ --cases-dir <captured-cases-dir> \ --repeats 3 \ --warmup-repeats 1Each row uses 100 captured calls and 300 timed replays.
PYTHONPATH=/app/helloworld/inference:/app/helloworld/inference/inference_models \ python -m pytest -q \ tests/inference/unit_tests/core/interfaces/stream/test_workflows.py \ tests/inference/unit_tests/core/interfaces/stream/test_interface_pipeline.py::test_inference_pipeline_drain_enqueues_flush_results_with_bound_frames \ tests/inference/unit_tests/core/interfaces/stream/test_interface_pipeline.py::test_resolve_prediction_futures_recursively_resolves_nested_values \ tests/inference/unit_tests/core/interfaces/stream/test_interface_pipeline.py::test_inference_pipeline_close_calls_handler_close_hook \ tests/inference/unit_tests/core/models/test_inference_models_adapters.py \ tests/inference/unit_tests/core/utils/test_rle_to_polygon.pyResult:
17 passed.PYTHONPATH=/app/helloworld/inference/inference_models:/app/helloworld/inference \ python -m pytest -q tests/unit_tests/test_configuration.py \ tests/unit_tests/models/test_instance_segmentation_future.pyResult:
16 passed.Latest 1080p review-cleanup verification:
ENABLE_AUTO_CUDA_GRAPHS_FOR_TRT_BACKEND=true \ INFERENCE_MODELS_RFDETR_TRITON_PREPROC_ENABLED=true \ INFERENCE_MODELS_RFDETR_TRITON_POSTPROC_ENABLED=true \ RFDETR_PIPELINE_DEPTH=2 \ PYTHONPATH=/app/helloworld/inference/inference_models:/app/helloworld/inference \ python development/stream_interface/rfdetr_nano_seg_trt_workflow.py \ --video_reference vehicles_1080p.mp4 \ --backend trtResult:
frames=538 elapsed=6.50s fps=82.72.Note: after rebasing onto newer
main, the workflow benchmark explicitly setsenforce_dense_masks_in_inference_models=Falseso the optimized sparse/RLE postprocess path is measured instead of the newer dense-mask workflow default.How It Works
Pipeline scheduling
With
RFDETR_PIPELINE_DEPTH=2, depth means one CPU stage and one GPU stage in flight. CPU prepares frameN, submits GPU work for frameN, immediately prepares/submits frameN+1, and only then finalizes the older response. The key ordering fix is submitting postprocess GPU work before releasing an older response for CPU finalization.Sparse RLE to polygon
The old response path decoded each COCO RLE into a full
H x Wdense mask before callingcv2.findContours. The new path parses uncompressed RLE counts into foreground intervals, materializes only the tight foreground crop, and calls OpenCV with an offset so output polygon coordinates match the legacy dense path.Checklist
Additional Context
This PR intentionally does not introduce new Triton preproc/postproc kernels; those are in the two lower PRs. This branch wires them into the stream pipeline and optimizes the remaining CPU response path so the GPU queue stays close to full.