How to solve "RuntimeError: expected scalar type Double but found Float" when loading torchscript model in C++

username: thw1021
cooked: <p>Hi all,<br>
I try to load a torchscript model in C++, but got an error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/7/878247df8c7abf17d88f9dc99ec5f2fb42d419ce.png" data-download-href="https://discuss.pytorch.org/uploads/default/878247df8c7abf17d88f9dc99ec5f2fb42d419ce" title="Screenshot from 2020-11-18 22-20-36"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/7/878247df8c7abf17d88f9dc99ec5f2fb42d419ce_2_690x254.png" alt="Screenshot from 2020-11-18 22-20-36" data-base62-sha1="jkLAbTImwoGRLNj2BylpiPmYxhc" width="690" height="254" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/7/878247df8c7abf17d88f9dc99ec5f2fb42d419ce_2_690x254.png, https://discuss.pytorch.org/uploads/default/original/3X/8/7/878247df8c7abf17d88f9dc99ec5f2fb42d419ce.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/7/878247df8c7abf17d88f9dc99ec5f2fb42d419ce.png 2x" data-dominant-color="30333C"><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">Screenshot from 2020-11-18 22-20-36</span><span class="informations">961×354 41.2 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><br>
The input data is actually double type. I tried to use <code>.double()</code> following <a href="https://discuss.pytorch.org/t/runtimeerror-expected-object-of-scalar-type-double-but-got-scalar-type-float-for-argument-2-weight/38961">here</a> but cannot run <code>make</code>.<br>
My code:</p>
<pre><code class="lang-auto">#include &lt;torch/script.h&gt; // One-stop header.

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

int main(int argc, const char* argv[]) {
  std::vector&lt;double&gt; inputdata({ 1.0, 2.0, 3.0, 4.0, 5.0 });
  std::cout &lt;&lt; inputdata &lt;&lt; std::endl;

  auto opts = torch::TensorOptions().dtype(torch::kDouble);
  torch::Tensor input = torch::from_blob(inputdata.data(), {1, (int)inputdata.size()}, opts).to(torch::kDouble);
  std::cout &lt;&lt; input &lt;&lt; std::endl;

  if (argc != 2) {
    std::cerr &lt;&lt; "usage: example-app &lt;path-to-exported-script-module&gt;\n";
    return -1;
  }

  torch::jit::script::Module module;
  try {
    // Deserialize the ScriptModule from a file using torch::jit::load().
    module = torch::jit::load(argv[1]);
  }
  catch (const c10::Error&amp; e) {
    std::cerr &lt;&lt; "error loading the model\n";
    return -1;
  }

  std::cout &lt;&lt; "ok" &lt;&lt; std::endl;

    
  std::vector&lt;torch::jit::IValue&gt; input_invar;
  input_invar.push_back(input);
  std::cout &lt;&lt; "run forward" &lt;&lt; std::endl;
  at::Tensor output = module.forward(input_invar).toTensor();

  std::vector&lt;double&gt; out_vect(output.data_ptr&lt;double&gt;(), output.data_ptr&lt;double&gt;() + output.numel());

  std::cout &lt;&lt; out_vect &lt;&lt; std::endl;
}
</code></pre>
<p>Could you give me some suggestions, please ?<br>
Best.</p>

username: ptrblck
cooked: <p>Based on the error message it seems your model is using the default <code>FloatTensors</code>, while you are trying to pass <code>DoubleTensors</code> to its <code>forward</code> method.<br>
Transform the model to <code>double</code> or the input tensors to <code>float</code> and it should work. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>