Loading tensors from pytorch to torch C++

username: veryluckyxyz
cooked: <p>Hi,<br>
I am trying to load tensor from pytorch to c++, following suggestions <a href="https://github.com/pytorch/pytorch/issues/20356" rel="nofollow noopener">this</a> github issue. However, it looks like <a href="https://pytorch.org/cppdocs/api/structtorch_1_1jit_1_1script_1_1_module.html#exhale-struct-structtorch-1-1jit-1-1script-1-1-module" rel="nofollow noopener">torch::jit::script::Module</a> no longer has  <code>get_attribute</code>  and I having a tough time understanding how to get named parameters using the API.</p>
<p>Can you please suggest how to get named_parameters with the current API?</p>

username: wanchaol
cooked: <p>Hey sorry for the confusion here. The TorchScript C++ API is still experimental and we made some changes to the API, we will update the doc in the next release. To answer your question, we are trying to mimic the module API in python, so you can get the named parameters via <code>named_parameters()</code> call in C++, see a list of methods <a href="https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/script/module.h#L156" rel="nofollow noopener">here</a></p>

username: veryluckyxyz
cooked: <p>Thank you for your response. i was able to get the values using  <code>attr</code>  but I still could not understand how to extract values from  <code>slot_iterator_impl</code>.<br>
If its not too much trouble, can I please request sample code to extract for the same example (reproduced below)? It can help me better understand the codebase!</p>
<p>Thank you again!</p>
<pre><code class="lang-auto">#include &lt;torch/script.h&gt;

#include &lt;iostream&gt;
#include &lt;memory&gt;

int main(int argc, const char *argv[]) {
  torch::jit::script::Module container = torch::jit::load("container.pt");

  // Load values by name
  torch::Tensor a = container.get_attribute("a").toTensor();
  std::cout &lt;&lt; a &lt;&lt; "\n";

  torch::Tensor b = container.get_attribute("b").toTensor();
  std::cout &lt;&lt; b &lt;&lt; "\n";

  std::string c = container.get_attribute("c").toStringRef();
  std::cout &lt;&lt; c &lt;&lt; "\n";

  int64_t d = container.get_attribute("d").toInt();
  std::cout &lt;&lt; d &lt;&lt; "\n";

  return 0;
}
</code></pre>

username: driazati
cooked: <p>If you are just trying to move values between Python and C++, the API in <a href="https://github.com/pytorch/pytorch/issues/20356#issuecomment-567663701">this comment</a> is now the blessed way to do that. But to answer your question, for a model like</p>
<pre><code class="lang-python">import torch

class Model(torch.nn.Module):

    def __init__(self):
        super().__init__()
        self.w1 = torch.nn.Parameter(torch.ones(2, 2))
        self.w2 = torch.nn.Parameter(torch.ones(2, 2))

    def forward(self):
        return self.w1 + self.w2

m = torch.jit.script(Model())
torch.jit.save(m, 'model.pt')
</code></pre>
<p>You can iterate over the parameters like:</p>
<pre><code class="lang-cpp">#include &lt;torch/script.h&gt;

int main() {
  auto m = torch::jit::load("model.pt");
  for (torch::jit::script::Named&lt;at::Tensor&gt; p :
       m.named_parameters(/*recurse=*/true)) {
    std::cout &lt;&lt; p.name &lt;&lt; ": " &lt;&lt; p.value &lt;&lt; "\n";
  }
  return 0;
}

</code></pre>

username: widdiot
cooked: <p>Tried to load it using the mentioned <a href="https://github.com/pytorch/pytorch/issues/20356#issuecomment-567663701" rel="noopener nofollow ugc">link</a> . Im getting error on the line<br>
torch::IValue x = torch::pickle_load(f);<br>
i tried suing libtorch nightly as well as libtorch 1.7.1<br>
error: ‘pickle_load’ is not a member of ‘torch’</p>

username: widdiot
cooked: <p>torch::jit::pickle_load works</p>