From 878bcbaad9d683d9f9316d473f15fa053fdec305 Mon Sep 17 00:00:00 2001 From: Hardik Sharma Date: Wed, 10 Sep 2025 21:04:56 -0700 Subject: [PATCH] Allow zero-element tensors for shared inputs. (#13935) Summary: Adds support for the case where method inputs are zero-element. Similar to https://github.com/pytorch/executorch/pull/13623, but adds support for shared input instead. Reviewed By: JacobSzwejbka, DrJessop Differential Revision: D81653487 --- runtime/core/exec_aten/util/tensor_util_portable.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/runtime/core/exec_aten/util/tensor_util_portable.cpp b/runtime/core/exec_aten/util/tensor_util_portable.cpp index bc971c72f50..9626974ad7d 100644 --- a/runtime/core/exec_aten/util/tensor_util_portable.cpp +++ b/runtime/core/exec_aten/util/tensor_util_portable.cpp @@ -147,12 +147,17 @@ Error share_tensor_data( t_dst.nbytes(), t_src.nbytes()); + // Either the t_src is empty or contains valid data. ET_CHECK_OR_RETURN_ERROR( - t_src.mutable_data_ptr() != nullptr, + t_src.mutable_data_ptr() != nullptr || t_src.nbytes() == 0, InvalidArgument, "Source tensor should have data_ptr not being nullptr."); + + // Setting data_ptr to nullptr explicitly when t_src is empty. + void* t_src_data_ptr = + t_src.numel() == 0 ? nullptr : t_src.mutable_data_ptr(); // Assign internal data_ptr as the one in forwarded tensor - t_dst.unsafeGetTensorImpl()->set_data(t_src.mutable_data_ptr()); + t_dst.unsafeGetTensorImpl()->set_data(t_src_data_ptr); return Error::Ok; }