Big trouble in export deformable detr

Zalways: <p>when i try to export the deformable detr model into torchscript,it shows the error message:</p>
<pre><code class="lang-auto">Could not export Python function call 'MSDeformAttnFunction'**. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to __constants__:
/root/autodl-tmp/project/deepsolo/adet/layers/ms_deform_attn.py(165): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/deepsolo/adet/layers/deformable_transformer.py(286): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/deepsolo/adet/layers/deformable_transformer.py(413): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/deepsolo/adet/layers/deformable_transformer.py(173): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/deepsolo/adet/modeling/model/detection_transformer.py(200): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/deepsolo/adet/modeling/text_spotter_v1.py(222): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/detectron2/export/flatten.py(259): &lt;lambda&gt;
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/detectron2/export/flatten.py(294): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/jit/_trace.py(952): trace_module
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/jit/_trace.py(735): trace
deploy/export_model00.py(126): export_tracing
deploy/export_model00.py(226): &lt;module&gt;
</code></pre>
<p>and i tried the method:<br>
<img src="https://github.com/fundamentalvision/Deformable-DETR/assets/95404038/4b6b296c-bf3d-4e39-9c43-902926d24529" alt="image" width="690" height="191"></p>
<p>it exported successfully,but the exported model doesn’t work! it only can inference on the image that i used for exporting the model,but for other image,it cann’t work. it shows the error message:</p>
<pre><code class="lang-auto">/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py:1051: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /pytorch/c10/core/TensorImpl.h:1156.)
  return forward_call(*input, **kwargs)
Traceback (most recent call last):
  File "/root/autodl-tmp/project/deploy/export_model.py", line 264, in &lt;module&gt;
    out1 = m(data)
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
RuntimeError: The following operation failed in the TorchScript interpreter.
Traceback of TorchScript, serialized code (most recent call last):
  File "code/__torch__/detectron2/export/flatten.py", line 9, in forward
  def forward(self: __torch__.detectron2.export.flatten.TracingAdapter,
    argument_1: Tensor) -&gt; Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:
    _0, _1, _2, _3, _4, _5, _6, = (self.model).forward(argument_1, )
                                   ~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
    return (_0, _1, _2, _3, _4, _5, _6)
  File "code/__torch__/adet/modeling/text_spotter.py", line 23, in forward
    batched_imgs = torch.unsqueeze_(_7, 0)
    x0 = torch.contiguous(batched_imgs)
    _8, _9, _10, _11, = (_0).forward(x0, image_size, )
                         ~~~~~~~~~~~ &lt;--- HERE
    _12 = torch.softmax(_9, -1)
    prob = torch.sigmoid(torch.mean(_8, [-2]))
  File "code/__torch__/adet/modeling/model/detection_transformer.py", line 50, in forward
    _29 = getattr(self.input_proj, "1")
    _30 = getattr(self.input_proj, "0")
    _31 = (self.backbone).forward(x, image_size, )
           ~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
    _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, = _31
    _58 = (_30).forward(_32, )
  File "code/__torch__/adet/modeling/text_spotter.py", line 104, in forward
    image_size: Tensor) -&gt; Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:
    _61 = getattr(self, "1")
    _62 = (getattr(self, "0")).forward(x, image_size, )
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
    _63, _64, _65, _66, _67, _68, _69, = _62
    pos_embed = torch.to((_61).forward(_63, ), 6)
  File "code/__torch__/adet/modeling/text_spotter.py", line 143, in forward
    _92 = torch.slice(torch.slice(_91, 0, 0, 125), 1, 0, 138)
    _93 = torch.view(CONSTANTS.c2, annotate(List[int], []))
    _94 = torch.copy_(_92, torch.expand(_93, [125, 138]))
          ~~~~~~~~~~~ &lt;--- HERE
    masks_per_feature_level0 = torch.ones([_85, _86, _87], dtype=11, layout=None, device=torch.device("cpu"), pin_memory=False)
    _95 = torch.select(masks_per_feature_level0, 0, 0)

Traceback of TorchScript, original code (most recent call last):
/root/autodl-tmp/project/adet/modeling/text_spotter.py(60): mask_out_padding
/root/autodl-tmp/project/adet/modeling/text_spotter.py(43): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/adet/modeling/text_spotter.py(21): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/adet/modeling/model/detection_transformer.py(168): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/adet/modeling/text_spotter.py(220): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/detectron2/export/flatten.py(259): &lt;lambda&gt;
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/detectron2/export/flatten.py(294): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/jit/_trace.py(952): trace_module
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/jit/_trace.py(735): trace
/root/autodl-tmp/project/deploy/export_model.py(125): export_tracing
/root/autodl-tmp/project/deploy/export_model.py(224): &lt;module&gt;
/root/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py(18): execfile
/root/.pycharm_helpers/pydev/pydevd.py(1496): _exec
/root/.pycharm_helpers/pydev/pydevd.py(1489): run
/root/.pycharm_helpers/pydev/pydevd.py(2177): main
/root/.pycharm_helpers/pydev/pydevd.py(2195): &lt;module&gt;
RuntimeError: The size of tensor a (50) must match the size of tensor b (125) at non-singleton dimension 0
</code></pre>

Zalways: <p>btw, after failed to export the model into torchscript,i tried to export it into onnx,but i still met some problem and failed:<br>
when i use torch1.9 and cuda 111, the error message is:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "deploy/export_model00.py", line 226, in &lt;module&gt;
    sample_inputs = get_sample_inputs(args)
  File "deploy/export_model00.py", line 132, in export_tracing
    torch.onnx.export(traceable_model, (image,), f,opset_version=11)
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/onnx/__init__.py", line 275, in export
    return utils.export(model, args, f, export_params, verbose, training,
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/onnx/utils.py", line 88, in export
    _export(model, args, f, export_params, verbose, training, input_names, output_names,
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/onnx/utils.py", line 689, in _export
    _model_to_graph(model, args, verbose, input_names,
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/onnx/utils.py", line 501, in _model_to_graph
    params_dict = torch._C._jit_pass_onnx_constant_fold(graph, params_dict,
RuntimeError: expected scalar type Long but found Float
</code></pre>
<p>i searched on the Internet,and find a method: upgrade my torch to 1.10, i changed my torch to 1.10,still error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "deploy/export_model00.py", line 227, in &lt;module&gt;
    exported_model = export_tracing(torch_model, sample_inputs)
  File "deploy/export_model00.py", line 132, in export_tracing
    torch.onnx.export(traceable_model, image, f, input_names=['input'], output_names=['output'], opset_version=11,dynamic_axes={'input':[1,2],'output':[1,2]})
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/onnx/__init__.py", line 316, in export
    return utils.export(model, args, f, export_params, verbose, training,
  File "/root/miniconda3/envs/deepsolo/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 "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/onnx/utils.py", line 724, in _export
    _model_to_graph(model, args, verbose, input_names,
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/onnx/utils.py", line 493, in _model_to_graph
    graph, params, torch_out, module = _create_jit_graph(model, args)
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/onnx/utils.py", line 437, in _create_jit_graph
    graph, torch_out = _trace_and_get_graph_from_model(model, args)
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/onnx/utils.py", line 388, in _trace_and_get_graph_from_model
    torch.jit._get_trace_graph(model, args, strict=False, _force_outplace=False, _return_inputs_states=True)
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/jit/_trace.py", line 1166, in _get_trace_graph
    outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/jit/_trace.py", line 127, in forward
    graph, out = torch._C._create_graph_by_tracing(
RuntimeError: 0INTERNAL ASSERT FAILED at "../torch/csrc/jit/ir/alias_analysis.cpp":584, please report a bug to PyTorch. We don't have an op for aten::fill_ but it isn't a special case.  Argument types: Tensor, bool, 
</code></pre>

Zalways: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  i’ll appreciate if you could reply!</p>

Zalways: <p>Details of problem I met:<br>
<strong>What I’m tring to do:</strong><br>
I try to exported the model so that I can inference in c++ environment.(the model is from <a href="https://github.com/ViTAE-Transformer/DeepSolo" class="inline-onebox" rel="noopener nofollow ugc">GitHub - ViTAE-Transformer/DeepSolo: The official repo for [CVPR'23] "DeepSolo: Let Transformer Decoder with Explicit Points Solo for Text Spotting" &amp; [ArXiv'23] "DeepSolo++: Let Transformer Decoder with Explicit Points Solo for Text Spotting"</a> )</p>
<p><strong>How I export the model:</strong><br>
I use the official code(<a href="https://github.com/facebookresearch/detectron2/tree/main/tools/deploy" class="inline-onebox" rel="noopener nofollow ugc">detectron2/tools/deploy at main · facebookresearch/detectron2 · GitHub</a>):export_model.py<br>
parameters:<br>
–export-method tracing<br>
–format torchscript<br>
MODEL.DEVICE cpu<br>
MODEL.WEIGHTS (is from <a href="https://github.com/ViTAE-Transformer/DeepSolo" class="inline-onebox" rel="noopener nofollow ugc">GitHub - ViTAE-Transformer/DeepSolo: The official repo for [CVPR'23] "DeepSolo: Let Transformer Decoder with Explicit Points Solo for Text Spotting" &amp; [ArXiv'23] "DeepSolo++: Let Transformer Decoder with Explicit Points Solo for Text Spotting"</a></p>
<p>–config-file:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/ViTAE-Transformer/DeepSolo/blob/main/configs/R_50/ReCTS/finetune.yaml">
  <header class="source">

      <a href="https://github.com/ViTAE-Transformer/DeepSolo/blob/main/configs/R_50/ReCTS/finetune.yaml" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/ViTAE-Transformer/DeepSolo/blob/main/configs/R_50/ReCTS/finetune.yaml" target="_blank" rel="noopener nofollow ugc">ViTAE-Transformer/DeepSolo/blob/main/configs/R_50/ReCTS/finetune.yaml</a></h4>


      <pre><code class="lang-yaml">_BASE_: "../Base_det.yaml"

MODEL:
  WEIGHTS: "./output/R50/rects/pretrain/model_final.pth"
  TRANSFORMER:
    VOC_SIZE: 5462
    CUSTOM_DICT: "chn_cls_list"
    INFERENCE_TH_TEST: 0.35
    LOSS:
      POINT_TEXT_WEIGHT: 1.0
  
DATASETS:
  TRAIN: ("rects_train", "rects_val",)
  TEST: ("rects_test",)
INPUT:
  ROTATE: False
SOLVER:
  IMS_PER_BATCH: 8
  BASE_LR: 1e-5
  LR_BACKBONE: 1e-6
</code></pre>



  This file has been truncated. <a href="https://github.com/ViTAE-Transformer/DeepSolo/blob/main/configs/R_50/ReCTS/finetune.yaml" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>After running the export_model.py, it shows error message:</p>
<pre><code class="lang-auto">Could not export Python function call 'MSDeformAttnFunction'. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to __constants__:
</code></pre>
<p>Then I search on the internet, and finally solve this by (<a href="https://github.com/facebookresearch/Mask2Former/issues/37" class="inline-onebox" rel="noopener nofollow ugc">Can model convert to torchscript? · Issue #37 · facebookresearch/Mask2Former · GitHub</a>)<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/4/64773be663cd5dfce23880b78ea40790d4c64fc0.png" data-download-href="https://discuss.pytorch.org/uploads/default/64773be663cd5dfce23880b78ea40790d4c64fc0" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/4/64773be663cd5dfce23880b78ea40790d4c64fc0_2_690x414.png" alt="image" data-base62-sha1="ekLcGMtZV7zOnZUU3wbgt2JBkQM" width="690" height="414" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/4/64773be663cd5dfce23880b78ea40790d4c64fc0_2_690x414.png, https://discuss.pytorch.org/uploads/default/original/3X/6/4/64773be663cd5dfce23880b78ea40790d4c64fc0.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/4/64773be663cd5dfce23880b78ea40790d4c64fc0.png 2x" data-dominant-color="F5F6F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">865×520 118 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Then I got the exported model: model.ts file,</p>
<p>This is how the input image been processed:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f592927bcfff6c433a915c4dcdde9c4adbffeb98.png" data-download-href="https://discuss.pytorch.org/uploads/default/f592927bcfff6c433a915c4dcdde9c4adbffeb98" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f592927bcfff6c433a915c4dcdde9c4adbffeb98_2_690x251.png" alt="image" data-base62-sha1="z2r0BuDoqoe5n895jkwTLGKL0WA" width="690" height="251" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f592927bcfff6c433a915c4dcdde9c4adbffeb98_2_690x251.png, https://discuss.pytorch.org/uploads/default/original/3X/f/5/f592927bcfff6c433a915c4dcdde9c4adbffeb98.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/f/5/f592927bcfff6c433a915c4dcdde9c4adbffeb98.png 2x" data-dominant-color="323232"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">865×315 114 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>But when I try to use the model.ts to inference image,</p>
<pre><code class="lang-auto">Model_script=torch.jit.load("model.ts")
sample_inputs_onlyimg=sample_inputs[0]["image"]
out11 = Model_script (sample_inputs_onlyimg)
</code></pre>
<p>it only can inference the image “args.sample_image”, but when I change “args.sample_image” to another image,it shows the error massage:</p>
<pre><code class="lang-auto">/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py:1051: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /pytorch/c10/core/TensorImpl.h:1156.)
  return forward_call(*input, **kwargs)
Traceback (most recent call last):
  File "/root/autodl-tmp/project/deploy/export_model.py", line 264, in &lt;module&gt;
    out1 = m(data)
  File "/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
RuntimeError: The following operation failed in the TorchScript interpreter.
Traceback of TorchScript, serialized code (most recent call last):
  File "code/__torch__/detectron2/export/flatten.py", line 9, in forward
  def forward(self: __torch__.detectron2.export.flatten.TracingAdapter,
    argument_1: Tensor) -&gt; Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:
    _0, _1, _2, _3, _4, _5, _6, = (self.model).forward(argument_1, )
                                   ~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
    return (_0, _1, _2, _3, _4, _5, _6)
  File "code/__torch__/adet/modeling/text_spotter.py", line 23, in forward
    batched_imgs = torch.unsqueeze_(_7, 0)
    x0 = torch.contiguous(batched_imgs)
    _8, _9, _10, _11, = (_0).forward(x0, image_size, )
                         ~~~~~~~~~~~ &lt;--- HERE
    _12 = torch.softmax(_9, -1)
    prob = torch.sigmoid(torch.mean(_8, [-2]))
  File "code/__torch__/adet/modeling/model/detection_transformer.py", line 50, in forward
    _29 = getattr(self.input_proj, "1")
    _30 = getattr(self.input_proj, "0")
    _31 = (self.backbone).forward(x, image_size, )
           ~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
    _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, = _31
    _58 = (_30).forward(_32, )
  File "code/__torch__/adet/modeling/text_spotter.py", line 104, in forward
    image_size: Tensor) -&gt; Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:
    _61 = getattr(self, "1")
    _62 = (getattr(self, "0")).forward(x, image_size, )
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
    _63, _64, _65, _66, _67, _68, _69, = _62
    pos_embed = torch.to((_61).forward(_63, ), 6)
  File "code/__torch__/adet/modeling/text_spotter.py", line 143, in forward
    _92 = torch.slice(torch.slice(_91, 0, 0, 125), 1, 0, 138)
    _93 = torch.view(CONSTANTS.c2, annotate(List[int], []))
    _94 = torch.copy_(_92, torch.expand(_93, [125, 138]))
          ~~~~~~~~~~~ &lt;--- HERE
    masks_per_feature_level0 = torch.ones([_85, _86, _87], dtype=11, layout=None, device=torch.device("cpu"), pin_memory=False)
    _95 = torch.select(masks_per_feature_level0, 0, 0)

Traceback of TorchScript, original code (most recent call last):
/root/autodl-tmp/project/adet/modeling/text_spotter.py(60): mask_out_padding
/root/autodl-tmp/project/adet/modeling/text_spotter.py(43): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/adet/modeling/text_spotter.py(21): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/adet/modeling/model/detection_transformer.py(168): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/autodl-tmp/project/adet/modeling/text_spotter.py(220): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/detectron2/export/flatten.py(259): &lt;lambda&gt;
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/detectron2/export/flatten.py(294): forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1039): _slow_forward
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/nn/modules/module.py(1051): _call_impl
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/jit/_trace.py(952): trace_module
/root/miniconda3/envs/deepsolo/lib/python3.8/site-packages/torch/jit/_trace.py(735): trace
/root/autodl-tmp/project/deploy/export_model.py(125): export_tracing
/root/autodl-tmp/project/deploy/export_model.py(224): &lt;module&gt;
/root/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py(18): execfile
/root/.pycharm_helpers/pydev/pydevd.py(1496): _exec
/root/.pycharm_helpers/pydev/pydevd.py(1489): run
/root/.pycharm_helpers/pydev/pydevd.py(2177): main
/root/.pycharm_helpers/pydev/pydevd.py(2195): &lt;module&gt;
RuntimeError: The size of tensor a (50) must match the size of tensor b (125) at non-singleton dimension 0
</code></pre>
<p>That’s all the informations I can tell you, and looking forward to your reply!!<br>
Thank you very much!!!</p>

ptrblck: <p>TorchScript is in maintenance mode and I doubt it will get any major fixes at this time.<br>
You could try to <code>torch.compile</code> the model and see if this would work.</p>

Zalways: <p>but my model is based on Detectron2, which hasn’t been updated in over a year. The current v0.6 release only supports CUDA11.3 + PyTorch 1.10.0 at most.<br>
So you mean it’s possible that torchscript doesn’t support exporting my model?<br>
I was confused because my model had been successfully exported to torchscript via trace, but I just couldn’t inference on all the images，only can inference on the image that i used for exporting the model.</p>

ptrblck: <p>Tracing the model will bake in all conditions and won’t record them (as would be the case while scripting the model). It’s unclear to me if the shape mismatch is caused by exactly this limitation, i.e. if the lack of scripting dynamic behavior of the model now causes the shape mismatch, or where exactly it’s coming from.<br>
My response explaining that TorchScript is in maintenance mode was just a warning that you might not expect to see any fixes in it and since you’ve tagged me.</p>