Skip to content

About exporting to ONNX for BEVFormer backbone in another custom model #114

@BaophanN

Description

@BaophanN

Dear author, thanks for this amazing work. Can you please help me with this problem.
I am trying to export a custom model that uses BEVFormer as backbone to ONNX. My problem is that when I export to ONNX. I got the following error:

Traceback (most recent call last):
  File "tools/convert_lanesegnet_to_onnx.py", line 183, in <module>
    main()
  File "tools/convert_lanesegnet_to_onnx.py", line 148, in main
    torch.onnx.export(
  File "/opt/conda/lib/python3.8/site-packages/torch/onnx/__init__.py", line 316, in export
    return utils.export(model, args, f, export_params, verbose, training,
  File "/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py", line 107, in export
    _export(model, args, f, export_params, verbose, training, input_names, output_names,
  File "/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py", line 737, in _export
    proto, export_map, val_use_external_data_format = graph._export_onnx(
RuntimeError: ONNX export failed: Couldn't export Python operator MultiScaleDeformableAttnFunction_fp32

Defined at:
/workspace/source/Mapless/plugin/LaneSegNet/bevformer/modules/temporal_self_attention.py(396): forward_trt
/workspace/source/Mapless/plugin/LaneSegNet/bevformer/modules/encoder.py(614): forward_trt
/workspace/source/Mapless/plugin/LaneSegNet/bevformer/modules/encoder.py(263): forward_trt
/workspace/source/Mapless/plugin/LaneSegNet/lanesegnet/models/modules/bevformer_constructer.py(319): forward_trt
/workspace/source/Mapless/plugin/LaneSegNet/lanesegnet/models/detectors/lanesegnet.py(298): forward_trt
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py(1090): _slow_forward
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py(1102): _call_impl
/opt/conda/lib/python3.8/site-packages/torch/jit/_trace.py(118): wrapper
/opt/conda/lib/python3.8/site-packages/torch/jit/_trace.py(127): forward
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py(1102): _call_impl
/opt/conda/lib/python3.8/site-packages/torch/jit/_trace.py(1166): _get_trace_graph
/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py(388): _trace_and_get_graph_from_model
/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py(437): _create_jit_graph
/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py(493): _model_to_graph
/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py(724): _export
/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py(107): export
/opt/conda/lib/python3.8/site-packages/torch/onnx/__init__.py(316): export
tools/convert_lanesegnet_to_onnx.py(148): main
tools/convert_lanesegnet_to_onnx.py(183): <module>

I understand that I need to write corresponding ONNX operator for MultiScaleDeformableAttnFunction_fp32.
I have read your code and see that class MultiScaleDeformableAttnFunction_fp32 is the same to that of pytorch model. I do not understand how did you implement this class so that it can be sucessfully exported to ONNX. Can you please explain to me.

mmcv 1.5.2 
torch 1.9.1 
CUDA 11.1
onnx 
onnxruntime

The error happens right at these lines:

Image
As far as I understand, the folder TensorRT/plugin contains custom operator you have implemented. But I dont understand how I can implement my custom ONNX operator in a different code base. I can see some relevant files relating to exporting to onnx:

Image
but do not understand where the symbolic function is called since the above temporal self attn uses MultiScaleDeformableAttnFunction_fp32

Thank you. Any help is appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions