Fail to Build the TRT Engine of a multi-GatherND ONNX Model for large input volume

Description

I fail to build the TRT Engine of a 2-GatherND-GEMM model when maxShape’s first dim >= 16043:

The onnx model can be generated via follow torch code:

import torch
import torch.nn as nn

class GatherNDModel(nn.Module):
  def __init__(self, depth=2, channels=36):
    super().__init__()
    self.depth = depth
    self.channels = channels
    self.proj = nn.Linear(channels, channels)

  def forward(self, x, order):
    y = x
    for _ in range(self.depth):
      y = y[order] # this will cause TRT Engine build failed
      # y = torch.index_select(y, 0, order)
      y = self.proj(y)
    return y
  
  def generate_input(self, device="cuda"):
    num_points = 1000
    x = torch.randn(num_points, self.channels, device=device)
    order = torch.randint(0, num_points, (num_points,), device=device)
    return x, order
  
  def input_names(self):
    return ["x", "order"]
  
  def output_names(self):
    return ["y"]
  
  def dynamic_shapes(self):
    return {
      "x": {0: "num_points"},
      "order": {0: "num_points"}
    }

if __name__ == "__main__":
  model = GatherNDModel()
  model.cuda()
  model.eval()

  x, order = model.generate_input()
  y = model(x, order)
  print(y.shape)

# export onnx
  inputs = (x, order)
  input_names = model.input_names()
  output_names = model.output_names()
  dynamic_shapes = model.dynamic_shapes()
  custom_translation_table = {}

  onnx_path = "gathernd_model.onnx"
  torch.onnx.export(
    model,
    inputs,
    onnx_path,
    input_names=input_names,
    output_names=output_names,
    dynamo=True,
    report=True,
    dynamic_shapes=dynamic_shapes,
    custom_translation_table=custom_translation_table,
  )

  import onnx
  try:
      onnx.checker.check_model(onnx_path, full_check=True)
      print(f"[onnx.checker] {onnx_path} passed structural + shape checks.")
  except onnx.checker.ValidationError as e:
      print(f"[onnx.checker] validation failed: {e}")

I use following command to build TRT Engine:

trtexec \
    --onnx=gathernd_model.onnx \
    --minShapes=x:1x36,order:1 \
    --optShapes=x:1145x36,order:1145 \
    --maxShapes=x:16043x36,order:16043 \
    --saveEngine=gathernd_model.engine \
    --fp16 \
    --skipInference --builderOptimizationLevel=0 --verbose > trt_build.log 2>&1

log output:

[05/01/2026-19:31:06] [V] [TRT] Set number of tactics to 1 at optimization level 0
Internal Error: MyelinCheckException: nvrtc_compile.cpp:1118: CHECK(success) failed. NVRTC Compilation failure
[05/01/2026-19:31:09] [E] Error[9]: Error Code: 9: Skipping tactic 0x0000000000000000 due to exception [myelin_graph.h:1182: attachExceptionMsgToGraph] MyelinCheckException: nvrtc_compile.cpp:1118: CHECK(success) failed. NVRTC Compilation failure In compileGraph at /_src/optimizer/myelin/codeGenerator.cpp:1548
[05/01/2026-19:31:09] [V] [TRT] {ForeignNode[node_Unsqueeze_5...y_castOut]} (Myelin[0x80000023]) profiling completed in 2.75539 seconds. Fastest Tactic: 0xd15ea5edd15ea5ed Time: inf
[05/01/2026-19:31:09] [E] Error[10]: IBuilder::buildSerializedNetworkToStream: Error Code 10: Internal Error (Could not find any implementation for node {ForeignNode[node_Unsqueeze_5...y_castOut]}. In computeCosts at /_src/optimizer/common/tactic/optimizer.cpp:4143)
[05/01/2026-19:31:09] [I] Created engine with size: 0 MiB
[05/01/2026-19:31:09] [I] Engine built in 3.01337 sec.
[05/01/2026-19:31:09] [E] Assertion failure: false && "Attempting to access an empty engine!"
[gathernd\_issue.zip|attachment](upload://miumjuBDDQflV2mcQpz0YmJ77Pf.zip) (13.6 KB)

Let x be in shape [num_pts, 36]. Something observation:

  • If self.depth>1, the maximal successful num_pts is always 16042 (increasing depth will not decrease the num_pts).
  • If self.depth=1, num_pts can be larger than 16042 (e.g.: 16043, 160420).
  • If self.proj (GEMM ONNX node) is removed from the forward function, num_pts can be larger than 16042 for any depth.
  • If y = y[order] (GatherND ONNX node) is replaced by y = torch.index_select(y, 0, order) (Gather ONNX node), num_pts can be larger than 16042 for any depth.

I am wondering whether this issue is caused by TRT support for GatherND ONNX node.

The 2-depth onnx model and logs are attached. Thanks for help!

Environment

TensorRT Version: 10.14.1.48
GPU Type: NVIDIA RTX PRO 6000 Blackwell Server Edition
Nvidia Driver Version: 580.105.08
CUDA Version: 13.0
CUDNN Version: 9.14.0
Operating System + Version: ubuntu 22.04
Python Version (if applicable): 3.13.12
TensorFlow Version (if applicable):
PyTorch Version (if applicable): 2.11.0+cu130
Baremetal or Container (if container which image + tag): nvcr.io/nvidia/cuda:13.0.2-cudnn-devel-ubuntu22.04

ONNX related Version:

$ pip list | grep onnx 
onnx                      1.21.0
onnx-ir                   0.2.0
onnxruntime               1.24.4
onnxscript                0.6.2

Relevant Files

Please attach or include links to any models, data, files, or scripts necessary to reproduce your issue. (Github repo, Google Drive, Dropbox, etc.)

Steps To Reproduce

Please include:

  • Exact steps/commands to build your repro
  • Exact steps/commands to run your repro
  • Full traceback of errors encountered
    gathernd_issue.zip (13.6 KB)

Hi @allenchou873, thanks for the clean bisection (especially isolating the depth and the Gather-vs-GatherND swap independently).

Quick framing on what’s happening before the workarounds. From your verbose log, the entire graph (Unsqueeze + 2x GatherND + 2x Gemm) collapses into a single Myelin ForeignNode (After Myelin optimization: 1 layers), and the actual failure is NVRTC failing to compile the kernel Myelin generates for that fused subgraph:

Internal Error: MyelinCheckException: nvrtc_compile.cpp:1118: CHECK(success) failed. NVRTC Compilation failure
[E] Error[10]: Could not find any implementation for node {ForeignNode[node_Unsqueeze_5...y_castOut]}.

The 16042 / 16043 cliff and your other three observations point to the Myelin codegen path for the chained GatherND -> Gemm -> GatherND -> Gemm pattern crossing an NVRTC compile-time limit (register pressure, unroll factor, or shared-memory layout) keyed off the max dim. Single GatherND (depth=1), GatherND alone with no Gemm, and ONNX Gather (your torch.index_select variant) all avoid the fusion that triggers this. Combined with --builderOptimizationLevel=0 only attempting one tactic, there is no fallback and the build aborts. So you are correct that this looks like a TRT/Myelin issue on GatherND, not an ONNX-side problem.

Here are the workarounds, ordered by blast radius:

1. Use torch.index_select permanently

You already proved this works, and it is the recommended fix for your use case rather than just a debugging workaround. tensor[order] lowers via aten.index.Tensor to ONNX GatherND with an Unsqueeze(axes=[-1]) of the 1-D index. For your specific case (1-D index, gather along axis 0), torch.index_select(y, 0, order) is mathematically identical and exports to the simpler ONNX Gather op, which TRT handles via the native gather path and never enters the problematic Myelin fusion.

for _ in range(self.depth):
    y = torch.index_select(y, 0, order)
    y = self.proj(y)

2. Drop --builderOptimizationLevel=0

At opt level 0 with a single tactic, NVRTC failing on one kernel is fatal. The default (level 3) lets Myelin try multiple tactics for the ForeignNode; one of them may compile cleanly even when the most-aggressive one does not. Try this even if you ultimately ship option 1:

trtexec \
    --onnx=gathernd_model.onnx \
    --minShapes=x:1x36,order:1 \
    --optShapes=x:1145x36,order:1145 \
    --maxShapes=x:16043x36,order:16043 \
    --saveEngine=gathernd_model.engine \
    --fp16 \
    --skipInference --verbose

3. Move to --stronglyTyped instead of --fp16

Your build log surfaces this deprecation warning:

Weakly-typed networks have been deprecated in TensorRT. You can use the AutoCast tool [...] to convert the network to be strongly typed.

Strongly-typed builds use precision strictly as declared in the ONNX (the dynamo exporter already produces a clean one), and on some Myelin paths the change in precision tracking alters the generated kernel enough to side-step codegen issues. This will not necessarily fix this specific cliff, but it is the right move regardless.

--stronglyTyped

⚠️ Heads up if you also try option 4 below: weakly-typed networks are removed in TensorRT 11.0, so --fp16 will not carry forward and you will need --stronglyTyped (or the AutoCast tool / polygraphy surgeon insert-cast) for the upgrade either way.

4. Try the latest TensorRT release

You are on 10.14.1.48 against a brand-new Blackwell SM120 (libnvinfer_builder_resource_sm120.so.10.14.1). Myelin codegen fixes land in nearly every minor release, and the latest GA on the release-notes index is now 11.0.0. Try the latest release in the line you want to stay on:

One data point that decides next steps: your bisection did not cover whether the 16043 cliff also fires at the default --builderOptimizationLevel (3). At level 0 you are pinned to a single tactic, so if that one tactic’s kernel fails NVRTC, the build has no fallback. The default level lets Myelin try multiple tactics, and some may decompose the fusion differently. If workaround #2 (dropping the flag) lets the engine build at maxShapes=x:16043x36, this is most likely a tactic-search UX issue and the error message could be clearer. If the default level also fails at that max shape, that indicates a Myelin codegen regression on the chained GatherND -> Gemm fusion that I would file internally so the compiler team can take a look. In that case please reply back here with the verbose log from the default-opt-level build, and I will escalate.

For the FP32-vs-FP16 question: if you have a moment to drop --fp16 and re-run, that tells us whether the kernel cliff is FP16-specific. Helpful context if we do end up filing, but lower priority than the opt-level test.

Thanks,
Atharva