Mask rcnn *.pt file causes exception

squareroot: <p>Hello, I successfully trained standard mask rcnn model from  Pytorch (example based on the book: computer vision with Pytorch).<br>
I used next code in order to create pt file:</p>
<pre><code class="lang-auto">sample = torch.rand(1, 3, 640, 480).to(device)
model = model.to(device)
scripted_model = torch.jit.script(model, sample)
scripted_model.save('my_script.pt')
</code></pre>
<p>I can succesfully load model in python, using code below:</p>
<pre><code class="lang-auto">model = torch.jit.load("my_script.pt", map_location='cuda')
model.eval()
</code></pre>
<p>But when i try to load model in c++ i get the exception:</p>
<pre><code class="lang-auto">    torch::jit::script::Module module;
    try
    {
      auto device = torch::kCUDA;
      // Deserialize the ScriptModule from a file using torch::jit::load().
      module = torch::jit::load("my_script.pt", device);
    }
    catch (const c10::Error&amp; e)
    {
      std::cerr &lt;&lt; "error loading the model\n";
      return -1;
    }
</code></pre>
<p>Where i’m wrong ?</p>

squareroot: <p>I have got additional diagnostic  information:</p>
<blockquote>
<p>terminate called after throwing an instance of ‘torch::jit::ErrorReport’<br>
what():<br>
Unknown builtin op: torchvision::nms.<br>
Could not find any similar ops to torchvision::nms. This op may not exist or may not be currently supported in TorchScript.<br>
:<br>
File “code/<strong>torch</strong>/torchvision/ops/boxes.py”, line 138<br>
_59 = <strong>torch</strong>.torchvision.extension._assert_has_ops<br>
_60 = _59()<br>
_61 = ops.torchvision.nms(boxes, scores, iou_threshold)<br>
~~~~~~~~~~~~~~~~~~~ &lt;— HERE<br>
return _61<br>
‘nms’ is being compiled since it was called from ‘_batched_nms_vanilla’<br>
File “/home/user/.local/lib/python3.10/site-packages/torchvision/ops/boxes.py”, line 109<br>
for class_id in torch.unique(idxs):<br>
curr_indices = torch.where(idxs == class_id)[0]<br>
curr_keep_indices = nms(boxes[curr_indices], scores[curr_indices], iou_threshold)<br>
~~~ &lt;— HERE<br>
keep_mask[curr_indices[curr_keep_indices]] = True<br>
keep_indices = torch.where(keep_mask)[0]<br>
Serialized   File “code/<strong>torch</strong>/torchvision/ops/boxes.py”, line 83<br>
_31 = torch.index(boxes, _30)<br>
_32 = annotate(List[Optional[Tensor]], [curr_indices])<br>
curr_keep_indices = <strong>torch</strong>.torchvision.ops.boxes.nms(_31, torch.index(scores, _32), iou_threshold, )<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;— HERE<br>
_33 = annotate(List[Optional[Tensor]], [curr_keep_indices])<br>
_34 = torch.index(curr_indices, _33)<br>
‘_batched_nms_vanilla’ is being compiled since it was called from ‘batched_nms’<br>
Serialized   File “code/<strong>torch</strong>/torchvision/ops/boxes.py”, line 35<br>
idxs: Tensor,<br>
iou_threshold: float) → Tensor:<br>
_9 = <strong>torch</strong>.torchvision.ops.boxes._batched_nms_vanilla</p>
<pre><code class="lang-auto">_10 = __torch__.torchvision.ops.boxes._batched_nms_coordinate_trick
_11 = torch.numel(boxes)
'batched_nms' is being compiled since it was called from 'RegionProposalNetwork.filter_proposals'
Serialized   File "code/__torch__/torchvision/models/detection/rpn.py", line 72
  _11 = __torch__.torchvision.ops.boxes.clip_boxes_to_image
  _12 = __torch__.torchvision.ops.boxes.remove_small_boxes
  _13 = __torch__.torchvision.ops.boxes.batched_nms
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
  num_images = (torch.size(proposals))[0]
  device = ops.prim.device(proposals)
'RegionProposalNetwork.filter_proposals' is being compiled since it was called from 'RegionProposalNetwork.forward'
File "/home/user/.local/lib/python3.10/site-packages/torchvision/models/detection/rpn.py", line 372
      proposals = self.box_coder.decode(pred_bbox_deltas.detach(), anchors)
      proposals = proposals.view(num_images, -1, 4)
      boxes, scores = self.filter_proposals(proposals, objectness, images.image_sizes, num_anchors_per_level)
                      ~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
  
      losses = {}
Serialized   File "code/__torch__/torchvision/models/detection/rpn.py", line 43
  proposals0 = torch.view(proposals, [num_images, -1, 4])
  image_sizes = images.image_sizes
  _8 = (self).filter_proposals(proposals0, objectness0, image_sizes, num_anchors_per_level, )
                                                                     ~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
  boxes, scores, = _8
  losses = annotate(Dict[str, Tensor], {})</code></pre>
</blockquote>

squareroot: <p>I added diagnostic information in notebook:<br>
print(torch.<strong>version</strong>)<br>
print(torchvision.<strong>version</strong>)<br>
print(torch_snippets.<strong>version</strong>)<br>
result is:<br>
2.2.2+cu121</p>
<p>0.17.2+cu121</p>
<p>0.528</p>