Converting vector<double> to TorchScript model input

NumesSanguis: <h1>Question</h1>
<p>How do I convert a <code>std::vector&lt;double&gt;</code> into a <code>std::vector &lt;torch::jit::IValue&gt;</code> which can be input to a TorchScript model?</p>
<h2>Background</h2>
<p>I trained a model in Python and followed this guide to use the model in C++: <a href="https://pytorch.org/tutorials/advanced/cpp_export.html#step-4-executing-the-script-module-in-c" rel="noopener nofollow ugc">https://pytorch.org/tutorials/advanced/cpp_export.html#step-4-executing-the-script-module-in-c</a></p>
<p>Please note that I have almost no experience with C++</p>
<h1>Working code with dummy Tensor</h1>
<p>Everything works fine when I make a dummy Tensor directly in C++:</p>
<pre><code class="lang-cpp">#include &lt;torch/script.h&gt; // One-stop header.

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

using namespace std::chrono;

int main(int argc, const char* argv[]) {
    std::cout &lt;&lt; "ok\n";
    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\n";

    // Create a vector of inputs.
    int sample_rate = 16000;
    int input = sample_rate * 100;
    std::vector&lt;torch::jit::IValue&gt; inputs;
    inputs.push_back(torch::ones({1, input}));

    // Execute the model and turn its output into a tensor.
    at::Tensor output = module.forward(inputs).toTensor().sigmoid();
    std::cout &lt;&lt; output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) &lt;&lt; '\n';
</code></pre>
<h1>Attempting the same with vector double</h1>
<p>In our real application the audio data is of the format <code>std::vector&lt;double&gt;</code>, however. This cannot be changed.<br>
From my Googling, what I would likely need is the function <code>torch::from_blob()</code>, so I tried to replace:</p>
<pre><code class="lang-cpp">// Create a vector of inputs.
int sample_rate = 16000;
int input = sample_rate * 100;
std::vector&lt;torch::jit::IValue&gt; inputs;
inputs.push_back(torch::ones({1, input}));
</code></pre>
<p>with:</p>
<pre><code class="lang-cpp">std::vector &lt;torch::jit::IValue&gt; inputs;
// dummy double
std::vector&lt;double&gt; wavData = std::vector&lt;double&gt;(5);
auto options = torch::TensorOptions().dtype(torch::kFloat32);
inputs.push_back(torch::from_blob(wavData, {1, input}, options));
</code></pre>
<h2>Problem</h2>
<p>The code I found only used these 3 arguments. But this throws the error:</p>
<pre><code class="lang-cpp">path/example-app.cpp: In function ‘int main(int, const char**)’:
path/example-app.cpp:47:71: error: no matching function for call to ‘from_blob(std::vector&lt;double, std::allocator&lt;double&gt; &gt;&amp;, &lt;brace-enclosed initializer list&gt;, c10::TensorOptions&amp;)’ inputs.push_back(torch::from_blob(wavData, {1, input}, options));
</code></pre>
<p>It seems I also need to supply a <code>const Deleter&amp; deleter</code>, but I have no clue what how I’m supposed to create that.<br>
Unfortunately the <a href="https://pytorch.org/cppdocs/api/function_namespacetorch_1ad7fb2a7759ef8c9443b489ddde494787.html" rel="noopener nofollow ugc">docs page of from_blob()</a> is broken and therefore also not of much help.</p>
<p>Any insights are appreciated!</p>

glaringlee: <p><a class="mention" href="/u/numessanguis">@NumesSanguis</a><br>
At first glance, should you use torch::kFloat64? Your raw data is doulbe. I did not test on my machine though.</p>

NumesSanguis: <p><a class="mention" href="/u/glaringlee">@glaringlee</a> You might have identified the issue coming after this! It does not solve the original issue, however.</p>
<p>This standalone script:</p>
<pre data-code-wrap="cpp"><code class="lang-cpp">#include &lt;torch/script.h&gt; // One-stop header.
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;memory&gt;

int main(int argc, char** argv)
{
    std::cout &lt;&lt; "Start successful\n";
    std::vector&lt;double&gt; wavData = std::vector&lt;double&gt;(5);
    int len = wavData.size();
    auto options = torch::TensorOptions().dtype(torch::kFloat64);
    torch::from_blob(wavData, {1, len}, options);
}
</code></pre>
<p>Produces the following error:</p>
<pre><code class="lang-auto">to_tensor_test.cpp.o
filepath/to_tensor_test.cpp: In function ‘int main(int, char**)’:
filepath/to_tensor_test.cpp:12:48: error: no matching function for call to ‘from_blob(std::vector&lt;double, std::allocator&lt;double&gt; &gt;&amp;, &lt;brace-enclosed initializer list&gt;, c10::TensorOptions&amp;)’
     torch::from_blob(wavData, {1, len}, options);
                                                ^
In file included from filepath/libtorch/include/torch/csrc/api/include/torch/types.h:7:0,
                 from filepath/libtorch/include/torch/script.h:3,
                 from filepath/to_tensor_test.cpp:1:
filepath/libtorch/include/torch/csrc/autograd/generated/variable_factories.h:57:19: note: candidate: at::Tensor torch::from_blob(void*, c10::IntArrayRef, c10::IntArrayRef, const Deleter&amp;, const c10::TensorOptions&amp;)
 inline at::Tensor from_blob(
                   ^~~~~~~~~
filepath/libtorch/include/torch/csrc/autograd/generated/variable_factories.h:57:19: note:   candidate expects 5 arguments, 3 provided
filepath/libtorch/include/torch/csrc/autograd/generated/variable_factories.h:76:19: note: candidate: at::Tensor torch::from_blob(void*, c10::IntArrayRef, c10::IntArrayRef, const c10::TensorOptions&amp;)
 inline at::Tensor from_blob(
                   ^~~~~~~~~
filepath/libtorch/include/torch/csrc/autograd/generated/variable_factories.h:76:19: note:   no known conversion for argument 1 from ‘std::vector&lt;double, std::allocator&lt;double&gt; &gt;’ to ‘void*’
filepath/libtorch/include/torch/csrc/autograd/generated/variable_factories.h:95:19: note: candidate: at::Tensor torch::from_blob(void*, c10::IntArrayRef, const Deleter&amp;, const c10::TensorOptions&amp;)
 inline at::Tensor from_blob(
                   ^~~~~~~~~
filepath/libtorch/include/torch/csrc/autograd/generated/variable_factories.h:95:19: note:   no known conversion for argument 1 from ‘std::vector&lt;double, std::allocator&lt;double&gt; &gt;’ to ‘void*’
filepath/libtorch/include/torch/csrc/autograd/generated/variable_factories.h:112:19: note: candidate: at::Tensor torch::from_blob(void*, c10::IntArrayRef, const c10::TensorOptions&amp;)
 inline at::Tensor from_blob(
                   ^~~~~~~~~
filepath/libtorch/include/torch/csrc/autograd/generated/variable_factories.h:112:19: note:   no known conversion for argument 1 from ‘std::vector&lt;double, std::allocator&lt;double&gt; &gt;’ to ‘void*’
CMakeFiles/to_tensor_test.dir/build.make:82: recipe for target 'CMakeFiles/to_tensor_test.dir/to_tensor_test.cpp.o' failed
make[3]: *** [CMakeFiles/to_tensor_test.dir/to_tensor_test.cpp.o] Error 1
CMakeFiles/Makefile2:95: recipe for target 'CMakeFiles/to_tensor_test.dir/all' failed
make[2]: *** [CMakeFiles/to_tensor_test.dir/all] Error 2
CMakeFiles/Makefile2:102: recipe for target 'CMakeFiles/to_tensor_test.dir/rule' failed
make[1]: *** [CMakeFiles/to_tensor_test.dir/rule] Error 2
Makefile:138: recipe for target 'to_tensor_test' failed
make: *** [to_tensor_test] Error 2
</code></pre>
<p>CLion also puts a red line below <code>torch::from_blob</code> with the following:</p>
<blockquote>
<p>No matching function for call to ‘from_blob’. candidate function not viable: requires at least 4 arguments, but 3 were provided</p>
</blockquote>
<p>Note that I’m using C++ 17 (cannot use 14)</p>