Skip to content

Add C++ inference recipes for LT-DETR object detection#909

Draft
gabrielfruet wants to merge 3 commits into
mainfrom
gabriel-trn-2330-create-c-inference-scripts
Draft

Add C++ inference recipes for LT-DETR object detection#909
gabrielfruet wants to merge 3 commits into
mainfrom
gabriel-trn-2330-create-c-inference-scripts

Conversation

@gabrielfruet

@gabrielfruet gabrielfruet commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

What has changed and why?

C++ inference recipes for LT-DETR object detection under examples/cpp/, for ONNX Runtime (CUDA EP) and TensorRT (EP), both with zero-copy GPU input allocation. Postprocessing (top-k over query×class, cxcywh→xyxy, class remap) mirrors ObjectDetectionPostprocessor.decode() exactly. These are copy-paste-and-edit recipes (hardcoded constants, no CLI/JSON parsing), matching the style of the existing export notebooks. Linked from object_detection_export.ipynb.

Known limitation, not fixed here: model.export_tensorrt(precision="fp32") on ltdetrv2-s-coco currently produces a numerically degraded engine (max score drops from ~0.97 to ~0.32) — TensorRT's weakly-typed builder appears to pick FP16 tactics in the transformer decoder despite fp32 being requested. strongly_typed=True fixes it. Tracking that separately; the TensorRT recipe here is verified correct against a strongly_typed engine.

How has it been tested?

Built and ran both recipes on a GPU node (Docker, ONNX Runtime 1.20.1 + TensorRT 10.13.3.9) against ltdetrv2-s-coco on the COCO 000000039769.jpg sample. ONNX Runtime recipe matches model.predict(threshold=0.6) almost exactly (scores within ~0.01, boxes within ~0.1px). TensorRT recipe matches Python's TensorRT backend byte-for-byte given the same engine.

Did you update CHANGELOG.md?

  • Yes

Did you update the documentation?

  • Yes

Add ONNX Runtime CUDA EP and TensorRT EP recipes under examples/cpp/,
mirroring the postprocessing contract of ObjectDetectionPostprocessor
and the existing export notebook.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ec14cfff81

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread examples/cpp/cmake/FindTensorRT.cmake
@gabrielfruet

Copy link
Copy Markdown
Contributor Author

Verification: compute-02-ubuntu-4x4090 (1 GPU, Docker — CUDA 12.4.1 devel, ONNX Runtime GPU 1.20.1 tarball, TensorRT 10.13.3.9 via apt), ltdetrv2-s-coco on COCO 000000039769.jpg.

ONNX Runtime recipe vs model.predict(threshold=0.6): cat 0.967/0.963, couch 0.962, remote 0.915/0.855 — matches within ~0.01 score, boxes within ~0.1px.

TensorRT recipe: default precision="fp32" export is weakly-typed and picks FP16 tactics in the decoder, so max score collapses to ~0.32 (should be ~0.97). Rebuilt with strongly_typed=True: couch score=0.962, box=(0.2, 1.1, 641.9, 474.3) — matches Python's 0.9618 / [1.24, 1.30, 639.52, 474.11]. Repro'd the precision bug independently with a pure-Python TensorRT harness mirroring TensorRTBackend, so it's an export-side issue, not the C++ code — this recipe is verified correct against the strongly_typed engine. Not fixing the export heuristic here; will open a separate Linear ticket for it.

No screenshots — verification ran headless on the cluster and the image was dropped afterward.

@gabrielfruet
gabrielfruet marked this pull request as draft July 24, 2026 19:29
@gabrielfruet

gabrielfruet commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

Correction to my earlier comment — the TensorRT validation claim was overstated.

  • strongly_typed=True doesn't fix the precision bug, just masks it partially: top scores land at 0.91/0.72 instead of 0.97/0.96, and 3 of 5 ONNX Runtime detections are still missing. Couch happened to land close, which is all the earlier comment checked.
  • fp32_attention_scores=True (forcing decoder attention to FP32) has no effect. Ruled out.
  • Root cause, found by bisecting: marking subsets of the ~1600 intermediate ONNX tensors as outputs and rebuilding. It's RT-DETR's encoder-to-decoder anchor selection block (class-score head -> ReduceMax -> TopK). TensorRT fuses it into one kernel and computes the anchor-ranking scores at reduced precision, so different anchors get picked than in the reference model, corrupting every downstream detection.

Only the ONNX Runtime recipe is verified against model.predict(). TensorRT fix tracked in TRN-2341 — not blocking this PR, the recipe code faithfully reproduces whatever engine it's given.

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.

1 participant