From 7f3d772425815029d943c1bdf4067516ed591797 Mon Sep 17 00:00:00 2001 From: Stephen Jia Date: Wed, 25 Jun 2025 13:08:51 -0700 Subject: [PATCH] [ET-VK][ez] Handle zero-element tensors when building Vulkan graph ## Changes As title. ## Motivation Some models may have parameter tensors which are zero-shape (i.e. no elements). In this case, trying to serialize the tensor data will result in a null pointer exception. Differential Revision: [D77281492](https://our.internmc.facebook.com/intern/diff/D77281492/) [ghstack-poisoned] --- .../serialization/vulkan_graph_serialize.py | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/backends/vulkan/serialization/vulkan_graph_serialize.py b/backends/vulkan/serialization/vulkan_graph_serialize.py index ebb13bbb97d..2ceedf73d10 100644 --- a/backends/vulkan/serialization/vulkan_graph_serialize.py +++ b/backends/vulkan/serialization/vulkan_graph_serialize.py @@ -191,19 +191,23 @@ def serialize_constant_tensors( current_offset = len(raw_bytes) for tensor in const_tensors: - array_type = ctypes.c_char * tensor.untyped_storage().nbytes() - array = ctypes.cast( - tensor.untyped_storage().data_ptr(), - ctypes.POINTER(array_type), - ).contents - - tensor_bytes = bytes(array) - # Pad the tensor bytes to the next 16 byte boundary - raw_bytes += tensor_bytes - raw_bytes += b"\x00" * padding_required(len(tensor_bytes)) - - vk_graph.constants.append(VkBytes(current_offset, len(tensor_bytes))) - current_offset += aligned_size(len(tensor_bytes)) + if tensor.numel() == 0: + vk_graph.constants.append(VkBytes(current_offset, 0)) + continue + else: + array_type = ctypes.c_char * tensor.untyped_storage().nbytes() + array = ctypes.cast( + tensor.untyped_storage().data_ptr(), + ctypes.POINTER(array_type), + ).contents + + tensor_bytes = bytes(array) + # Pad the tensor bytes to the next 16 byte boundary + raw_bytes += tensor_bytes + raw_bytes += b"\x00" * padding_required(len(tensor_bytes)) + + vk_graph.constants.append(VkBytes(current_offset, len(tensor_bytes))) + current_offset += aligned_size(len(tensor_bytes)) def serialize_custom_shaders(