Hello,
I have a vision model based on DEIM-DFINE. I can convert this model into ONNX (with tracing) and I can convert it to Torchscript, but when I try to convert the Torchscript model to ONNX, it fails.
I need one control flow in the final ONNX model, which is why I can’t use tracing, I need the torchscript conversion before.
However for some reason, ONNX is not able to infer shapes, even when I hardcode them. This leads to continuous errors in many parts of the model and I have no idea how to resolve any of them, or if it is even possible. The model itself would operate always on the same input and output dimensions, so it is quite basic regarding shapes. I could literally in most layers define the shapes by hand, but I see no way this is possible.
I tried several things for the error message below. Converting to float, hardcoding shapes like this for example:
shape_tensor = torch.tensor([1, 256, 15, 15], dtype=torch.int64)
value_att = self.norm1(value_att.view(shape_tensor)
torch.onnx.errors.SymbolicValueError: Unsupported: ONNX export of operator group_norm, unknown input rank. Please feel free to request
support or submit a pull request on PyTorch GitHub: https://github.com/pytorch/pytorch/issues
[Caused by the value ‘1182 defined in (%1182 : FloatTensor(device=cpu) = onnx::Castto=1, scope: engine.deim.deim.DEIM::/
engine.deim.hybrid_encodereff.HybridEncoderEff::encoder/torch.nn.modules.container.Sequential::pre_small/engine.deim.hybrid_encodereff.LowFormerEncoderLayer::0
# Detec/tools/deployment/../../engine/deim/hybrid_encodereff.py:782:31
)’ (type ‘Tensor’) in the TorchScript graph. The containing node has kind ‘onnx::Cast’.]
(node defined in File “Detec/tools/deployment/../../engine/deim/hybrid_encodereff.py”, line 782
# value_att = value_att# shape_tensor = torch.tensor([1, 256, 15, 15], dtype=torch.int64)value_att = self.norm1(value_att.float())
~~~~~~~~~~~~~~~ <— HERE
)Inputs:#0: value_att.3 defined in (%value_att.3 : FloatTensor(device=cpu) = onnx::Add(%1179, %1180),
scope: engine.deim.deim.DEIM::/engine.deim.hybrid_encodereff.HybridEncoderEff::encoder/torch.nn.modules.container.Sequential::
pre_small/engine.deim.hybrid_encodereff.LowFormerEncoderLayer::0 # Detec/tools/
But nothing worked and it seems these hardcoded shapes are thrown away during torchscript conversion, so onnx conversion can’t access them.
Is there a way to create an onnx model with simple control flow from a pytorch model without torchscript? Can I fix the torchscript model, such that onnx conversion will work? The whole model is unfortunately quite big.
Thanks in advance!
(for some reason the formatting of the error message is messed sry for that!)
Some other error message more refering to the problems I talk about.
torch.onnx.errors.SymbolicValueError: Cannot determine scalar type for this '<class 'torch.TensorType'>' instance and a default value was not provided.
[Caused by the value '1182 defined in (%1182 : Tensor(*, *, *, *) = onnx::Reshape[allowzero=0](%value_att.3, %1181),
scope: engine.deim.deim.DEIM::/engine.deim.hybrid_encodereff.HybridEncoderEff::encoder/torch.nn.modules.container.Sequential::pre_small/engine.deim.hybrid_encodereff.LowFormerEncoderLayer::0
# Detec/tools/deployment/../../engine/deim/hybrid_encodereff.py:782:31
)' (type 'Tensor') in the TorchScript graph. The containing node has kind 'onnx::Reshape'.]
(node defined in File "Detec/tools/deployment/../../engine/deim/hybrid_encodereff.py", line 782
# value_att = value_att
shape_tensor = torch.Size([1, 256, 15, 15])
value_att = self.norm1(value_att.view(shape_tensor))
~~~~~~~~~~~~~~ <--- HERE)
Inputs:#0: value_att.3 defined in (%value_att.3 : Tensor = onnx::Add(%1124, %value_att),
scope: engine.deim.deim.DEIM::/engine.deim.hybrid_encodereff.HybridEncoderEff::encoder/torch.nn.modules.container.Sequential::pre_small/engine.deim.hybrid_encodereff.LowFormerEncoderLayer::0
# Detec/tools/deployment/../../engine/deim/hybrid_encodereff.py:779:20) (type 'Tensor')
#1: 1181 defined in (%1181 : Long(4, strides=[1], device=cpu) = onnx::Constant[value= 1 256 15 15 [ CPULongType{4} ]](),
scope: engine.deim.deim.DEIM::/engine.deim.hybrid_encodereff.HybridEncoderEff::encoder/torch.nn.modules.container.Sequential::pre_small/engine.deim.hybrid_encodereff.LowFormerEncoderLayer::0
# Detec/tools/deployment/../../engine/deim/hybrid_encodereff.py:782:31 ) (type 'Tensor')
Outputs: #0: 1182 defined in (%1182 : Tensor(*, *, *, *) = onnx::Reshape[allowzero=0](%value_att.3, %1181),
scope: engine.deim.deim.DEIM::/engine.deim.hybrid_encodereff.HybridEncoderEff::encoder/torch.nn.modules.container.Sequential::pre_small/engine.deim.hybrid_encodereff.LowFormerEncoderLayer::0
# Detec/tools/deployment/../../engine/deim/hybrid_encodereff.py:782:31) (type 'Tensor')