perf(inference_models): batch instance-seg RLE mask encoding#2517
Draft
alexnorell wants to merge 2 commits into
Draft
perf(inference_models): batch instance-seg RLE mask encoding#2517alexnorell wants to merge 2 commits into
alexnorell wants to merge 2 commits into
Conversation
Instance-segmentation post-processing encoded masks one detection at a time via align_instance_segmentation_results_to_rle_masks, whose torch_mask_to_coco_rle does a device->host .cpu() sync per detection. On Jetson those per-detection syncs serialize the GPU N times per frame and dominate seg post-processing. Replace it with the batched align_instance_segmentation_results plus a new torch_masks_to_coco_rle_batch helper that encodes all masks with a single device->host transfer and one vectorized pycocotools.encode call. Output is byte-identical (locked by the added equivalence tests). Applied to RF-DETR (non-Triton fallback) and YOLOv5/7/8/26 + YOLACT.
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
Batch the instance-segmentation RLE mask encoding so post-processing does one device→host transfer per image instead of one per detection.
Why
Instance-seg post-processing encoded masks one detection at a time via
align_instance_segmentation_results_to_rle_masks, whosetorch_mask_to_coco_rleperforms a.cpu()sync per mask (lengths.cpu().tolist()+ a GPU scalar read). On Jetson those per-detection syncs serialize the GPU~2·Ntimes per frame and dominate seg post-processing — a major contributor to the ~50% throughput drop seen running rf-detr-seg on a JetPack 6.2 device vs. detection.How
torch_masks_to_coco_rle_batch(masks)inmodels/common/rle_utils.py: a single[N,H,W]→[H,W,N]device→host transfer + one vectorizedpycocotools.mask.encode.align_instance_segmentation_results(the same routine the dense path uses) followed by the batched encode, replacing the per-detection generator loop.mainis unchanged) and YOLOv5 / YOLOv7 / YOLOv8 / YOLO26 / YOLACT.Behavior-preserving
Output is byte-identical — same boxes, same compressed RLE
counts. Locked by a new test,tests/unit_tests/models/common/test_rle_batch_encode.py:torch_masks_to_coco_rle_batch== per-masktorch_mask_to_coco_rle(random / single / all-zero / all-one / empty).align_instance_segmentation_results+ batch encode == the per-detection generator, for both boxes and RLE, across no-crop / scaled / static-crop / odd-dims / single / empty cases.Test plan
pytest tests/unit_tests/models/common/test_rle_batch_encode.py tests/unit_tests/models/common/test_rle_utils.py→ green (CUDA cases skip on CPU).