Skip to content

perf(inference_models): batch instance-seg RLE mask encoding#2517

Draft
alexnorell wants to merge 2 commits into
mainfrom
perf/seg-rle-batch-encode
Draft

perf(inference_models): batch instance-seg RLE mask encoding#2517
alexnorell wants to merge 2 commits into
mainfrom
perf/seg-rle-batch-encode

Conversation

@alexnorell

Copy link
Copy Markdown
Contributor

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, whose torch_mask_to_coco_rle performs a .cpu() sync per mask (lengths.cpu().tolist() + a GPU scalar read). On Jetson those per-detection syncs serialize the GPU ~2·N times 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

  • New torch_masks_to_coco_rle_batch(masks) in models/common/rle_utils.py: a single [N,H,W]→[H,W,N] device→host transfer + one vectorized pycocotools.mask.encode.
  • Switch the RLE post-process to the already-existing batched align_instance_segmentation_results (the same routine the dense path uses) followed by the batched encode, replacing the per-detection generator loop.
  • Applied to: RF-DETR (the non-Triton fallback path; the Triton fast path on main is 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-mask torch_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).
  • No accuracy change expected; this is a scheduling/transfer optimization only.

Draft: opening for review of the approach + scope before marking ready.

alexnorell and others added 2 commits June 23, 2026 12:37
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants