Skip to content

Add TRT plugin custom ops support - #13

Merged
chilo-ms merged 11 commits into
mainfrom
chi/add_custom_ops
Jun 23, 2026
Merged

Add TRT plugin custom ops support#13
chilo-ms merged 11 commits into
mainfrom
chi/add_custom_ops

Conversation

@chilo-ms

Copy link
Copy Markdown
Collaborator

Summary

Register TensorRT plugins as custom ops under the trt.plugins domain, enabling ORT to recognize and route nodes with TRT plugin op types to the TensorRT EP.

Reference: upstream ORT implementation

Changes

New files

  • src/tensorrt_execution_provider_custom_ops.h - Defines TensorRTCustomOp (using Ort::CustomOpBase) and TensorRTCustomKernel structs with variadic inputs/outputs, since TRT plugin API doesn't expose I/O counts.
  • src/tensorrt_execution_provider_custom_ops.cc - Implements CreateTensorRTCustomOpDomainList():
    • Loads extra TRT plugin libraries (LoadLibraryA/dlopen)
    • Initializes default TRT plugins via initLibNvInferPlugins
    • Enumerates all registered plugin creators (V1 and V3One types)
    • Creates custom ops under the trt.plugins domain
    • Uses static storage with mutex for thread-safe, process-lifetime persistence

Modified files

  • src/tensorrt_provider_factory.h - Added GetNumCustomOpDomainsImpl/GetCustomOpDomainsImpl static methods and custom_op_domain_list_ member.
  • src/tensorrt_provider_factory.cc - Wired the two function pointers in the constructor and implemented the static methods.

Tests

  • tests/tensorrt_basic_test.cc - Added TRTPluginsCustomOpTest adapted from the upstream TensorrtExecutionProviderTest.TRTPluginsCustomOpTest. Validates that a model with trt.plugins domain ops can be loaded and session creation succeeds.
  • tests/testdata/trt_plugin_custom_op_test.py - Python script to generate the test model.
  • tests/testdata/trt_plugin_custom_op_test.onnx - Generated test model with DisentangledAttention_TRT node.

Register TensorRT plugins as custom ops under the 'trt.plugins' domain,
enabling ORT to recognize and route nodes with TRT plugin op types to
the TensorRT EP.

Implementation:
- Add TensorRTCustomOp/TensorRTCustomKernel structs using Ort::CustomOpBase
  with variadic inputs/outputs (TRT plugin API doesn't expose I/O counts)
- Add CreateTensorRTCustomOpDomainList() that initializes default TRT plugins,
  loads extra plugin libraries, enumerates all registered plugin creators
  (V1 and V3One), and creates custom ops in the 'trt.plugins' domain
- Wire GetNumCustomOpDomains/GetCustomOpDomains on OrtEpFactory
- Add unit test adapted from upstream TRTPluginsCustomOpTest

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Use DISCOVERY_MODE PRE_TEST so test discovery happens at ctest runtime
instead of build time. This avoids exit code 0xc0000135 (DLL not found)
when onnxruntime.dll and CUDA DLLs are not in PATH during the build step.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The trt_ep_tests unit tests need onnxruntime.dll/libonnxruntime.so at
runtime but it was not included in the uploaded artifact. Copy it from
the NuGet-fetched ORT package during the build-trtep step so it's
available alongside the test exe.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The test binary links against libonnxruntime.so.1 (versioned soname)
but the NuGet package file is named libonnxruntime.so. Create a symlink
so the dynamic linker can find it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
chilo-ms and others added 4 commits June 22, 2026 11:19
…parser errors

The output type mapping in CreateNodeComputeInfoFromGraph was using
positional indexing (graph_output[i]) assuming TRT network outputs are
in the same order as the ONNX graph proto outputs. This causes out-of-
bounds access when TRT reorders outputs, leading to a segfault.

Fix by building a name-to-index map and looking up output types by name.

Also add error checking for trt_parser->parse() to fail gracefully
instead of continuing with an invalid network state.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…Node

Match upstream ORT behavior by checking the 'source' attribute of EPContext
nodes in GraphHasCtxNode. If the source doesn't match TensorrtExecutionProvider,
the node is skipped so the EP doesn't claim it. This prevents segfaults when
processing EPContext nodes from other EPs (e.g., OpenVINO) that contain
invalid engine data for TRT.

Also adds a secondary source check in GetEpContextFromGraph as defense-in-depth.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ach call

The static TensorrtLogger in GetTensorrtLogger() captured the OrtLogger
reference at first construction and never updated it. When multiple sessions
are created sequentially (as in unit tests), the first session's OrtLogger
is destroyed, leaving a dangling reference. The next session's TRT operations
would trigger logging through the stale reference, causing a segfault.

Fix: Change TensorrtLogger to store OrtLogger as a pointer, add update_logger()
method, and call it on every GetTensorrtLogger() invocation. Also add null check
before logging.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

2 participants