I got this error while converting an onnx model to DLC model using snpe-onnx-to-dlc, Error: ValueError: getBroadcastedTensorShape: Unable to broadcast shapes: IrTensorShape(dims = [1,64,18,0] , dy_axes=[]) and IrTensorShape(dims = [1,64,0,2147483647] , dy_axes=[])
2025-05-30 18:33:53,388 - 240 - ERROR - Node /bg_predictor/bg_encoder/layer1/layer1.0/Add: getBroadcastedTensorShape: Unable to broadcast shapes: IrTensorShape(dims = [1,64,18,0] , dy_axes=[]) and IrTensorShape(dims = [1,64,0,2147483647] , dy_axes=[])
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This error, "ValueError: getBroadcastedTensorShape: Unable to broadcast shapes: IrTensorShape(dims = [1,64,18,0] , dy_axes=[]) and IrTensorShape(dims = [1,64,0,2147483647] , dy_axes=[])" during ONNX to DLC conversion using snpe-onnx-to-dlc, indicates an issue with shape compatibility when an Add operation is being performed in your ONNX model. Specifically, it points to the bg_predictor/bg_encoder/layer1/layer1.0/Add node as the culprit.
Here's a breakdown of the problem and potential solutions:
Understanding the Error
Broadcasting: Broadcasting is a mechanism in numerical libraries (like NumPy, which ONNX utilizes) that allows operations between arrays (tensors) with different shapes under certain conditions. The smaller array is "broadcast" across the larger array to make the shapes compatible.
getBroadcastedTensorShape: Unable to broadcast shapes: This error signifies that the Add operation is trying to add two tensors whose shapes are fundamentally incompatible for broadcasting.
IrTensorShape(dims = [1,64,18,0] , dy_axes=[]) and IrTensorShape(dims = [1,64,0,2147483647] , dy_axes=[]): These are the shapes of the two input tensors to the Add operation. The dy_axes part likely refers to dynamic axes (if your model uses dynamic shapes). The problem is the presence of 0 in the dimensions. A dimension with a size of 0 is generally not allowed with SNPE, because zero-sized tensors often lead to undefined behavior in the underlying Qualcomm hardware and software that SNPE is designed to leverage. Also, 2147483647 is the maximum value for a signed 32-bit integer. This suggests a possible integer overflow or some other calculation error resulting in a very large, and probably unintended, dimension.
Possible Causes and Solutions
Incorrect Dimensions in the ONNX Model:
Root Cause: This is the most likely scenario. The ONNX model might have been exported with incorrect dimensions in one of its layers, leading to the shape mismatch during the addition. This is particularly relevant if the model was converted from another framework (e.g., PyTorch, TensorFlow) and there was an issue during the conversion process.
Solution:
Inspect the ONNX graph: Use a tool like Netron (https://netron.app/) to visualize your ONNX model. Carefully examine the shapes of the tensors flowing into the bg_predictor/bg_encoder/layer1/layer1.0/Add node and the layers before it. See if you can identify where the dimension with a size of 0 or 2147483647 originates.
Debugging in the Original Framework: If you have the original model in PyTorch, TensorFlow, or another framework, run a forward pass with some dummy data and inspect the shapes of the intermediate tensors at the equivalent layer to bg_predictor/bg_encoder/layer1/layer1.0. This can help you pinpoint if the issue is in the original model itself.
Fix the Model: Once you've identified the layer producing the incorrect shape, modify the original model (in PyTorch, TensorFlow, etc.) to correct the shape calculation. This might involve:
Adjusting kernel sizes or strides in convolutional layers.
Changing padding settings.
Fixing errors in reshape operations.
Correcting broadcasting behavior in your original code.
Re-export the ONNX model: After correcting the original model, export it to ONNX again.
Dynamic Shape Issues and SNPE Limitations:
Root Cause: While SNPE supports dynamic input shapes to some extent, it often has limitations, especially with intermediate layers having fully dynamic shapes. The dy_axes in the error message hints at dynamic axes. The SNPE toolchain might not be able to handle the variability in shape that the Add operation is expecting. It's also possible that the use of a value of zero is intended for masking, and that zero is not intended to represent a physical size of the dimension.
Solution:
Static Shapes (Preferred): The ideal solution is to make the model fully static (i.e., no dynamic shapes). This usually provides the best performance and compatibility with SNPE. You might need to pad or crop inputs to fixed sizes during preprocessing. Check if fixing the input size removes the dynamic dimension.
Specify Dynamic Axes: If you must use dynamic shapes, ensure that you're specifying the dynamic axes correctly when using the snpe-onnx-to-dlc tool. Refer to the SNPE documentation for the correct way to declare dynamic input/output axes. However, remember that SNPE has limitations.
Shape Inference Issues: SNPE may not be inferring the shapes correctly, especially with dynamic dimensions.
ONNX Version Incompatibility:
Root Cause: Sometimes, the ONNX version used to export the model might be incompatible with the version supported by the snpe-onnx-to-dlc tool.
Solution:
Check ONNX Version: Find out the ONNX version supported by your SNPE SDK. Usually this is documented along with release notes.
Upgrade/Downgrade: Upgrade or downgrade your ONNX version (using pip install onnx==<version>) to match the supported version.
Re-export: Re-export the ONNX model using the compatible ONNX version.
SNPE Bug:
Root Cause: Although less likely, there's a small chance this could be a bug in the SNPE toolchain itself, particularly if you're using a relatively new or older version of the SDK.
Solution:
Update SNPE SDK: Try updating to the latest version of the SNPE SDK.
Qualcomm Support: If the problem persists, consider contacting Qualcomm support and providing them with the ONNX model and the error logs. They can investigate if it's a bug in the toolchain.
Steps to Diagnose and Fix
Use Netron to Inspect the ONNX Model: This is your first step. Look at the bg_predictor/bg_encoder/layer1/layer1.0/Add node and the layers connected to it. Pay close attention to the shape attributes of the tensors.
Simplify the Model (if possible): If your model is complex, try simplifying it by removing parts of the network before the Add node to see if the error disappears. This can help you isolate the source of the problem.
Test with a Simple ONNX Model: Create a very simple ONNX model that performs an Add operation with hardcoded tensors of similar (but valid) shapes to see if snpe-onnx-to-dlc can handle it. This will help rule out general SNPE issues.
Check the ONNX Graph's Validity: Use the ONNX checker to ensure the model is well-formed:
python
import onnx
model = onnx.load("your_model.onnx")
onnx.checker.check_model(model) # Will raise an exception if invalid
Consider Quantization Settings: If you are using quantization (e.g., with snpe-onnx-to-dlc -i <onnx_model> -q), try disabling it temporarily to see if that resolves the issue. Sometimes, quantization can exacerbate shape-related problems.
Example Workflow:
Let's say you suspect a problem with a Reshape layer before the Add node.
Netron: Open the ONNX model in Netron and find the Reshape layer that feeds into the Add node. Examine the shape attribute of the output tensor of the Reshape layer.
Original Code: In your original PyTorch/TensorFlow code, find the equivalent reshape operation and print the shape of the tensor after the reshape:
python
# PyTorch example
import torch
# ... your model definition ...
x = torch.randn(batch_size, channels, height, width) # Example input
# ... layers up to the reshape operation ...
reshaped_tensor = your_reshape_layer(previous_layer_output)
print(reshaped_tensor.shape)
Compare: Compare the shape you see in Netron with the shape you print in your original code. If they don't match, the problem is likely in the ONNX export process. If the shape in the original model is the problem, then fix your model and try again.
By systematically investigating the shapes in your model and comparing them to the expected shapes, you should be able to identify the root cause of the broadcasting error and fix your ONNX model for successful conversion to DLC format. Remember to double-check for issues with dynamic shapes and ONNX versions.