TracerWarning: There are 2 live references to the data region being modified when tracing in-place operator copy_

alchemi5t: <p>Hi there,</p>
<p>I am trying to convert a pytorch model to onnx and use tensorrt to optimize it. I can convert it to onnx and i have generated a trt engine but the output is completely useless. The onnx engine creation is what i suspect is the issue here. I do have a lot of tracerwarning of which most are of this sort:</p>
<p>TracerWarning: There are 2 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.<br>
pred_boxes[:, :, 0::4] = pred_ctr_x - 0.5 * pred_w</p>
<p>From what i understand, slicing and assigning is the issue here?<br>
How would i go about fixing this, (Also, am i right to assume this is the issue.).</p>
<p>the rest of the warnings are of the sort:<br>
TracerWarning: Converting a tensor to a Python float might cause the trace to be incorrect. We can’t record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!</p>
<p>which i dont think is the issue as of now, because i am using the same shape inputs.</p>

alchemi5t: <p>I believe this should work for the _copy (at assignment) warning :</p>
<aside class="quote quote-modified" data-post="1" data-topic="64158">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/thearheus/48/12377_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/unable-to-convert-pytorch-model-to-onnx/64158">Unable to convert PyTorch model to onnx</a> <a class="badge-category__wrapper " href="/c/deployment/14"><span data-category-id="14" data-drop-close="true" class="badge-category " title="A category of posts focused on production usage of PyTorch. Mobile deployment is out of scope for this category (for now…  )"><span class="badge-category__name">deployment</span></span></a>
  </div>
  <blockquote>
    Hello. I have some troubles in converting my yolo model to onnx format. 
I have next errors: 
C:\Users\1\PycharmProjects\untitled\my_utils.py:145: TracerWarning: There are 3 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, …
  </blockquote>
</aside>

<pre><code class="lang-auto">
There cannot be any in-place assignment, like:

pred[:, :, 0] = torch.sigmoid(pred[:, :, 0])

I think that you need to modify it as:


pred = torch.cat((torch.sigmoid(pred[:, :, 0:1]), pred[:, :, 1:]), dim=2)


Anyway, slicing cannot occur on the left side of “=”
</code></pre>
<p>But how do I fix<br>
TracerWarning: There are 2 live references to the data region being modified when tracing in-place operator clamp_. This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.<br>
boxes[i,:,3::4].clamp_(0, im_shape[i, 0]-1)</p>

albanD: <p>Hi,</p>
<p>This is a limitation of onnx I guess: they do not support inplace operations.</p>
<p>You will need to use <code>cat</code> here as well and use the not-inplace version of <code>.clamp()</code>.</p>

alchemi5t: <p>Thanks for the input. By non inplace clamp, do you mean using the out key word argument in torch clamp?</p>

albanD: <p>I mean the function without the <code>_</code>. Which won’t modify the input inplace but return a new Tensor containing the result. See the doc <a href="https://pytorch.org/docs/stable/tensors.html?highlight=clamp#torch.Tensor.clamp">here</a>.</p>

Viet_Tuan_Nguyen: <p>Hey, I also met some tracerwarnings that are same with yours and I know that you solved this warning Could you tell me how to solve this problem?</p>