Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ All notable changes to this project will be documented in this file.

### Added
- `docs/TensorRT.md` now includes steps for running the ONNX Deploy path on GPU
with `onnxruntime-gpu` (CUDA/cuDNN version matching, CUDA 11.8 vs 12.x).
with `onnxruntime-gpu` (CUDA/cuDNN version matching, CUDA 11.8 vs 12.x), and a
"Known limitation" note that two-stage detectors (Faster/Mask R-CNN) fail to
build a TensorRT engine because their `RoiAlign` op needs an unregistered plugin.

### Fixed
- fp16 ONNX export crashed: first with `module 'onnxconverter_common.float16' has
no attribute 'convert_float_to_fp16'` (wrong API name), then — once corrected to
`convert_float_to_float16(..., keep_io_types=True)` — with an invalid fp16 graph
for Detectron2 models (type-mismatched Cast nodes that onnxruntime rejects). The
fp16 pass is now fail-safe: it converts to a temp file, verifies it loads, and
only then replaces the model — otherwise it keeps the valid fp32 model and logs a
warning. onnxruntime validation is also non-fatal so it can't fail a completed export.

## [0.4.2] - 2026-05-31

Expand Down
55 changes: 44 additions & 11 deletions alchemydetect/core/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import importlib.util
import json
import os
import shutil
from datetime import datetime, timezone
from pathlib import Path
Expand Down Expand Up @@ -395,21 +396,47 @@ def inference(model, inputs):


def _convert_fp16(onnx_path, log_fn):
"""Convert an ONNX model to fp16 in place, if the tooling is available."""
"""Convert an ONNX model to fp16, keeping the fp32 model if the result is invalid.

onnxconverter-common's float16 pass does not reliably handle complex graphs
(e.g. Detectron2 detectors): it can emit type-mismatched Cast nodes that fail
to load. To never ship a broken model, we convert to a temp file, verify it
actually loads, and only then replace the original — otherwise we keep fp32.
"""
if not is_onnx_available():
log_fn("onnx package not installed; skipping fp16 conversion.")
return
try:
import onnx
from onnxconverter_common import float16
except ImportError:
log_fn("onnxconverter-common not installed; skipping fp16 conversion.")
return

log_fn("Converting ONNX graph to fp16...")
log_fn("Converting ONNX graph to fp16...")
tmp_path = onnx_path + ".fp16.tmp"
try:
model = onnx.load(onnx_path)
model = float16.convert_float_to_fp16(model)
onnx.save(model, onnx_path)
# keep_io_types=True leaves the input/output tensors as float32 so the
# deployment runtime's float32 preprocessing still feeds the model correctly.
fp16_model = float16.convert_float_to_float16(model, keep_io_types=True)
onnx.save(fp16_model, tmp_path)
# Verify the fp16 graph actually loads before replacing the fp32 model.
if is_onnxruntime_available():
import onnxruntime as ort

ort.InferenceSession(tmp_path, providers=["CPUExecutionProvider"])
else:
onnx.checker.check_model(tmp_path)
os.replace(tmp_path, onnx_path)
log_fn("fp16 conversion complete.")
except ImportError:
log_fn("onnxconverter-common not installed; skipping fp16 conversion.")
except Exception as e:
if os.path.exists(tmp_path):
os.remove(tmp_path)
log_fn(
"fp16 conversion produced an invalid graph for this model; keeping fp32. "
f"({type(e).__name__}: {str(e)[:150]})"
)


def validate_onnx(onnx_path, input_size, log_fn):
Expand All @@ -426,11 +453,17 @@ def validate_onnx(onnx_path, input_size, log_fn):
import onnxruntime as ort

log_fn("Validating with onnxruntime...")
session = ort.InferenceSession(onnx_path, providers=["CPUExecutionProvider"])
height, width = input_size
dummy = (np.random.rand(3, height, width) * 255.0).astype(np.float32)
input_name = session.get_inputs()[0].name
outputs = session.run(None, {input_name: dummy})
try:
session = ort.InferenceSession(onnx_path, providers=["CPUExecutionProvider"])
height, width = input_size
dummy = (np.random.rand(3, height, width) * 255.0).astype(np.float32)
input_name = session.get_inputs()[0].name
outputs = session.run(None, {input_name: dummy})
except Exception as e:
# Validation is a best-effort sanity check run after the artifacts are
# already written; never fail the whole export because of it.
log_fn(f"Validation could not run (model still written): {type(e).__name__}: {str(e)[:150]}")
return None

shapes = {}
for meta, arr in zip(session.get_outputs(), outputs):
Expand Down
25 changes: 25 additions & 0 deletions docs/TensorRT.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ Triggered from the Deploy tab when you load a `.engine`:
onnxruntime-GPU; this is what genuinely beats a PyTorch GPU `.pth` (often 2–5×).
- **No Python per-op overhead** — one compiled artifact, not Python-dispatched ops.

### ⚠️ Known limitation — two-stage detectors (R-CNN) don't convert out of the box

Faster R-CNN and Mask R-CNN export to an ONNX graph that contains **`RoiAlign`**
(and NMS) operators. TensorRT's ONNX parser maps these to **plugins that are not
registered by default**, so the engine build fails at parse time, e.g.:

```
UNSUPPORTED_NODE: ROIAlign plugin was not found in the plugin registry!
(node /roi_heads/box_pooler/.../RoiAlign)
```

AlchemyDetect's exporter does not register or supply these plugins. Converting
two-stage Detectron2 detectors to TensorRT is a known hard case that requires
NVIDIA's dedicated tooling and/or custom plugins, and may still hit unsupported
ops. **So TensorRT export currently only works for models whose ops TensorRT
supports natively; Faster/Mask R-CNN is not supported out of the box.** RetinaNet
(single-stage) has a better chance but is unverified.

This is independent of the GPU/version issue below — even on a supported GPU with
TensorRT 8.6, an R-CNN model still hits the RoiAlign plugin error.

---

## 2. Prerequisites (read first)
Expand Down Expand Up @@ -303,6 +324,10 @@ reads `Runtime: onnxruntime — CUDAExecutionProvider` instead of `CPUExecutionP
- **`Could not load ... cublasLt64_12.dll` / `cudnn... is missing`** — your CUDA/
cuDNN runtime doesn't match the TensorRT (or onnxruntime-gpu) build. Align the
CUDA major version across CUDA Toolkit, cuDNN, TensorRT, and PyTorch.
- **`ROIAlign plugin was not found in the plugin registry` (build fails parsing
ONNX)** — the model is a two-stage detector (Faster/Mask R-CNN) whose `RoiAlign`
op needs a TensorRT plugin that isn't registered. Not supported out of the box —
see "Known limitation" in §1.
- **`Failed to deserialize the TensorRT engine`** — the engine was built on a
different GPU or TensorRT version. Engines are not portable; **rebuild** on the
target machine.
Expand Down