Libtorch IValue from std::vector<IValue>

Christian_Kauten: <p>What is the expected way to create an IValue with a list of IValues? The C++ constructor expects a <code>c10::List&lt;IVector&gt;</code>, i.e., a (<code>GenericList</code>), but this structure appears to be impossible to construct. Any form of syntax I’ve tried to create one of these list structures fails at compilation with a</p>
<pre><code class="lang-auto">static_assert(!std::is_same&lt;T, IValue&gt;::value, "This constructor is not valid for List&lt;IValue&gt;. Please use c10::impl::GenericList(elementType) instead.");
</code></pre>
<p>Using the recommended <code>c10::impl</code> implementation does not change anything.</p>
<p>I would expect a constructor for <code>IValue</code> to exist with a <code>std::vector&lt;IValue&gt;</code> signature, which is true for constructing an <code>IValue</code> from a tensor vector of type <code>std::vector&lt;Tensor&gt;</code>.</p>

Christian_Kauten: <p>This question has been asked previously, but nobody seems to know how this structure works</p>
<aside class="quote quote-modified" data-post="1" data-topic="112261">
  <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/bycob/48/34805_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-create-a-c10-list-ivalue/112261">How to create a c10::List&lt;IValue&gt;?</a> <a class="badge-category__wrapper " href="/c/c/11"><span data-category-id="11" data-drop-close="true" class="badge-category " title="Topics related to the C++ Frontend, C++ API or C++ Extensions"><span class="badge-category__name">C++</span></span></a>
  </div>
  <blockquote>
    I try to create a List from C++ API, but I keep getting a compilation error for code as simple as this one: 
c10::List&lt;c10::IValue&gt; list;

I get this error: 
In file included from /usr/local/include/ATen/core/List.h:472:0,
                 from /usr/local/include/ATen/core/ivalue_inl.h:13,
                 from /usr/local/include/ATen/core/ivalue.h:895,
                 from /usr/local/include/ATen/ATen.h:33,
                 from /usr/local/include/torch/csrc/api/include/torch/types.h:3,
      …
  </blockquote>
</aside>

<aside class="quote quote-modified" data-post="1" data-topic="115012">
  <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/bycob/48/34805_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-create-a-c10-list-ivalue-with-libtorch-api/115012">How to create a c10::List&lt;IValue&gt; with libtorch API?</a> <a class="badge-category__wrapper " href="/c/c/11"><span data-category-id="11" data-drop-close="true" class="badge-category " title="Topics related to the C++ Frontend, C++ API or C++ Extensions"><span class="badge-category__name">C++</span></span></a>
  </div>
  <blockquote>
    I posted about this problem last month and got no answer, but I still have not found a way to resolve it, hence the repost. 
I try to create a List from C++ API, but I keep getting a compilation error for code as simple as this one: 
c10::List&lt;c10::IValue&gt; list;

I get this error: 
In file included from /usr/local/include/ATen/core/List.h:472:0,
                 from /usr/local/include/ATen/core/ivalue_inl.h:13,
                 from /usr/local/include/ATen/core/ivalue.h:895,
                 fr…
  </blockquote>
</aside>


Christian_Kauten: <p>Even basic stack allocations of these list structures fails. Neither of these statements will compile with the same uninformative error.</p>
<pre><code class="lang-auto">c10::impl::GenericList inputs;
</code></pre>
<pre><code class="lang-auto">c10::List&lt;torch::IValue&gt; inputs;
</code></pre>

mreso: <p>Hi,</p>
<p>I ran into the same issue when my model expected two lists of strings input via *args.</p>
<p>Using vector&lt;vector&gt; gave me the static assert.</p>
<p>Turned out that the inner list could not simply be vector but needed to be a Generic list which had to be initialized with the type.</p>
<p>In my case I solved it like that:</p>
<pre><code class="lang-auto">auto batch_1 = c10::impl::GenericList(c10::StringType::get());
batch_1.emplace_back("FOO");
auto batch_2 = c10::impl::GenericList(c10::StringType::get());
batch_2.emplace_back("BAR");

model.forward({batch_1, batch_2});
</code></pre>
<p>Hope that helps!</p>