From f8cf74e79b4b1715b2bf8532cbf4f7268417468c Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Sat, 8 Aug 2026 16:08:54 -0700 Subject: [PATCH] fix(executorch): do not resize a 0-d delegate output A model whose graph passes a scalar from a TensorRT partition to another backend fails at run time: Attempted to change the tensor rank which is immutable: old=0, new=1 TensorRTBackend::execute: resize_tensor failed for output 'output15' A 0-d tensor has an immutable rank of zero, while TensorRT reports a scalar as a 1-element one-dimensional shape. The output resize is unconditional, so the two descriptions disagree and the resize is rejected even though the buffer is already the right size. Skip the resize when the output is 0-d and the engine reports a single element. Any other shape mismatch still resizes and still reports a failure. This comes up with a length-aware attention kernel, where a scalar sequence length crosses the partition boundary into a kernel that consumes it. --- .../torch_tensorrt/executorch/TensorRTBackend.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cpp/src/torch_tensorrt/executorch/TensorRTBackend.cpp b/cpp/src/torch_tensorrt/executorch/TensorRTBackend.cpp index b2e3b08232..c8ed903240 100644 --- a/cpp/src/torch_tensorrt/executorch/TensorRTBackend.cpp +++ b/cpp/src/torch_tensorrt/executorch/TensorRTBackend.cpp @@ -522,10 +522,17 @@ Error TensorRTBackend::execute(BackendExecutionContext& context, DelegateHandle* for (int d = 0; d < actual_dims.nbDims; ++d) { new_sizes[d] = static_cast(actual_dims.d[d]); } - Error resize_err = executorch::runtime::resize_tensor(et_out, {new_sizes, static_cast(actual_dims.nbDims)}); - if (resize_err != Error::Ok) { - ET_LOG(Error, "TensorRTBackend::execute: resize_tensor failed for output '%s'", name.c_str()); - return resize_err; + // A 0-d output has an immutable rank of zero, and TensorRT reports it as a + // 1-element 1-D shape, so resizing would be rejected. Skip it when the + // element count already agrees. + const bool scalar_output = et_out.dim() == 0 && actual_dims.nbDims == 1 && actual_dims.d[0] == 1; + if (!scalar_output) { + Error resize_err = + executorch::runtime::resize_tensor(et_out, {new_sizes, static_cast(actual_dims.nbDims)}); + if (resize_err != Error::Ok) { + ET_LOG(Error, "TensorRTBackend::execute: resize_tensor failed for output '%s'", name.c_str()); + return resize_err; + } } void* bind_ptr = nullptr;