Unable to get hidden layer result with libtorch JIT C++

username: Edwinzero
cooked: <p>I am trying to use the hidden layer result to generate heatmap with libtorch jit<br>
The code snippet is shown below:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/0/f0d0703c718b42cf8696ebde15c4bb7f8c1085b7.png" data-download-href="https://discuss.pytorch.org/uploads/default/f0d0703c718b42cf8696ebde15c4bb7f8c1085b7" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/0/f0d0703c718b42cf8696ebde15c4bb7f8c1085b7.png" alt="image" data-base62-sha1="ymlay7MLQjFZGVeHCvvKZeWCcU7" width="690" height="201" data-dominant-color="2A2A2A"><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">838×245 15 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>The return value of the model should be two parameters, such as hidden_output and predict_result.<br>
From some materials online, it seems that <code>toTensorList()</code> function is an appropriate choice for multiple return values based on the discussion at <a href="https://discuss.pytorch.org/t/convert-c10-ivalue-to-tensor-or-tuple-failed/52396/2">convert-c10-ivalue-to-tensor-or-tuple-failed</a>.</p>
<p>I try to use <code>toTensorList()</code> but I came across the “std ambiguous” problem.<br>
This error is resolved by adding the <code>":: "</code> to the beginning of <code>"std::"</code> based on the discussion at <a href="https://discuss.pytorch.org/t/compiler-report-std-ambiguous-symbol-error-under-visual-c-2017/39459/4">compiler-report-std-ambiguous-symbol-error-under-visual-c-2017</a><br>
However, the exception problem keeps occurring which is very confused to me. Is there any solution to this problem?</p>
<p>Thank you very much for your attention to this problem.</p>

username: peterjc123
cooked: <p>Please check thr following things:</p>
<ol>
<li>Are you mixing the Debug and Release configuration?</li>
<li>Have you copied all the DLLs in LibTorch to the location of your executable?</li>
<li>Have you moved the model to the desired device before calculation?</li>
</ol>
<p>btw, which version of libtorch are you using?</p>

username: Edwinzero
cooked: <p>Thank you very much for your reply <a class="mention" href="/u/peterjc123">@peterjc123</a> !</p>
<p><em><strong>1. Are you mixing the Debug and Release configuration?</strong></em><br>
No. I have separate libtorch for Debug and Release configuration. The loading model code snippet runs well, which I think there is no configuration error.</p>
<p><em><strong>2. Have you copied all the DLLs in LibTorch to the location of your executable?</strong></em><br>
Yes, I have all the DLL files copied.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/3/338ce0a72e74274c2fb51fa04dcba4dd988697c8.png" data-download-href="https://discuss.pytorch.org/uploads/default/338ce0a72e74274c2fb51fa04dcba4dd988697c8" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/3/338ce0a72e74274c2fb51fa04dcba4dd988697c8.png" alt="image" data-base62-sha1="7m2b8YnB7VTAbn9n59HMLeAZXLy" width="690" height="445" data-dominant-color="F5F5F5"><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">695×449 10.8 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><em><strong>3. Have you moved the model to the desired device before calculation?</strong></em><br>
The model is training on the GPU.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/a/e/ae8e8af2c8a3c47c9a5a475263f38dc76a5806f4.png" alt="image" data-base62-sha1="oUctx0kH8jQJWtKzIpcMwUv3E20" width="542" height="190"><br>
And I convert all the tensor data to GPU before calling forward() function.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/1/41b5df12bd6e92bdfb6b82cafbbee594deb2639e.png" data-download-href="https://discuss.pytorch.org/uploads/default/41b5df12bd6e92bdfb6b82cafbbee594deb2639e" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/1/41b5df12bd6e92bdfb6b82cafbbee594deb2639e.png" alt="image" data-base62-sha1="9niGYI5RKRKFaNbKkvtavrRK8SG" width="641" height="500" data-dominant-color="232423"><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">850×662 31 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><em><strong>4.which version of libtorch are you using?</strong></em><br>
The libtorch version is the latest 1.4. And I use pyTorch 1.4 for training the network.</p>
<p>My forward() function in python code returns as shown below:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/e/0/e0069657827a9648991f9c0912a9b5410fd8806e.png" alt="image" data-base62-sha1="vXP47O7yrnAe0CHOzFJRNhMH5xk" width="162" height="52"><br>
Is this the proper way to return multiple values? Does this work in the JIT tracing for the C++ library?</p>

username: Edwinzero
cooked: <p>Solution:</p>
<p>After I checked each variable’s value, I find the return value of the function is tuple instead of tensorlist.<br>
The method is changed to <code>toTuple()</code> instead of <code>toTensorList()</code>.<br>
Then the problem is solved.<br>
I think I just misunderstand the answer at <a href="https://discuss.pytorch.org/t/convert-c10-ivalue-to-tensor-or-tuple-failed/52396/2">convert_ivalue_to_tensor_or_tuple</a>.</p>